1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
|
# Copyright 2006-2017,2020 by Peter Cock. All rights reserved.
#
# This file is part of the Biopython distribution and governed by your
# choice of the "Biopython License Agreement" or the "BSD 3-Clause License".
# Please see the LICENSE file that should have been included as part of this
# package.
#
# This module is for reading and writing FASTA format files as SeqRecord
# objects. The code is partly inspired by earlier Biopython modules,
# Bio.Fasta.* and the now removed module Bio.SeqIO.FASTA
"""Bio.SeqIO support for the "fasta" (aka FastA or Pearson) file format.
You are expected to use this module via the Bio.SeqIO functions.
"""
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio import BiopythonDeprecationWarning
from .Interfaces import _clean
from .Interfaces import _get_seq_string
from .Interfaces import _TextIOSource
from .Interfaces import SequenceIterator
from .Interfaces import SequenceWriter
import warnings
def SimpleFastaParser(handle):
"""Iterate over Fasta records as string tuples.
Arguments:
- handle - input stream opened in text mode
For each record a tuple of two strings is returned, the FASTA title
line (without the leading '>' character), and the sequence (with any
whitespace removed). The title line is not divided up into an
identifier (the first word) and comment or description.
>>> with open("Fasta/dups.fasta") as handle:
... for values in SimpleFastaParser(handle):
... print(values)
...
('alpha', 'ACGTA')
('beta', 'CGTC')
('gamma', 'CCGCC')
('alpha (again - this is a duplicate entry to test the indexing code)', 'ACGTA')
('delta', 'CGCGC')
"""
# Skip any text before the first record (e.g. blank lines, comments)
for line in handle:
if line[0] == ">":
title = line[1:].rstrip()
break
else:
# no break encountered - probably an empty file
return
# Main logic
# Note, remove trailing whitespace, and any internal spaces
# (and any embedded \r which are possible in mangled files
# when not opened in universal read lines mode)
lines = []
for line in handle:
if line[0] == ">":
yield title, "".join(lines).replace(" ", "").replace("\r", "")
lines = []
title = line[1:].rstrip()
continue
lines.append(line.rstrip())
yield title, "".join(lines).replace(" ", "").replace("\r", "")
def FastaTwoLineParser(handle):
"""Iterate over no-wrapping Fasta records as string tuples.
Arguments:
- handle - input stream opened in text mode
Functionally the same as SimpleFastaParser but with a strict
interpretation of the FASTA format as exactly two lines per
record, the greater-than-sign identifier with description,
and the sequence with no line wrapping.
Any line wrapping will raise an exception, as will excess blank
lines (other than the special case of a zero-length sequence
as the second line of a record).
Examples
--------
This file uses two lines per FASTA record:
>>> with open("Fasta/aster_no_wrap.pro") as handle:
... for title, seq in FastaTwoLineParser(handle):
... print("%s = %s..." % (title, seq[:3]))
...
gi|3298468|dbj|BAA31520.1| SAMIPF = GGH...
This equivalent file uses line wrapping:
>>> with open("Fasta/aster.pro") as handle:
... for title, seq in FastaTwoLineParser(handle):
... print("%s = %s..." % (title, seq[:3]))
...
Traceback (most recent call last):
...
ValueError: Expected FASTA record starting with '>' character. Perhaps this file is using FASTA line wrapping? Got: 'MTFGLVYTVYATAIDPKKGSLGTIAPIAIGFIVGANI'
"""
idx = -1 # for empty file
for idx, line in enumerate(handle):
if idx % 2 == 0: # title line
if line[0] != ">":
raise ValueError(
"Expected FASTA record starting with '>' character. "
"Perhaps this file is using FASTA line wrapping? "
f"Got: '{line}'"
)
title = line[1:].rstrip()
else: # sequence line
if line[0] == ">":
raise ValueError(
"Two '>' FASTA lines in a row. Missing sequence line "
"if this is strict two-line-per-record FASTA format. "
f"Have '>{title}' and '{line}'"
)
yield title, line.strip()
if idx == -1:
pass # empty file
elif idx % 2 == 0: # on a title line
raise ValueError(
"Missing sequence line at end of file if this is strict "
f"two-line-per-record FASTA format. Have title line '{line}'"
)
else:
assert line[0] != ">", "line[0] == '>' ; this should be impossible!"
class FastaIterator(SequenceIterator):
"""Parser for plain Fasta files without comments."""
modes = "t"
def __init__(
self,
source: _TextIOSource,
alphabet: None = None,
) -> None:
"""Iterate over Fasta records as SeqRecord objects.
Arguments:
- source - input stream opened in text mode, or a path to a file
- alphabet - optional alphabet, not used. Leave as None.
This parser expects a plain Fasta format without comments or header
lines.
By default this will act like calling Bio.SeqIO.parse(handle, "fasta")
with no custom handling of the title lines:
>>> with open("Fasta/dups.fasta") as handle:
... for record in FastaIterator(handle):
... print(record.id)
...
alpha
beta
gamma
alpha
delta
If you want to modify the records before writing, for example to change
the ID of each record, you can use a generator function as follows:
>>> def modify_records(records):
... for record in records:
... record.id = record.id.upper()
... yield record
...
>>> with open('Fasta/dups.fasta') as handle:
... for record in modify_records(FastaIterator(handle)):
... print(record.id)
...
ALPHA
BETA
GAMMA
ALPHA
DELTA
"""
if alphabet is not None:
raise ValueError("The alphabet argument is no longer supported")
super().__init__(source, fmt="Fasta")
try:
line = next(self.stream)
except StopIteration:
line = None
else:
if not line.startswith(">"):
warnings.warn(
"Previously, the FASTA parser silently ignored comments at the "
"beginning of the FASTA file (before the first sequence).\n"
"\n"
"Nowadays, the FASTA file format is usually understood not to "
"have any such comments, and most software packages do not allow "
"them. Therefore, the use of comments at the beginning of a FASTA "
"file is now deprecated in Biopython.\n"
"\n"
"In a future Biopython release, this deprecation warning will be "
"replaced by a ValueError. To avoid this, there are three "
"options:\n"
"\n"
"(1) Modify your FASTA file to remove such comments at the "
"beginning of the file.\n"
"\n"
"(2) Use SeqIO.parse with the 'fasta-pearson' format instead of "
"'fasta'. This format is consistent with the FASTA format defined "
"by William Pearson's FASTA aligner software. Thie format allows "
"for comments before the first sequence; lines starting with the "
"';' character anywhere in the file are also regarded as comment "
"lines and are ignored.\n"
"\n"
"(3) Use the 'fasta-blast' format. This format regards any lines "
"starting with '!', '#', or ';' as comment lines. The "
"'fasta-blast' format may be safer than the 'fasta-pearson' "
"format, as it explicitly indicates which lines are comments. ",
BiopythonDeprecationWarning,
)
for line in self.stream:
if line.startswith(">"):
break
else:
line = None
self._line = line
def __next__(self):
line = self._line
if line is None:
raise StopIteration
title = line[1:].rstrip()
# Main logic
# Note, remove trailing whitespace, and any internal spaces
# (and any embedded \r which are possible in mangled files
# when not opened in universal read lines mode)
lines = []
for line in self.stream:
if line[0] == ">":
break
lines.append(line)
else:
line = None
self._line = line
sequence = "".join(lines).encode().translate(None, b" \t\r\n")
try:
first_word = title.split(None, 1)[0]
except IndexError:
assert not title, repr(title)
# Should we use SeqRecord default for no ID?
first_word = ""
return SeqRecord._from_validated(
Seq(sequence), id=first_word, name=first_word, description=title
)
class FastaTwoLineIterator(SequenceIterator):
"""Parser for Fasta files with exactly two lines per record."""
modes = "t"
def __init__(self, source):
"""Iterate over two-line Fasta records (as SeqRecord objects).
Arguments:
- source - input stream opened in text mode, or a path to a file
This uses a strict interpretation of the FASTA as requiring
exactly two lines per record (no line wrapping).
Only the default title to ID/name/description parsing offered
by the relaxed FASTA parser is offered.
"""
super().__init__(source, fmt="FASTA")
self._data = FastaTwoLineParser(self.stream)
def __next__(self):
try:
title, sequence = next(self._data)
except StopIteration:
raise StopIteration from None
try:
first_word = title.split(None, 1)[0]
except IndexError:
assert not title, repr(title)
# Should we use SeqRecord default for no ID?
first_word = ""
return SeqRecord(
Seq(sequence), id=first_word, name=first_word, description=title
)
class FastaBlastIterator(SequenceIterator):
"""Parser for Fasta files, allowing for comments as in BLAST."""
modes = "t"
def __init__(
self,
source: _TextIOSource,
alphabet: None = None,
) -> None:
"""Iterate over Fasta records as SeqRecord objects.
Arguments:
- source - input stream opened in text mode, or a path to a file
- alphabet - optional alphabet, not used. Leave as None.
This parser expects the data to be in FASTA format. As in BLAST, lines
starting with '#', '!', or ';' are interpreted as comments and ignored.
This iterator acts like calling Bio.SeqIO.parse(handle, "fasta-blast")
with no custom handling of the title lines:
>>> with open("Fasta/dups.fasta") as handle:
... for record in FastaIterator(handle):
... print(record.id)
...
alpha
beta
gamma
alpha
delta
If you want to modify the records before writing, for example to change
the ID of each record, you can use a generator function as follows:
>>> def modify_records(records):
... for record in records:
... record.id = record.id.upper()
... yield record
...
>>> with open('Fasta/dups.fasta') as handle:
... for record in modify_records(FastaIterator(handle)):
... print(record.id)
...
ALPHA
BETA
GAMMA
ALPHA
DELTA
"""
if alphabet is not None:
raise ValueError("The alphabet argument is no longer supported")
super().__init__(source, fmt="FASTA")
for line in self.stream:
if line[0] not in "#!;":
if not line.startswith(">"):
raise ValueError(
"Expected FASTA record starting with '>' character.\n"
"If this line is a comment, please use '#', '!', or ';' as "
"the first character, or use the 'fasta-pearson' "
"format for parsing.\n"
f"Got: '{line}'"
)
self._line = line
break
else:
self._line = None
def __next__(self):
line = self._line
if line is None:
raise StopIteration
title = line[1:].rstrip()
lines = []
for line in self.stream:
# Main logic
# Note, remove trailing whitespace, and any internal spaces
# (and any embedded \r which are possible in mangled files
# when not opened in universal read lines mode)
if line[0] in "#!;":
pass
elif line[0] == ">":
self_line = line
break
else:
lines.append(line.rstrip())
else:
self._line = None
try:
first_word = title.split(None, 1)[0]
except IndexError:
first_word = ""
sequence = "".join(lines).replace(" ", "").replace("\r", "")
return SeqRecord(
Seq(sequence), id=first_word, name=first_word, description=title
)
class FastaPearsonIterator(SequenceIterator):
"""Parser for Fasta files, allowing for comments as in the FASTA aligner."""
modes = "t"
def __init__(
self,
source: _TextIOSource,
alphabet: None = None,
) -> None:
"""Iterate over Fasta records as SeqRecord objects.
Arguments:
- source - input stream opened in text mode, or a path to a file
- alphabet - optional alphabet, not used. Leave as None.
This parser expects a Fasta format allowing for a header (before the
first sequence record) and comments (lines starting with ';') as in
William Pearson's FASTA aligner software.
This iterator acts as calling Bio.SeqIO.parse(handle, "fasta-pearson")
with no custom handling of the title lines:
>>> with open("Fasta/dups.fasta") as handle:
... for record in FastaIterator(handle):
... print(record.id)
...
alpha
beta
gamma
alpha
delta
If you want to modify the records before writing, for example to change
the ID of each record, you can use a generator function as follows:
>>> def modify_records(records):
... for record in records:
... record.id = record.id.upper()
... yield record
...
>>> with open('Fasta/dups.fasta') as handle:
... for record in modify_records(FastaIterator(handle)):
... print(record.id)
...
ALPHA
BETA
GAMMA
ALPHA
DELTA
"""
if alphabet is not None:
raise ValueError("The alphabet argument is no longer supported")
super().__init__(source, fmt="Fasta")
for line in self.stream:
if line.startswith(">"):
self._line = line
break
else:
self._line = None
def __next__(self):
line = self._line
if line is None:
raise StopIteration
title = line[1:].rstrip()
lines = []
for line in self.stream:
# Main logic
# Note, remove trailing whitespace, and any internal spaces
# (and any embedded \r which are possible in mangled files
# when not opened in universal read lines mode)
if line[0] == ";":
pass
elif line[0] == ">":
self._line = line
break
else:
lines.append(line.rstrip())
else:
self._line = None
try:
first_word = title.split(None, 1)[0]
except IndexError:
first_word = ""
sequence = "".join(lines).replace(" ", "").replace("\r", "")
return SeqRecord(
Seq(sequence), id=first_word, name=first_word, description=title
)
class FastaWriter(SequenceWriter):
"""Class to write Fasta format files (OBSOLETE).
Please use the ``as_fasta`` function instead, or the top level
``Bio.SeqIO.write()`` function instead using ``format="fasta"``.
"""
modes = "t"
def __init__(self, target, wrap=60, record2title=None):
"""Create a Fasta writer (OBSOLETE).
Arguments:
- target - Output stream opened in text mode, or a path to a file.
- wrap - Optional line length used to wrap sequence lines.
Defaults to wrapping the sequence at 60 characters
Use zero (or None) for no wrapping, giving a single
long line for the sequence.
- record2title - Optional function to return the text to be
used for the title line of each record. By default
a combination of the record.id and record.description
is used. If the record.description starts with the
record.id, then just the record.description is used.
You can either use::
handle = open(filename, "w")
writer = FastaWriter(handle)
writer.write_file(myRecords)
handle.close()
Or, follow the sequential file writer system, for example::
handle = open(filename, "w")
writer = FastaWriter(handle)
...
Multiple writer.write_record() and/or writer.write_records() calls
...
handle.close()
"""
super().__init__(target)
if wrap:
if wrap < 1:
raise ValueError
self.wrap = wrap
self.record2title = record2title
def write_record(self, record):
"""Write a single Fasta record to the file."""
if self.record2title:
title = self.clean(self.record2title(record))
else:
id = self.clean(record.id)
description = self.clean(record.description)
if description and description.split(None, 1)[0] == id:
# The description includes the id at the start
title = description
elif description:
title = f"{id} {description}"
else:
title = id
assert "\n" not in title
assert "\r" not in title
self.handle.write(f">{title}\n")
data = _get_seq_string(record) # Catches sequence being None
assert "\n" not in data
assert "\r" not in data
if self.wrap:
for i in range(0, len(data), self.wrap):
self.handle.write(data[i : i + self.wrap] + "\n")
else:
self.handle.write(data + "\n")
class FastaTwoLineWriter(FastaWriter):
"""Class to write 2-line per record Fasta format files (OBSOLETE).
This means we write the sequence information without line
wrapping, and will always write a blank line for an empty
sequence.
Please use the ``as_fasta_2line`` function instead, or the top level
``Bio.SeqIO.write()`` function instead using ``format="fasta"``.
"""
def __init__(self, handle, record2title=None):
"""Create a 2-line per record Fasta writer (OBSOLETE).
Arguments:
- handle - Handle to an output file, e.g. as returned
by open(filename, "w")
- record2title - Optional function to return the text to be
used for the title line of each record. By default
a combination of the record.id and record.description
is used. If the record.description starts with the
record.id, then just the record.description is used.
You can either use::
handle = open(filename, "w")
writer = FastaWriter(handle)
writer.write_file(myRecords)
handle.close()
Or, follow the sequential file writer system, for example::
handle = open(filename, "w")
writer = FastaWriter(handle)
...
Multiple writer.write_record() and/or writer.write_records() calls
...
handle.close()
"""
super().__init__(handle, wrap=None, record2title=record2title)
def as_fasta(record):
"""Turn a SeqRecord into a FASTA formatted string.
This is used internally by the SeqRecord's .format("fasta")
method and by the SeqIO.write(..., ..., "fasta") function.
"""
id = _clean(record.id)
description = _clean(record.description)
if description and description.split(None, 1)[0] == id:
# The description includes the id at the start
title = description
elif description:
title = f"{id} {description}"
else:
title = id
assert "\n" not in title
assert "\r" not in title
lines = [f">{title}\n"]
data = _get_seq_string(record) # Catches sequence being None
assert "\n" not in data
assert "\r" not in data
for i in range(0, len(data), 60):
lines.append(data[i : i + 60] + "\n")
return "".join(lines)
def as_fasta_2line(record):
"""Turn a SeqRecord into a two-line FASTA formatted string.
This is used internally by the SeqRecord's .format("fasta-2line")
method and by the SeqIO.write(..., ..., "fasta-2line") function.
"""
id = _clean(record.id)
description = _clean(record.description)
if description and description.split(None, 1)[0] == id:
# The description includes the id at the start
title = description
elif description:
title = f"{id} {description}"
else:
title = id
assert "\n" not in title
assert "\r" not in title
data = _get_seq_string(record) # Catches sequence being None
assert "\n" not in data
assert "\r" not in data
return f">{title}\n{data}\n"
if __name__ == "__main__":
from Bio._utils import run_doctest
run_doctest(verbose=0)
|