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