| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Copyright (c) 2005 Gavin E. Crooks <[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 |
"""Read and write raw, unformatted sequence data. The whole file is read |
| 28 |
in as a sequence. Whitespace is removed. |
| 29 |
|
| 30 |
|
| 31 |
--- Example Plain/Raw/Text File --- |
| 32 |
|
| 33 |
--------------------------LENSTSPYDYGENESD-------FSDSPPCPQDF |
| 34 |
--------------------------LENLEDLF-WELDRLD------NYNDTSLVENH- |
| 35 |
--------------------------MSNITDPQMWDFDDLN-------FTGMPPADEDY |
| 36 |
-----------------------------------YTSDN---------YSGSGDYDSNK |
| 37 |
-SL-------NFDRTFLPALYSLLFLLGLLGNGAVAAVLLSQRTALSSTDTFLLHLAVAD |
| 38 |
--LC-PATMASFKAVFVPVAYSLIFLLGVIGNVLVLVILERHRQTRSSTETFLFHLAVAD |
| 39 |
-SPC-MLETETLNKYVVIIAYALVFLLSLLGNSLVMLVILYSRVGRSVTDVYLLNLALAD |
| 40 |
-EPC-RDENVHFNRIFLPTIYFIIFLTGIVGNGLVILVMGYQKKLRSMTDKYRLHLSVAD |
| 41 |
""" |
| 42 |
|
| 43 |
from typing import Iterator, List, Optional, TextIO |
| 44 |
|
| 45 |
from ..seq import Alphabet, Seq, SeqList |
| 46 |
from ..utils import remove_whitespace |
| 47 |
|
| 48 |
example = """ |
| 49 |
--------------------------LENSTSPYDYGENESD-------FSDSPPCPQDF |
| 50 |
--------------------------LENLEDLF-WELDRLD------NYNDTSLVENH- |
| 51 |
--------------------------MSNITDPQMWDFDDLN-------FTGMPPADEDY |
| 52 |
-----------------------------------YTSDN---------YSGSGDYDSNK |
| 53 |
-SL-------NFDRTFLPALYSLLFLLGLLGNGAVAAVLLSQRTALSSTDTFLLHLAVAD |
| 54 |
--LC-PATMASFKAVFVPVAYSLIFLLGVIGNVLVLVILERHRQTRSSTETFLFHLAVAD |
| 55 |
-SPC-MLETETLNKYVVIIAYALVFLLSLLGNSLVMLVILYSRVGRSVTDVYLLNLALAD |
| 56 |
-EPC-RDENVHFNRIFLPTIYFIIFLTGIV |
| 57 |
""" |
| 58 |
|
| 59 |
names = ("plain", "raw") |
| 60 |
extensions = () |
| 61 |
|
| 62 |
|
| 63 |
def read(fin: TextIO, alphabet: Optional[Alphabet] = None) -> SeqList: |
| 64 |
"""Read a file of raw sequence data. |
| 65 |
|
| 66 |
Args: |
| 67 |
fin -- A stream or file to read |
| 68 |
alphabet -- The expected alphabet of the data, if given |
| 69 |
Returns: |
| 70 |
SeqList -- A list of sequences |
| 71 |
Raises: |
| 72 |
ValueError -- If the file is unparsable |
| 73 |
""" |
| 74 |
seqs = [s for s in iterseq(fin, alphabet)] |
| 75 |
return SeqList(seqs) |
| 76 |
|
| 77 |
|
| 78 |
def iterseq(fin: TextIO, alphabet: Optional[Alphabet] = None) -> Iterator[Seq]: |
| 79 |
"""Read the sequence data and yield one (and only one) sequence. |
| 80 |
|
| 81 |
Args: |
| 82 |
fin -- A stream or file to read |
| 83 |
alphabet -- The expected alphabet of the data, if given |
| 84 |
Yields: |
| 85 |
Seq -- One alphabetic sequence at a time. |
| 86 |
Raises: |
| 87 |
ValueError -- If the file is unparsable |
| 88 |
""" |
| 89 |
|
| 90 |
alphabet = Alphabet(alphabet) |
| 91 |
lines = [] |
| 92 |
for linenum, line in enumerate(fin): |
| 93 |
if line.isspace(): |
| 94 |
continue # Blank line |
| 95 |
line = line.strip() |
| 96 |
|
| 97 |
if line[0] == ">": # probably a fasta file. Fail. |
| 98 |
raise ValueError("Parse Error on input line: %d " % (linenum)) |
| 99 |
line = remove_whitespace(line) |
| 100 |
|
| 101 |
if not alphabet.alphabetic(line): |
| 102 |
raise ValueError( |
| 103 |
"Character on line: %d not in alphabet: %s : %s" |
| 104 |
% (linenum, alphabet, line) |
| 105 |
) |
| 106 |
lines.append(line) |
| 107 |
|
| 108 |
yield Seq("".join(lines), alphabet) |
| 109 |
|
| 110 |
|
| 111 |
def write(afile: TextIO, seqs: List[Seq]) -> None: |
| 112 |
"""Write raw sequence data, one line per sequence. |
| 113 |
|
| 114 |
arguments: |
| 115 |
afile -- A writable stream. |
| 116 |
seqs -- A list of Seq's |
| 117 |
""" |
| 118 |
for s in seqs: |
| 119 |
writeseq(afile, s) |
| 120 |
|
| 121 |
|
| 122 |
def writeseq(afile: TextIO, seq: Seq) -> None: |
| 123 |
"""Write a single sequence in raw format. |
| 124 |
|
| 125 |
arguments: |
| 126 |
afile -- A writable stream. |
| 127 |
seq -- A Seq instance |
| 128 |
""" |
| 129 |
print(seq, file=afile) |