Mercurial > repos > devteam > weblogo3
annotate rgWebLogo3.py @ 5:dc25a5169a91 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 10:19:39 -0500 |
parents | c1a9e644d580 |
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 | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
5 from __future__ import print_function |
0 | 6 |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
7 import optparse |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
8 import os |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
9 import subprocess |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
10 import sys |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
11 import tempfile |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
12 |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
13 WEBLOGO = 'weblogo' # executable name for weblogo3 - confusing isn't it? |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
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 | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
23 def __init__(self, opts=None): |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
24 assert opts is not None, 'WL3 class needs opts passed in - got None' |
0 | 25 self.opts = opts |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
26 self.fastaf = open(self.opts.input, 'r') |
0 | 27 self.clparams = {} |
28 | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
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: | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
40 print('## rgWebLogo3.py error - cannot locate the weblogo binary %s on the current path' % (WEBLOGO), file=sys.stderr) |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
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) |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
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' | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
47 fd, templog = tempfile.mkstemp(suffix='rgtempRun.txt') |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
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() | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
52 tlogs = ''.join(open(templog, 'r').readlines()) |
0 | 53 if len(tlogs) > 1: |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
54 s = '## executing %s returned status %d and log (stdout/stderr) records: \n%s\n' % (cl, rval, tlogs) |
0 | 55 else: |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
56 s = '## executing %s returned status %d. Nothing appeared on stderr/stdout\n' % (cl, rval) |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
57 os.unlink(templog) # always |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
58 if rval != 0: |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
59 print('## rgWebLogo3.py error - executing %s returned error code %d' % (cl, rval), file=sys.stderr) |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
60 print('## This may be a data problem or a tool dependency (%s) installation problem' % WEBLOGO, file=sys.stderr) |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
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 | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
71 for i, row in enumerate(self.fastaf): |
0 | 72 if row.startswith(self.FASTASTARTSYM): |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
73 if seqname is not None: # already in a sequence |
0 | 74 s = ''.join(aseq) |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
75 ls = len(s) |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
76 yield (seqname, ls) |
0 | 77 seqname = row[1:].strip() |
78 aseq = [] | |
79 else: | |
80 if i > 0: | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
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: | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
84 seqname = row[1:].strip() |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
85 else: # sequence row |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
86 if seqname is None: |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
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) |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
88 sys.exit(1) |
0 | 89 else: |
90 aseq.append(row.strip()) | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
91 |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
92 if seqname is not None: # last one |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
93 las = len(''.join(aseq)) |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
94 yield (seqname, las) |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
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() | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
103 for i, (seqname, seqlen) in enumerate(f): |
0 | 104 lasti = i |
105 if i == 0: | |
106 flen = seqlen | |
107 else: | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
108 if seqlen != flen: |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
109 print(self.badseq % self.opts.input, file=sys.stderr) |
0 | 110 sys.exit(1) |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
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 | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
117 self.clparams['-t'] = '"%s"' % self.opts.logoname # must be wrapped as a string |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
118 self.clparams['-F'] = self.opts.outformat |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
119 if self.opts.size is not None: |
0 | 120 self.clparams['-s'] = self.opts.size |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
121 if self.opts.lower is not None: |
0 | 122 self.clparams['-l'] = self.opts.lower |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
123 if self.opts.upper is not None: |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
124 self.clparams['-u'] = self.opts.upper |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
125 if self.opts.colours is not None: |
0 | 126 self.clparams['-c'] = self.opts.colours |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
127 if self.opts.units is not None: |
0 | 128 self.clparams['-U'] = self.opts.units |
129 s = self.runCL() | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
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') | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
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) | |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
142 op.add_option('-u', '--upper', default=None) |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
143 op.add_option('-U', '--units', default=None) |
0 | 144 opts, args = op.parse_args() |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
145 assert opts.input is not None, 'weblogo3 needs a -i parameter with a fasta input file - cannot open' |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
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) |
5
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
148 checks, s = w.run() |
dc25a5169a91
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/weblogo3 commit d0f167e74e705788adcd9c846db011aee490074b
iuc
parents:
4
diff
changeset
|
149 print(checks, file=sys.stdout) # for info |