| 1 |
# Copyright (c) 2005 Gavin E. Crooks <[email protected]> |
| 2 |
# Copyright (c) 2006, The Regents of the University of California, through |
| 3 |
# Lawrence Berkeley National Laboratory (subject to receipt of any required |
| 4 |
# approvals from the U.S. Dept. of Energy). All rights reserved. |
| 5 |
|
| 6 |
# This software is distributed under the new BSD Open Source License. |
| 7 |
# <http://www.opensource.org/licenses/bsd-license.html> |
| 8 |
# |
| 9 |
# Redistribution and use in source and binary forms, with or without |
| 10 |
# modification, are permitted provided that the following conditions are met: |
| 11 |
# |
| 12 |
# (1) Redistributions of source code must retain the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer. |
| 14 |
# |
| 15 |
# (2) Redistributions in binary form must reproduce the above copyright |
| 16 |
# notice, this list of conditions and the following disclaimer in the |
| 17 |
# documentation and or other materials provided with the distribution. |
| 18 |
# |
| 19 |
# (3) Neither the name of the University of California, Lawrence Berkeley |
| 20 |
# National Laboratory, U.S. Dept. of Energy nor the names of its contributors |
| 21 |
# may be used to endorse or promote products derived from this software |
| 22 |
# without specific prior written permission. |
| 23 |
# |
| 24 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 25 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 28 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 31 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 32 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 34 |
# POSSIBILITY OF SUCH DAMAGE. |
| 35 |
|
| 36 |
|
| 37 |
""" |
| 38 |
Sequence file reading and writing |
| 39 |
--------------------------------- |
| 40 |
|
| 41 |
Biological sequence data is stored and transmitted using a wide variety of |
| 42 |
different file formats. This package provides convenient methods to read and |
| 43 |
write several of these file fomats. |
| 44 |
|
| 45 |
WebLogo is often capable of guessing the correct file type, either from the |
| 46 |
file extension or the structure of the file:: |
| 47 |
|
| 48 |
>>> import weblogo.seq_io |
| 49 |
>>> afile = open("test_weblogo/data/cap.fa") |
| 50 |
>>> seqs = weblogo.seq_io.read(afile) |
| 51 |
|
| 52 |
Alternatively, each sequence file type has a separate module named FILETYPE_io |
| 53 |
(e.g. fasta_io, clustal_io):: |
| 54 |
|
| 55 |
>>> import weblogo.seq_io.fasta_io |
| 56 |
>>> afile = open("test_weblogo/data/cap.fa") |
| 57 |
>>> seqs = weblogo.seq_io.fasta_io.read(afile) |
| 58 |
|
| 59 |
Sequence data can also be written back to files:: |
| 60 |
|
| 61 |
>>> fout = open("out.fa", "w") |
| 62 |
>>> weblogo.seq_io.fasta_io.write(fout, seqs) |
| 63 |
|
| 64 |
|
| 65 |
Supported File Formats |
| 66 |
---------------------- |
| 67 |
|
| 68 |
:: |
| 69 |
|
| 70 |
Module Name Extension read write features |
| 71 |
--------------------------------------------------------------------------- |
| 72 |
array_io array, flatfile yes yes none |
| 73 |
clustal_io clustalw aln yes yes |
| 74 |
fasta_io fasta, Pearson fa yes yes none |
| 75 |
genbank_io genbank gb yes |
| 76 |
intelligenetics_io intelligenetics ig yes yes |
| 77 |
msf_io msf msf yes |
| 78 |
nbrf_io nbrf, pir pir yes |
| 79 |
nexus_io nexus nexus yes |
| 80 |
phylip_io phylip phy yes |
| 81 |
plain_io plain, raw yes yes none |
| 82 |
table_io table tbl yes yes none |
| 83 |
|
| 84 |
|
| 85 |
Each IO module defines one or more of the following functions and variables: |
| 86 |
|
| 87 |
read(afile, alphabet=None) |
| 88 |
Read a file of sequence data and return a SeqList, a collection |
| 89 |
of Seq's (Alphabetic strings) and features. |
| 90 |
|
| 91 |
read_seq(afile, alphabet=None) |
| 92 |
Read a single sequence from a file. |
| 93 |
|
| 94 |
iter_seq(afile, alphabet =None) |
| 95 |
Iterate over the sequences in a file. |
| 96 |
|
| 97 |
index(afile, alphabet = None) |
| 98 |
Instead of loading all of the sequences into memory, scan the file and |
| 99 |
return an index map that will load sequences on demand. Typically not |
| 100 |
implemented for formats with interleaved sequences. |
| 101 |
|
| 102 |
write(afile, seqlist) |
| 103 |
Write a collection of sequences to the specified file. |
| 104 |
|
| 105 |
write_seq(afile, seq) |
| 106 |
Write one sequence to the file. Only implemented for non-interleaved, |
| 107 |
headerless formats, such as fasta and plain. |
| 108 |
|
| 109 |
example |
| 110 |
A string containing a short example of the file format |
| 111 |
|
| 112 |
names |
| 113 |
A list of synonyms for the file format. E.g. for fasta_io, ( 'fasta', |
| 114 |
'pearson', 'fa'). The first entry is the preferred format name. |
| 115 |
|
| 116 |
extensions |
| 117 |
A list of file name extensions used for this file format. e.g. |
| 118 |
fasta_io.extensions is ('fa', 'fasta', 'fast', 'seq', 'fsa', 'fst', 'nt', |
| 119 |
'aa','fna','mpfa'). The preferred or standard extension is first in the |
| 120 |
list. |
| 121 |
|
| 122 |
|
| 123 |
Attributes : |
| 124 |
* formats: Available seq_io format parsers |
| 125 |
* format_names: A map between format names and format parsers. |
| 126 |
* format_extensions: A map between filename extensions and parsers. |
| 127 |
|
| 128 |
""" |
| 129 |
|
| 130 |
# Dev. References : |
| 131 |
# |
| 132 |
# - http://iubio.bio.indiana.edu/soft/molbio/readseq/java/Readseq2-help.html |
| 133 |
# - http://www.ebi.ac.uk/help/formats_frame.html |
| 134 |
# - http://www.cmbi.kun.nl/bioinf/tools/crab_pir.html |
| 135 |
# - http://bioperl.org/HOWTOs/html/SeqIO.html |
| 136 |
# - http://emboss.sourceforge.net/docs/themes/SequenceFormats.html |
| 137 |
# - http://www.cse.ucsc.edu/research/compbio/a2m-desc.html (a2m) |
| 138 |
# - http://www.genomatix.de/online_help/help/sequence_formats.html |
| 139 |
|
| 140 |
from types import ModuleType |
| 141 |
from typing import List, Optional, TextIO |
| 142 |
|
| 143 |
from ..seq import Alphabet, SeqList |
| 144 |
from . import genbank_io # null_io, |
| 145 |
from . import ( |
| 146 |
array_io, |
| 147 |
clustal_io, |
| 148 |
fasta_io, |
| 149 |
intelligenetics_io, |
| 150 |
msf_io, |
| 151 |
nbrf_io, |
| 152 |
nexus_io, |
| 153 |
phylip_io, |
| 154 |
plain_io, |
| 155 |
stockholm_io, |
| 156 |
table_io, |
| 157 |
) |
| 158 |
|
| 159 |
__all__ = [ |
| 160 |
"clustal_io", |
| 161 |
"fasta_io", |
| 162 |
"msf_io", |
| 163 |
"nbrf_io", |
| 164 |
"nexus_io", |
| 165 |
"plain_io", |
| 166 |
"phylip_io", |
| 167 |
"null_io", |
| 168 |
"stockholm_io", |
| 169 |
"intelligenetics_io", |
| 170 |
"table_io", |
| 171 |
"array_io", |
| 172 |
"genbank_io", |
| 173 |
"read", |
| 174 |
"formats", |
| 175 |
"format_names", |
| 176 |
"format_extensions", |
| 177 |
] |
| 178 |
|
| 179 |
|
| 180 |
"""Available seq_io formats""" |
| 181 |
formats = ( |
| 182 |
clustal_io, |
| 183 |
fasta_io, |
| 184 |
plain_io, |
| 185 |
msf_io, |
| 186 |
genbank_io, |
| 187 |
nbrf_io, |
| 188 |
nexus_io, |
| 189 |
phylip_io, |
| 190 |
stockholm_io, |
| 191 |
intelligenetics_io, |
| 192 |
table_io, |
| 193 |
array_io, |
| 194 |
) |
| 195 |
|
| 196 |
|
| 197 |
def format_names() -> dict: |
| 198 |
"""Return a map between format names and format modules""" |
| 199 |
global formats |
| 200 |
fnames = {} |
| 201 |
for f in formats: |
| 202 |
for name in f.names: # type: ignore |
| 203 |
assert name not in fnames # Insanity check |
| 204 |
fnames[name] = f |
| 205 |
return fnames |
| 206 |
|
| 207 |
|
| 208 |
def format_extensions() -> dict: |
| 209 |
"""Return a map between filename extensions and sequence file types""" |
| 210 |
global formats |
| 211 |
fext = {} |
| 212 |
for f in formats: |
| 213 |
for ext in f.extensions: # type: ignore |
| 214 |
assert ext not in fext # Insanity check |
| 215 |
fext[ext] = f |
| 216 |
return fext |
| 217 |
|
| 218 |
|
| 219 |
# seq_io._parsers is an ordered list of sequence parsers that are tried, in |
| 220 |
# turn, on files of unknown format. Each parser must raise an exception when |
| 221 |
# fed a format further down the list. |
| 222 |
# |
| 223 |
# The general trend is most common to least common file format. However, |
| 224 |
# 'nbrf_io' is before 'fasta_io' because nbrf looks like fasta with extras, and |
| 225 |
# 'array_io' is last, since it is very general. |
| 226 |
_parsers = ( |
| 227 |
nbrf_io, |
| 228 |
fasta_io, |
| 229 |
clustal_io, |
| 230 |
phylip_io, |
| 231 |
genbank_io, |
| 232 |
stockholm_io, |
| 233 |
msf_io, |
| 234 |
nexus_io, |
| 235 |
table_io, |
| 236 |
array_io, |
| 237 |
) |
| 238 |
|
| 239 |
|
| 240 |
def _get_parsers(fin: TextIO) -> List[ModuleType]: |
| 241 |
global _parsers |
| 242 |
|
| 243 |
fnames = format_names() |
| 244 |
fext = format_extensions() |
| 245 |
parsers = list(_parsers) |
| 246 |
best_guess = parsers[0] |
| 247 |
|
| 248 |
# If a filename is supplied use the extension to guess the format. |
| 249 |
if hasattr(fin, "name") and "." in fin.name: |
| 250 |
extension = fin.name.split(".")[-1] |
| 251 |
if extension in fnames: |
| 252 |
best_guess = fnames[extension] |
| 253 |
elif extension in fext: |
| 254 |
best_guess = fext[extension] |
| 255 |
|
| 256 |
if best_guess in parsers: |
| 257 |
parsers.remove(best_guess) |
| 258 |
parsers.insert(0, best_guess) |
| 259 |
|
| 260 |
return parsers |
| 261 |
|
| 262 |
|
| 263 |
def read(fin: TextIO, alphabet: Optional[Alphabet] = None) -> SeqList: |
| 264 |
"""Read a sequence file and attempt to guess its format. |
| 265 |
First the filename extension (if available) is used to infer the format. |
| 266 |
If that fails, then we attempt to parse the file using several common |
| 267 |
formats. |
| 268 |
|
| 269 |
Note, fin cannot be unseekable stream such as sys.stdin |
| 270 |
|
| 271 |
returns : |
| 272 |
SeqList |
| 273 |
Raises : |
| 274 |
ValueError: If the file cannot be parsed. |
| 275 |
ValueError: Sequence do not conform to the alphabet. |
| 276 |
""" |
| 277 |
|
| 278 |
alphabet = Alphabet(alphabet) |
| 279 |
parsers = _get_parsers(fin) |
| 280 |
|
| 281 |
for p in parsers: |
| 282 |
fin.seek(0) |
| 283 |
try: |
| 284 |
return p.read(fin, alphabet) # type: ignore |
| 285 |
except ValueError: |
| 286 |
pass |
| 287 |
|
| 288 |
names = ", ".join([p.names[0] for p in parsers]) # type: ignore |
| 289 |
raise ValueError("Cannot parse sequence file: Tried %s " % names) |