annotate corebio/seq_io/stockholm_io.py @ 8:5149eb3a89c2

Uploaded
author davidmurphy
date Fri, 20 Jan 2012 09:03:40 -0500
parents c55bdc2fb9fa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
2 # Copyright (c) 2005 Gavin E. Crooks <gec@threeplusone.com>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
3 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
4 # This software is distributed under the MIT Open Source License.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
5 # <http://www.opensource.org/licenses/mit-license.html>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
6 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
7 # Permission is hereby granted, free of charge, to any person obtaining a
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
8 # copy of this software and associated documentation files (the "Software"),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
9 # to deal in the Software without restriction, including without limitation
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
10 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
11 # and/or sell copies of the Software, and to permit persons to whom the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
12 # Software is furnished to do so, subject to the following conditions:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
13 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
14 # The above copyright notice and this permission notice shall be included
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
15 # in all copies or substantial portions of the Software.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
16 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
23 # THE SOFTWARE.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
24 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
25
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
26 """Read a multiple sequence alignment in STOCKHOLM format.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
27
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
28 This file format is used by PFAM and HMMER. At present, all annotation
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
29 information is ignored.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
30
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
31 See:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
32 - http://www.cgb.ki.se/cgb/groups/sonnhammer/Stockholm.html
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
33 - HMMER manual
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
34
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
35 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
36
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
37 import re
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
38
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
39 from corebio.utils import *
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
40 from corebio.seq import *
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
41 from corebio.seq_io import *
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
42
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
43
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
44
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
45 example = """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
46 # STOCKHOLM 1.0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
47 #=GF ID CBS
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
48 #=GF AC PF00571
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
49 #=GF DE CBS domain
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
50 #=GF AU Bateman A
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
51 #=GF CC CBS domains are small intracellular modules mostly found
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
52 #=GF CC in 2 or four copies within a protein.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
53 #=GF SQ 67
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
54 #=GS O31698/18-71 AC O31698
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
55 #=GS O83071/192-246 AC O83071
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
56 #=GS O83071/259-312 AC O83071
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
57 #=GS O31698/88-139 AC O31698
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
58 #=GS O31698/88-139 OS Bacillus subtilis
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
59 O83071/192-246 MTCRAQLIAVPRASSLAE..AIACAQKM....RVSRVPVYERS
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
60 #=GR O83071/192-246 SA 999887756453524252..55152525....36463774777
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
61 O83071/259-312 MQHVSAPVFVFECTRLAY..VQHKLRAH....SRAVAIVLDEY
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
62 #=GR O83071/259-312 SS CCCCCHHHHHHHHHHHHH..EEEEEEEE....EEEEEEEEEEE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
63 O31698/18-71 MIEADKVAHVQVGNNLEH..ALLVLTKT....GYTAIPVLDPS
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
64 #=GR O31698/18-71 SS CCCHHHHHHHHHHHHHHH..EEEEEEEE....EEEEEEEEHHH
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
65 O31698/88-139 EVMLTDIPRLHINDPIMK..GFGMVINN......GFVCVENDE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
66 #=GR O31698/88-139 SS CCCCCCCHHHHHHHHHHH..HEEEEEEE....EEEEEEEEEEH
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
67 #=GC SS_cons CCCCCHHHHHHHHHHHHH..EEEEEEEE....EEEEEEEEEEH
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
68 O31699/88-139 EVMLTDIPRLHINDPIMK..GFGMVINN......GFVCVENDE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
69 #=GR O31699/88-139 AS ________________*__________________________
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
70 #=GR_O31699/88-139_IN ____________1______________2__________0____
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
71 //
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
72 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
73
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
74
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
75
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
76 names = ("stockholm", "pfam",)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
77 extensions = ('sth', 'stockholm', 'align')
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
78
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
79
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
80 header_line = re.compile(r'#\s+STOCKHOLM\s+1.\d\s+$')
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
81
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
82 def iterseq(fin, alphabet=None):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
83 """Iterate over the sequences in the file."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
84 # Default implementation
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
85 return iter(read(fin, alphabet) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
86
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
87
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
88 def read(fin, alphabet=None) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
89 alphabet = Alphabet(alphabet)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
90 seq_ids = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
91 seqs = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
92 block_count = 0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
93
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
94
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
95 for token in _scan(fin):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
96 if token.typeof== "begin_block":
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
97 block_count = 0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
98 elif token.typeof == "seq_id":
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
99 if len(seqs) <= block_count :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
100 seq_ids.append(token.data)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
101 seqs.append([])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
102 elif token.typeof == "seq":
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
103 if not alphabet.alphabetic(token.data) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
104 raise ValueError (
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
105 "Character on line: %d not in alphabet: %s : %s" % (
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
106 token.lineno, alphabet, token.data) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
107 seqs[block_count].append(token.data)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
108 block_count +=1
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
109
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
110
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
111 seqs = [ Seq("".join(s), alphabet, name= i) for s,i in zip(seqs,seq_ids)]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
112 return SeqList(seqs)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
113
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
114
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
115 def _scan( fin ):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
116
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
117 header, body, block = range(3)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
118
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
119 yield Token("begin")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
120 state = header
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
121 for L, line in enumerate(fin):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
122
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
123
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
124 if state==header :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
125 if line.isspace() : continue
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
126 m = header_line.match(line)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
127 state = body
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
128 if m is not None :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
129 # print "header: ", m.group()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
130 yield Token("header", m.group() )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
131 continue
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
132 else :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
133 raise ValueError("Parse error on line: %d" % L)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
134
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
135
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
136 if state == body :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
137 if line.isspace() : continue
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
138 yield Token("begin_block")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
139 state = block
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
140 # fall through to block
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
141
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
142 if state == block:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
143 if line.isspace() :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
144 yield Token("end_block")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
145 state = body
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
146 continue
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
147 if line.strip() == '//' :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
148 yield Token("end_block")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
149 return
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
150
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
151
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
152 if line[0] =='#' : # Comment or annotation line
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
153 continue
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
154
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
155 name_seq = line.split(None,1) # Split into two parts at first whitespace
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
156 if len(name_seq) != 2 :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
157 raise ValueError("Parse error on line: %d" % L)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
158
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
159
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
160 yield Token("seq_id", name_seq[0].strip() )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
161 yield Token("seq", name_seq[1].strip() )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
162 continue
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
163
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
164 # END state blocks. If I ever get here something has gone terrible wrong
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
165 raise RuntimeError()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
166
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
167
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
168
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
169
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
170
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
171
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
172