<commit_before>#!/usr/bin/env python
r"""
Parse biological sequences (:mod:`skbio.parse.sequences`)
=========================================================

.. currentmodule:: skbio.parse.sequences

This module provides functions for parsing sequence files.

Functions
---------

.. autosummary::
   :toctree: generated/

    parse_fasta
    parse_fastq


"""

#-----------------------------------------------------------------------------
# Copyright (c) 2013--, scikit-bio development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

from skbio.core.exception import FastqParseError, RecordError
from skbio.parse.record_finder import (LabeledRecordFinder,
                                       DelimitedRecordFinder)
from skbio.core.sequence import BiologicalSequence
from skbio.core.alignment import (Alignment, SequenceCollection,
                                  SequenceCollectionError)
from skbio.parse.clustal import ClustalParser
from skbio.core.sequence import DNA

def is_fasta_label(x):
    """Checks if x looks like a FASTA label line."""
    return x.startswith('>')


def is_blank_or_comment(x):
    """Checks if x is blank or a FASTA comment line."""
    return (not x) or x.startswith('#') or x.isspace()


def is_blank(x):
    """Checks if x is blank."""
    return (not x) or x.isspace()

def is_rfam_header_line(line):
    """Returns True if line is a header line"""
    return line.startswith('#=GF')

def is_rfam_seq_line(line):
    """Returns True if line is a sequence line"""
    return bool(line) and (not line[0].isspace()) and \
    (not line.startswith('#')) and (not line.startswith('//'))

def is_rfam_structure_line(line):
    """Returns True if line is a structure line"""
    return line.startswith('#=GC SS_cons')

def load_from_clustal(data, seq_constructor=BiologicalSequence, strict=True):
    recs = [(name, seq_constructor(seq)) for name, seq in\
        ClustalParser(data, strict)]
    lengths = [len(i[1]) for i in recs]

    if lengths and max(lengths) == min(lengths):
        return Alignment.from_fasta_records(recs, DNA)
    else:
        return SequenceCollection.from_fasta_records(recs, DNA)

def is_empty_or_html(line):
    """Return True for HTML line and empty (or whitespace only) line.

    line -- string

    The Rfam adaptor that retrieves records inlcudes two HTML tags in
    the record. These lines need to be ignored in addition to empty lines. 
    """
    if line.startswith('<pre') or line.startswith('</pre'):<commit_msg>MAINT: Use BiologicalSequence instead of DNA<commit_after>#!/usr/bin/env python
r"""
Parse biological sequences (:mod:`skbio.parse.sequences`)
=========================================================

.. currentmodule:: skbio.parse.sequences

This module provides functions for parsing sequence files.

Functions
---------

.. autosummary::
   :toctree: generated/

    parse_fasta
    parse_fastq


"""

#-----------------------------------------------------------------------------
# Copyright (c) 2013--, scikit-bio development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

from skbio.core.exception import FastqParseError, RecordError
from skbio.parse.record_finder import (LabeledRecordFinder,
                                       DelimitedRecordFinder)
from skbio.core.sequence import BiologicalSequence
from skbio.core.alignment import (Alignment, SequenceCollection,
                                  SequenceCollectionError)
from skbio.parse.clustal import ClustalParser
from skbio.core.sequence import DNA

def is_fasta_label(x):
    """Checks if x looks like a FASTA label line."""
    return x.startswith('>')


def is_blank_or_comment(x):
    """Checks if x is blank or a FASTA comment line."""
    return (not x) or x.startswith('#') or x.isspace()


def is_blank(x):
    """Checks if x is blank."""
    return (not x) or x.isspace()

def is_rfam_header_line(line):
    """Returns True if line is a header line"""
    return line.startswith('#=GF')

def is_rfam_seq_line(line):
    """Returns True if line is a sequence line"""
    return bool(line) and (not line[0].isspace()) and \
    (not line.startswith('#')) and (not line.startswith('//'))

def is_rfam_structure_line(line):
    """Returns True if line is a structure line"""
    return line.startswith('#=GC SS_cons')

def load_from_clustal(data, seq_constructor=BiologicalSequence, strict=True):
    recs = [(name, seq_constructor(seq)) for name, seq in\
        ClustalParser(data, strict)]
    lengths = [len(i[1]) for i in recs]

    if lengths and max(lengths) == min(lengths):
        return Alignment.from_fasta_records(recs, BiologicalSequence)
    else:
        return SequenceCollection.from_fasta_records(recs, BiologicalSequence)

def is_empty_or_html(line):
    """Return True for HTML line and empty (or whitespace only) line.

    line -- string

    The Rfam adaptor that retrieves records inlcudes two HTML tags in
    the record. These lines need to be ignored in addition to empty lines. 
    """
    if line.startswith('<pre') or line.startswith('</pre'):
{'fraction_non_alphanumeric': 0.1804553026096613, 'fraction_numerical': 0.002220988339811216, 'mean_word_length': 2.8271954674220963, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6579094', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1510, 'n_words': 488}