-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookmod_genome.py
executable file
·499 lines (442 loc) · 20.8 KB
/
lookmod_genome.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# A script allowing to look at the reference genome and to modify it
##############################IMPORTS##############################
import os
import sys
import getopt
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import IUPAC
import pandas as pd
##############################VISUALIZE##############################
##########
###LIST###
##########
# If you want to list the chromosomes of your fasta file, with their length
# python lookmod_genome.py -m list -f file.fasta
##########
###LOOK###
##########
# If you want to look at the surroundings spot of 9 bases let's say between 12 and 22 on chr1, where you want to insert a sequence, you can write
# python lookmod_genome.py -m look -f file.fasta -p 'chr1:12:22' -s 10
# You will get the following area 12-9, so 3, to 22+9, so 31
# from3to12**from22to31
##############################MODIFY##############################
# For the next mode, you will need a fasta file with the modifications you want
# Then, launch this command
# python lookmod_genome.py -m modify -f file.fasta -c changes_file.fasta
############
###MODIFY###
############
# All informations will be contained in the header of fasta sequences
# Exemples:
# ------DELETION------
# Delete sequence in chr4 from 40 to 50, will delete base from 41 to 49
# >deletion:chr4:40:50
# INFO : No sequence needed for deletion
# INFO : The result of >deletion:chr4:40:40 and >deletion:chr4:40:41 will delete nothing
# ------INSERTION------
# Insert sequence in chr1 at position 25
# >insertion:chr1:25:26
# GCTAGCTAGC
# Insert sequence in chr2 from 5 to 15 (so deletion from 6 to 14 then insertion after base 5)
# >insertion:chr1:5:15
# GTTGCATGCTATC
# INFO : >insertion:chr1:15:15 will insert position 25
# ------ADD------
# If you want to add a new chromosome, you will need to create a header like this, don't forget your sequence below
# >add:your_chromosome_name
# GTCGATCGTCATGGTT
# ------REMOVE------
# If you want to remove a chromosome from the reference genome, you will need to create a header like this
# >remove:your_chromosome_name
# INFO : No sequence needed for deletion
# INFO : You can combine DELETION, INSERTION, ADD and REMOVE in the same
# fasta file
# All positions will be calculated on reference genome, this way a modification will not impact the genome position for the next modification
# Use ":" to separate your entities
def usage():
print('Usage:')
print('\tpython ' + sys.argv[0] + ' -m <mode : list, look, modify> -g <genome type> -r <reference fasta file> -c <construction fasta file> -p <position> [-s <surroundings length> -i <info file>]')
print('\t\t-h or --help : display this help')
print('\t\t-m or --mode : list, look, modify')
print('\t\t-g or --genome : genome type')
print('\t\t-r or --file_reference : reference fasta file')
print('\t\t-c or --file_construction : construction fasta file (if mode is modify)')
print('\t\t-p or --position : position chrmX:start:end (if mode is look)')
print('\t\t-s or --surroundings : position (default : 10) (if mode is look)')
print('\t\t-i or --info : output in file the terminal ouput')
def main(argv):
mode = ""
genome = ""
file_reference = ""
file_construction = ""
position = ""
surroundings = 10
info_file = ""
full_seq = ""
chrom_dict = {}
try:
opts, args = getopt.getopt(sys.argv[1:], 'm:g:r:c:p:s:i:', [
'mode=', 'genome=', 'file_reference=', 'file_construction=', 'position=', 'surroundings=', 'info=', 'help'])
except getopt.GetoptError:
usage()
sys.exit(2)
##############################OPTIONS##############################
if not opts:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(2)
elif opt in ('-m', '--mode'):
mode = arg
elif opt in ('-g', '--genome'):
genome = arg
elif opt in ('-r', '--file_reference'):
file_reference = arg
elif opt in ('-c', '--file_construction'):
file_construction = arg
elif opt in ('-p', '--position'):
position = arg
elif opt in ('-s', '--surroundings'):
surroundings = int(arg)
elif opt in ('-i', '--info'):
info_file = arg
else:
print("Error : Bad option -> " + opt)
usage()
sys.exit(2)
# OPEN INFORMATION FILE IF SELECTED AS OPTION
if info_file != "":
if not os.path.exists(info_file):
info_handle = open(info_file, 'a')
else:
print("Error : This info file already exist !\n")
usage()
sys.exit(2)
# CHOOSE A MODE
if mode != "list" and mode != "modify" and mode != "look":
print("Error : You have to choose a mode : list, look or modify !\n")
usage()
sys.exit(2)
# CHECK THE REFERENCE FASTA FILE
if (file_reference[-3:] != ".fa" and file_reference[-4:] != ".fna" and file_reference[-6:] != ".fasta") or not os.path.exists(file_reference):
print("Error : The reference fasta file is missing, not .fa, .fna or .fasta !\n")
usage()
sys.exit(2)
# DICTIONNARY ALL CHROMOSOMES AND LENGTHS
else:
chrom_dict = {
record.id: len(record.seq)
for record in SeqIO.parse(file_reference, 'fasta')
if record.id not in chrom_dict
}
##############################CHECK UP/SET UP##############################
# CHECK FOR MODIFY MODE
if mode == "modify":
if (file_construction[-3:] != ".fa" and file_construction[-4:] != ".fna" and file_construction[-6:] != ".fasta") or not os.path.exists(file_construction):
print("Error : The construction file is missing, not .fa, .fna or .fasta !\n")
usage()
sys.exit(2)
if genome == "":
print("Error : The genome type is missing !\n")
usage()
sys.exit(2)
# CHECK FOR LOOK MODE
elif mode == "look":
# NOT MORE THAN 1000 FOR SURROUNDINGS
if surroundings < 1 or surroundings > 1000:
print("Error : Surroundings length has to be between 1 and 1000 !\n")
usage()
sys.exit(2)
# CHECK CORRECT POSITION
if position != "":
if len(position.split(":")) == 3:
chrom = position.split(":")[0]
try:
start = int(position.split(":")[1])
except ValueError:
print(
"Error : start need to be integer, please use this pattern 'chrX:start:end' !\n")
usage()
sys.exit(2)
try:
end = int(position.split(":")[2])
except ValueError:
print(
"Error : end need to be integer, please use this pattern 'chrX:start:end' !\n")
usage()
sys.exit(2)
if chrom not in chrom_dict:
print(
"Error : " + chrom + " is not in the reference genome file, please use this pattern 'chrX:start:end' !\n")
usage()
sys.exit(2)
if start < 1 or end < 1:
print(
"Error : start or end position < 1 is not allowed for look mode, please use this pattern 'chrX:start:end' !\n")
usage()
sys.exit(2)
if end < start:
print(
"Error : end < start is not allowed, please use this pattern 'chrX:start:end' !\n")
usage()
sys.exit(2)
else:
print(
"Error : The position entry is not correct, please use this pattern 'chrX:start:end' !\n")
usage()
sys.exit(2)
else:
print(
"Error : The position entry is empty, please use this pattern 'chrX:start:end' !\n")
usage()
sys.exit(2)
# SET UP OUTPUT FILE
output_rename = genome + ".fa"
##############################PRINTS##############################
print('\n-----------------------------------------')
print('Mode : ' + mode)
print('Fasta file : ' + file_reference)
if mode == "modify":
print('Construction file : ' + file_construction)
if mode == "look":
print('Position : ' + position)
print('Surroundings length : ' + str(surroundings))
if mode == "modify":
print('Output file rename: ' + output_rename)
if info_file != "":
print('Output information File : ' + info_file)
print('-----------------------------------------\n')
##############################PROGRAM##############################
# LIST MODE
if mode == "list":
print("----------------------")
print("------ LIST ------")
print("----------------------")
for key, value in chrom_dict.iteritems():
print(key + "\t->\t" + str(value))
if info_file != "":
info_handle.write(key + "\t" + str(value) + "\n")
# LOOK MODE
elif mode == "look":
for record in SeqIO.parse(file_reference, 'fasta'):
# CHECK IF CHROM IN REFERENCE FASTA FILE
if chrom == record.id:
# CHECK POSITIONS
if start > len(record.seq):
print(
"Error : Start is bigger than the reference sequence length, please use this pattern 'chrX:start:end' !\n")
usage()
sys.exit(2)
# RETRIEVE SEQUENCE
if end > len(record.seq):
print(
"Warning : End is bigger than the reference sequence length, only start will be shown' !\n")
if start < surroundings:
full_seq += record.seq[0:start] + "**"
else:
full_seq += record.seq[start -
surroundings:start] + "**"
else:
if start < surroundings:
full_seq += record.seq[0:start] + "**"
else:
full_seq += record.seq[start -
surroundings:start] + "**"
if len(record.seq) - end < surroundings:
full_seq += record.seq[end - 1:len(record.seq)]
else:
full_seq += record.seq[end - 1:end - 1 + surroundings]
print("----------------------")
print("------ LOOK ------")
print("----------------------")
print("-> " + full_seq)
if info_file != "":
info_handle.write(str(full_seq))
# MODIFY MODE
elif mode == "modify":
removed_list = []
added_dict = {}
# INITIALIZE DATAFRAME
df_construction = pd.DataFrame(
columns=["chr", "modif_type", "length", "start", "end", "seq"])
if info_file != "":
orig_stdout = sys.stdout
sys.stdout = info_handle
# FILTER FILE_CONSTRUCTION, KEEP GOOD DELETION AND INSERTION. DISPATCH
# ADD AND REMOVE IN LIST
for record in SeqIO.parse(file_construction, 'fasta'):
modif_type = ""
chrom = ""
start = 0
end = 0
check_good = True
# DISPATCH ADD AND REMOVE
if len(record.id.split(":")) == 2:
check_good = False
if record.id.split(":")[0] == "add":
if record.id.split(":")[0] not in added_dict:
added_dict[record.id.split(":")[1]] = record.seq
elif record.id.split(":")[0] == "remove":
removed_list.append(record.id.split(":")[1])
# CHECK GOOD DELETION AND INSERTION
elif len(record.id.split(":")) == 4:
modif_type = record.id.split(":")[0]
chrom = record.id.split(":")[1]
try:
start = int(record.id.split(":")[2])
except ValueError:
check_good = False
print(
"{" + record.id + "} start need to be integer, please use this pattern modif_type:chrmX:start:end !")
print("{" + record.id + "} will not be used")
try:
end = int(record.id.split(":")[3])
except ValueError:
check_good = False
print(
"Warning : {" + record.id + "} end need to be integer, please use this pattern modif_type:chrmX:start:end !")
print("Warning : {" + record.id + "} will not be used")
if modif_type != "insertion" and modif_type != "deletion":
check_good = False
print(
"Warning : {" + record.id + "} does not have the good format, please use modif_type:chrmX:start:end !")
print(
"Warning : To delete or insert use modif_type:chrmX:start:end !")
print("Warning : To add or remove use modif_type:chrmX !")
print("Warning : {" + record.id + "} will not be used")
if modif_type == "insertion" and record.seq == "":
check_good = False
print("Warning : {" + record.id +
"} does not have a sequence to insert")
print("Warning : {" + record.id + "} will not be used")
if chrom not in chrom_dict:
check_good = False
print(
"Warning : {" + record.id + "} chrom is not in the reference genome file, please use this pattern modif_type:chrmX:start:end !")
print("Warning : {" + record.id + "} will not be used")
if start < 0 or end < 1:
check_good = False
print(
"Warning : {" + record.id + "} start < 0 or end position < 1 is not allowed, please use this pattern modif_type:chrmX:start:end !")
print("Warning : {" + record.id + "} will not be used")
if end < start:
check_good = False
print(
"Warning : {" + record.id + "} end < start is not allowed, please use this pattern modif_type:chrmX:start:end !")
print("Warning : {" + record.id + "} will not be used")
else:
check_good = False
print("Warning : {" + record.id +
"} does not have the good format")
print("Warning : To delete or insert use modif_type:chrmX:start:end !")
print("Warning : To add or remove use modif_type:chrmX !")
print("Warning : {" + record.id + "} will not be used")
# IF CONSTRUCTION TYPE IS OK
if check_good:
# FOR INSERTION DO THE FOLLOWING
if modif_type == "insertion":
if start == end:
print("Warning : {" + record.id + "} can not insert between " + str(
start) + " and " + str(end) + " !")
print("Warning : {" + record.id + "} insertion between " +
str(start) + " and " + str(end + 1) + " !")
end += 1
if start > chrom_dict[chrom]:
print("Warning : {" + record.id +
"} start > chromosome length !")
print("Warning : {" + record.id +
"} insertion at the end !")
start = chrom_dict[chrom]
end = chrom_dict[chrom] + 1
if end > chrom_dict[chrom] + 1:
print("Warning : {" + record.id +
"} end > chromosome length +1 !")
print("Warning : {" + record.id +
"} insertion at the end !")
start = chrom_dict[chrom]
end = chrom_dict[chrom] + 1
df_construction = df_construction.append(pd.Series([chrom, modif_type, len(record.seq) - (end - start - 1), start, end, str(
record.seq)], index=["chr", "modif_type", "length", "start", "end", "seq"]), ignore_index=True)
# FOR DELETION DO THE FOLLOWING
elif modif_type == "deletion":
if start == end or start == end - 1:
print("Warning : {" + record.id + "} can not delete between " + str(
start) + " and " + str(end) + ", nothing to delete !")
elif start >= chrom_dict[chrom] and end > chrom_dict[chrom]:
print("Warning : {" + record.id + "} can not delete between " + str(
start) + " and " + str(end) + ", nothing to delete !")
else:
if start < chrom_dict[chrom] and end > chrom_dict[chrom]:
print("Warning : {" + record.id +
"} end > chromosome length +1 !")
print(
"Warning : {" + record.id + "} deletion from " + str(start) + " to the end !")
end = chrom_dict[chrom] + 1
df_construction = df_construction.append(pd.Series([chrom, modif_type, -(end - start - 1), start, end, ""], index=[
"chr", "modif_type", "length", "start", "end", "seq"]), ignore_index=True)
else:
print("Warning : {" + record.id +
"} does not have the good format")
print(
"Warning : To delete or insert use modif_type:chrmX:start:end !")
print("Warning : To add or remove use modif_type:chrmX !")
print("Warning : {" + record.id + "} will not be used")
sys.exit()
# SORT DATAFRAME
df_construction = df_construction.sort_values(
by=['chr', 'start', 'end'], ascending=[1, 1, 1])
pd.options.mode.chained_assignment = None
# print(df_construction)
records = []
# MODIFY EACH RECORD ACCORDING TO THE DATAFRAME, DO NOT OUTPUT THE
# REMOVED CHROMOSOME
for record in SeqIO.parse(file_reference, 'fasta'):
# RECORD NOT IN REMOVED LIST
if record.id not in removed_list:
# SUBSTRACT CONSTRCUTION DATAFRAME
df_temp = df_construction.loc[
df_construction['chr'] == record.id]
for index, row in df_temp.iterrows():
if row['modif_type'] == 'deletion':
record.seq = record.seq[
:row['start']] + record.seq[row['end'] - 1:]
elif row['modif_type'] == 'insertion':
record.seq = record.seq[
:row['start']] + row['seq'] + record.seq[row['end'] - 1:]
else:
print("Warning : {" + record.id +
"} does not have the good format")
print(
"Warning : To delete or insert use modif_type:chrmX:start:end !")
print("Warning : To add or remove use modif_type:chrmX !")
print("Warning : {" + record.id + "} will not be used")
sys.exit()
df_temp.start = df_temp.start + row['length']
df_temp.end = df_temp.end + row['length']
records.append(record)
# ADD ADDED CHROMOSOME TO RECORDS
for key, value in added_dict.iteritems():
records.append(SeqRecord(
Seq(str(value), IUPAC.unambiguous_dna), id=key, description=key, name=key))
# OUTPUT RECORDS
SeqIO.write(records, output_rename, "fasta")
if info_file != "":
sys.stdout = orig_stdout
print("------------------------")
print("------ MODIFY ------")
print("------------------------")
print("-> Done")
print(output_rename)
if info_file != "":
info_handle.write("-> Done\n")
info_handle.write(output_rename)
else:
print("Error : You have to choose a mode : list, look or modify !")
usage()
sys.exit(2)
if __name__ == '__main__':
main(sys.argv[1:])