| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Copyright (c) 2005 David D. Ding <[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 |
"""Reads Sequences in interleaved Phylip format (not sequential) and returns a |
| 28 |
list of sequences. Phylip is a very common phylogeny generating sequence type |
| 29 |
that has the following traits: |
| 30 |
1) First line contains number of species and number of characters in a species' |
| 31 |
sequence. Options may follow, and they can be spaced or unspaced. Options are |
| 32 |
simply letters such as A and W after the number of characters. |
| 33 |
2) Options don't have to contain U in order for a usertree to appear. |
| 34 |
3) If there are options then options appear first, then the sequences. For the |
| 35 |
first iteration of sequences the first ten spaces are reserved for names of |
| 36 |
options and species, the rest is for sequences. |
| 37 |
4) For the second and following iterations the names are removed, only |
| 38 |
sequence appears |
| 39 |
4) At end of file a usertree may appear. First there is a number that indicts |
| 40 |
the number of lines the usertree will take, and then the usertrees follow. |
| 41 |
|
| 42 |
Examples: |
| 43 |
6 50 W |
| 44 |
W 0101001111 0101110101 01011 |
| 45 |
dmras1 GTCGTCGTTG GACCTGGAGG CGTGG |
| 46 |
hschras GTGGTGGTGG GCGCCGGCCG TGTGG |
| 47 |
ddrasa GTTATTGTTG GTGGTGGTGG TGTCG |
| 48 |
spras GTAGTTGTAG GAGATGGTGG TGTTG |
| 49 |
scras1 GTAGTTGTCG GTGGAGGTGG CGTTG |
| 50 |
scras2 GTCGTCGTTG GTGGTGGTGG TGTTG |
| 51 |
|
| 52 |
0101001111 0101110101 01011 |
| 53 |
GTCGTCGTTG GACCTGGAGG CGTGG |
| 54 |
GTGGTGGTGG GCGCCGGCCG TGTGG |
| 55 |
GTTATTGTTG GTGGTGGTGG TGTCG |
| 56 |
GTAGTTGTAG GAGATGGTGG TGTTG |
| 57 |
GTAGTTGTCG GTGGAGGTGG CGTTG |
| 58 |
GTCGTCGTTG GTGGTGGTGG TGTTG |
| 59 |
|
| 60 |
1 |
| 61 |
((dmras1,ddrasa),((hschras,spras),(scras1,scras2))); |
| 62 |
|
| 63 |
|
| 64 |
""" |
| 65 |
|
| 66 |
from typing import Iterator, Optional, TextIO |
| 67 |
|
| 68 |
from ..seq import Alphabet, Seq, SeqList |
| 69 |
|
| 70 |
names = ("phylip",) |
| 71 |
extensions = ("phy",) |
| 72 |
|
| 73 |
|
| 74 |
def iterseq(fin: TextIO, alphabet: Optional[Alphabet] = None) -> Iterator[Seq]: |
| 75 |
"""Iterate over the sequences in the file.""" |
| 76 |
# Default implementation |
| 77 |
return iter(read(fin, alphabet)) |
| 78 |
|
| 79 |
|
| 80 |
# Read takes in a phylip file name, reads it, processes it, and returns a SeqList |
| 81 |
def read(fin: TextIO, alphabet: Optional[Alphabet] = None) -> SeqList: |
| 82 |
sequence: list = [] # where sequences are stored |
| 83 |
idents = [] |
| 84 |
num_seq = 0 |
| 85 |
num_total_seq = 0 # length of sequence of 1 species |
| 86 |
tracker = 0 # track what sequence the line is on |
| 87 |
usertree_tracker = 0 # track usertree lines |
| 88 |
options = "" # options |
| 89 |
num_options = 0 # number/lens of options - U |
| 90 |
|
| 91 |
line = fin.readline() |
| 92 |
while line: |
| 93 |
s_line = ( |
| 94 |
line.split() |
| 95 |
) # for ease of use, not used in all scenarios, but easier on the eye |
| 96 |
|
| 97 |
if s_line == []: # see nothing do nothing |
| 98 |
pass |
| 99 |
|
| 100 |
elif ( |
| 101 |
s_line[0].isdigit() |
| 102 |
and len(s_line) == 1 |
| 103 |
and len(sequence) == num_seq |
| 104 |
and len(sequence[0]) == num_total_seq |
| 105 |
): |
| 106 |
usertree_tracker = int(s_line[0]) |
| 107 |
pass |
| 108 |
|
| 109 |
elif num_options > 0: |
| 110 |
if len(sequence) < num_seq: |
| 111 |
if s_line[0][0] in options: |
| 112 |
num_options -= 1 |
| 113 |
pass |
| 114 |
else: |
| 115 |
raise ValueError( |
| 116 |
"Not an option, but it should be one" |
| 117 |
) # pragma: no cover |
| 118 |
else: |
| 119 |
num_options -= 1 |
| 120 |
pass |
| 121 |
|
| 122 |
elif usertree_tracker > 0: # basically skip usertree |
| 123 |
if len(sequence[num_seq - 1]) == num_total_seq: |
| 124 |
usertree_tracker -= 1 # pragma: no cover |
| 125 |
else: |
| 126 |
raise ValueError("User Tree in Wrong Place") |
| 127 |
|
| 128 |
# problems parse error unexpected |
| 129 |
elif s_line[0].isdigit(): |
| 130 |
if len(s_line) >= 2 and len(sequence) == 0: # identifies first line of file |
| 131 |
num_seq = int(s_line[0]) # get number of sequences |
| 132 |
num_total_seq = int(s_line[1]) # get length of sequences |
| 133 |
if len(s_line) > 2: # takes care of the options |
| 134 |
options = "".join(s_line[2:]) |
| 135 |
num_options = len(options) - options.count("U") |
| 136 |
else: |
| 137 |
raise ValueError("parse error") |
| 138 |
|
| 139 |
# when options end, this takes care of the sequence |
| 140 |
elif num_options == 0: |
| 141 |
if num_seq == 0: |
| 142 |
raise ValueError("Empty File, or possibly wrong file") |
| 143 |
elif tracker < num_seq: |
| 144 |
if num_seq > len(sequence): |
| 145 |
sequence.append("".join(line[10:].split())) # removes species name |
| 146 |
idents.append(line[0:10].strip()) |
| 147 |
tracker += 1 |
| 148 |
|
| 149 |
else: |
| 150 |
sequence[tracker] += "".join(s_line) |
| 151 |
tracker += 1 |
| 152 |
|
| 153 |
if tracker == num_seq: |
| 154 |
tracker = 0 |
| 155 |
num_options = len(options) - options.count("U") |
| 156 |
|
| 157 |
line = fin.readline() |
| 158 |
|
| 159 |
if len(sequence) != len(idents) or len(sequence) != num_seq: |
| 160 |
raise ValueError("Number of different sequences wrong") # pragma: no cover |
| 161 |
|
| 162 |
seqs = [] |
| 163 |
for i in range(0, len(idents)): |
| 164 |
if len(sequence[i]) == num_total_seq: |
| 165 |
seqs.append(Seq(sequence[i], alphabet, idents[i])) |
| 166 |
else: |
| 167 |
raise ValueError("extra sequence in list") # pragma: no cover |
| 168 |
|
| 169 |
return SeqList(seqs) |