Skip to content

Commit

Permalink
If match returns no rows, still output column names
Browse files Browse the repository at this point in the history
  • Loading branch information
matthuska committed Oct 30, 2023
1 parent 4432ba1 commit 45332c3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
10 changes: 6 additions & 4 deletions lib/sonardb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1999,7 +1999,9 @@ def match(
print("query: " + sql)
print("vals: ", where_vals)

return self.cursor.execute(sql, where_vals).fetchall()
cursor = self.cursor.execute(sql, where_vals)
column_names = [description[0] for description in cursor.description]
return cursor.fetchall(), column_names

# UPDATE DATA

Expand Down Expand Up @@ -3423,7 +3425,7 @@ def match(
# print(include_lin)

########################
rows = dbm.match(
rows, column_names = dbm.match(
include_profiles,
exclude_profiles,
include_acc,
Expand Down Expand Up @@ -3479,7 +3481,7 @@ def match(
elif count:
return rows[0]["count"]

return rows
return rows, column_names

# VALIDATION

Expand Down Expand Up @@ -3761,7 +3763,7 @@ def be_paranoid(self, acc, orig_seq, auto_delete=False, dbm=None):
)

# frameshift checks
row = self.match(accessions=[acc], ambig=True, dbm=dbm)[0]
row = self.match(accessions=[acc], ambig=True, dbm=dbm)[0][0]
fs = set()
for dna_var in row["dna_profile"].split(" "):
if dna_var.strip() == "":
Expand Down
19 changes: 8 additions & 11 deletions sonar.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ def match_genomes(
frameshifts=0,
tsv=False,
):
rows = self.db.match(
rows, column_names = self.db.match(
include_profiles=include_profiles,
exclude_profiles=exclude_profiles,
accessions=accessions,
Expand Down Expand Up @@ -765,7 +765,7 @@ def match_genomes(
if count:
print(rows)
else:
self.rows_to_csv(rows, na="*** no match ***", tsv=tsv)
self.rows_to_csv(rows, column_names, na="*** no match ***", tsv=tsv)

def update_metadata(
self,
Expand Down Expand Up @@ -921,17 +921,14 @@ def show_db_info(self):
spacer = " " * (maxlen - len(field))
print(" " + field + " information:" + spacer, f"{c} ({p:.{2}f}%)")

def rows_to_csv(self, rows, file=None, na="*** no data ***", tsv=False):
def rows_to_csv(self, rows, header, file=None, na="*** no data ***", tsv=False):
if len(rows) == 0:
print(na, file=sys.stderr)
else:
file = sys.stdout if file is None else open(file, "w")
sep = "\t" if tsv else ","
writer = csv.DictWriter(
file, rows[0].keys(), delimiter=sep, lineterminator=os.linesep
)
writer.writeheader()
writer.writerows(rows)
file = sys.stdout if file is None else open(file, "w")
sep = "\t" if tsv else ","
writer = csv.DictWriter(file, header, delimiter=sep, lineterminator=os.linesep)
writer.writeheader()
writer.writerows(rows)

def get_db_size(self, decimal_places=3):
size = os.path.getsize(self.dbfile)
Expand Down

0 comments on commit 45332c3

Please sign in to comment.