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

Sort by category #57

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
31 changes: 29 additions & 2 deletions src/sourmash_plugin_betterplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,10 @@ def __init__(self, subparser):
"--no-y-labels", action="store_true",
help="disable Y axis labels"
)
subparser.add_argument(
"--sort-by-category", action="store_true",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"--sort-by-category", action="store_true",
"--sort-rows-by-category", action="store_true",

You could imagine we might want to sort cols by category too ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(so having this be --sort-rows... leaves that option open - not that we need to implement it now)

help="Sort rows by category, instead of clustering them"
)

def main(self, args):
super().main(args)
Expand All @@ -864,14 +868,30 @@ def main(self, args):
# pick out all the distinct queries/matches.
notify(f"loaded {len(rows)} rows from '{args.manysearch_csv}'")

query_d = manysearch_rows_to_index(rows, column_name='query_name')
against_d = manysearch_rows_to_index(rows, column_name='match_name')

# optional sorting samples by category
if args.sort_by_category:
query_d = manysearch_rows_to_index(rows, column_name='query_name')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code looks good! can you move into a separate function?

# make df for color input
df_col = pd.read_csv(args.row_categories_csv)
# make a df from query d
df_query = pd.DataFrame(list(query_d.items()), columns=['label', 'order'])
# merge, sort on category and reorder
df_col = df_col.merge(df_query, on='label')
df_col = df_col.sort_values(by=['category'])
df_col['order'] = range(len(df_col))
# put reordered back into a dict
query_d = df_col.set_index('label')['order'].to_dict()
else:
query_d = manysearch_rows_to_index(rows, column_name='query_name')

against_d = manysearch_rows_to_index(rows, column_name='match_name')
notify(f"loaded {len(query_d)} x {len(against_d)} total elements")

query_d_items = list(sorted(query_d.items(), key=lambda x: x[1]))
against_d_items = list(sorted(against_d.items(), key=lambda x: x[1]))


mat = numpy.zeros((len(query_d), len(against_d)))

colname = args.use_column
Expand Down Expand Up @@ -910,6 +930,7 @@ def main(self, args):
if args.boolean: # turn off colorbar if boolean.
kw_args['cbar_pos'] = None


yticklabels=sample_d_to_idents(query_d_items)
xticklabels=sample_d_to_idents(against_d_items)
if args.no_labels:
Expand All @@ -920,6 +941,11 @@ def main(self, args):
elif args.no_y_labels:
yticklabels = []

if args.sort_by_category:
row_cluster=False
else:
row_cluster=True

# turn into dissimilarity matrix
# plot!
fig = sns.clustermap(
Expand All @@ -928,6 +954,7 @@ def main(self, args):
vmin=args.vmin,
vmax=args.vmax,
col_colors=col_colors,
row_cluster=row_cluster,
row_colors=row_colors,
xticklabels=xticklabels,
yticklabels=yticklabels,
Expand Down
Loading