Skip to content

Commit ae5e534

Browse files
Itol render (#96)
* deleting old test data * allow None as input to itol specs file
1 parent 60c312f commit ae5e534

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

q2_qemistree/_itol_metadata.py

+42-40
Original file line numberDiff line numberDiff line change
@@ -38,71 +38,73 @@ def classyfire_to_colors(classified_feature_data: pd.DataFrame,
3838
help='Path to feature data with Classyfire taxonomy.')
3939
@click.option('--feature-data-column', default='class', type=str,
4040
help='One of the columns in feature data table')
41+
@click.option('--color-palette', default='bright', type=str,
42+
help='Color palette for tree clades. One of the options'
43+
' allowed by seaborn.color_palette()')
4144
@click.option('--ms2-label', default=True, type=bool,
4245
help='Option to label tree tips with MS/MS library match')
43-
@click.option('--color-file-path', default='./itol_colors.txt', type=str,
46+
@click.option('--color-file-path', default=None, type=str,
4447
help='Path to file with colors specifications for tree clades')
45-
@click.option('--label-file-path', default='./itol_labels.txt', type=str,
48+
@click.option('--label-file-path', default=None, type=str,
4649
help='Path to file with label specifications for tree tips')
47-
@click.option('--color-palette', default='bright', type=str,
48-
help='Color palette for tree clades. One of the options'
49-
' allowed by seaborn.color_palette()')
50-
@click.option('--sample-feature-table', type=str,
50+
@click.option('--feature-table', default=None, type=str,
5151
help='Path to sample feature table.')
52-
@click.option('--sample-metadata', type=str,
52+
@click.option('--sample-metadata', default=None, type=str,
5353
help='Path to sample metadata.')
54-
@click.option('--sample-metadata-column', type=str,
54+
@click.option('--sample-metadata-column', default=None, type=str,
5555
help='Categorical sample metadata column.')
56-
@click.option('--barchart-file-path', default='./itol_bars.txt', type=str,
56+
@click.option('--barchart-file-path', default=None, type=str,
5757
help='Path to file with values for multi-value bar chart')
5858
def get_itol_visualization(classified_feature_data: str,
5959
feature_data_column: str = 'class',
6060
ms2_label: bool = True,
61-
color_file_path: str = './itol_colors.txt',
62-
label_file_path: str = './itol_labels.txt',
61+
color_file_path: str = None,
62+
label_file_path: str = None,
6363
color_palette: str = 'husl',
64-
sample_feature_table: str = None,
64+
feature_table: str = None,
6565
sample_metadata: str = None,
6666
sample_metadata_column: str = None,
67-
barchart_file_path: str = './itol_bars.qza'):
67+
barchart_file_path: str = None):
6868
'''This function creates iTOL metadata files to specify clade colors and
6969
tip labels based on Classyfire annotations. It also adds bar plots of the
7070
abundance of features stratified by user-specified sample metadata column.
7171
'''
7272
fdata = Artifact.load(classified_feature_data).view(pd.DataFrame)
7373
color_map = classyfire_to_colors(fdata, feature_data_column, color_palette)
74-
with open(color_file_path, 'w+') as fh:
75-
fh.write('TREE_COLORS\n'
76-
'SEPARATOR TAB\n'
77-
'DATA\n')
78-
for idx in fdata.index:
79-
color = color_map[fdata.loc[idx, feature_data_column]]
80-
if fdata.loc[idx, 'annotation_type'] == 'MS2':
81-
fh.write(idx + '\t' + 'clade\t' +
82-
color + '\tnormal\t6\n')
83-
if fdata.loc[idx, 'annotation_type'] == 'CSIFingerID':
84-
fh.write(idx + '\t' + 'clade\t' +
85-
color + '\tdashed\t4\n')
86-
with open(label_file_path, 'w+') as fh:
87-
fh.write('LABELS\n'
88-
'SEPARATOR TAB\n'
89-
'DATA\n')
90-
if ms2_label:
74+
if color_file_path:
75+
with open(color_file_path, 'w+') as fh:
76+
fh.write('TREE_COLORS\n'
77+
'SEPARATOR TAB\n'
78+
'DATA\n')
9179
for idx in fdata.index:
92-
ms2_compound = fdata.loc[idx, 'ms2_compound']
93-
if pd.notna(ms2_compound) and not ms2_compound.isspace():
94-
label = ms2_compound
95-
else:
80+
color = color_map[fdata.loc[idx, feature_data_column]]
81+
if fdata.loc[idx, 'annotation_type'] == 'MS2':
82+
fh.write(idx + '\t' + 'clade\t' +
83+
color + '\tnormal\t6\n')
84+
if fdata.loc[idx, 'annotation_type'] == 'CSIFingerID':
85+
fh.write(idx + '\t' + 'clade\t' +
86+
color + '\tdashed\t4\n')
87+
if label_file_path:
88+
with open(label_file_path, 'w+') as fh:
89+
fh.write('LABELS\n'
90+
'SEPARATOR TAB\n'
91+
'DATA\n')
92+
if ms2_label:
93+
for idx in fdata.index:
94+
ms2_compound = fdata.loc[idx, 'ms2_compound']
95+
if pd.notna(ms2_compound) and not ms2_compound.isspace():
96+
label = ms2_compound
97+
else:
98+
label = fdata.loc[idx, feature_data_column]
99+
fh.write(idx + '\t' + label + '\n')
100+
else:
101+
for idx in fdata.index:
96102
label = fdata.loc[idx, feature_data_column]
97-
fh.write(idx + '\t' + label + '\n')
98-
else:
99-
for idx in fdata.index:
100-
label = fdata.loc[idx, feature_data_column]
101-
fh.write(idx + '\t' + label + '\n')
103+
fh.write(idx + '\t' + label + '\n')
102104

103105
# generate bar chart
104106
if barchart_file_path:
105-
get_itol_barchart(fdata, sample_feature_table, sample_metadata,
107+
get_itol_barchart(fdata, feature_table, sample_metadata,
106108
sample_metadata_column, barchart_file_path)
107109

108110

0 commit comments

Comments
 (0)