| 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 sequence information in tab-delimited format. |
| 28 |
|
| 29 |
This very simple format has two columns per line. The first column is a sequence name, the second |
| 30 |
column is the sequence itself. The columns are separated by a single tab ("\\t") character. |
| 31 |
|
| 32 |
""" |
| 33 |
|
| 34 |
from typing import Iterator, List, Optional, TextIO |
| 35 |
|
| 36 |
from ..seq import Alphabet, Seq, SeqList |
| 37 |
|
| 38 |
names = ("table", "tab") |
| 39 |
extensions = ("tbl",) |
| 40 |
|
| 41 |
example = """ |
| 42 |
EC0001 MKRISTTITTTITITTGNGAG |
| 43 |
EC0002 MRVLKFGGTSVANAERFLRVADILESNARQGQVATVLSAPAKITNHLVAM |
| 44 |
EC0003 MVKVYAPASSANMSVGFDVLGAAVTPVDGALLGDVVTVEAAETFSLNNLG |
| 45 |
EC0004 MKLYNLKDHNEQVSFAQAVTQGLGKNQGLFFPHDLPEFSLTEIDEMLKLD |
| 46 |
EC0005 MKKMQSIVLALSLVLVAPMAAQAAEITLVPSVKLQIGDRDNRGYYWDGGH |
| 47 |
EC0006 MLILISPAKTLDYQSPLTTTRYTLPELLDNSQQLIHEARKLTPPQISTLM |
| 48 |
EC0007 MPDFFSFINSVLWGSVMIYLLFGAGCWFTFRTGFVQFRYIRQFGKSLKNS |
| 49 |
EC0008 MTDKLTSLRQYTTVVADTGDIAAMKLYQPQDATTNPSLILNAAQIPEYRK |
| 50 |
EC0009 MNTLRIGLVSISDRASSGVYQDKGIPALEEWLTSALTTPFELETRLIPDE |
| 51 |
EC0010 MGNTKLANPAPLGLMGFGMTTILLNLHNVGYFALDGIILAMGIFYGGIAQ |
| 52 |
""" |
| 53 |
|
| 54 |
|
| 55 |
def read(fin: TextIO, alphabet: Optional[Alphabet] = None) -> SeqList: |
| 56 |
"""Read and parse file. |
| 57 |
|
| 58 |
Args: |
| 59 |
fin -- A stream or file to read |
| 60 |
alphabet -- The expected alphabet of the data, if given |
| 61 |
Returns: |
| 62 |
SeqList -- A list of sequences |
| 63 |
Raises: |
| 64 |
ValueError -- If the file is unparsable |
| 65 |
""" |
| 66 |
seqs = [s for s in iterseq(fin, alphabet)] |
| 67 |
return SeqList(seqs) |
| 68 |
|
| 69 |
|
| 70 |
def iterseq(fin: TextIO, alphabet: Optional[Alphabet] = None) -> Iterator[Seq]: |
| 71 |
"""Parse a file and generate sequences. |
| 72 |
|
| 73 |
Args: |
| 74 |
fin -- A stream or file to read |
| 75 |
alphabet -- The expected alphabet of the data, if given |
| 76 |
Yields: |
| 77 |
Seq -- One alphabetic sequence at a time. |
| 78 |
Raises: |
| 79 |
ValueError -- If the file is unparsable |
| 80 |
""" |
| 81 |
alphabet = Alphabet(alphabet) |
| 82 |
|
| 83 |
for lineno, line in enumerate(fin): |
| 84 |
line = line.strip() |
| 85 |
if line == "": |
| 86 |
continue |
| 87 |
|
| 88 |
columns = line.split("\t") |
| 89 |
if len(columns) != 2: |
| 90 |
raise ValueError( |
| 91 |
"Parse failed on line %d: did not find two columns separated by a tab." |
| 92 |
% lineno |
| 93 |
) |
| 94 |
yield Seq(columns[1], alphabet=alphabet, name=columns[0]) |
| 95 |
|
| 96 |
|
| 97 |
def write(fout: TextIO, seqs: List[Seq]) -> None: |
| 98 |
"""Write a two column, tab-delimited file. |
| 99 |
|
| 100 |
Args: |
| 101 |
fout -- A writable stream. |
| 102 |
seqs -- A list of Seq's |
| 103 |
""" |
| 104 |
for s in seqs: |
| 105 |
writeseq(fout, s) |
| 106 |
|
| 107 |
|
| 108 |
def writeseq(fout: TextIO, seq: Seq) -> None: |
| 109 |
"""Write a single sequence in fasta format. |
| 110 |
|
| 111 |
Args: |
| 112 |
afile -- A writable stream. |
| 113 |
seq -- A Seq instance |
| 114 |
""" |
| 115 |
name = seq.name or "" |
| 116 |
print(name, seq, sep="\t", file=fout) |