-
Notifications
You must be signed in to change notification settings - Fork 0
/
kaas.py
executable file
·285 lines (285 loc) · 9.42 KB
/
kaas.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
#!/usr/bin/env python
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2,urllib,re,time,getopt,sys,BeautifulSoup,os
class CurlWeb:
def __init__(self,Key,Mail="[email protected]",jobid="no",ResultFileName="Results"):
self.fungi="sce, ago, kla, ppa, vpo, cgr, dha, pic, lel, cal, yli, ncr, mgr, fgr, ssl, bfu, ani, afm, aor, ang, pcs, cpw, ure, tml, spo, cne, ppl, mpr, uma, mgl, ecu"
self.gene="hsa, dme, cel, ath, sce, cho, eco, nme, hpy, rpr, bsu, lla, cac, mge, mtu, ctr, bbu, syn, bth, dra, aae, mja, ape"
self.euk="hsa, mmu, rno, dre, dme, cel, ath, sce, ago, cal, spo, ecu, pfa, cho, ehi, eco, nme, hpy, bsu, lla, mge, mtu, syn, aae, mja, ape"
self.pro="hsa, dme, ath, sce, pfa, eco, sty, hin, pae, nme, hpy, rpr, mlo, bsu, sau, lla, spn, cac, mge, mtu, ctr, bbu, syn, aae, mja, afu, pho, ape"
self.Mail=Mail
self.jobid=jobid
self.ResultFileName=ResultFileName
self.Key=Key
def CurlKaas(self,FastaFile,MissionName,Org,Way,Type):
#self.ResultFileName=ResultFileName
#self.Mail=Mail
#self.jobid="no"
register_openers() #must need
if Type=="p":
datagen, headers = multipart_encode({"file": open(FastaFile,"rb"),"uptype":"q_file","qname":MissionName,"mail":self.Mail,"dbmode":"manual","org_list":Org,"way":Way,"mode":"compute"})
elif Type=="n":
datagen, headers = multipart_encode({"file": open(FastaFile,"rb"),"peptide2":"n","uptype":"q_file","qname":MissionName,"mail":self.Mail,"dbmode":"manual","org_list":Org,"way":Way,"mode":"compute"})
request = urllib2.Request("http://www.genome.jp/kaas-bin/kaas_main/kaas_main", datagen, headers)
fd=urllib2.urlopen(request)
for x in fd.readlines():
x=x.rstrip()
print x
m=re.search("job ID:(.*?)</p>",x)
if m:
self.jobid=m.group(1).replace(" ","")
flag=1
#print self.jobid
#if not self.jobid:
# print "web can't submit data."
# sys.exit()
print self.jobid
def GetKaasResults(self):
if not os.path.exists("map"):
os.mkdir("map")
urllib2.urlopen("http://www.genome.jp/kaas-bin/kaas_main?mode=brite&id=%s&mail=%s&key=%s"%(self.jobid,self.Mail,self.Key))
#BRITE hierarchies
Fc=open(self.ResultFileName+"_KEGG_Orthology.keg","w")
Fc.write(urllib2.urlopen("http://www.genome.jp/kegg-bin/download_htext?htext=q00001.keg&format=htext&filedir=/tools/kaas/files/log/result/%s"%self.jobid).read())
Fc.close()
Fc=open(self.ResultFileName+"_KEGG_modules.keg","w")
Fc.write(urllib2.urlopen("http://www.genome.jp/kegg-bin/download_htext?htext=q00002.keg&format=htext&filedir=/tools/kaas/files/log/result/%s"%self.jobid).read())
Fc.close()
Fc=open(self.ResultFileName+"_KEGG_reaction_modules.keg","w")
Fc.write(urllib2.urlopen("http://www.genome.jp/kegg-bin/download_htext?htext=q00003.keg&format=htext&filedir=/tools/kaas/files/log/result/%s"%self.jobid).read())
Fc.close()
l=urllib2.urlopen("http://www.genome.jp/kaas-bin/kaas_main?mode=map&id=%s&mail=%s&key=%s"%(self.jobid,self.Mail,self.Key)).read()#need change
#print l
Fq=open("map/query.ko","w")
Fq.write(urllib2.urlopen("http://www.genome.jp/tools/kaas/files/dl/%s/query.ko"%self.jobid).read())
Fq.close()
fr=open(self.ResultFileName+".stat","w")
#print l
p=re.compile("<input type=\"hidden\" name=\"unclassified\" value=\"(.*?)\">",re.S) #match
m=p.search(l)
k=m.group(1)
#print k
register_openers()
datagen, headers = multipart_encode({"org_name":"ko","sort":"pathway","default":"#bfffbf","reference":"white","unclassified":k})
request = urllib2.Request("https://www.genome.jp/kegg-bin/color_pathway_object", datagen, headers)
fd=urllib2.urlopen(request).readlines()
#print fd
self.PathWayUrlList=[]
for x in fd:
x=x.rstrip()
if "show_pathway" in x:
soup=BeautifulSoup.BeautifulSoup(x)
l=soup.findAll("a")
fr.write("#"+l[0].text+" "+x.split("</a>")[1].split(" ")[0]+"\t"+l[1].text+"\n")
PngName=l[0].get('href').split("/")[-1].split(".")[0]
self.PathWayUrlList.append(["http://www.genome.jp"+l[0].get('href'),PngName])
elif "_bget" in x:
soup=BeautifulSoup.BeautifulSoup(x)
l=soup.findAll("a")
fr.write(l[0].text+"\t"+x.split("</a>")[1].strip()+"\n")
fr.close()
def GetPathWayImage(self):
red={}
for u in self.PathWayUrlList:
PathWay=urllib2.urlopen(u[0]).read()
psoup=BeautifulSoup.BeautifulSoup(PathWay)
for I in psoup.findAll("img"):
iu=I.get("src")
#print iu
if u[1] in iu:
ImgUrl=iu
break
#print ImgUrl
try:
temp=red[ImgUrl]
except:
red[ImgUrl]=0
Fpng=open("map/"+u[1]+".png","w")
try:
Fpng.write(urllib2.urlopen("http://www.genome.jp"+ImgUrl).read())
except:
print ImgUrl
Fpng.close()
def FormatResults(self):
fg=open(self.ResultFileName+".gmt","w")
fc=open(self.ResultFileName+".stat.change","w")
#os.system("tar -xzvf %s"%(self.ResultFileName+".tar.gz"))
dg={}
dd={}
for x in open("map/query.ko"):
x=x.rstrip()
l=x.split("\t")
if len(l)>1:
if not l[1] in dg:
dg[l[1]]=[l[0]]
else:
dg[l[1]].append(l[0])
for x in open(self.ResultFileName+".stat"):
x=x.rstrip()
m=re.search("^#",x)
if not m:
l=x.split("\t")
p=l[0].split(":")[1]
#dd[p]=l[1]
dd[pa].append(p)
fc.write(l[0]+"\t"+",".join(dg[p])+"\t"+l[1]+"\n")
else:
fc.write(x+"\n")
pa=x.split("\t")[0].replace("#","").replace(" ","_")
dd[pa]=[]
for x in dd:
print x,len(dd[x])
l=x.split("__")
g=[]
for y in dd[x]:
g+=dg[y]
fm=l[1]+"\t"+l[0]+"\t"+"\t".join(g)
fg.write(fm+"\n")
fc.close()
fg.close()
dfg={}
dfgn={}
for x in open(self.ResultFileName+".stat.change"):
x=x.rstrip()
if x.startswith("#"):
continue
l=x.split("\t")
for g in l[1].split(","):
if not g in dfg:
dfg[g]=[]
if not g in dfgn:
dfgn[g]=[]
#print l
if ";" in l[2]:
dfg[g].append(l[0].split(":")[1]+":"+l[2].split(";")[1])
dfgn[g].append(l[2].split(";")[0])
else:
dfg[g].append(l[0].split(":")[1]+":"+l[2])
dfgn[g].append("-")
fgn=open(self.ResultFileName+"_gene.xls","w")
fgn.write("gene\tabbreviation\tdescription\n")
for g in dfg:
fm=g+"\t"+";".join(list(set(dfgn[g])))+"\t"+";".join(list(set(dfg[g])))
fgn.write(fm+"\n")
fgn.close()
def StatisticsLevelB(self,FileQ):
Fr=open(FileQ.split(".")[0]+"_levelB.xls","w")#need change
Fr.write("Category\tnum\n")
d={}
dl=[]
for x in open(FileQ):
x=x.rstrip()
if x.startswith("B"):
m=re.search("<b>(.*)</b>",x)
if m:
#print m.group(1)
term=m.group(1)
dl.append(term)
d[term]=[]
if x.startswith("D"):
l=x.split()
d[term].append(l[1])
for x in dl:
Fr.write(x+"\t"+str(len(set(d[x])))+"\n")
Fr.close()
if __name__=="__main__":
def KaasOption():
print "\n-h\thelp"
print "-f\tfasta file #!!"
print "-n\tMission name DF:query"
print "-m\tE-mail DF:[email protected]"
print "-l\torganisms list(fungi,gene,euk,pro) DF:fungi"
print "-w\tAssignment method(b:BBH(bi-directional best hit),s:SBH(single-directional best hit)) DF:b"
print "-g\tcustom organisms list file(opt)"
print "-t\tprotein or nucleic(p,n) DF:p"
print "-o\toutput file DF:results"
print "--justformat\tjust do format result.gmt and result.stat.change,not do kegg(y,n) DF:n\n"
print "example:kaas.py -f t.fasta -t p -n test -m [email protected] -l fungi -w b --justformat n -o test\n\n\tkaas.py -f t.fasta -t p -n test -m [email protected] -g org.txt -w b --justformat n -o test\n\njust do format:kaas.py --justformat y -o result\n"
print "Note:custom organisms list file must have one line like this:hsa, dme, cel, ath\n\n\tthe final result are test.stat, test.tar.gz,test.gmt,test.stat.change\n"
sys.exit()
try:
options,args = getopt.getopt(sys.argv[1:],"hf:n:m:l:g:w:o:t:",["justformat="])
except getopt.GetoptError:
KaasOption()
FastaFile=""
MissionName="query"
Email="[email protected]"
Org="fungi"
Way="b"
OutPut="results"
Type="p"
Format="n"
for name,value in options:
if name=="-h":
KaasOption()
if name=="-f":
FastaFile=value
if not os.path.exists(FastaFile):
print "File is not exist."
KaasOption()
elif os.stat(FastaFile).st_size==0:
print "File is empty."
KaasOption()
elif name=="-n":
if value!="":
MissionName=value
elif name=="-m":
if value!="":
Email=value
elif name=="-l":
if not value in ["fungi","gene","euk","pro"]:
print "-l must be gene,euk,pro or fungi"
KaasOption()
else:
Org=value
elif name=="-g":
try:
lf=open(value).readlines()
except:
print "can't open %s"%(value)
KaasOption()
Org=lf[0].rstrip()
elif name=="-w":
if not value in ["b","s"]:
print "-w must be b or s"
KaasOption()
else:
Way=value
elif name=="-t":
if not value in ["p","n"]:
print "-t must be p or n"
KaasOption()
else:
Type=value
elif name=="--justformat":
if value!="":
Format=value
elif name=="-o":
if value!="":
OutPut=value
if Format=="y":
p=CurlWeb(Mail=Email,ResultFileName=OutPut)
p.FormatResults()
elif FastaFile and MissionName and Email and Org and Way and OutPut and Type:
print " ".join(sys.argv)
p=CurlWeb(Mail=Email,ResultFileName=OutPut)
if Org=="gene":
Org=p.gene
elif Org=="euk":
Org=p.euk
elif Org=="pro":
Org=p.pro
elif Org=="fungi":
Org=p.fungi
p.CurlKaas(FastaFile,MissionName,Org,Way,Type)
print p.jobid
p.GetKaasResults()
p.FormatResults()
FimageUrl=open("image.url","w")
for x in p.PathWayUrlList:
FimageUrl.write(x+"\n")
FimageUrl.close()
p.GetPathWayImage()
else:
KaasOption()