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