-
Notifications
You must be signed in to change notification settings - Fork 1
/
orient_fastas_to_reference.py
executable file
·381 lines (255 loc) · 9.3 KB
/
orient_fastas_to_reference.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
#!/usr/bin/env python3
name = 'oreint_fastas_to_reference.py'
version = '0.3.4'
updated = '2023-04-12'
usage = f"""
NAME {name}
VERSION {version}
UPDATED {updated}
SYNOPSIS Performs a BLASTN search of a set of FASTAs against a reference assembly,
assigning FASTA files to corresponding genome segments, ordering the FASTAs
by occurrence on the genome segment.
USAGE {name} \\
-f 50507/50507.processed.fasta \\
-r REF/assembly.fasta
OPTIONS
-f (--fasta) FASTA files to orient
-r (--ref) Reference genome assembly
-o (--outdir) Output directory [Default:'oriented_fastas']
-i (--min_pident) Minimum percent identity to assign segment to reference [Default: 95%]
-a (--min_palign) Minumum percent of the contig participating in alignment to assign segment to reference [Default: 5%]
-v (--max_overlp) Maximum percent of alignment allowed to overlap a previous alignment to assign segment to reference [Default: 5%]
"""
from sys import argv
if len(argv) < 2:
print(f"\n\n{usage}\n")
exit()
from argparse import ArgumentParser
from os.path import isdir,basename
from os import makedirs,system
from textwrap import wrap
GetOptions = ArgumentParser()
GetOptions.add_argument("-f","--fasta",nargs='+',required=True)
GetOptions.add_argument("-r","--ref",required=True)
GetOptions.add_argument("-o","--outdir",default='oriented_fastas')
GetOptions.add_argument("-i","--min_pident",default=95,type=float,choices=[x for x in range(0,101)])
GetOptions.add_argument("-a","--min_palign",default=5,type=float,choices=[x for x in range(0,101)])
GetOptions.add_argument("-v","--max_overlp",default=5,type=float,choices=[x for x in range(0,101)])
args = GetOptions.parse_args()
fastas = args.fasta
ref = args.ref
outdir = args.outdir
min_pident = args.min_pident
min_palign = args.min_palign
max_overlp = args.max_overlp
############################################################
## Useful functions
############################################################
def reverse_complement(sequence):
bases = {
'A':'T','a':'t',
'T':'A','t':'a',
'C':'G','c':'g',
'G':'C','g':'c',
'N':'N','n':'n'
}
seq = ""
for base in sequence[::-1]:
seq += bases[base]
return seq
def BLASTN(query,subject,outfile):
system(f"""
blastn \\
-query {query} \\
-subject {subject} \\
-outfmt "6 qseqid sseqid length pident qstart qend qlen sstart send slen sstrand bitscore" \\
-out {outfile} \\
2>/dev/null
""")
# -max_target_seqs 1 \\
############################################################
## Get sequences of REFERENCE assembly
############################################################
reference_seqs = {}
locus = False
REF = open(ref,'r')
for line in REF:
line = line.strip()
if line[0] == ">":
locus = line.split()[0][1:]
reference_seqs[locus] = ''
elif locus:
reference_seqs[locus] += line
REF.close()
sseqids = [x for x in sorted(reference_seqs.keys())]
############################################################
## Iterate over provided FASTA files
############################################################
if not isdir(outdir):
makedirs(outdir,mode=0o755)
for file in fastas:
filename = basename(file).split(".")[0]
print(f"\nWorking on {filename}\n")
############################################################
## Retrieve sequences from FASTA
############################################################
sequences = {}
locus = False
FASTA = open(file,'r')
for line in FASTA:
line = line.strip()
if line[0] == '>':
locus = line[1:].split(" ")[0]
sequences[locus] = ""
elif locus:
sequences[locus] += line
FASTA.close()
qseqids = [x for x in sorted(sequences.keys())]
############################################################
## Create output directory
############################################################
temp_dir = f"{outdir}/{filename}"
if not isdir(temp_dir):
makedirs(temp_dir,mode=0o755)
############################################################
## Perform BLAST of FASTA vs REFERENCE
############################################################
BLASTN(query=file,subject=ref,outfile=f"{temp_dir}/results.blastn.6")
############################################################
## Retrieve BLAST hits between FASTA and REFERENCE
############################################################
hits = {}
BLAST = open(f"{temp_dir}/results.blastn.6",'r')
for line in BLAST:
qseqid,sseqid,length,pident,qstart,qend,qlen,sstart,send,slen,strand,bitscore = line.strip().split("\t")
pident,qstart,qend,qlen,sstart,send,slen,length,bitscore = float(pident),int(qstart),int(qend),int(qlen),int(sstart),int(send),int(slen),int(length),float(bitscore)
if qseqid not in hits.keys():
hits[qseqid] = []
hits[qseqid].append(
{
'bitscore':bitscore,
'pident':pident,
'sseqid':sseqid,
'length':length,
'qlen':qlen,
'qstart':qstart,
'qend':qend,
'slen':slen,
'sstart':sstart,
'send':send,
'strand':strand,
}
)
BLAST.close()
orientations = {}
ref_assignment = {x:-1 for x in qseqids}
alignment = {}
assigned_locations = {}
for qseqid in sorted(hits.keys()):
assigned_bps = {x:False for x in range(len(sequences[qseqid]))}
for result in sorted(hits[qseqid],key=lambda x: x['bitscore'],reverse=True):
sseqid = result['sseqid']
qstart = result['qstart']
qend = result['qend']
qlen = result['qlen']
sstart = result['sstart']
send = result['send']
slen = result['slen']
pident = result['pident']
length = result['length']
strand = result['strand']
occupied_count = 0
for bp in range(qstart,qend):
if assigned_bps[bp]:
occupied_count += 1
if occupied_count < (min_palign/100)*length and pident > min_pident and (abs(qstart-qend)+1) > (max_overlp/100)*qlen:
for x in range(qstart,qend):
assigned_bps[x] = True
if sseqid not in assigned_locations.keys():
assigned_locations[sseqid] = []
if qseqid not in orientations.keys():
orientations[qseqid] = strand
ref_assignment[qseqid] = sseqids.index(sseqid)
if strand != 'plus':
sstart,send = send,sstart
if strand != orientations[qseqid]:
print("Inversion!")
qstart,qend = qend,qstart
if qseqid not in alignment.keys():
alignment[qseqid] = [qstart,qend]
assigned_locations[sseqid].append(
{
'qseqid':qseqid,
'qstart':qstart,
'qend':qend,
'sstart':sstart,
'send':send,
'pident':pident,
'qlen':qlen,
'slen':slen,
'length':length,
}
)
ORIENT = open(f"{temp_dir}/{filename}.oriented.fasta",'w')
UNMATCHED = open(f"{temp_dir}/{filename}.unmatched.fasta",'w')
for qseqid in qseqids:
seq = sequences[qseqid]
if qseqid in orientations.keys():
if orientations[qseqid] == 'minus':
seq = reverse_complement(seq)
seq = "\n".join(wrap(seq,60))
if qseqid in orientations.keys():
ORIENT.write(f">{qseqid}\n")
ORIENT.write(f"{seq}\n")
else:
UNMATCHED.write(f">{qseqid}\n")
UNMATCHED.write(f"{seq}\n")
ORIENT.close()
UNMATCHED.close()
LINKS = open(f"{temp_dir}/links.txt",'w')
REF_MAP = open(f"{temp_dir}/all.map",'w')
REF_MAP.write("## >REFERENCE_HIT\tREFERENCE_LENGTH\n")
REF_MAP.write("## >>FASTA_HIT\tALIGN_TYPE\tPIDENT\tFASTA_HIT_START\tFASTA_HIT_END\tFRACTION_FASTA_ALIGNED\tPERCENTAGE_FASTA_ALIGNED")
REF_MAP.write("\tREFERENCE_HIT_START\tREFERENCE_HIT_END\tFRACTION_REFERENCE_ALIGNED\tPERCENTAGE_REFERENCE_ALIGNED\n\n")
for ref_hit in sorted(assigned_locations.keys()):
REF_MAP.write(f">>{ref_hit}\t{len(reference_seqs[ref_hit])}\n")
for assignment in sorted(assigned_locations[ref_hit],key = lambda x: x['sstart']):
pident = assignment['pident']
qseqid = assignment['qseqid']
qstart = assignment['qstart']
qend = assignment['qend']
sstart = assignment['sstart']
send = assignment['send']
qlen = assignment['qlen']
slen = assignment['slen']
length = assignment['length']
aligned_bases = abs(qend-qstart)+1
aligned_percent = aligned_bases/qlen*100
reference_covered = length/slen*100
REF_MAP.write(f" >{qseqid}")
if qstart == alignment[qseqid][0] and qend == alignment[qseqid][-1]:
REF_MAP.write(f"\tPrimary")
LINKS.write(f"chr{(sseqids.index(ref_hit))+1} {sstart} {send} con{(qseqids.index(qseqid))+1} {qstart} {qend} color=0,255,255,.25,z=0\n")
else:
REF_MAP.write(f"\tSecondary")
LINKS.write(f"chr{(sseqids.index(ref_hit))+1} {sstart} {send} con{(qseqids.index(qseqid))+1} {qstart} {qend} color=255,0,255,.25,z=10\n")
REF_MAP.write(f"\t{pident}%")
REF_MAP.write(f"\t{qstart}")
REF_MAP.write(f"\t{qend}")
REF_MAP.write(f"\t{aligned_bases}/{qlen}")
REF_MAP.write(f"\t{aligned_percent:.2f}%")
REF_MAP.write(f"\t{sstart}")
REF_MAP.write(f"\t{send}")
REF_MAP.write(f"\t{length}/{slen}")
REF_MAP.write(f"\t{reference_covered:.2f}%\n")
REF_MAP.write("\n")
REF_MAP.close()
LINKS.close()
KARYO = open(f"{temp_dir}/karyotype.txt",'w')
KARYO.write(f"# reference karyotype\n")
for index,key in enumerate(sorted(reference_seqs.keys())):
KARYO.write(f"chr - chr{index+1} {key} 0 {len(reference_seqs[key])} chr1\n")
KARYO.write(f"\n# assembly karyotype\n")
for key in sorted(ref_assignment.keys(),key = lambda x: ref_assignment[x],reverse=True):
index = qseqids.index(key)
KARYO.write(f"chr - con{index+1} {key} 0 {len(sequences[key])} chr5\n")