annotate mkpthwpng.py @ 35:ea52b23f1141

Bug fixes for Draw variants, Phylip, and gd_d_tools
author Richard Burhans <burhans@bx.psu.edu>
date Wed, 20 Nov 2013 13:46:10 -0500
parents 66a183c44dd5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
1 #!/usr/bin/env python
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
2 # -*- coding: utf-8 -*-
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
3 #
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
4 # mkpthwpng.py
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
5 #
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
6 # Copyright 2011 Oscar Bedoya-Reina <oscar@niska.bx.psu.edu>
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
7 #
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
8 # This program is free software; you can redistribute it and/or modify
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
9 # it under the terms of the GNU General Public License as published by
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
10 # the Free Software Foundation; either version 2 of the License, or
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
11 # (at your option) any later version.
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
12 #
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
16 # GNU General Public License for more details.
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
17 #
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
18 # You should have received a copy of the GNU General Public License
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
19 # along with this program; if not, write to the Free Software
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
21 # MA 02110-1301, USA.
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
22
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
23 import argparse,mechanize,os,sys
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
24
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
25 #this return an image made up from a list of genes and pathway code
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
26 def rtnHTMLformat(tmpddGenrcgenPresent,sppPrefx,pthwcod,ouPthwpng):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
27 inpx='\n'.join(tmpddGenrcgenPresent)#inpx="ALDH2 color \nALDH3A1 color"
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
28 request=mechanize.Request("http://www.genome.jp/kegg/tool/map_pathway2.html")
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
29 response = mechanize.urlopen(request)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
30 forms = mechanize.ParseResponse(response, backwards_compat=False)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
31 form=forms[0]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
32 form["unclassified"]=inpx
23
66a183c44dd5 bugfix for Pathway Image tool
Richard Burhans <burhans@bx.psu.edu>
parents: 0
diff changeset
33 form["org"]=sppPrefx
0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
34 request2 = form.click()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
35 response2 = mechanize.urlopen(request2)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
36 a=str(response2.read()).split('href="/kegg-bin/show_pathway?')[1]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
37 code=a.split('/')[0]#response2.read()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
38 request=mechanize.Request("http://www.genome.jp/kegg-bin/show_pathway?%s/%s.args"%(code,pthwcod))#request=mechanize.Request("http://www.genome.jp/kegg-bin/show_pathway?%s/%s.args"%('13171478854246','hsa00410'))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
39 response = mechanize.urlopen(request)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
40 forms = mechanize.ParseResponse(response, backwards_compat=False)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
41 form=forms[1]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
42 status=' NOT '
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
43 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
44 imgf=str(forms[1]).split('/mark_pathway')[1].split('/')[0]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
45 os.system("wget --quiet http://www.genome.jp/tmp/mark_pathway%s/%s.png -O %s"%(imgf,pthwcod,ouPthwpng))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
46 status=' '
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
47 except:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
48 pass
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
49 return 'A pathway image was%ssuccefully produced...'%status
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
50
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
51
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
52 def main():
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
53 parser = argparse.ArgumentParser(description='Obtain KEGG images from a list of genes.')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
54 parser.add_argument('--input',metavar='input TXT file',type=str,help='the input file with the table in txt format')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
55 parser.add_argument('--output',metavar='output PNG image',type=str,help='the output image file in png format')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
56 parser.add_argument('--KEGGpath',metavar='KEGG pathway code (i.e. cfa00230)',type=str,help='the code of the pathway of interest')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
57 parser.add_argument('--posKEGGclmn',metavar='column number',type=int,help='the column with the KEGG pathway code/name')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
58 parser.add_argument('--KEGGgeneposcolmn',metavar='column number',type=int,help='column with the KEGG gene code')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
59 #~Open arguments
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
60 class C(object):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
61 pass
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
62 fulargs=C()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
63 parser.parse_args(sys.argv[1:],namespace=fulargs)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
64 #test input vars
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
65 inputf,outputf,KEGGpathw,posKEGGclmn,Kgeneposcolmn=fulargs.input,fulargs.output,fulargs.KEGGpath,fulargs.posKEGGclmn,fulargs.KEGGgeneposcolmn
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
66 # make posKEGGclmn, Kgeneposcolmn 0-based
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
67 sppPrefx= KEGGpathw[:3]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
68 posKEGGclmn -= 1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
69 Kgeneposcolmn -= 1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
70 #make a dictionary of valid genes
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
71 dKEGGcPthws=dict([(x.split('\t')[Kgeneposcolmn],set([y.split('=')[0] for y in x.split('\t')[posKEGGclmn].split('.')])) for x in open(inputf).read().splitlines()[1:] if x.strip()])
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
72 for mt1gene in [x for x in dKEGGcPthws.keys() if x.find('.')>-1]:#to crrect names with more than one gene
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
73 pthwsAssotd=dKEGGcPthws.pop(mt1gene)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
74 for eachg in mt1gene.split('.'):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
75 dKEGGcPthws[eachg]=pthwsAssotd
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
76 tmpddGenrcgenPresent=set()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
77 sKEGGc=dKEGGcPthws.keys()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
78 lsKEGGc=len(sKEGGc)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
79 ctPthw=0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
80 while ctPthw < lsKEGGc:#to save memory
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
81 eachK=sKEGGc.pop()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
82 alPthws=dKEGGcPthws[eachK]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
83 if KEGGpathw in alPthws:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
84 tmpddGenrcgenPresent.add('\t'.join([eachK,'red']))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
85 ctPthw+=1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
86 #run the program
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
87 rtnHTMLformat(tmpddGenrcgenPresent,sppPrefx,KEGGpathw,outputf)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
88 return 0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
89
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
90
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
91 if __name__ == '__main__':
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
92 main()