-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmage.py
187 lines (147 loc) · 7.63 KB
/
mage.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
import argparse
import configparser
import pandas as pd
import numpy as np
import time
# Custom Libraries
import gisu
import meta_analysis
import multivariate
import os
import plots
import enrichment_analysis
import simple_meta_analysis
from statsmodels.stats import multitest
global settings, gprofiler_settings, version, studies
settings = {}
studies = []
studies_transform = []
version = '1.0.4'
def parse_args():
print("Preparing System Arguments")
"""Parse input arguments."""
parser = argparse.ArgumentParser(description='Process Tool Arguments')
# One argument for the configuration file conf.txt
parser.add_argument('-c', metavar='--conf', required=True, help='Configuration File', type=str, default='conf.txt')
parser.add_argument('-o', metavar='--output', required=True, help='Output Directory', type=str, default='results/')
args = parser.parse_args()
return args
def apply_multiple_testing_corrections(df, alpha):
"""
Apply multiple hypothesis testing corrections to a column of p-values in a DataFrame.
Parameters:
- df: Pandas DataFrame containing a column of p-values.
- p_value_column: The name of the column containing p-values. Default is 'p_value'.
- alpha: Significance level for the hypothesis tests. Default is 0.0001.
Returns:
- Modified DataFrame with new columns for each of the adjusted p-values.
"""
# Extract the p-values from the specified column
pvals = df['p_value']
# Apply multiple testing correction methods
methods = ['fdr_bh', 'holm-sidak', 'simes-hochberg', 'bonferroni', 'holm']
corrected_p_values = {}
for method in methods:
corrected_p_values[method] = multitest.multipletests(pvals, alpha=alpha, method=method, is_sorted=False)[1]
# Add the corrected p-values as new columns in the DataFrame
for method, values in corrected_p_values.items():
df[f'{method}_adj_p_value'] = values
return df
def parse_conf(conf_filename):
print("Preparing System Configuration (" + conf_filename + ")")
"""Parse configuration arguments."""
conf = configparser.ConfigParser(comment_prefixes='#', allow_no_value=True)
conf.read(conf_filename)
for key, val in conf.items('SETTINGS'):
settings[key] = val
if __name__ == '__main__':
t0 = time.time()
print("MAGE :: Meta-Analysis of Gene Expression")
print("Version " + version + "; December 2022")
print("Copyright (C) 2021 Pantelis Bagos")
print("Freely distributed under the GNU General Public Licence (GPLv3)")
print("--------------------------------------------------------------------------")
# initialization step
args = parse_args()
parse_conf(args.c)
filepath = args.o
print("Loading File data")
#file_list = list(settings['study_files'].split(","))
file_list = os.listdir(settings['study_dir'])
print(file_list)
alpha = float(settings['significance_level'])
if settings.get('run_simple_meta_analysis') == 'YES':
# Run simple meta-analysis
metanalysis_df = simple_meta_analysis.simple_meta_analysis(file_list,settings['study_dir'],float(settings['significance_level']),settings['multiple_comparisons'])
metanalysis_df.to_csv(filepath + 'meta_analysis_results.txt', sep='\t', mode='w')
# create and save plots
if settings.get('plots') == 'YES':
plots.meta_analysis_plots(metanalysis_df, filepath, alpha)
if settings.get('enrichment_analysis') == 'YES':
print('Enrichment Analysis started')
genes_for_ea = metanalysis_df['Genes'].where(
np.array(metanalysis_df['p_value'], dtype=float) < np.array(metanalysis_df['simes'],
dtype=float)).dropna().tolist()
print(str(len(genes_for_ea)) + ' genes for Enrichment Analysis')
pd.DataFrame(genes_for_ea).to_csv(filepath + 'stat_significant_genes.txt', sep='\t', mode='w')
enrichment_analysis_df = enrichment_analysis.run(settings, genes_for_ea)
enrichment_analysis_df.to_csv(filepath + 'enrichment_analysis_results.txt',
header=enrichment_analysis_df.columns, index=None, sep='\t', mode='w')
if settings.get('plots') == 'YES':
plots.ea_manhattan_plot(enrichment_analysis_df, filepath, settings['threshold'])
plots.ea_heatmap_plot(enrichment_analysis_df, filepath)
t1 = time.time()
total = t1 - t0
print("Execution time = " + str(total) + '\t' + ' seconds')
exit()
for i in range(len(file_list)):
# Read file data
studypath = settings['study_dir'] +'/'+ file_list[i].strip()
file = pd.read_csv(studypath, sep='\t', low_memory=False, header=None,encoding = 'unicode_escape')
studies.append(file)
if settings.get('run_gisu') == 'YES':
print("Gene ID/Symbol update started")
platforms = list(settings['platform'].split(","))
for study in studies:
if settings['updated_genes'] == 'YES':
study_transform = gisu.run_updated_genes(settings, study)
else:
study_transform = gisu.run(settings, study, platforms[i])
studies_transform.append(study_transform)
data = studies_transform
else:
data = studies
if settings['multivariate'] == 'YES':
print('Multivariate Analysis started')
metanalysis_df = multivariate.run(settings, data, filepath)
metanalysis_df.to_csv(filepath + 'multivariate_analysis_results.txt', sep='\t', mode='w')
else:
print('Meta-analysis started')
metanalysis_df = meta_analysis.run(settings, data)
if settings ['bayesian_meta_analysis'] == 'YES':
print('Bayesian Meta-analysis started')
metanalysis_df.to_csv(filepath + 'bayesian_meta_analysis_results.txt', sep='\t', mode='w')
print('Bayesian Meta-analysis finished')
exit()
else:
#metanalysis_df = metanalysis_df.drop(['p_values_one_step', 'p_values_step_up', 'p_values_step_down','genes_one_step'], axis=1)
metanalysis_df = apply_multiple_testing_corrections(metanalysis_df, alpha=alpha)
metanalysis_df.to_csv(filepath + 'meta_analysis_results.txt', sep='\t', mode='w')
# create and save plots
if settings.get('plots') == 'YES':
plots.meta_analysis_plots(metanalysis_df, filepath,alpha)
if settings.get('enrichment_analysis') == 'YES':
print('Enrichment Analysis started')
genes_for_ea = metanalysis_df['Genes'].where(
np.array(metanalysis_df['p_value'],dtype=float) < np.array(metanalysis_df['simes'], dtype=float) ).dropna().tolist()
print(str(len(genes_for_ea))+' genes for Enrichment Analysis')
pd.DataFrame(genes_for_ea).to_csv(filepath + 'stat_significant_genes.txt', sep='\t', mode='w')
enrichment_analysis_df = enrichment_analysis.run(settings, genes_for_ea)
enrichment_analysis_df.to_csv(filepath + 'enrichment_analysis_results.txt',
header=enrichment_analysis_df.columns, index=None, sep='\t', mode='w')
if settings.get('plots') == 'YES':
plots.ea_manhattan_plot(enrichment_analysis_df, filepath, settings['threshold'])
plots.ea_heatmap_plot(enrichment_analysis_df, filepath)
t1 = time.time()
total = t1 - t0
print("Execution time = " + str(total) + '\t' + ' seconds')