| 1 |
# Copyright (c) 2006, The Regents of the University of California, through |
| 2 |
# Lawrence Berkeley National Laboratory (subject to receipt of any required |
| 3 |
# approvals from the U.S. Dept. of Energy). All rights reserved. |
| 4 |
|
| 5 |
# This software is distributed under the new BSD Open Source License. |
| 6 |
# <http://www.opensource.org/licenses/bsd-license.html> |
| 7 |
# |
| 8 |
# Redistribution and use in source and binary forms, with or without |
| 9 |
# modification, are permitted provided that the following conditions are met: |
| 10 |
# |
| 11 |
# (1) Redistributions of source code must retain the above copyright notice, |
| 12 |
# this list of conditions and the following disclaimer. |
| 13 |
# |
| 14 |
# (2) Redistributions in binary form must reproduce the above copyright |
| 15 |
# notice, this list of conditions and the following disclaimer in the |
| 16 |
# documentation and or other materials provided with the distribution. |
| 17 |
# |
| 18 |
# (3) Neither the name of the University of California, Lawrence Berkeley |
| 19 |
# National Laboratory, U.S. Dept. of Energy nor the names of its contributors |
| 20 |
# may be used to endorse or promote products derived from this software |
| 21 |
# without specific prior written permission. |
| 22 |
# |
| 23 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 24 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 27 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 28 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 29 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 30 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 31 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 32 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 33 |
# POSSIBILITY OF SUCH DAMAGE. |
| 34 |
|
| 35 |
"""Sequence IO for NBRF/PIR format. |
| 36 |
|
| 37 |
The format is similar to fasta. The header line consistins of '>', a two- |
| 38 |
letter sequence type (P1, F1, DL, DC, RL, RC, or XX), a semicolon, and a |
| 39 |
sequence ID. The next line is a textual description of the sequence, |
| 40 |
followed by one or more lines containing the sequence data. The end of |
| 41 |
the sequence is marked by a "*" (asterisk) character. |
| 42 |
|
| 43 |
type_code -- A map between NBRF two letter type codes and Alphabets. |
| 44 |
|
| 45 |
|
| 46 |
see: http://www.cmbi.kun.nl/bioinf/tools/crab_pir.html |
| 47 |
|
| 48 |
--- Example NBRF File --- |
| 49 |
|
| 50 |
>P1;CRAB_ANAPL |
| 51 |
ALPHA CRYSTALLIN B CHAIN (ALPHA(B)-CRYSTALLIN). |
| 52 |
MDITIHNPLI RRPLFSWLAP SRIFDQIFGE HLQESELLPA SPSLSPFLMR |
| 53 |
SPIFRMPSWL ETGLSEMRLE KDKFSVNLDV KHFSPEELKV KVLGDMVEIH |
| 54 |
GKHEERQDEH GFIAREFNRK YRIPADVDPL TITSSLSLDG VLTVSAPRKQ |
| 55 |
SDVPERSIPI TREEKPAIAG AQRK* |
| 56 |
|
| 57 |
>P1;CRAB_BOVIN |
| 58 |
ALPHA CRYSTALLIN B CHAIN (ALPHA(B)-CRYSTALLIN). |
| 59 |
MDIAIHHPWI RRPFFPFHSP SRLFDQFFGE HLLESDLFPA STSLSPFYLR |
| 60 |
PPSFLRAPSW IDTGLSEMRL EKDRFSVNLD VKHFSPEELK VKVLGDVIEV |
| 61 |
HGKHEERQDE HGFISREFHR KYRIPADVDP LAITSSLSSD GVLTVNGPRK |
| 62 |
QASGPERTIP ITREEKPAVT AAPKK* |
| 63 |
|
| 64 |
""" |
| 65 |
|
| 66 |
from typing import Iterator, Optional, TextIO |
| 67 |
|
| 68 |
from ..seq import ( |
| 69 |
Alphabet, |
| 70 |
Seq, |
| 71 |
SeqList, |
| 72 |
dna_alphabet, |
| 73 |
generic_alphabet, |
| 74 |
protein_alphabet, |
| 75 |
rna_alphabet, |
| 76 |
) |
| 77 |
|
| 78 |
names = ( |
| 79 |
"nbrf", |
| 80 |
"pir", |
| 81 |
) |
| 82 |
extensions = ("nbrf", "pir", "ali") |
| 83 |
|
| 84 |
type_code = { |
| 85 |
"P1": protein_alphabet, # Protein (complete) |
| 86 |
"F1": protein_alphabet, # Protein (fragment) |
| 87 |
"DL": dna_alphabet, # DNA (linear) |
| 88 |
"DC": dna_alphabet, # DNA (circular) |
| 89 |
"RC": rna_alphabet, # RNA (linear) |
| 90 |
"RL": rna_alphabet, # RNA (circular) |
| 91 |
"N3": rna_alphabet, # tRNA |
| 92 |
"N1": rna_alphabet, # other functional RNA |
| 93 |
"XX": generic_alphabet, |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
def read(fin: TextIO, alphabet: Optional[Alphabet] = None) -> SeqList: |
| 98 |
"""Read and parse a NBRF sequence file. |
| 99 |
|
| 100 |
Args: |
| 101 |
fin -- A stream or file to read |
| 102 |
alphabet -- The expected alphabet of the data. If not supplied, then |
| 103 |
an appropriate alphabet will be inferred from the data. |
| 104 |
Returns: |
| 105 |
SeqList -- A list of sequences |
| 106 |
Raises: |
| 107 |
ValueError -- If the file is unparsable |
| 108 |
""" |
| 109 |
seqs = [s for s in iterseq(fin, alphabet)] |
| 110 |
return SeqList(seqs) |
| 111 |
|
| 112 |
|
| 113 |
def iterseq(fin: TextIO, alphabet: Optional[Alphabet] = None) -> Iterator[Seq]: |
| 114 |
"""Generate sequences from an NBRF file. |
| 115 |
|
| 116 |
arguments: |
| 117 |
fin -- A stream or file to read |
| 118 |
alphabet -- |
| 119 |
yields : |
| 120 |
Seq |
| 121 |
raises : |
| 122 |
ValueError -- On a parse error. |
| 123 |
""" |
| 124 |
|
| 125 |
body, header, sequence = range(3) # Internal states |
| 126 |
|
| 127 |
state = body |
| 128 |
seq_id = None |
| 129 |
seq_desc = None |
| 130 |
seq_alpha = None |
| 131 |
seqs = [] |
| 132 |
|
| 133 |
for lineno, line in enumerate(fin): |
| 134 |
if state == body: |
| 135 |
if line == "" or line.isspace(): |
| 136 |
continue |
| 137 |
if line[0] == ">": |
| 138 |
seq_type, seq_id = line[1:].split(";") |
| 139 |
if alphabet: |
| 140 |
seq_alpha = alphabet |
| 141 |
else: |
| 142 |
seq_alpha = type_code[seq_type] |
| 143 |
state = header |
| 144 |
continue |
| 145 |
raise ValueError("Parse error on line: %d" % lineno) |
| 146 |
|
| 147 |
elif state == header: |
| 148 |
seq_desc = line.strip() |
| 149 |
state = sequence |
| 150 |
continue |
| 151 |
|
| 152 |
elif state == sequence: |
| 153 |
data = "".join(line.split()) # Strip out white space |
| 154 |
if data[-1] == "*": |
| 155 |
# End of sequence data |
| 156 |
seqs.append(data[:-1]) |
| 157 |
|
| 158 |
seq = Seq( |
| 159 |
"".join(seqs), |
| 160 |
name=seq_id.strip(), # type: ignore |
| 161 |
description=seq_desc, |
| 162 |
alphabet=seq_alpha, |
| 163 |
) |
| 164 |
|
| 165 |
yield seq |
| 166 |
state = body |
| 167 |
seq_id = None |
| 168 |
seq_desc = None |
| 169 |
seqs = [] |
| 170 |
continue |
| 171 |
else: |
| 172 |
seqs.append(data) |
| 173 |
continue |
| 174 |
else: |
| 175 |
# If we ever get here something has gone terrible wrong |
| 176 |
assert False # pragma: no cover |
| 177 |
|
| 178 |
# end for |