-
Notifications
You must be signed in to change notification settings - Fork 2
/
sort_exon.py
57 lines (54 loc) · 2.31 KB
/
sort_exon.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
from Bio import SeqIO
import glob
def sort(exonsFasta, sortedFasta,sortpara):
exonsDict = {}
for record in SeqIO.parse(exonsFasta,"fasta"):
try:
chromosome = record.id.split("|")[2] #chromosome information
if chromosome not in exonsDict:
exonsDict.update({chromosome:[record]})
else:
exonsDict[chromosome].append(record)
except IndexError:
pass
## print exonsDict
with open(sortedFasta, "w") as sortedHandle:
geneDict = {}
for chromosome, records in sorted(exonsDict.items(),key = lambda ch: ch[0]):
exonLocation = lambda r: (int(r.id.split('|')[sortpara]),(int(r.id.split('|')[5]) - int(r.id.split('|')[4]) + 1)*(-1)) #exon start information
lastStart = 0
lastEnd = 0
for record in sorted(records, key = exonLocation): #sorted by exon start
headList = record.id.split("|")
gene = headList[1]
exonStart = headList[4]
exonEnd = headList[5]
if sortpara== 5:
if lastEnd == exonEnd:
continue
else:
lastStart = exonStart
lastEnd = exonEnd
record.description = ""
SeqIO.write(record,sortedHandle,"fasta")
elif sortpara==4:
if lastStart == exonStart:
continue
else:
lastStart = exonStart
lastEnd = exonEnd
if gene not in geneDict:
geneDict.update({gene:1})
else:
geneDict[gene] += 1
if gene:
headList[1] = gene+'_'+str(geneDict[gene]) #give exons names
record.id = "|".join(headList)
record.description = ""
SeqIO.write(record,sortedHandle,"fasta")
else:
raise Exception("Unknown sortpara: "+sortpara)
if __name__ == "__main__":
exonsFasta = "human_exon.fas"
sortedFasta = "human_exon_sorted_1.fas"
sort(exonsFasta, sortedFasta)