Skip to content
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ node_modules/
.otto

# mkdocs
site/
site/

# PyCharm files.
.idea/
29 changes: 24 additions & 5 deletions query_phenomizer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import click
import json

from os import linesep
from query_phenomizer import query, validate_term

from getpass import getpass

from .log import (configure_stream, LEVELS)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -61,18 +64,23 @@ def test_args(*args):
@click.pass_context
def cli(ctx, hpo_term, check_terms, output, p_value_limit, verbose, username,
password, to_json):
"Give hpo terms either on the form 'HP:0001623', or '0001623'"
"""Give hpo terms either on the form 'HP:0001623', or '0001623'.

If -p is not used, a password prompt will appear instead."""
loglevel = LEVELS.get(min(verbose, 3))
configure_stream(level=loglevel)

if not hpo_term:
logger.info("Please specify at least one hpo term with '-t/--hpo_term'.")
ctx.abort()

if not (username and password):
logger.info("Please specify username with -u and password with -p.")
if not username:
logger.info("Please specify username with -u (and password with -p).")
logger.info("Contact sebastian.koehler@charite.de.")
ctx.abort()

if not password:
password = getpass("password:")

hpo_list = []
for term in hpo_term:
Expand All @@ -95,21 +103,32 @@ def cli(ctx, hpo_term, check_terms, output, p_value_limit, verbose, username,
ctx.abort()
else:
try:
if output:
header = "p-value\tdisease-id\tdisease-name\tgene-symbols" + linesep # The file header.
output.write(header.encode()) # "a bytes-like object is required"
for result in query(username, password, *hpo_list):
if to_json:
click.echo(json.dumps(result))
else:
print_string = "{0}\t{1}:{2}\t{3}\t{4}".format(
result['p_value'],
result['p_value'],
result['disease_source'],
result['disease_nr'],
result['description'],
','.join(result['gene_symbols'])
)
p_value = result['p_value']
if p_value <= p_value_limit:
click.echo(print_string)
if output:
print_string += linesep # Adds line separator to output.
output.write(print_string.encode()) # "a bytes-like object is required"
else:
click.echo(print_string)

except RuntimeError as e:
click.echo(e)
ctx.abort()
finally:
if output:
output.flush()
output.close()