Skip to content

Commit

Permalink
Update UI (#2)
Browse files Browse the repository at this point in the history
* Squashed 'pages/libs/mpxsonar/' content from commit 36581b5
  • Loading branch information
silenus092 authored Nov 18, 2022
1 parent 7dfdd5f commit b5cca15
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 179 deletions.
10 changes: 10 additions & 0 deletions pages/libs/mpxsonar/.env.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# DASH APP
SERVER="0.0.0.0"
PORT="80"
SECRET_KEY="XX58602833XX"
DEBUG=True

# Database URL with generic format of connection.
# https://USERNAME:PASSWORD@IP:PORT/DBNAME
DB_URL="https://super_user:123456@localhost:3306/mpx"

# Logging
# DEBUG, INFO, WARNING, ERROR, CRITICAL
LOG_LEVEL=DEBUG
Empty file added pages/libs/mpxsonar/197190
Empty file.
37 changes: 14 additions & 23 deletions pages/libs/mpxsonar/example-dash/app_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def match(
with sonarDBManager(db, debug=debug) as dbm:
if format == "vcf" and reference is None:
reference = dbm.get_default_reference_accession()

cursor = dbm.match(
*profiles,
reserved_props=reserved_props_dict,
Expand All @@ -45,31 +46,29 @@ def match(
output_column=output_column,
showNX=showNX,
)
# print(cursor)
if format == "csv" or format == "tsv":
# cursor => list of dict
df = pd.DataFrame(cursor)
df.drop(["IMPORTED", "MODIFIED"], axis=1, inplace=True)
if "IMPORTED" in df.columns and "MODIFIED" in df.columns:
df.drop(["IMPORTED", "MODIFIED"], axis=1, inplace=True)
# print(df)
output = df
tsv = True if format == "tsv" else False
sonarBasics.exportCSV(
cursor, outfile=outfile, na="*** no match ***", tsv=tsv
)
if len(df) == 0:
output = "*** no match ***"
else:
output = df
# tsv = True if format == "tsv" else False
# sonarBasics.exportCSV(
# cursor, outfile=outfile, na="*** no match ***", tsv=tsv
# )
elif format == "count":
output = cursor.fetchone()["count"]
if outfile:
with open(outfile, "w") as handle:
handle.write(str(output))
else:
print(output)
elif format == "vcf":
# TODO: remove this. and change
sonarBasics.exportVCF(
cursor, reference=reference, outfile=outfile, na="*** no match ***"
)
else:
sys.exit("error: '" + format + "' is not a valid output format")

return output

def list_prop(db=None):
Expand Down Expand Up @@ -120,15 +119,7 @@ def get_freq_mutation(args):
def match_controller(args): # noqa: C901
props = {}
reserved_props = {}
# for reserved keywords
reserved_key = ["sample"]
for pname in reserved_key:
if hasattr(args, pname):
if pname == "sample" and len(getattr(args, pname)) > 0:
# reserved_props[pname] = set([x.strip() for x in args.sample])
reserved_props = sonarBasics.set_key(
reserved_props, pname, getattr(args, pname)
)

with sonarDBManager(args.db, readonly=False, debug=args.debug) as dbm:
for pname in dbm.properties:
if hasattr(args, pname):
Expand Down Expand Up @@ -160,7 +151,7 @@ def match_controller(args): # noqa: C901
)
# reserved_props[pname] = getattr(args, pname)
format = "count" if args.count else args.format

print(props)
output = sonarBasicsChild.match(
args.db,
args.profile,
Expand Down
273 changes: 211 additions & 62 deletions pages/libs/mpxsonar/example-dash/example.app.py

Large diffs are not rendered by default.

88 changes: 0 additions & 88 deletions pages/libs/mpxsonar/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pages/libs/mpxsonar/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mpxsonar"
version = "0.3.7"
version = "0.3.8"
description = "A database-driven system for handling genomic sequences and screening genomic profiles."
authors = ["Stephan Fuchs <FuchsS@rki.de>", "Kunaphas Kongkitimanon <KongkitimanonK@rki.de>", "Matthew Huska <HuskaM@rki.de>"]
license = "GPL3"
Expand Down
26 changes: 22 additions & 4 deletions pages/libs/mpxsonar/src/mpxsonar/dbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,7 @@ def match( # noqa: 901
# if 'sample' is presented we just use only samples
samples_condition = []
for pname, vals in reserved_props.items():
print("sample" + str(vals))
if pname == "sample":
for x in vals:
samples_condition.append('"' + x + '"')
Expand All @@ -1741,6 +1742,7 @@ def match( # noqa: 901
'"' + reference_accession + '"'
)
else:
# should we use only sample or all alignment
sample_selection_sql = "SELECT id FROM sample"
if self.debug:
logging.info(f"sample_selection_sql: {sample_selection_sql}")
Expand Down Expand Up @@ -1838,7 +1840,7 @@ def match( # noqa: 901
_1_rows = self.cursor.fetchall()

_2_final_sql = (
" SELECT name AS `sample.name`, nt_profile.reference_accession, nt_profile._profile AS NUC_PROFILE, aa_profile._profile AS AA_PROFILE \
" SELECT name AS `sample.name`, nt_profile.reference_accession AS REFERENCE_ACCESSION, nt_profile._profile AS NUC_PROFILE, aa_profile._profile AS AA_PROFILE \
FROM ( SELECT `sample.id`, `reference.accession` AS reference_accession, group_concat("
+ m
+ "`variant.label`) AS _profile \
Expand Down Expand Up @@ -1906,7 +1908,14 @@ def match( # noqa: 901
# _1_rows = list(filter(None, _1_rows))
# ------ alternative solution convert to df
df_1 = pd.DataFrame(_1_rows)
df_1.sort_values(by=["sample.name"], inplace=True)
if self.debug:
logging.debug(df_1["sample.name"])
# sample.name REFERENCE_ACCESSION NUC_PROFILE AA_PROFILE
df_2 = pd.DataFrame(_2_rows)
df_2.sort_values(by=["sample.name"], inplace=True)
if self.debug:
logging.debug(df_2["sample.name"])
"""
merge_df = pd.merge(
df_1,
Expand All @@ -1917,18 +1926,26 @@ def match( # noqa: 901
)
"""
# [tmp solution.]- we remove unused column TODO: fix this in the future.
df_1.drop(
columns=["NUC_PROFILE", "AA_PROFILE", "NUC_N_PROFILE", "AA_X_PROFILE"],
inplace=True,
)
# NOTE: some samples might have only meta info, if we choose inner map
# the result will be intersection of two datafram.
merge_df = pd.merge(
df_1,
df_2,
how="outer",
left_on=["sample.name"],
right_on=["sample.name"],
)
# fix column for ref with noCDS

# fix column for ref with no CDS
merge_df.loc[
merge_df["reference_accession"].isin(all_ref_nocds), "AA_PROFILE_y"
merge_df["REFERENCE_ACCESSION"].isin(all_ref_nocds), "AA_PROFILE"
] = "-"

merge_df.fillna("-", inplace=True)
_1_rows = merge_df.to_dict("records")
# filter column
if output_column != "all":
Expand All @@ -1942,6 +1959,7 @@ def match( # noqa: 901
return _1_rows # list(rows.values())

elif format == "count":
logging.info("'--count' will return only unique sample.")
sql = (
"SELECT COUNT(DISTINCT s2p.id) AS `count` FROM ("
+ sample_selection_sql
Expand Down
1 change: 0 additions & 1 deletion pages/libs/mpxsonar/src/mpxsonar/sonar.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,6 @@ def main(args): # noqa: C901
reserved_props, "sample", line.strip()
)
format = "count" if args.count else args.format

sonarBasics.match(
args.db,
args.profile,
Expand Down

0 comments on commit b5cca15

Please sign in to comment.