-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathublastn64ram.py
169 lines (121 loc) · 4.43 KB
/
ublastn64ram.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
from Bio import SeqIO
import sys
import os
from StringIO import StringIO # Python 2
import subprocess
from random import randrange
r2=str(randrange(100000))
if os.path.isfile('/db/gbnt.udb')== False:
print "loading db to ram"
subprocess.Popen("sudo mkdir -p /db && sudo mount -t tmpfs tmpfs /db && sudo chmod 777 /db && cp -n ~/data/db/gbbctnt/gbnt.udb /db/gbnt.udb", shell=True).wait()
else:
print "db already exists"
def chunkstring(string, length):
return (string[0+i:length+i] for i in range(0, len(string), length))
#count reads
inputfile = sys.argv[1]
outputfile=sys.argv[2]
chunksize = int(str(sys.argv[3])) #number of reads in each division of the input file
threads = int(sys.argv[4])
fmt = sys.argv[5] #input format
fetchsize = sys.argv[6]
subprocess.Popen("rm -rf ~/scripts/tmp2", shell=True).wait()
subprocess.Popen("mkdir ~/scripts/tmp2", shell=True).wait()
out1=open('/OSM/HOME-MEL/all29c/scripts/tmp2/finaloutput%s.ublast' %(r2),'w')
f2=open('/OSM/HOME-MEL/all29c/scripts/tmp2/inp%s.fa' %(r2),'w')
#split large reads/contigs if >50kb
print "splitting large seq records if >50kb..."
for i in SeqIO.parse(inputfile,fmt):
if len(i.seq)>50000:
seqlist=list(chunkstring(i.seq,50000))
c=-1
for t in seqlist:
c=c+1
print i.id, c+1
f2.write(">"+i.id+"-part-"+str(c+1)+"\n"+str(t)+"\n")
else:
f2.write(">"+i.id+"\n"+str(i.seq)+"\n")
f2.close()
inputfile2=open('/OSM/HOME-MEL/all29c/scripts/tmp2/inp%s.fa' %(r2),'r')
count = SeqIO.index('/OSM/HOME-MEL/all29c/scripts/tmp2/inp%s.fa' %(r2), fmt)
c= len(count)
#print c
a= int(c)
print "num reads=",a
if chunksize>a:
chunksize=a
numchunks = (a/chunksize)
#print"Number of chunks=",numchunks
#sys.stdout.write(str(numchunks))
t=0
f=1
print"Writing split files..."
outfile = "/OSM/HOME-MEL/all29c/scripts/tmp2/%subin" %(r2)+str(f)+".fna"
g=open(outfile,'w')
for i in SeqIO.parse(inputfile2,fmt):
t=t+1
if t>chunksize:
f=f+1
outfile = "/OSM/HOME-MEL/all29c/scripts/tmp2/%subin" %(r2)+str(f)+".fna"
g=open(outfile,'w')
t=1
#print"file",f,"record",t
#print i.seq
#raw_input()
SeqIO.write(i,g,"fasta")
print "Number of chunks=",numchunks
print "Files written"
print "Multithreading ublast"
#numt = number of times to run multithread
p1=[];p2=[];p3=[];p4=[];p5=[];p6=[];p7=[];p8=[];p9=[];p10=[]
q=0
numreps = int(numchunks/threads)
if numreps<1:
numreps=1
threads=numchunks
for setp in range(1,threads+2):
p1.append("")
for rep in range(1,numreps+1):
for x in range(1,threads+1):
q=q+1 #file number
p1[x] = subprocess.Popen("usearch -ublast ~/scripts/tmp2/%subin%d.fna -db /db/gbnt.udb -strand both -accel 0.5 -id 0.97 -maxaccepts 25 -maxrejects 256 -maxhits 10 -userout ~/scripts/tmp2/%subin%dout.txt -userfields query+target+id+ql+qlo+qhi+tlo+thi+tl+alnlen+evalue+bits" %(r2,q,r2,q), shell=True)
#wait for n threads to finish
for x1 in range(1,threads+1):
print "waiting for usearch to finish........."
p1[x1].wait()
for x2 in range(1,threads+1):
print "concatenating.....", x2
subprocess.Popen("cat ~/scripts/tmp2/%subin%dout.txt >> ~/scripts/tmp2/finaloutput%s.ublast" %(r2,x2,r2), shell=True).wait()
#subprocess.Popen("rm -rf ~/scripts/tmp2/ubin* ~/scripts/tmp2/1ubin* ~/scripts/tmp2/2ubin* ~/scripts/tmp2/3ubin* ~/scripts/tmp2/4ubin* ~/scripts/tmp2/5ubin* ", shell=True).wait()
print"adding taxonomy.."
subprocess.Popen("python ~/scripts/taxonfetch.py ~/scripts/tmp2/finaloutput%s.ublast %s n %s" %(r2,outputfile,fetchsize), shell=True).wait()
#############otufreq.py
f2 = open(outputfile,'r')
g2=open(outputfile+".species",'w')
g3=open(outputfile+".genera",'w')
# read output file species into list
species=[]
genus=[]
k=[]
print "Getting species.."
for line in f2:
k=line.rstrip("\n").split("\t")[13].split(" ")
print k
if len(k)>1:
species.append(" ".join(str(x5) for x5 in k[0:2])) #nb makes two element list into a single string
if len(k)==1:
species.append(k[0])
genus.append(line.split("\t")[13].split(" ")[0])
species_set=set(species)
genus_set=set(genus)
print "Counting species.."
for i in species_set:
g2.write(i+"\t"+str(species.count(i))+"\n")
for i in genus_set:
g3.write(i+"\t"+str(genus.count(i))+"\n")
subprocess.Popen("rm /OSM/HOME-MEL/all29c/scripts/tmp2/finaloutput%s.ublast" %(r2), shell=True).wait()
subprocess.Popen("rm /OSM/HOME-MEL/all29c/scripts/tmp2/inp%s.fa" %(r2), shell=True).wait()
subprocess.Popen("rm -r /OSM/HOME-MEL/all29c/scripts/tmp2/%subin*" %(r2), shell=True).wait()
f2.close()
g2.close()
g3.close()