annotate tRNAscan.py @ 0:d34f31cbc9dd draft

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