-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLI_post_process_nfcore_differentialabundance.py
More file actions
275 lines (256 loc) · 10.4 KB
/
CLI_post_process_nfcore_differentialabundance.py
File metadata and controls
275 lines (256 loc) · 10.4 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python3
"""Post-process nf-core/differentialabundance outputs via rnaseq_utils helpers.
example usage ran from repo_root/scripts/ directory:
python post_process_nfcore_differentialabundance.py \
--nfcore-rnaseq-dir ~/projects/gitbenlewis/nfcore_outputs/rnaseq/results \
--nfcore-da-dir ~/projects/gitbenlewis/nfcore_outputs/differentialabundance/results \
--h2m-map-file ~/projects/gitbenlewis/data/h2m_agg.csv \
--m2h-map-file ~/projects/gitbenlewis/data/m2h_agg.csv \
--organism human
verbose example usage with all options:
python post_process_nfcore_differentialabundance.py \
--nfcore-rnaseq-dir ~/projects/gitbenlewis/nfcore_outputs/rnaseq/results \
--nfcore-da-dir ~/projects/gitbenlewis/nfcore_outputs/differentialabundance/results \
--h2m-map-file ~/projects/gitbenlewis/data/h2m_agg.csv \
--m2h-map-file ~/projects/gitbenlewis/data/m2h_agg.csv \
--raw-table-suffix .deseq2.results.tsv \
--raw-table-separator '\t' \
--processed-table-suffix .deseq2.results.csv \
--concat-prefix differentialabundance \
--organism human \
--skip-make-gene-map \
--skip-add-gene-names \
--skip-rank-metric \
--skip-concat \
--no-add-homologs \
--no-sort-rank \
--no-save-concat
"""
import argparse
from pathlib import Path
from typing import Optional, Sequence
from ._rnaseq_utils import (
add_Rank_Metric_S_to_all_deseq2_tables_nfcore_differentialabundance,
add_gene_names_all_to_deseq2_tables_nfcore_differentialabundance,
concat_deseq2_files_nfcore_differentialabundance,
make_gene_id2gene_name_file,
)
def parse_args(argv: Optional[Sequence[str]] = None) -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Annotate and merge nf-core/differentialabundance DESeq2 tables.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--nfcore-rnaseq-dir",
type=Path,
#default=DEFAULT_RNASEQ_DIR,
help="Directory containing nf-core/rnaseq results (expects star_salmon subdir).",
)
parser.add_argument(
"--nfcore-da-dir",
type=Path,
#default=DEFAULT_DA_DIR,
help="nf-core/differentialabundance results directory (expects tables/differential).",
)
parser.add_argument(
"--output-dir",
type=Path,
help="Optional : Output directory for processed differential abundance results.If None, saves to nf-core DA output dir.",
)
parser.add_argument(
"--h2m-map-file",
type=Path,
#default=DEFAULT_H2M_FILE,
help="CSV mapping human gene IDs to mouse homologs (used if homologs enabled).",
)
parser.add_argument(
"--m2h-map-file",
type=Path,
#default=DEFAULT_M2H_FILE,
help="CSV mapping mouse gene IDs to human homologs (used if homologs enabled).",
)
parser.add_argument(
"--raw-table-suffix",
default=".deseq2.results.tsv",
help="Suffix for the raw nf-core DESeq2 result tables.",
)
parser.add_argument(
"--raw-table-separator",
default="\t",
help="Delimiter used inside the raw nf-core DESeq2 tables.",
)
parser.add_argument(
"--processed-table-suffix",
default=".deseq2.results.csv",
help="Suffix assigned to DESeq2 tables after adding gene names.",
)
parser.add_argument(
"--concat-prefix",
default="differentialabundance",
help="Prefix for the concatenated DESeq2 CSV file.",
)
parser.add_argument(
"--organism",
choices=("human", "mouse"),
default="human",
help="Reference organism for homolog annotations.",
)
parser.add_argument(
"--dataset-name",
type=str,
default="RNAseq Dataset",
help="Name of the dataset for plot titles and annotations.",
)
parser.add_argument(
"--skip-also-save-post-process-results-to-src-dir",
action="store_false",
default=True,
dest="also_save_post_process_results_to_src_dir",
help="Do not also save gene_id2gene_name.csv and processed DESeq2 tables back to the nf-core source directories.",
)
parser.add_argument(
"--skip-make-gene-map",
action="store_true",
help="Assume gene_id2gene_name.csv already exists and skip rebuilding it.",
)
parser.add_argument(
"--skip-add-gene-names",
action="store_true",
help="Skip adding gene name annotations to DESeq2 tables.",
)
parser.add_argument(
"--skip-rank-metric",
action="store_true",
help="Skip computing the Rank_Metric_S column across DESeq2 tables.",
)
parser.add_argument(
"--skip-concat",
action="store_true",
help="Skip concatenating the processed DESeq2 tables.",
)
parser.add_argument(
"--skip-plots",
action="store_true",
help="Skip generating volcano and MA plots from processed DESeq2 tables.",
)
parser.add_argument(
"--no-add-homologs",
action="store_false",
dest="add_homologs",
help="Disable merging homolog annotations when building the gene map.",
)
parser.add_argument(
"--no-sort-rank",
action="store_false",
dest="sort_rank",
help="Do not sort individual DESeq2 tables after computing Rank_Metric_S.",
)
parser.add_argument(
"--no-save-concat",
action="store_false",
dest="save_concat",
help="Do not write the concatenated CSV to disk (still returns dataframe).",
)
parser.set_defaults(add_homologs=True, sort_rank=True, save_concat=True, also_save_post_process_results_to_src_dir=True)
return parser.parse_args(argv)
def run_pipeline(args: argparse.Namespace) -> None:
nfcore_rnaseq_dir = str(args.nfcore_rnaseq_dir)
nfcore_da_dir = str(args.nfcore_da_dir)
output_dir = str(args.output_dir) if args.output_dir is not None else None
h2m_file = str(args.h2m_map_file)
m2h_file = str(args.m2h_map_file)
if args.skip_make_gene_map:
print("Skipping gene_id2gene_name generation (per flag).")
else:
print(f"Creating gene_id2gene_name.csv in {args.output_dir}")
if args.also_save_post_process_results_to_src_dir:
print(f"also Creating gene_id2gene_name.csv in {args.nfcore_rnaseq_dir / 'star_salmon'}")
make_gene_id2gene_name_file(
nfcore_output_rnaseq_dir=nfcore_rnaseq_dir,
add_homologs=args.add_homologs,
organism=args.organism,
h2m_agg_file=h2m_file,
m2h_agg_file=m2h_file,
output_dir=output_dir,
also_save_to_src_dir=args.also_save_post_process_results_to_src_dir
)
if args.skip_add_gene_names:
print("Skipping gene name annotation of DESeq2 tables (per flag).")
else:
print("Adding gene names to nf-core/differentialabundance DESeq2 tables.")
add_gene_names_all_to_deseq2_tables_nfcore_differentialabundance(
nfcore_DA_output_dir=nfcore_da_dir,
nfcore_output_rnaseq_dir=nfcore_rnaseq_dir,
input_file_suffix=args.raw_table_suffix,
input_file_separator=args.raw_table_separator,
output_file_suffix=args.processed_table_suffix,
run_make_gene_id2gene_name_file=True,
add_homologs=args.add_homologs,
organism=args.organism,
h2m_map_file=h2m_file,
m2h_map_file=m2h_file,
output_dir=output_dir,
also_save_to_src_dir=args.also_save_post_process_results_to_src_dir
)
if args.skip_rank_metric:
print("Skipping Rank_Metric_S calculation (per flag).")
else:
print("Adding Rank_Metric_S to processed DESeq2 tables.")
add_Rank_Metric_S_to_all_deseq2_tables_nfcore_differentialabundance(
nfcore_DA_output_dir=nfcore_da_dir,
input_file_suffix=args.processed_table_suffix,
sort_local=args.sort_rank,
output_dir=output_dir,
)
if args.skip_concat:
print("Skipping concatenation of DESeq2 tables (per flag).")
else:
print("Concatenating annotated DESeq2 tables.")
if output_dir is not None:
print(f"Saving concatenated file to output dir: {output_dir}")
concat_deseq2_files_nfcore_differentialabundance(
nfcore_DA_output_dir=nfcore_da_dir,
non_nfcore_DA_output_dir=output_dir,
input_file_suffix=args.processed_table_suffix,
output_file_prefix=args.concat_prefix,
save_output=args.save_concat,
output_dir=output_dir,
also_save_to_src_dir=args.also_save_post_process_results_to_src_dir
)
if args.skip_plots:
print("Skipping generation of volcano and MA plots (per flag).")
else:
from . import _rnaseq_utils as rnaseq_utils
rnaseq_utils.make_volcano_MA_plots_from_nf_deseq_output_dir(
output_dir,
file_sufix='.deseq2.results.csv',
plot_dataset_title=args.dataset_name,
log2FoldChange_threshold=0.5, ylimit_volcano=None,xlimit_volcano_l2fc=None, xlimit_MAonly=None,
label_gene_name=False,figsize=(12,12),
save_plots=True,
save_dir='std_plots'
)
def main(argv: Optional[Sequence[str]] = None) -> None:
args = parse_args(argv)
run_pipeline(args)
if __name__ == "__main__":
main()
# ---------------------------------------------------------------------
# Example usage:
# python scripts/post_process_nfcore_differentialabundance.py \
# --nfcore-rnaseq-dir ~/projects/gitbenlewis/nfcore_outputs/rnaseq/results \
# --nfcore-da-dir ~/projects/gitbenlewis/nfcore_outputs/differentialabundance/results \
# --organism human
# ---------------------------------------------------------------------
# Verbose usage:
# python scripts/post_process_nfcore_differentialabundance.py \
# --nfcore-rnaseq-dir ~/projects/gitbenlewis/nfcore_outputs/rnaseq/results \
# --nfcore-da-dir ~/projects/gitbenlewis/nfcore_outputs/differentialabundance/results \
# --h2m-map-file ~/projects/gitbenlewis/data/h2m_agg.csv \
# --m2h-map-file ~/projects/gitbenlewis/data/m2h_agg.csv \
# --raw-table-suffix .deseq2.results.tsv \
# --raw-table-separator $'\\t' \
# --processed-table-suffix .deseq2.results.csv \
# --concat-prefix differentialabundance \
# --organism human
# ---------------------------------------------------------------------