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