-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_network_creation.py
387 lines (334 loc) · 17.2 KB
/
run_network_creation.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
"""
Description:
This script iterates through a directory of weight matrices generated
by a feature construction algorithm trained on a transcriptomic dataset.
We allow this analysis to be run on 1 or more weight matrices with
the same shape[*], in the event that multiple models of the data were
produced by the same algorithm initialized with different random seeds.
[*]shape [n,k], where n = all genes for which we have transcriptomic data,
k = number of features constructed.
Defaults are set up for the P. aeruginosa eADAGE analysis provided
in the PathCORE-T paper.
Output (all .tsv files):
Significant pathways files produced from the pathway overrepresentation
analysis. For each model, indicates which pathways are enriched in which
features.
`networks` directory:
Networks built using each of the top-level significant pathways files
If -m | --metadata:
An additional `metadata` directory is created in the output directory.
The metadata files contain more information (specific to each
model) about
* what the gene signature(s) were for each feature
* the genes in the gene signature(s) that were annotated to a pathway
after crosstalk-correction in each feature, if applicable.
Usage:
run_network_creation.py
<models-dir> <output-dir> <pathway-definitions>
[--n-genes=<n-genes>] [--n-features=<n-features>]
[--signature=<signature>] [--signature-args=<s-args>]
[--alpha=<alpha>]
[--genes-list=<genes-list>]
[--n-cores=<n-cores>]
[--replace=<replace>] [--shorten=<pathway-shorten>]
[-o | --overlap-correction] [-a | --all-genes]
[-m | --metadata]
run_network_creation.py -h | --help
Options:
-h --help Show this screen.
<models-dir> Path to the directory containing the models
generated by an unsupervised feature
construction algorithm.
<output-dir> Path to the directory that will store the
output files. Will be automatically created
if the directory does not currently exist.
<pathway-definitions> Path to the pathway definitions file.
Formatted as tab-delimited columns:
pathway, N (num. genes), geneA;geneB;...geneN
--n-genes=<n-genes> Number of genes for which we have recorded
expression values in the original dataset
--n-features=<n-features> Number of constructed features in each model
(currently, they must all contain the same
number of features)
[default: 300]
--signature=<signature> Specify the gene signature definition to use
in this analysis. Please review
`constants/gene_signature_definitions.py`
to select or modify the file to include
a partial function that determines the gene
signature for a given feature construction
algorithm.
[default: eADAGE]
--signature-args=<s-args> Depends on the gene signature definition
selected <signature>. Provide a
semicolon-separated list of input arguments
needed to use partial function.
In the eADAGE signature definition, we only
require a user to specify standard deviations
from the mean---hence the default.
[default: 2.5]
--alpha=<alpha> Significance level for pathway enrichment.
Overrepresentation is determined by a Fisher's
exact test with false-discovery rate
correction.
[default: 0.05]
--genes-list=<genes-list> If the weight matrix files do not include the
gene identifiers corresponding to each row,
please include the path to a file that contains
these identifiers (1 gene id per line).
--n-cores=<n-cores> Number of cores used to run the analysis
on models in parallel
[default: num. available cores - 1]
--replace=<replace> The resulting file keeps the naming convention
of the input file, with the exception of this
substring, which will be replaced by
'SigPathways'
[default: network]
--shorten=<pathway-shorten> Specify a function to shorten pathway
names for the network. Useful for
readability in network visualizations.
Please review
`constants/shorten_pathway_names.py`
and select or modify the file to include
a function that takes in a pathway label
and outputs a shortened version of it.
-o --overlap-correction Donato et al.'s (2013) crosstalk correction
algorithm is applied to the input pathway
definitions.
[default: True]
-a --all-genes If `--overlap-correction`, this flag
applies the correction to the full
gene set annotated to each pathway, as opposed
to only genes that are also in a feature's gene
signature.
[default: True]
-m --metadata Collect metadata after overrepresentation
analysis of each constructed feature.
Used for setting up the PathCORE-T Flask
application.
[default: False]
"""
import csv
import multiprocessing
import os
from time import time
from docopt import docopt
from joblib import Parallel, delayed
import pandas as pd
from pathcore import pathway_enrichment_with_overlap_correction, \
pathway_enrichment_no_overlap_correction
from pathcore import CoNetwork
from constants import GENE_SIGNATURE_DEFINITIONS
from constants import SHORTEN_PATHWAY_NAMES
from utils import load_pathway_definitions, load_weight_matrix
# include in the significant pathways output filename
OUTPUT_FILE_NAMING = "SigPathway"
# include in the metadata filenames
METADATA_FILE_ENDS_WITH = "pathcore_overrepresentation_analysis.tsv"
def process(current_weight_matrix, pathway_definitions,
partial_function_signature,
alpha=0.05, overlap_correction=True, correct_all_genes=True,
metadata=False):
"""
Determines the pathway coverage of a weight matrix
Parameters
-----------
current_weight_matrix : (str, pandas.DataFrame)
(weight matrix filename, weight matrix dataframe of size [n,k])
pathway_definitions : dict(str -> set(str))
Pathway definitions, pre-overlap-correction.
A pathway (key) is defined by a set of genes (value).
partial_function_signature : functools.partial
Accepts a feature weight vector of type pandas.Series(float)
and returns (set(), set()), the feature's positive and/or negative
gene signatures. See `gene_signature_definitions.py` in
`constants` for examples.
alpha : float, default=0.05
Significance level for pathway enrichment.
overlap_correction : bool (default=True)
Apply Donato et al.'s (2013) maximum impact estimation algorithm
on the pathway definitions to remove gene overlap.
correct_all_genes : bool (default=True)
For `overlap_correction=True`, the correction is always
applied to the gene signature(s).
When `correct_all_genes=True`, the correction is applied to genes
outside of the signature(s)--termed *remaining* genes.
metadata : bool (default=False)
When True, outputs additional files about the per-feature
overrepresentation analysis, gene signatures, and pathway annotations
into a `metadata` directory.
Returns
-----------
dict() with the following items:
- "filename": str filename
- "significant_pathways": pandas.DataFrame significant
pathways dataframe
- "feature_metadata": dict(int -> dict) if applicable.
* keys are the feature numbers
* values are the metadata dicts
"""
filename, weight_matrix = current_weight_matrix
n_genes, _ = weight_matrix.shape
print("Processing model {0}".format(filename))
significant_pathways_df = pd.DataFrame()
feature_metadata = {}
for feature in weight_matrix:
if overlap_correction:
feature_df, additional = \
pathway_enrichment_with_overlap_correction(
weight_matrix[feature], pathway_definitions,
partial_function_signature,
alpha=alpha, correct_all_genes=correct_all_genes,
metadata=metadata)
else:
feature_df, additional = \
pathway_enrichment_no_overlap_correction(
weight_matrix[feature], pathway_definitions,
partial_function_signature,
alpha=alpha, metadata=metadata)
if feature_df is not None:
feature_df.loc[:, "feature"] = pd.Series(
[feature] * len(feature_df.index), index=feature_df.index)
significant_pathways_df = pd.concat(
[significant_pathways_df, feature_df], axis=0)
if additional:
feature_metadata[feature] = additional
significant_pathways_df.reset_index(drop=True, inplace=True)
return {"filename": filename,
"significant_pathways": significant_pathways_df,
"feature_metadata": feature_metadata}
def _write_metadata_tsv(partial_filename,
rows,
model_number,
output_directory):
"""A small helper function for writing the metadata information
to a .tsv file
"""
filepath = os.path.join(
output_directory,
"{0}_{1}_{2}".format(
model_number, partial_filename, METADATA_FILE_ENDS_WITH))
with open(filepath, "w", newline="") as fp:
writer = csv.writer(fp, delimiter="\t")
for row in rows:
writer.writerow(row)
if __name__ == "__main__":
arguments = docopt(
__doc__, version="build co-occurrence network 1.0")
models_directory = arguments["<models-dir>"]
output_directory = arguments["<output-dir>"]
pathway_definitions_file = arguments["<pathway-definitions>"]
n_genes = None
if arguments["--n-genes"]:
n_genes = int(arguments["--n-genes"])
n_features = int(arguments["--n-features"])
if arguments["--signature"] not in GENE_SIGNATURE_DEFINITIONS:
raise ValueError("The gene signature definition specified ({0}) "
"is not in the `GENE_SIGNATURE_DEFINITIONS` "
"dict. Please include your definition in "
"`gene_signature_definitions.py` before running "
"this script.".format(arguments["--signature"]))
which_signature = GENE_SIGNATURE_DEFINITIONS[arguments["--signature"]]
gene_signature_args = arguments["--signature-args"].split(";")
# NOTE: no type casting done, all arguments will be strings.
partial_function_signature = which_signature(*gene_signature_args)
alpha = float(arguments["--alpha"])
path_to_genes_file = arguments["--genes-list"]
substring_to_replace = arguments["--replace"]
overlap_correction = arguments["--overlap-correction"]
correct_all_genes = arguments["--all-genes"]
network_output_directory = os.path.join(
output_directory, "networks")
os.makedirs(output_directory, exist_ok=True)
os.makedirs(network_output_directory, exist_ok=True)
metadata = arguments["--metadata"]
metadata_output_directory = None
if metadata:
metadata_output_directory = os.path.join(
output_directory, "metadata")
os.makedirs(metadata_output_directory, exist_ok=True)
if arguments["--n-cores"].isdigit():
n_cores = int(arguments["--n-cores"])
else:
n_cores = multiprocessing.cpu_count() - 1
t_o = time()
shorten_function_key = arguments["--shorten"]
shorten_pathway_names = None
if shorten_function_key and shorten_function_key in SHORTEN_PATHWAY_NAMES:
shorten_pathway_names = SHORTEN_PATHWAY_NAMES[shorten_function_key]
elif shorten_function_key not in SHORTEN_PATHWAY_NAMES:
raise ValueError("The shorten pathway function specified ({0}) "
"is not in the `SHORTEN_PATHWAY_NAMES` "
"dict. Please include your definition in "
"`shorten_pathway_names.py` before running this"
"script.".format(arguments["shorten_function_key"]))
pathway_definitions = load_pathway_definitions(
pathway_definitions_file,
shorten_pathway_names=shorten_pathway_names)
weight_matrices = []
for model in os.listdir(models_directory):
path_to_model_file = os.path.join(models_directory, model)
weight_matrix = load_weight_matrix(
path_to_model_file, n_features,
n_genes=n_genes, path_to_genes_file=path_to_genes_file)
weight_matrices.append((model, weight_matrix))
process_model_args = [
pathway_definitions, partial_function_signature,
alpha, overlap_correction, correct_all_genes, metadata
]
# Please note that we are not accounting for cases where `results`
# may be too large to fit into memory. The information stored in
# `results` is not written to any files until after all weight matrices
# have been processed. (see: line 313)
with Parallel(n_jobs=n_cores) as parallel:
results = parallel(
delayed(process)(weight_matrix_info, *process_model_args)
for weight_matrix_info in weight_matrices)
t_f = time() - t_o
print("{0} model(s) took {1} seconds to run on {2} core(s).".format(
len(results), t_f, n_cores))
for model_number, model_results in enumerate(results):
model_filename = model_results["filename"]
significant_pathways_df = model_results["significant_pathways"]
# write to significant pathways file
significant_pathways_out = model_filename.replace(
substring_to_replace, OUTPUT_FILE_NAMING)
significant_pathways_out_path = os.path.join(
output_directory, "{0}_{1}".format(
model_number, significant_pathways_out))
significant_pathways_df.to_csv(
path_or_buf=significant_pathways_out_path, sep="\t", index=False)
# write to the network file
model_network = CoNetwork(
n_features, significant_pathways=significant_pathways_df)
model_network_df = model_network.to_dataframe()
network_out = "{0}_pathcore_network.tsv".format(model_number)
network_out_path = os.path.join(network_output_directory, network_out)
model_network_df.to_csv(network_out_path, sep="\t", index=False)
# write to metadata files
if model_results["feature_metadata"]:
# both lists will store the rows that are written to their
# corresponding files.
# initialize both with the column names as the first row.
gene_signatures = [
("feature", "positive_signature", "negative_signature")]
signature_pathway_definitions = [
("feature", "pathway", "gene_signature_definition")]
for feature_number, metadata_info in \
model_results["feature_metadata"].items():
gene_signatures.append(
(feature_number,
";".join(metadata_info["positive_signature"]),
";".join(metadata_info["negative_signature"])))
for pathway, definition in \
metadata_info["pathway_definitions"].items():
definition_genes = ";".join(definition)
if definition_genes:
signature_pathway_definitions.append(
(feature_number, pathway, definition_genes))
_write_metadata_tsv(
"FEATURE_SIGNATURES", gene_signatures,
model_number, metadata_output_directory)
_write_metadata_tsv(
"FEATURE_PATHWAYS", signature_pathway_definitions,
model_number, metadata_output_directory)