-
Notifications
You must be signed in to change notification settings - Fork 9
/
_plot.py
129 lines (119 loc) · 3.62 KB
/
_plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# ----------------------------------------------------------------------------
# Copyright (c) 2018--, Qurro development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE.txt, distributed with this software.
# ----------------------------------------------------------------------------
import logging
from biom import load_table
import click
from qurro._parameter_descriptions import (
EXTREME_FEATURE_COUNT,
TABLE,
ASSUME_GNPS_FEATURE_METADATA,
)
from qurro.generate import process_and_generate
from qurro._rank_utils import read_rank_file
from qurro._metadata_utils import (
read_metadata_file,
read_gnps_feature_metadata_file,
)
from qurro._df_utils import escape_columns
@click.command()
@click.option(
"-r",
"--ranks",
required=True,
help="Feature differentials or a biplot OrdinationResults.",
)
@click.option("-t", "--table", required=True, help=TABLE)
@click.option(
"-fm", "--feature-metadata", default=None, help="Feature metadata file."
)
@click.option(
"-sm", "--sample-metadata", required=True, help="Sample metadata file."
)
@click.option(
"-o",
"--output-dir",
required=True,
help=(
"Directory to write the HTML/JS/... files defining a Qurro "
"visualization to. If this directory already exists, "
"files/directories already within it will be overwritten if necessary."
),
)
@click.option(
"-x",
"--extreme-feature-count",
default=None,
type=int,
help=EXTREME_FEATURE_COUNT,
)
@click.option(
"-gnps",
"--assume-gnps-feature-metadata",
is_flag=True,
help=ASSUME_GNPS_FEATURE_METADATA,
)
@click.option(
"-v",
"--verbose",
is_flag=True,
help="If passed, this will output debug messages.",
)
def plot(
ranks: str,
table: str,
sample_metadata: str,
feature_metadata: str,
output_dir: str,
extreme_feature_count: int,
assume_gnps_feature_metadata: bool,
verbose: bool,
) -> None:
"""Generates a visualization of feature rankings and log ratios.
The resulting visualization contains two plots. The first plot shows
how features are ranked, and the second plot shows the log ratio
of "selected" features' abundances within samples.
The visualization is interactive, so which features are "selected" to
construct log ratios -- as well as various other properties of the
visualization -- can be changed by the user.
"""
# inspired by https://stackoverflow.com/a/14098306/10730311
if verbose:
logging.basicConfig(level=logging.DEBUG)
logging.debug("Starting the standalone Qurro script.")
loaded_biom = load_table(table)
logging.debug("Loaded BIOM table.")
df_sample_metadata = escape_columns(
read_metadata_file(sample_metadata), "sample metadata"
)
feature_ranks = read_rank_file(ranks)
df_feature_metadata = None
if feature_metadata is not None:
if assume_gnps_feature_metadata:
df_feature_metadata = read_gnps_feature_metadata_file(
feature_metadata, feature_ranks
)
else:
df_feature_metadata = escape_columns(
read_metadata_file(feature_metadata), "feature metadata"
)
logging.debug("Read in metadata.")
process_and_generate(
feature_ranks,
df_sample_metadata,
loaded_biom,
output_dir,
df_feature_metadata,
extreme_feature_count,
)
print(
"Successfully generated a visualization in the folder {}.".format(
output_dir
)
)
if __name__ == "__main__":
plot()