| 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 IntelliGenetics format. |
| 28 |
|
| 29 |
A sequence file in IG format can contain several sequences, each consisting of a |
| 30 |
number of comment lines that must begin with a semicolon (";"), a line with the |
| 31 |
sequence name and the sequence itself terminated with the termination character |
| 32 |
'1' for linear or '2' for circular sequences. The termination character is |
| 33 |
defacto optional. |
| 34 |
|
| 35 |
--- Example IG File --- |
| 36 |
|
| 37 |
;H.sapiens fau mRNA, 518 bases |
| 38 |
HSFAU |
| 39 |
ttcctctttctcgactccatcttcgcggtagctgggaccgccgttcagtc |
| 40 |
actcttaagtcttttgtaattctggctttctctaataaaaaagccactta |
| 41 |
gttcagtcaaaaaaaaaa1 |
| 42 |
;H.sapiens fau 1 gene, 2016 bases |
| 43 |
HSFAU1 |
| 44 |
ctaccattttccctctcgattctatatgtacactcgggacaagttctcct |
| 45 |
gatcgaaaacggcaaaactaaggccccaagtaggaatgccttagttttcg |
| 46 |
gggttaacaatgattaacactgagcctcacacccacgcgatgccctcagc |
| 47 |
tcctcgctcagcgctctcaccaacagccgtagcccgcagccccgctggac |
| 48 |
accggttctccatccccgcagcgtagcccggaacatggtagctgccatct |
| 49 |
ttacctgctacgccagccttctgtgcgcgcaactgtctggtcccgcccc2 |
| 50 |
|
| 51 |
""" |
| 52 |
|
| 53 |
from typing import Iterator, List, Optional, TextIO |
| 54 |
|
| 55 |
from ..seq import Alphabet, Seq, SeqList |
| 56 |
from ..utils import remove_whitespace |
| 57 |
|
| 58 |
names = ( |
| 59 |
"intelligenetics", |
| 60 |
"ig", |
| 61 |
"stanford", |
| 62 |
) |
| 63 |
extensions = ("ig",) |
| 64 |
|
| 65 |
example = """ |
| 66 |
;H.sapiens fau mRNA, 518 bases |
| 67 |
HSFAU |
| 68 |
ttcctctttctcgactccatcttcgcggtagctgggaccgccgttcagtc |
| 69 |
actcttaagtcttttgtaattctggctttctctaataaaaaagccactta |
| 70 |
gttcagtcaaaaaaaaaa1 |
| 71 |
;H.sapiens fau 1 gene, 2016 bases |
| 72 |
HSFAU1 |
| 73 |
ctaccattttccctctcgattctatatgtacactcgggacaagttctcct |
| 74 |
gatcgaaaacggcaaaactaaggccccaagtaggaatgccttagttttcg |
| 75 |
gggttaacaatgattaacactgagcctcacacccacgcgatgccctcagc |
| 76 |
tcctcgctcagcgctctcaccaacagccgtagcccgcagccccgctggac |
| 77 |
accggttctccatccccgcagcgtagcccggaacatggtagctgccatct |
| 78 |
ttacctgctacgccagccttctgtgcgcgcaactgtctggtcccgcccc2 |
| 79 |
""" |
| 80 |
|
| 81 |
|
| 82 |
def read(fin: TextIO, alphabet: Optional[Alphabet] = None) -> SeqList: |
| 83 |
"""Read and parse an IG file. |
| 84 |
|
| 85 |
Args: |
| 86 |
fin -- A stream or file to read |
| 87 |
alphabet -- The expected alphabet of the data, if given |
| 88 |
Returns: |
| 89 |
SeqList -- A list of sequences |
| 90 |
Raises: |
| 91 |
ValueError -- If the file is unparsable |
| 92 |
""" |
| 93 |
seqs = [s for s in iterseq(fin, alphabet)] |
| 94 |
return SeqList(seqs) |
| 95 |
|
| 96 |
|
| 97 |
def iterseq(fin: TextIO, alphabet: Optional[Alphabet] = None) -> Iterator[Seq]: |
| 98 |
"""Parse an IG file and generate sequences. |
| 99 |
|
| 100 |
Args: |
| 101 |
fin -- A stream or file to read |
| 102 |
alphabet -- The expected alphabet of the data, if given |
| 103 |
Yields: |
| 104 |
Seq -- One alphabetic sequence at a time. |
| 105 |
Raises: |
| 106 |
ValueError -- If the file is unparsable |
| 107 |
""" |
| 108 |
alphabet = Alphabet(alphabet) |
| 109 |
|
| 110 |
seqs: list = [] |
| 111 |
header: list = [] |
| 112 |
start_lineno = -1 |
| 113 |
name = None |
| 114 |
|
| 115 |
def build_seq( |
| 116 |
seqs: List[str], |
| 117 |
alphabet: Alphabet, |
| 118 |
name: Optional[str], |
| 119 |
comments: List[str], |
| 120 |
lineno: int, |
| 121 |
) -> Seq: |
| 122 |
try: |
| 123 |
desc = "\n".join(comments) |
| 124 |
s = Seq("".join(seqs), alphabet, name=name, description=desc) |
| 125 |
except ValueError: |
| 126 |
raise ValueError( |
| 127 |
"Parse failed with sequence starting at line %d: " |
| 128 |
"Character not in alphabet: %s" % (lineno, alphabet) |
| 129 |
) |
| 130 |
return s |
| 131 |
|
| 132 |
for lineno, line in enumerate(fin): |
| 133 |
line = line.strip() |
| 134 |
if line == "": |
| 135 |
continue |
| 136 |
if line.startswith(";"): |
| 137 |
if seqs: |
| 138 |
# end of sequence |
| 139 |
yield build_seq(seqs, alphabet, name, header, start_lineno) |
| 140 |
header = [] |
| 141 |
seqs = [] |
| 142 |
name = None |
| 143 |
header.append(line[1:]) |
| 144 |
start_lineno = lineno |
| 145 |
elif not name: |
| 146 |
name = line |
| 147 |
elif line[-1] == "1" or line[-1] == "2": |
| 148 |
# End of sequence |
| 149 |
seqs.append(remove_whitespace(line[0:-1])) |
| 150 |
yield build_seq(seqs, alphabet, name, header, start_lineno) |
| 151 |
header = [] |
| 152 |
seqs = [] |
| 153 |
name = None |
| 154 |
else: |
| 155 |
seqs.append(remove_whitespace(line)) |
| 156 |
|
| 157 |
if seqs: |
| 158 |
yield build_seq(seqs, alphabet, name, header, start_lineno) |
| 159 |
return |
| 160 |
|
| 161 |
|
| 162 |
def write(fout: TextIO, seqs: SeqList) -> None: |
| 163 |
"""Write an IG file. |
| 164 |
|
| 165 |
Args: |
| 166 |
fout -- A writable stream. |
| 167 |
seqs -- A list of Seq's |
| 168 |
Raises: |
| 169 |
ValueError -- If a sequence is missing a name |
| 170 |
""" |
| 171 |
for s in seqs: |
| 172 |
writeseq(fout, s) |
| 173 |
|
| 174 |
|
| 175 |
def writeseq(fout: TextIO, seq: Seq) -> None: |
| 176 |
"""Write a single sequence in IG format. |
| 177 |
|
| 178 |
Args: |
| 179 |
fout -- A writable stream. |
| 180 |
seq -- A Seq instance |
| 181 |
Raises: |
| 182 |
ValueError -- If a sequence is missing a name |
| 183 |
""" |
| 184 |
desc = seq.description or "" |
| 185 |
|
| 186 |
# We prepend ';' to each line |
| 187 |
for h in desc.splitlines(): |
| 188 |
print(";" + h, file=fout) |
| 189 |
if not seq.name: |
| 190 |
raise ValueError("Write failed with missing sequence name: %s" % str(seq)) |
| 191 |
print(seq.name, file=fout) |
| 192 |
L = len(seq) |
| 193 |
line_length = 80 |
| 194 |
for n in range(1 + L // line_length): |
| 195 |
print(seq[n * line_length : (n + 1) * line_length], file=fout) |
| 196 |
print(file=fout) |