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