-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_non_essential_mapqual.py
289 lines (261 loc) · 11.4 KB
/
filter_non_essential_mapqual.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
# A script allowing to filter out junctions flagged mapqual when there are
# outside the experimental duplicated parts
##############################IMPORTS##############################
import os
import sys
import getopt
import numpy as np
import pandas as pd
import csv
# Experimentaly, we duplicate part of the genome (chr12 to chr6). This
# lead to trigger the mapqual filter of the translocPipeline for this
# regions. We want to keep these junctions. So we disabled the mapqual
# filter but then we need to remove the "real" mapqual junctions.
def usage():
print('Usage:\n')
print('\tpython ' +
sys.argv[0] + ' -m <metadata file> -g <genome type> -o <output mark> -t <results directory> -n <duplicate file> [-i <input mark>]')
print('\t\t-h or --help : display this help')
print('\t\t-m or --file_metadata : metadata file')
print('\t\t-g or --genome : only filter librairies results with this genome')
print('\t\t-o or --output_mark : mark added to the output file name')
print('\t\t-t or --dir_results : results directory')
print('\t\t-n or --file_duplicate : duplicate positions from the experimental duplicate')
print('\t\t-i or --input_mark : marks from input file')
def main(argv):
file_metadata = ""
genome = ""
output_mark = ""
dir_results = ""
input_mark = ""
file_duplicate = ""
file_input_extension = ""
file_output_extension = ""
try:
opts, args = getopt.getopt(sys.argv[1:], 'm:g:o:t:n:i:', [
'file_metadata=', 'genome=', 'output_mark=', 'dir_results=', 'file_duplicate=', 'input_mark', 'help'])
except getopt.GetoptError:
usage()
sys.exit(2)
##############################OPTIONS##############################
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(2)
elif opt in ('-m', '--file_metadata'):
file_metadata = arg
elif opt in ('-g', '--genome'):
genome = arg
elif opt in ('-o', '--output_mark'):
output_mark = arg
elif opt in ('-t', '--dir_results'):
dir_results = arg
elif opt in ('-n', '--file_duplicate'):
file_duplicate = arg
elif opt in ('-i', '--input_mark'):
input_mark = arg
else:
print("Error : Bad option -> " + opt)
usage()
sys.exit(2)
##############################CHECK UP/SET UP##############################
# CHECK METADATA FILE
if file_metadata == "" or not os.path.exists(file_metadata):
print("Error : You have to set a metadata file !\n")
usage()
sys.exit(2)
else:
# READ METADATA FILE
metadata = pd.read_table(file_metadata, sep='\t')
# FILTER METADATA FILE IF GENOME INPUT
if genome != "":
metadata = metadata.loc[metadata['Assembly'] == genome]
if metadata.empty:
print("Error : This assembly does not exist in the metadata file !\n")
usage()
sys.exit(2)
else:
print("Error : You have to set a genome name !\n")
usage()
sys.exit(2)
# CHECK RESULTS DIRECTORY
if not os.path.exists(dir_results):
print("Error : You have to set a results directory !\n")
usage()
sys.exit(2)
else:
if dir_results[-1] != "/":
dir_results += "/"
# CHECK INPUT MARKS HISTORY
if input_mark == "":
print("Warning : You will filter the raw file !\n")
# CHECK OUTPUT MARK
if output_mark == "":
print("Error : You have to set an output mark !\n")
usage()
sys.exit(2)
# SELECT INPUT FILES
if input_mark == "":
file_input_extension = ".tlx"
else:
file_input_extension = "_" + "_".join(input_mark.split(",")) + ".tlx"
# TEST IF INPUT EXIST IN AT LEAST ON LIBRARY
check_input_mark = False
for i in metadata['Library'].tolist():
if os.path.exists(dir_results + i + "/" + i + file_input_extension):
check_input_mark = True
if not check_input_mark:
print("Error : Your input marks can not localize a good input file !\n")
usage()
sys.exit(2)
# SELECT OUTPUT FILES
if file_input_extension != "":
file_output_extension = file_input_extension[
:-4] + "_" + output_mark + ".tlx"
else:
file_output_extension = "_" + output_mark + ".tlx"
# CHECK DUPLICATE FILE
if file_duplicate == "" or not os.path.exists(file_duplicate):
print("Error : You have to set a duplicate file !\n")
usage()
sys.exit(2)
else:
# READ DUPLICATE FILE
duplicate_locus = pd.read_table(
file_duplicate, sep='\t', header=None)
for index, row in duplicate_locus.iterrows():
# DONOR CHECK
if row[0][0:3] != 'chr':
print("Error : line." + str(index + 1) +
", col.1 of your duplicate file !\n")
print("Error : Unknown chromosome : " + row[0] + " !\n")
usage()
sys.exit(2)
try:
if int(row[1]) < 1:
print("Error : line." + str(index + 1) +
", col.2 of your duplicate file !\n")
print("Error : Start position has to be positive integer !\n")
usage()
sys.exit(2)
except:
print("Error : line." + str(index + 1) +
", col.2 of your duplicate file !\n")
print("Error : Unknown start position : " +
str(row[1]) + " !\n")
usage()
sys.exit(2)
try:
if int(row[2]) < 1:
print("Error : line." + str(index + 1) +
", col.3 of your duplicate file !\n")
print("Error : End position has to be positive integer !\n")
usage()
sys.exit(2)
except:
print("Error : line." + str(index + 1) +
", col.3 of your duplicate file !\n")
print("Error : Unknown end position : " + str(row[2]) + " !\n")
usage()
sys.exit(2)
if int(row[2]) < int(row[1]):
print("Error : line." + str(index + 1) +
" of your duplicate file !\n")
print("Error : End position is smaller than start position !\n")
usage()
sys.exit(2)
if row[3] != '+' and row[3] != '-':
print("Error : line." + str(index + 1) +
", col.4 of your duplicate file !\n")
print("Error : Unknown strand (+,-) : " + str(row[3]) + " !\n")
usage()
sys.exit(2)
# ACCEPTOR CHECK
if row[4][0:3] != 'chr':
print("Error : line." + str(index + 1) +
", col.5 of your duplicate file !\n")
print("Error : Unknown chromosome : " + row[4] + " !\n")
usage()
sys.exit(2)
try:
if int(row[5]) < 1:
print("Error : line." + str(index + 1) +
", col.6 of your duplicate file !\n")
print("Error : Start position has to be positive integer !\n")
usage()
sys.exit(2)
except:
print("Error : line." + str(index + 1) +
", col.6 of your duplicate file !\n")
print("Error : Unknown start position : " +
str(row[5]) + " !\n")
usage()
sys.exit(2)
try:
if int(row[6]) < 1:
print("Error : line." + str(index + 1) +
", col.7 of your duplicate file !\n")
print("Error : End position has to be positive integer !\n")
usage()
sys.exit(2)
except:
print("Error : line." + str(index + 1) +
", col.7 of your duplicate file !\n")
print("Error : Unknown end position : " + str(row[6]) + " !\n")
usage()
sys.exit(2)
if int(row[6]) < int(row[5]):
print("Error : line." + str(index + 1) +
" of your duplicate file !\n")
print("Error : End position is smaller than start position !\n")
usage()
sys.exit(2)
if row[7] != '+' and row[7] != '-':
print("Error : line." + str(index + 1) +
", col.8 of your duplicate file !\n")
print("Error : Unknown strand (+,-) : " + str(row[7]) + " !\n")
usage()
sys.exit(2)
##############################PRINTS##############################
print('\n-----------------------------------------')
print('Metadata file : ' + file_metadata)
print('Genome : ' + genome)
print('Results directory : ' + dir_results)
print('Duplicate file : ' + file_duplicate)
print('Input file extension: ' + file_input_extension)
print('Output file extension : ' + file_output_extension)
print('-----------------------------------------\n')
##############################PROGRAM##############################
# LOOP OVER EACH LIBRARIES
for library in metadata['Library'].tolist():
print(library)
# CHECK RESULTS DIRECTORY EXISTS
if not os.path.exists(dir_results + library):
print("Warning : " + dir_results +
" does not contains {" + library + "}")
print("Warning : {" + library + "} will not be filtered")
else:
# CHECK INPUT FILE EXISTS
if os.path.exists(dir_results + library + "/" + library + file_input_extension):
file_output = open(dir_results + library + "/" +
library + file_output_extension, 'a')
dataframe_input = pd.read_table(
dir_results + library + "/" + library + file_input_extension, sep='\t')
file_output.write("\t".join(list(dataframe_input)) + "\n")
for index, row in dataframe_input.iterrows():
# IF MAPQUAL
if row.mapqual == 1:
# CHECK IF IN DUPLICATE
if duplicate_locus[(duplicate_locus[0] == row['Rname']) & (duplicate_locus[1] < row['Junction']) & (duplicate_locus[2] > row['Junction'])].empty or duplicate_locus[(duplicate_locus[4] == row['Rname']) & (duplicate_locus[5] < row['Junction']) & (duplicate_locus[6] > row['Junction'])].empty:
pd.DataFrame(row).T.to_csv(
file_output, sep='\t', header=None, index=False)
else:
pd.DataFrame(row).T.to_csv(
file_output, sep='\t', header=None, index=False)
else:
print("Warning : " + dir_results + library + "/" + library +
file_input_extension + " does not exist")
print("Warning : {" + dir_results + library + "/" + library +
file_input_extension + "} will not be filtered")
if __name__ == '__main__':
main(sys.argv[1:])