-
Notifications
You must be signed in to change notification settings - Fork 0
/
sex_bias_asex_edgeR_tidier.py
317 lines (219 loc) · 7.7 KB
/
sex_bias_asex_edgeR_tidier.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
##### sex_bias_asex_edgeR_tidier.py
import sys
import os
import getopt
import decimal
try:
opts, args = getopt.getopt(sys.argv[1:], 'i:o:s:a:f:c:x:h')
except getopt.GetoptError:
print('ERROR getting options, please see help by specifing -h')
sys.exit(2) ### close ofter error from try
arg_len = len(opts)
if arg_len == 0:
print('No options provided. Please see help by specifing -h')
sys.exit(2)
in_dir_name = "NOTHINGSET"
SB_file_names = "NOTHINGSET" ### sexual female vs sexual male
Asex_file_name_sub = "NOTHINGSET" ### asexual female vs sexual female file name sub for the "sex_bias" part of the sex_bias file names
output_filename_base = "NOTHINGSET"
FDR_cutoff = "NOTHINGSET"
cpm_filename = "NOTHINGSET"
FPKM_filename = "NOTHINGSET"
#print (opts) ## see args
for opt, arg in opts:
if opt in ('-h', '--help'):
print("\n**** sex_bias_asex_edgeR_tidier_5.py ****\n")
print("\ntakes 2 egdeR outputfiles from Timema samples and an expression file and joins them all together: \
1: where sex bias has been compared per contig in sexuals, 2: of a different comparsion (e.g. sex/asex) 3 (opt) cpm file 4 (opt) FPKM file")
print("NOTE it outputs genes alphabetical order. If gene is missing from a sample, values are set to NA")
print("\n**** USAGE **** \n")
print("\nSee README file \n")
sys.exit(2)
elif opt in ('-i'):
in_dir_name = arg
elif opt in ('-o'):
output_filename_base = arg
elif opt in ('-s'):
SB_file_names = arg
elif opt in ('-a'):
Asex_file_name_sub = arg
elif opt in ('-f'):
FDR_cutoff = arg
elif opt in ('-c'):
cpm_filename = arg
elif opt in ('-x'):
FPKM_filename = arg
else:
print("i dont know")
sys.exit(2)
if FDR_cutoff == "NOTHINGSET":
print("\nFDR cutoff for calling sex-bias set to 0.05. Use -f to alter this\n")
FDR_cutoff = 0.05
else:
print("\nFDR cutoff set to " + str(FDR_cutoff) +"\n")
if cpm_filename == "NOTHINGSET":
print("\nSkipping cpm in the output. Specify -c to add this\n")
else:
print("Using CPM values from " + cpm_filename)
### GET ALL SB gene names
all_gene_names = set()
sample_name_list = []
sb_filename_list = SB_file_names.split(",")
for f in sb_filename_list:
SB_file_path = os.path.join(in_dir_name, f)
SB_file = open(SB_file_path)
#print(f)
sample_name = f.split("_")[2] + "_" + f.split("_")[5]
sample_name = sample_name.split(".")[0]
sample_name_list.append(sample_name)
#print(sample_name)
line_N = 0
for line in SB_file:
line_N = line_N + 1
if line_N != 1:
line = line.rstrip("\n").split(",")
gene_name = line[0]
all_gene_names.add(gene_name)
SB_file.close()
print("\nNumber of unique genes in input files: " + str(len(all_gene_names)) + "\n")
### get SB info to dict
SB_dict_2 = {}
sb_filename_list = SB_file_names.split(",")
for f in sb_filename_list:
SB_file_path = os.path.join(in_dir_name, f)
SB_file = open(SB_file_path)
#print(f)
sample_name = f.split("_")[2] + "_" + f.split("_")[5]
sample_name = sample_name.split(".")[0]
#print(sample_name)
line_N = 0
for line in SB_file:
line_N = line_N + 1
if line_N > 1:
line = line.rstrip("\n").split(",")
gene_name = line[0] + "SPLTTTYNNNN____HH_" + sample_name
log2_FC = decimal.Decimal(line[1])
FDR_val = decimal.Decimal(line[5])
bias = ""
if FDR_val < FDR_cutoff and log2_FC > 0: #### female biased
bias = "female_biased"
elif FDR_val < FDR_cutoff and log2_FC < 0: #### male biased
bias = "male_biased"
elif FDR_val < FDR_cutoff and log2_FC == 0: #### just in case
bias = "ERROR?"
else:
bias = "Unbiased"
### add to dict
new_rec = str(log2_FC) + "," + str(FDR_val) + "," + bias
SB_dict_2[gene_name] = new_rec
# print(gene_name)
# print(new_rec)
SB_file.close()
#####################
## same for asex
SA_dict_2 = {}
for f in sb_filename_list:
asex_name = f.replace("sex_bias", Asex_file_name_sub)
#print(asex_name)
asex_file_path = os.path.join(in_dir_name, asex_name)
asex_file = open(asex_file_path)
sample_name = f.split("_")[2] + "_" + f.split("_")[5]
sample_name = sample_name.split(".")[0]
#print(sample_name)
line_N = 0
for line in asex_file:
#print(line)
line_N = line_N + 1
if line_N > 1:
line = line.rstrip("\n").split(",")
gene_name = line[0] + "SPLTTTYNNNN____HH_" + sample_name
log2_FC = decimal.Decimal(line[1])
FDR_val = decimal.Decimal(line[5])
### add to dict
new_rec = str(log2_FC) + "," + str(FDR_val)
SA_dict_2[gene_name] = new_rec
asex_file.close()
####################################################################################################################
##### cpm
cpm_dict = {}
cpm_header = ""
if cpm_filename != "NOTHINGSET":
cpm_file_path = os.path.join(in_dir_name, cpm_filename)
cpm_file = open(cpm_file_path)
line_N = 0
for line in cpm_file:
line_N = line_N + 1
line = line.rstrip("\n").replace('"', '').split(",")
if line_N == 1:
cpm_header = ""
for c in range(1,len(line)):
cpm_header = cpm_header + ",cpm_" + line[c]
else:
gene_name = line[0]
rest_of_line = ""
for c in range(1,len(line)):
rest_of_line = rest_of_line + "," + line[c]
cpm_dict[gene_name] = rest_of_line
# print(line)
# print(rest_of_line)
####################################################################################################################
##### FPKM
FPKM_dict = {}
FPKM_header = ""
if FPKM_filename != "NOTHINGSET":
FPKM_file_path = os.path.join(in_dir_name, FPKM_filename)
FPKM_file = open(FPKM_file_path)
line_N = 0
for line in FPKM_file:
line_N = line_N + 1
line = line.rstrip("\n").replace('"', '').split(",")
if line_N == 1:
FPKM_header = ""
for c in range(1,len(line)):
FPKM_header = FPKM_header + ",FPKM_" + line[c]
else:
gene_name = line[0]
rest_of_line = ""
for c in range(1,len(line)):
rest_of_line = rest_of_line + "," + line[c]
FPKM_dict[gene_name] = rest_of_line
# print(line)
# print(rest_of_line)
####################################################################################################################
##### export
## order genes:
all_gene_names_l = sorted(list(all_gene_names))
output_filename = output_filename_base + "_SB_asex.csv"
output_file = open(output_filename, "w")
### make header
out_header = ""
for s in sample_name_list:
out_header = out_header + "," + s + "_log2FC_SB" + "," + s + "_FDR_SB" + "," + s + "_sexbias" + "," + s + "_log2FC_SA" + "," + s + "_FDR_SA"
out_header = "genename" + out_header
if cpm_filename != "NOTHINGSET":
out_header = out_header + cpm_header
if FPKM_filename != "NOTHINGSET":
out_header = out_header + FPKM_header
output_file.write(out_header + "\n")
for el in all_gene_names_l:
gene_rec = ""
for s in sample_name_list:
gene_name = el + "SPLTTTYNNNN____HH_" + s
SB_rec = SB_dict_2.get(gene_name)
if SB_rec == None:
SB_rec = "NA,NA,NA"
#print(SB_rec)
SA_rec = SA_dict_2.get(gene_name)
if SA_rec == None:
SA_rec = "NA,NA"
#print(SA_rec)
gene_rec = gene_rec + "," + SB_rec + "," + SA_rec
if cpm_filename != "NOTHINGSET":
cpm_rec = cpm_dict.get(el)
gene_rec = gene_rec + cpm_rec
if FPKM_filename != "NOTHINGSET":
FPKM_rec = FPKM_dict.get(el)
gene_rec = gene_rec + FPKM_rec
output_file.write(el + gene_rec + "\n")
print("Output file: " + output_filename)
print("\n\n\nFinished, Philip Raven\n\n")