Mercurial > repos > bgruening > trna_prediction
annotate tRNAscan.py @ 2:358f58401cd6 draft default tip
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/trna_prediction commit cfb19d75629f02e0dea4475c16c016ed5510eb44
author | bgruening |
---|---|
date | Wed, 26 Jul 2017 10:14:05 -0400 |
parents | d34f31cbc9dd |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
4 Converts tRNAScan output back to fasta-sequences. | |
5 """ | |
6 import sys | |
7 from Bio import SeqIO | |
8 from Bio.SeqRecord import SeqRecord | |
9 import subprocess | |
10 | |
11 def main(args): | |
12 """ | |
13 Call from galaxy: | |
14 tRNAscan.py $organism $mode $showPrimSecondOpt $disablePseudo $showCodons $tabular_output $inputfile $fasta_output | |
15 | |
2
358f58401cd6
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/trna_prediction commit cfb19d75629f02e0dea4475c16c016ed5510eb44
bgruening
parents:
0
diff
changeset
|
16 tRNAscan-SE $organism $mode $showPrimSecondOpt $disablePseudo $showCodons -Q -y -q -b -o $tabular_output $inputfile; |
0 | 17 """ |
18 cmd = """tRNAscan-SE -Q -y -q -b %s""" % ' '.join( args[:-1] ) | |
19 child = subprocess.Popen(cmd.split(), | |
20 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
21 stdout, stderr = child.communicate() | |
22 return_code = child.returncode | |
23 if return_code: | |
24 sys.stdout.write(stdout) | |
25 sys.stderr.write(stderr) | |
26 sys.stderr.write("Return error code %i from command:\n" % return_code) | |
27 sys.stderr.write("%s\n" % cmd) | |
28 else: | |
29 sys.stdout.write(stdout) | |
30 sys.stdout.write(stderr) | |
31 | |
32 outfile = args[-1] | |
33 sequence_file = args[-2] | |
34 tRNAScan_file = args[-3] | |
35 | |
36 with open( sequence_file ) as sequences: | |
37 sequence_recs = SeqIO.to_dict(SeqIO.parse(sequences, "fasta")) | |
38 | |
39 tRNAs = [] | |
40 with open(tRNAScan_file) as tRNA_handle: | |
41 for line in tRNA_handle: | |
42 line = line.strip() | |
43 if not line or line.startswith('#'): | |
44 continue | |
45 cols = line.split() | |
46 iid = cols[0].strip() | |
47 start = int(cols[2]) | |
48 end = int(cols[3]) | |
49 aa = cols[4] | |
50 codon = cols[5] | |
51 rec = sequence_recs[ iid ] | |
52 if start > end: | |
53 new_rec = rec[end:start] | |
54 new_rec.seq = new_rec.seq.reverse_complement() | |
55 new_rec.description = "%s %s %s %s %s" % (rec.description, aa, codon, start, end) | |
56 new_rec.id = rec.id | |
57 new_rec.name = rec.name | |
58 tRNAs.append( new_rec ) | |
59 else: | |
60 new_rec = rec[start:end] | |
61 new_rec.id = rec.id | |
62 new_rec.name = rec.name | |
63 new_rec.description = "%s %s %s %s %s" % (rec.description, aa, codon, start, end) | |
64 tRNAs.append( new_rec ) | |
65 | |
66 SeqIO.write(tRNAs, open(outfile, 'w+'), "fasta") | |
67 | |
68 | |
69 if __name__ == '__main__': | |
70 main(sys.argv[1:]) |