[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EXP: try out some new manifest methods #2599

Open
wants to merge 8 commits into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add --labels-from to sourmash plot
  • Loading branch information
ctb committed Apr 29, 2023
commit 1a4a6b628807657578f446b64a6ac303fffc12d2
4 changes: 4 additions & 0 deletions src/sourmash/cli/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def subparser(subparsers):
help='write clustered matrix and labels out in CSV format (with column'
' headers) to this file'
)
subparser.add_argument(
'--labels-from', '--labels-load',
help='a CSV file containing label information to use on plot',
)


def main(args):
Expand Down
32 changes: 25 additions & 7 deletions src/sourmash/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,17 @@ def compare(args):
notify(f'saving labels to: {labeloutname}')
with sourmash_args.FileOutputCSV(labeloutname) as fp:
w = csv.writer(fp)
w.writerow(['md5', 'label', 'name', 'filename', 'signature_file'])
w.writerow(['order', 'md5', 'label', 'name', 'filename',
'signature_file'])

for ss, location in siglist:
for n, (ss, location) in enumerate(siglist):
md5 = ss.md5sum()
sigfile = location
label = str(ss)
name = ss.name
filename = ss.filename

w.writerow([md5, label, name, filename, sigfile])
w.writerow([str(n+1), md5, label, name, filename, sigfile])

# output CSV?
if args.csv:
Expand Down Expand Up @@ -271,10 +272,27 @@ def plot(args):
# not sure how to change this to use f-strings
notify('...got {} x {} matrix.', *D.shape)

if args.labeltext:
labelfilename = args.labeltext
notify(f'loading labels from {labelfilename}')
labeltext = [ x.strip() for x in open(labelfilename) ]
if args.labels_from: # use output from --labels-to => labels
if args.labeltext:
notify("ERROR: cannot supply both --labeltext and --labels-from")
sys.exit(-1)

labelfilename = args.labels_from
notify(f"loading labels from CSV file '{labelfilename}'")

labeltext = []
with sourmash_args.FileInputCSV(labelfilename) as r:
for row in r:
order, label = row['order'], row['label']
labeltext.append((int(order), label))
labeltext.sort()
labeltext = [ t[1] for t in labeltext ]
else: # use default or --labeltext text file
if args.labeltext:
labelfilename = args.labeltext
notify(f"loading labels from text file '{labelfilename}'")
labeltext = [ x.strip() for x in open(labelfilename) ]

if len(labeltext) != D.shape[0]:
error('{} labels != matrix size, exiting', len(labeltext))
sys.exit(-1)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_sourmash.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ def test_plot_override_labeltext(runtmp):

print(runtmp.last_result.out)

assert 'loading labels from new.labels.txt' in runtmp.last_result.err
assert "loading labels from text file 'new.labels.txt'" in runtmp.last_result.err

expected = """\
0\ta
Expand All @@ -947,7 +947,7 @@ def test_plot_override_labeltext_fail(runtmp):
print(runtmp.last_result.out)
print(runtmp.last_result.err)
assert runtmp.last_result.status != 0
assert 'loading labels from new.labels.txt' in runtmp.last_result.err
assert "loading labels from text file 'new.labels.txt'" in runtmp.last_result.err
assert '3 labels != matrix size, exiting' in runtmp.last_result.err


Expand Down