forked from enormandeau/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgene_expression_correlation.py
More file actions
executable file
·231 lines (198 loc) · 6.7 KB
/
gene_expression_correlation.py
File metadata and controls
executable file
·231 lines (198 loc) · 6.7 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Correlating gene expression measures from two experiments
# Example, comparing NGS vs microarray data
__authors__ = "Eric Normandeau"
__program_name__ = "gene_expression_correlation"
__version_info__ = ('0', '0', '1')
__version__ = '.'.join(__version_info__)
__revision_date__ = "2010-12-14"
# Importing modules
import os
import sys
import getopt
import platform
# Function definitions
def find_ratios(r1, r2, c1, c2):
"""Desctiption
"""
return avg_ratios(c1, c2, import_ratios(r1, r2))
def import_ratios(r1, r2):
"""Create dictionary of sequence IDs and ratios from two files
"""
ratios = dict()
with open(r1) as f:
for l in f:
line = l.strip()
if line != "":
info = line.split("\t")
ratios[info[0]] = info[1]
with open(r2) as f:
for l in f:
line = l.strip()
if line != "":
info = line.split("\t")
ratios[info[0]] = info[1]
return ratios
def avg_ratios(c1, c2, ratios):
"""Create dictionary of cluster IDs and average ratios
"""
avg_ratios = dict()
with open(c1) as f:
for l in f:
line = l.strip()
if line != "":
info = line.split("\t")
ids = info[1:]
data = []
for i in ids:
data.append(float(ratios[i]))
avg_ratios[info[0]] = sum(data)/len(data)
with open(c2) as f:
for l in f:
line = l.strip()
if line != "":
info = line.split("\t")
ids = info[1:]
data = []
for i in ids:
data.append(float(ratios[i]))
avg_ratios[info[0]] = sum(data)/len(data)
return avg_ratios
def correlation(ratios, corr, output):
"""Desctiption
"""
with open(output, "w") as out_f:
with open(corr) as f:
for l in f:
line = l.strip()
if line != "":
data = line.split("\t")
out = "\t".join([data[0], data[1],
str(ratios[data[0]]), str(ratios[data[1]])])
out_f.write(out + "\n")
def help():
_plateform = platform.system()
name = __program_name__
text = """
%s(1) User Commands %s(1)
\033[1mNAME\033[0m
\t%s - NGS versus Microarray expression ratios
\033[1mSYNOPSIS\033[0m
\t\033[1mpython %s.py \033[0m[\033[4mOPTION\033[0m] [\033[4mFILE\033[0m]...
\033[1mDESCRIPTION\033[0m
\tGet corresponding expression ratios from NGS and Microarray data
\t%s uses expression ratios from two experiments (NGS and Microarray)
\tand returns the corresponding ratios in order to make plots of
\texpression correlation between the two experiments.
\033[1mOPTIONS\033[0m
\t\033[1m-h, --help\033[0m
\t\tDisplay the help of this program
\t\033[1m-r, --ratios_1\033[0m
\t\tInput file for ratios of the first experiment
\t\033[1m-R, --ratios_2\033[0m
\t\tInput file for ratios of the second experiment
\t\033[1m-c, --clusters_1\033[0m
\t\tInput file for sequence clusters of the first experiment
\t\033[1m-C, --clusters_2\033[0m
\t\tInput file for sequence clusters of the second experiment
\t\033[1m-k, --correspondance\033[0m
\t\tInput file for cluster correspondance between the experiments
\t\033[1m-o, --output\033[0m
\t\tOutput text file, in tab separated format.
\033[1mAUTHORS\033[0m
\t%s
%s %s %s %s(1)
"""%(name, name, name, name, name, __authors__, name, __version__, \
__revision_date__, name)
if _plateform != 'Windows' and "this is great news":
print text
else:
__Windows__ = "This is an abomination"
remove = ["\033[1m","\033[0m","\033[4m"]
for i in remove:
text = text.replace(i, "")
print text
del(__Windows__) # If only we could...
# Expression correlation
# Pray you confirm results
# From many techniques
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "hr:R:c:C:k:o:", ["help",
"ratios_1", "ratios_2", "clusters_1", "clusters_2"
"correspondance", "output="])
except getopt.GetoptError, e:
print "Input error. Use -h for help"
sys.exit(0)
output_file = None
header = True
for option, value in opts:
if option in ('-h', '--help'):
help()
sys.exit(0)
elif option in ('-r', '--ratios_1'):
ratios_1_file = value
elif option in ('-R', '--ratios_2'):
ratios_2_file = value
elif option in ('-c', '--clusters_1'):
clusters_1_file = value
elif option in ('-C', '--clusters_2'):
clusters_2_file = value
elif option in ('-k', '--correspondance'):
correspondance_file = value
elif option in ('-o', '--output'):
output_file = value
try:
with open(ratios_1_file) as test:
pass
except:
print "Input Error: No ratios_1 file specified or file not found."
print "Use -h for help."
sys.exit(0)
try:
with open(ratios_2_file) as test:
pass
except:
print "Input Error: No ratios_2 file specified or file not found."
print "Use -h for help."
sys.exit(0)
try:
with open(clusters_1_file) as test:
pass
except:
print "Input Error: No clusters_1 file specified or file not found."
print "Use -h for help."
sys.exit(0)
try:
with open(clusters_2_file) as test:
pass
except:
print "Input Error: No clusters_2 file specified or file not found."
print "Use -h for help."
sys.exit(0)
try:
with open(correspondance_file) as test:
pass
except:
print "Input Error: No correspondance file specified or file not found."
print "Use -h for help."
sys.exit(0)
try:
with open(output_file, "w") as test:
pass
except:
print "Output Error: No output file specified or incorect path."
print "Use -h for help."
sys.exit(0)
print "Using version:", __version__, "of", __program_name__
print "Last revision:", __revision_date__
print "By:", __authors__
print
# Return dict of cluster names and average ratios for both groups
cluster_ratios = find_ratios(ratios_1_file, ratios_2_file,
clusters_1_file, clusters_2_file)
# Put corresponding ratios together (with their IDs) and print to file
correlation(cluster_ratios, correspondance_file, output_file)
if __name__ == "__main__":
main()