annotate extract_features.py @ 5:a4fab0c1ae1a

Uploaded
author bjoern-gruening
date Sun, 09 Jun 2013 07:54:25 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
1 #!/usr/bin/env python
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
2
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
3 import os
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
4 import sys
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
5 import argparse
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
6 import textwrap
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
7
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
8 def main( args ):
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
9 """
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
10 Extract the protein and coding section from an augustus gff, gtf file
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
11 Example file:
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
12 HS04636 AUGUSTUS stop_codon 6901 6903 . + 0 Parent=g1.t1
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
13 HS04636 AUGUSTUS transcription_end_site 8857 8857 . + . Parent=g1.t1
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
14 # protein sequence = [MLARALLLCAVLALSHTANPCCSHPCQNRGVCMSVGFDQYKCDCTRTGFYGENCSTPEFLTRIKLFLKPTPNTVHYIL
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
15 # THFKGFWNVVNNIPFLRNAIMSYVLTSRSHLIDSPPTYNADYGYKSWEAFSNLSYYTRALPPVPDDCPTPLGVKGKKQLPDSNEIVEKLLLRRKFIPD
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
16 # PQGSNMMFAFFAQHFTHQFFKTDHKRGPAFTNGLGHGVDLNHIYGETLARQRKLRLFKDGKMKYQIIDGEMYPPTVKDTQAEMIYPPQVPEHLRFAVG
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
17 # QEVFGLVPGLMMYATIWLREHNRVCDVLKQEHPEWGDEQLFQTSRLILIGETIKIVIEDYVQHLSGYHFKLKFDPELLFNKQFQYQNRIAAEFNTLYH
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
18 # WHPLLPDTFQIHDQKYNYQQFIYNNSILLEHGITQFVESFTRQIAGRVAGGRNVPPAVQKVSQASIDQSRQMKYQSFNEYRKRFMLKPYESFEELTGE
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
19 # KEMSAELEALYGDIDAVELYPALLVEKPRPDAIFGETMVEVGAPFSLKGLMGNVICSPAYWKPSTFGGEVGFQIINTASIQSLICNNVKGCPFTSFSV
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
20 # PDPELIKTVTINASSSRSGLDDINPTVLLKERSTEL]
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
21 # end gene g1
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
22 ###
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
23 #
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
24 # ----- prediction on sequence number 2 (length = 2344, name = HS08198) -----
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
25 #
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
26 # Predicted genes for sequence number 2 on both strands
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
27 # start gene g2
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
28 HS08198 AUGUSTUS gene 86 2344 1 + . ID=g2
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
29 HS08198 AUGUSTUS transcript 86 2344 . + . ID=g2.t1;Parent=g2
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
30 HS08198 AUGUSTUS transcription_start_site 86 86 . + . Parent=g2.t1
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
31 HS08198 AUGUSTUS exon 86 582 . + . Parent=g2.t1
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
32 HS08198 AUGUSTUS start_codon 445 447 . + 0 Parent=g2.t1
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
33 """
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
34 protein_seq = ''
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
35 coding_seq = ''
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
36 if args.protein:
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
37 po = open( args.protein, 'w+' )
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
38 if args.codingseq:
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
39 co = open( args.codingseq, 'w+' )
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
40
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
41 for line in sys.stdin:
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
42 # protein- and coding-sequence are stored as comments
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
43 if line.startswith('#'):
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
44 line = line[2:].strip()
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
45 if line.startswith('start gene'):
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
46 gene_name = line[11:].strip()
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
47
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
48 if args.protein and line.startswith('protein sequence = ['):
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
49 if line.endswith(']'):
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
50 protein_seq = line[20:-1]
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
51 po.write( '>%s\n%s\n' % (gene_name, '\n'.join( textwrap.wrap( protein_seq, 80 ) ) ) )
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
52 protein_seq = ''
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
53 else:
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
54 line = line[20:]
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
55 protein_seq = line
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
56
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
57 if args.codingseq and line.startswith('coding sequence = ['):
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
58 if line.endswith(']'):
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
59 coding_seq = line[19:-1]
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
60 co.write( '>%s\n%s\n' % (gene_name, '\n'.join( textwrap.wrap( coding_seq, 80 ) ) ) )
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
61 coding_seq = ''
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
62 else:
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
63 line = line[19:]
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
64 coding_seq = line
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
65
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
66 if protein_seq:
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
67 if line.endswith(']'):
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
68 protein_seq += line[:-1]
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
69 po.write( '>%s\n%s\n' % (gene_name, '\n'.join( textwrap.wrap( protein_seq, 80 ) ) ) )
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
70 protein_seq = ''
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
71 else:
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
72 protein_seq += line
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
73
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
74 if coding_seq:
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
75 if line.endswith(']'):
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
76 coding_seq += line[:-1]
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
77 co.write( '>%s\n%s\n' % (gene_name, '\n'.join( textwrap.wrap( coding_seq, 80 ) ) ) )
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
78 coding_seq = ''
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
79 else:
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
80 coding_seq += line
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
81 if args.codingseq:
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
82 co.close()
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
83 if args.protein:
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
84 po.close()
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
85
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
86 if __name__ == '__main__':
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
87 parser = argparse.ArgumentParser()
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
88 parser.add_argument('-p', '--protein', help='Path to the protein file.')
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
89 parser.add_argument('-c', '--codingseq', help='Path to the coding file.')
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
90
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
91 args = parser.parse_args()
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
92 main( args )
a4fab0c1ae1a Uploaded
bjoern-gruening
parents:
diff changeset
93