annotate rgWebLogo3.py @ 0:f64b81fda062 draft

Imported from capsule None
author devteam
date Thu, 23 Jan 2014 12:30:59 -0500
parents
children ced02f5334a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
1 """
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
2 # modified june 2 ross lazarus to add units option at Assaf Gordon's suggestion
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
3 # rgWebLogo3.py
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
4 # wrapper to check that all fasta files are same length
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
5
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
6 """
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
7 import optparse, os, sys, subprocess, tempfile
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
8
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
9 WEBLOGO = 'weblogo' # executable name for weblogo3 - confusing isn't it?
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
10
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
11 class WL3:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
12 """
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
13 simple wrapper class to check fasta sequence lengths are all identical
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
14 """
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
15 FASTASTARTSYM = '>'
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
16 badseq = '## error - sequences in file %s are not all the same length - cannot proceed. Please read the tool documentation carefully'
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
17
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
18 def __init__(self,opts=None):
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
19 assert opts<>None,'WL3 class needs opts passed in - got None'
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
20 self.opts = opts
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
21 self.fastaf = file(self.opts.input,'r')
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
22 self.clparams = {}
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
23
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
24 def whereis(self,program):
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
25 for path in os.environ.get('PATH', '').split(':'):
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
26 if os.path.exists(os.path.join(path, program)) and not os.path.isdir(os.path.join(path, program)):
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
27 return os.path.join(path, program)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
28 return None
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
29
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
30 def runCL(self):
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
31 """ construct and run a command line
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
32 """
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
33 wl = self.whereis(WEBLOGO)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
34 if not wl:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
35 print >> sys.stderr, '## rgWebLogo3.py error - cannot locate the weblogo binary %s on the current path' % WEBLOGO
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
36 print >> sys.stderr, '## Please ensure it is installed and working from http://code.google.com/p/weblogo'
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
37 sys.exit(1)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
38 cll = [WEBLOGO,]
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
39 cll += [' '.join(it) for it in list(self.clparams.items())]
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
40 cl = ' '.join(cll)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
41 assert cl > '', 'runCL needs a command line as clparms'
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
42 fd,templog = tempfile.mkstemp(suffix='rgtempRun.txt')
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
43 tlf = open(templog,'w')
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
44 process = subprocess.Popen(cl, shell=True, stderr=tlf, stdout=tlf)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
45 rval = process.wait()
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
46 tlf.close()
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
47 tlogs = ''.join(open(templog,'r').readlines())
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
48 if len(tlogs) > 1:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
49 s = '## executing %s returned status %d and log (stdout/stderr) records: \n%s\n' % (cl,rval,tlogs)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
50 else:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
51 s = '## executing %s returned status %d. Nothing appeared on stderr/stdout\n' % (cl,rval)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
52 os.unlink(templog) # always
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
53 if rval <> 0:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
54 print >> sys.stderr, '## rgWebLogo3.py error - executing %s returned error code %d' % (cl,rval)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
55 print >> sys.stderr, '## This may be a data problem or a tool dependency (%s) installation problem' % WEBLOGO
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
56 print >> sys.stderr, '## Please ensure %s is correctly installed and working on the command line -see http://code.google.com/p/weblogo' % WEBLOGO
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
57 sys.exit(1)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
58 return s
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
59
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
60
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
61 def iter_fasta(self):
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
62 """
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
63 generator for fasta sequences from a file
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
64 """
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
65 aseq = []
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
66 seqname = None
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
67 for i,row in enumerate(self.fastaf):
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
68 if row.startswith(self.FASTASTARTSYM):
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
69 if seqname <> None: # already in a sequence
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
70 s = ''.join(aseq)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
71 l = len(s)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
72 yield (seqname,l)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
73 seqname = row[1:].strip()
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
74 aseq = []
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
75 else:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
76 if i > 0:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
77 print >> sys.stderr,'Invalid fasta file %s - does not start with %s - please read the tool documentation carefully' % (self.opts.input,self.FASTASTARTSYM)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
78 sys.exit(1)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
79 else:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
80 seqname = row[1:].strip()
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
81 else: # sequence row
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
82 if seqname == None:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
83 print >> sys.stderr,'Invalid fasta file %s - does not start with %s - please read the tool documentation carefully' % (self.opts.input,self.FASTASTARTSYM)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
84 sys.exit(1)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
85 else:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
86 aseq.append(row.strip())
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
87
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
88 if seqname <> None: # last one
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
89 l = len(''.join(aseq))
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
90 yield (seqname,l)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
91
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
92
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
93 def fcheck(self):
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
94 """ are all fasta sequence same length?
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
95 might be mongo big
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
96 """
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
97 flen = None
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
98 lasti = None
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
99 f = self.iter_fasta()
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
100 for i,(seqname,seqlen) in enumerate(f):
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
101 lasti = i
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
102 if i == 0:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
103 flen = seqlen
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
104 else:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
105 if seqlen <> flen:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
106 print >> sys.stderr,self.badseq % self.opts.input
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
107 sys.exit(1)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
108 return '# weblogo input %s has %d sequences all of length %d' % (self.opts.input,lasti,flen)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
109
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
110
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
111 def run(self):
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
112 check = self.fcheck()
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
113 self.clparams['-f'] = self.opts.input
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
114 self.clparams['-o'] = self.opts.output
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
115 self.clparams['-t'] = '"%s"' % self.opts.logoname # must be wrapped as a string
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
116 self.clparams['-F'] = self.opts.outformat
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
117 if self.opts.size <> None:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
118 self.clparams['-s'] = self.opts.size
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
119 if self.opts.lower <> None:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
120 self.clparams['-l'] = self.opts.lower
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
121 if self.opts.upper <> None:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
122 self.clparams['-u'] = self.opts.upper
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
123 if self.opts.colours <> None:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
124 self.clparams['-c'] = self.opts.colours
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
125 if self.opts.units <> None:
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
126 self.clparams['-U'] = self.opts.units
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
127 s = self.runCL()
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
128 return check,s
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
129
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
130
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
131 if __name__ == '__main__':
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
132 '''
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
133 called as
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
134 <command interpreter="python">
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
135 rgWebLogo3.py --outformat $outformat -s $size -i $input -o $output -t "$logoname" -c "$colours"
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
136 #if $range.mode == 'part'
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
137 -l "$range.seqstart" -u "$range.seqend"
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
138 #end if
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
139 </command>
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
140
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
141 '''
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
142 op = optparse.OptionParser()
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
143 op.add_option('-i', '--input', default=None)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
144 op.add_option('-F', '--outformat', default='png')
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
145 op.add_option('-s', '--size', default=None)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
146 op.add_option('-o', '--output', default='rgWebLogo3')
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
147 op.add_option('-t', '--logoname', default='rgWebLogo3')
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
148 op.add_option('-c', '--colours', default=None)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
149 op.add_option('-l', '--lower', default=None)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
150 op.add_option('-u', '--upper', default=None)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
151 op.add_option('-U', '--units', default=None)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
152 opts, args = op.parse_args()
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
153 assert opts.input <> None,'weblogo3 needs a -i parameter with a fasta input file - cannot open'
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
154 assert os.path.isfile(opts.input),'weblogo3 needs a valid fasta input file - cannot open %s' % opts.input
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
155 w = WL3(opts)
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
156 checks,s = w.run()
f64b81fda062 Imported from capsule None
devteam
parents:
diff changeset
157 print >> sys.stdout, checks # for info