| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Copyright (c) 2005 Clare Gollnick <[email protected]> |
| 4 |
# |
| 5 |
# This software is distributed under the MIT Open Source License. |
| 6 |
# <http://www.opensource.org/licenses/mit-license.html> |
| 7 |
# |
| 8 |
# Permission is hereby granted, free of charge, to any person obtaining a |
| 9 |
# copy of this software and associated documentation files (the "Software"), |
| 10 |
# to deal in the Software without restriction, including without limitation |
| 11 |
# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 |
# and/or sell copies of the Software, and to permit persons to whom the |
| 13 |
# Software is furnished to do so, subject to the following conditions: |
| 14 |
# |
| 15 |
# The above copyright notice and this permission notice shall be included |
| 16 |
# in all copies or substantial portions of the Software. |
| 17 |
# |
| 18 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
# THE SOFTWARE. |
| 25 |
# |
| 26 |
|
| 27 |
|
| 28 |
"""Read sequence information in MSF format. |
| 29 |
|
| 30 |
This is a file format for biological sequence data. The sequences are interweaved and each line is |
| 31 |
labeled with the sequence name. The MSF format can be identified in one or more of the following |
| 32 |
ways: |
| 33 |
1. The word PileUp on the first line (optional) |
| 34 |
2. the word !!AA_MULTIPLE_ALIGNMENT or !!NA_MULTIPLE_ALIGNMENT at the start of the file (optional) |
| 35 |
3. the word MSF on the first line of the file, and the characters ".." at the end of this line |
| 36 |
(optional) |
| 37 |
4. A header containing sequence information followed by a line with the characters "//" |
| 38 |
""" |
| 39 |
|
| 40 |
import re |
| 41 |
from typing import Iterator, Optional, TextIO |
| 42 |
|
| 43 |
from ..seq import Alphabet, Seq, SeqList |
| 44 |
from ..utils import Token |
| 45 |
|
| 46 |
example = """ |
| 47 |
|
| 48 |
PileUp |
| 49 |
|
| 50 |
|
| 51 |
MSF: 64 Type: P Check: 767 .. |
| 52 |
|
| 53 |
Name: Cow Len: 100 Check: 3761 Weight: 1.00 |
| 54 |
Name: Carp Len: 100 Check: 1550 Weight: 1.00 |
| 55 |
Name: Chicken Len: 100 Check: 2397 Weight: 1.00 |
| 56 |
Name: Human Len: 100 Check: 9021 Weight: 1.00 |
| 57 |
Name: Loach Len: 100 Check: 984 Weight: 1.00 |
| 58 |
Name: Mouse Len: 100 Check: 2993 Weight: 1.00 |
| 59 |
|
| 60 |
|
| 61 |
// |
| 62 |
|
| 63 |
|
| 64 |
Cow MAYPMQLGFQ DATSPIMEEL LHFHDHTLMI VFLISSLVLY IISLMLTTKL |
| 65 |
Carp MAHPTQLGFK DAAMPVMEEL LHFHDHALMI VLLISTLVLY IITAMVSTKL |
| 66 |
Chicken MANHSQLGFQ DASSPIMEEL VEFHDHALMV ALAICSLVLY LLTLMLMEKL |
| 67 |
Human MAHAAQVGLQ DATSPIMEEL ITFHDHALMI IFLICFLVLY ALFLTLTTKL |
| 68 |
Loach MAHPTQLGFQ DAASPVMEEL LHFHDHALMI VFLISALVLY VIITTVSTKL |
| 69 |
Mouse MAYPFQLGLQ DATSPIMEEL MNFHDHTLMI VFLISSLVLY IISLMLTTKL |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
Cow THTSTMDAQE VETIWTILPA IILILIALPS LRILYMMDEI NNPSLTVKTM |
| 74 |
Carp TNKYILDSQE IEIVWTILPA VILVLIALPS LRILYLMDEI NDPHLTIKAM |
| 75 |
Chicken S.SNTVDAQE VELIWTILPA IVLVLLALPS LQILYMMDEI DEPDLTLKAI |
| 76 |
Human TNTNISDAQE METVWTILPA IILVLIALPS LRILYMTDEV NDPSLTIKSI |
| 77 |
Loach TNMYILDSQE IEIVWTVLPA LILILIALPS LRILYLMDEI NDPHLTIKAM |
| 78 |
Mouse THTSTMDAQE VETIWTILPA VILIMIALPS LRILYMMDEI NNPVLTVKTM |
| 79 |
|
| 80 |
""" |
| 81 |
|
| 82 |
names = ("msf", "gcg-msf", "gcg", "PileUp") |
| 83 |
extensions = ("msf",) |
| 84 |
|
| 85 |
end_header = re.compile(r"(//)(\s*)$") |
| 86 |
seq_line = re.compile(r"\s*(\S+)\s+([\S\s.?]+)$") |
| 87 |
|
| 88 |
|
| 89 |
def iterseq(fin: TextIO, alphabet: Optional[Alphabet] = None) -> Iterator[Seq]: |
| 90 |
"""Iterate over the sequences in the file.""" |
| 91 |
# Default implementation |
| 92 |
return iter(read(fin, alphabet)) |
| 93 |
|
| 94 |
|
| 95 |
def read(fin: TextIO, alphabet: Optional[Alphabet] = None) -> SeqList: |
| 96 |
alphabet = Alphabet(alphabet) |
| 97 |
seq_ids = [] |
| 98 |
seqs: list = [] |
| 99 |
block_count = 0 |
| 100 |
|
| 101 |
for token in _line_is(fin): |
| 102 |
if token.typeof == "begin_block": |
| 103 |
block_count = 0 |
| 104 |
|
| 105 |
elif token.typeof == "seq_id": |
| 106 |
if len(seqs) <= block_count: |
| 107 |
seq_ids.append(token.data) |
| 108 |
seqs.append([]) |
| 109 |
elif token.typeof == "seq": |
| 110 |
data = token.data |
| 111 |
assert data is not None |
| 112 |
|
| 113 |
if not alphabet.alphabetic(data): |
| 114 |
raise ValueError( |
| 115 |
"Character on line: %d not in alphabet: %s : %s" |
| 116 |
% (token.lineno, alphabet, token.data) |
| 117 |
) |
| 118 |
seqs[block_count].append(data) |
| 119 |
block_count += 1 |
| 120 |
if seq_ids == []: |
| 121 |
raise ValueError("Parse error, possible wrong format") |
| 122 |
seqs = [Seq("".join(s), alphabet, name=i) for s, i in zip(seqs, seq_ids)] |
| 123 |
return SeqList(seqs) |
| 124 |
|
| 125 |
|
| 126 |
def _line_is(fin: TextIO) -> Iterator[Token]: |
| 127 |
header, body, block = range(3) |
| 128 |
yield Token("begin") |
| 129 |
state = header |
| 130 |
for L, line in enumerate(fin): |
| 131 |
if state == header: |
| 132 |
if line.isspace(): |
| 133 |
continue |
| 134 |
m = end_header.match(line) |
| 135 |
if m is not None: |
| 136 |
yield Token("end_header") |
| 137 |
state = body |
| 138 |
continue |
| 139 |
else: |
| 140 |
continue # pragma: no cover |
| 141 |
|
| 142 |
if state == body: |
| 143 |
if line.isspace(): |
| 144 |
continue |
| 145 |
yield Token("begin_block") |
| 146 |
state = block |
| 147 |
# skips to a block of sequences |
| 148 |
|
| 149 |
if state == block: |
| 150 |
if line.isspace(): |
| 151 |
yield Token("end_block") |
| 152 |
state = body |
| 153 |
continue |
| 154 |
m = seq_line.match(line) |
| 155 |
if m is None: |
| 156 |
raise ValueError("Parse error on line: %d" % L) # pragma: no cover |
| 157 |
if m.group(1).isdigit() and m.group(2).strip().isdigit(): |
| 158 |
continue |
| 159 |
yield Token("seq_id", m.group(1).strip()) |
| 160 |
data = m.group(2) |
| 161 |
data = "".join((data.split())) |
| 162 |
yield Token("seq", data.strip()) |