annotate rgweblogo/rgWebLogo3.py @ 6:be85d1912fc8 draft

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