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