comparison tRNAscan.py @ 0:d34f31cbc9dd draft

Uploaded
author bgruening
date Sat, 06 Jul 2013 10:37:13 -0400
parents
children 358f58401cd6
comparison
equal deleted inserted replaced
-1:000000000000 0:d34f31cbc9dd
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
12 def main(args):
13 """
14 Call from galaxy:
15 tRNAscan.py $organism $mode $showPrimSecondOpt $disablePseudo $showCodons $tabular_output $inputfile $fasta_output
16
17 tRNAscan-SE $organism $mode $showPrimSecondOpt $disablePseudo $showCodons -d -Q -y -q -b -o $tabular_output $inputfile;
18 """
19 cmd = """tRNAscan-SE -Q -y -q -b %s""" % ' '.join( args[:-1] )
20 child = subprocess.Popen(cmd.split(),
21 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
22 stdout, stderr = child.communicate()
23 return_code = child.returncode
24 if return_code:
25 sys.stdout.write(stdout)
26 sys.stderr.write(stderr)
27 sys.stderr.write("Return error code %i from command:\n" % return_code)
28 sys.stderr.write("%s\n" % cmd)
29 else:
30 sys.stdout.write(stdout)
31 sys.stdout.write(stderr)
32
33 outfile = args[-1]
34 sequence_file = args[-2]
35 tRNAScan_file = args[-3]
36
37 with open( sequence_file ) as sequences:
38 sequence_recs = SeqIO.to_dict(SeqIO.parse(sequences, "fasta"))
39
40 tRNAs = []
41 with open(tRNAScan_file) as tRNA_handle:
42 for line in tRNA_handle:
43 line = line.strip()
44 if not line or line.startswith('#'):
45 continue
46 cols = line.split()
47 iid = cols[0].strip()
48 start = int(cols[2])
49 end = int(cols[3])
50 aa = cols[4]
51 codon = cols[5]
52 rec = sequence_recs[ iid ]
53 if start > end:
54 new_rec = rec[end:start]
55 new_rec.seq = new_rec.seq.reverse_complement()
56 new_rec.description = "%s %s %s %s %s" % (rec.description, aa, codon, start, end)
57 new_rec.id = rec.id
58 new_rec.name = rec.name
59 tRNAs.append( new_rec )
60 else:
61 new_rec = rec[start:end]
62 new_rec.id = rec.id
63 new_rec.name = rec.name
64 new_rec.description = "%s %s %s %s %s" % (rec.description, aa, codon, start, end)
65 tRNAs.append( new_rec )
66
67 SeqIO.write(tRNAs, open(outfile, 'w+'), "fasta")
68
69
70 if __name__ == '__main__':
71 main(sys.argv[1:])