-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfasta2kraken2.py
94 lines (57 loc) · 1.6 KB
/
fasta2kraken2.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
#!/usr/bin/env python3
#r3.3
#convert paftol fasta format to kraken db library fasta format
import sys
import re
import glob
import subprocess
import os
def tokenize(filename):
digits = re.compile(r'(\d+)')
return tuple(int(token) if match else token for token, match in ((fragment, digits.search(fragment)) for fragment in digits.split(filename)))
def taxid2dict(taxidfile):
data={}
for i in taxidfile:
k=i.split("\t")
taxid=k[1]
spp=k[0] #.split(";")[-1].rstrip("\n")
if taxid!="no_taxid_found\n":
data[spp]=taxid
return data
def fasta2kraken2(infolder,taxids,outfile):
#paftol format
#>6483 Gene_Name:RPL13 Species:Cyperus_laevigatus Repository:INSDC Sequence_ID:ERR3650073
#kraken2 format
#>kraken:taxid|3635|NC_030074.1 Gossypium hirsutum cultivar TM-1 chromosome 1, ASM98774v1, whole genome shotgun sequence
filelist=glob.glob(infolder)
filelist.sort(key=tokenize)
errfile=open("taxa.notfound",'w')
for i in filelist:
print(i)
f=open(i,'r')
notfound=[]
c=0
for x in f:
if x[0]==">":
spp=x.split(":")[2].replace(" Repository","").replace("_"," ")
#print(spp)
#input()
try:
tax=taxids[spp]
c=1
except:
c=0
if c==1:
id1=">kraken:taxid|"+tax+"|"+x[1:]
outfile.write(id1)
else:
if c==1:
outfile.write(x)
def main():
infolder = sys.argv[1] #input folder/file
taxidfile=open(sys.argv[2],'r')
outfile = open(sys.argv[3],'w') #output file
taxids=taxid2dict(taxidfile)
fasta2kraken2(infolder, taxids,outfile)
print("done")
if __name__ == '__main__': main()