<commit_before>"""
Parser module specific to Stockholm sequence files
"""

__author__ = "<NAME>"
__date__ = "09 Sep 2016"
__version__ = 0.1

from conkit.core import Sequence
from conkit.core import SequenceFile
from conkit.io._ParserIO import _SequenceFileParser

import os
import re

V_RECORD = re.compile(r'^#(\s+STOCKHOLM.*)$')
GF_RECORD = re.compile(r'^#=GF\s+\S+\s+(.*)$')
GR_RECORD = re.compile(r'^#=GR\s+(\S+)\s+(\S+)\s+(.*)$')
GS_RECORD = re.compile(r'^#=GS\s+(\S+)\s+(\S+)\s+(.*)$')
SEQ_RECORD = re.compile(r'^(\S+)\s+([A-Z0-9~-]+)$')
END_RECORD = re.compile(r'^//$')


class StockholmIO(_SequenceFileParser):
    """Parser class for Stockholm sequence files
    """
    def __init__(self):
        super(StockholmIO, self).__init__()

    def read(self, f_handle, f_id='stockholm'):
        """Read a sequence file

        Parameters
        ----------
        f_handle
           Open file handle [read permissions]
        f_id : str, optional
           Unique sequence file identifier

        Returns
        -------
        :obj:`SequenceFile <conkit.core.SequenceFile>`

        """

        # Create a new sequence file instance
        sequence_file = SequenceFile(f_id)

        # Read any possible comments and store in file remarks
        while True:
            line = f_handle.readline().rstrip()

            if not line:
                continue
            elif V_RECORD.match(line):
                version = V_RECORD.match(line).group(1)
                # sequence_file.add_remark(version)
            elif GF_RECORD.match(line) or GS_RECORD.match(line):
                break

        # Read the sequence record(s)
        while True:
            if GF_RECORD.match(line):
                ident = GF_RECORD.match(line).group(1)[:-3]
                sequence_entry = Sequence(ident, "")
                sequence_file.add(sequence_entry)

            elif GS_RECORD.match(line):
                ident, _, desc = GS_RECORD.match(line).groups()
                sequence_entry = Sequence(ident, "")
                sequence_entry.remark = desc
                sequence_file.add(sequence_entry)

            elif GR_RECORD.match(line):
                pass

            elif SEQ_RECORD.match(line):
                ident, seq = SEQ_RECORD.match(line).groups()
                sequence_file[ident].seq = sequence_file[ident].seq + seq

            line = f_handle.readline().rstrip()

            # // char in alignment reached
            if END_RECORD.match(line):<commit_msg>bug fix - stockholm reading from phmmer didn't work<commit_after>"""
Parser module specific to Stockholm sequence files
"""

__author__ = "<NAME>"
__date__ = "09 Sep 2016"
__version__ = 0.1

from conkit.core import Sequence
from conkit.core import SequenceFile
from conkit.io._ParserIO import _SequenceFileParser

import os
import re

V_RECORD = re.compile(r'^#(\s+STOCKHOLM.*)$')
GF_RECORD = re.compile(r'^#=GF\s+\S+\s+(.*)$')
GR_RECORD = re.compile(r'^#=GR\s+(\S+)\s+(\S+)\s+(.*)$')
GS_RECORD = re.compile(r'^#=GS\s+(\S+)\s+(\S+)\s+(.*)$')
SEQ_RECORD = re.compile(r'^(\S+)\s+([A-Z0-9~-]+)$')
END_RECORD = re.compile(r'^//$')


class StockholmIO(_SequenceFileParser):
    """Parser class for Stockholm sequence files
    """
    def __init__(self):
        super(StockholmIO, self).__init__()

    def read(self, f_handle, f_id='stockholm'):
        """Read a sequence file

        Parameters
        ----------
        f_handle
           Open file handle [read permissions]
        f_id : str, optional
           Unique sequence file identifier

        Returns
        -------
        :obj:`SequenceFile <conkit.core.SequenceFile>`

        """

        # Create a new sequence file instance
        sequence_file = SequenceFile(f_id)

        # Read any possible comments and store in file remarks
        while True:
            line = f_handle.readline().rstrip()

            if not line:
                continue
            elif V_RECORD.match(line):
                version = V_RECORD.match(line).group(1)
                # sequence_file.add_remark(version)
            elif GF_RECORD.match(line) or GS_RECORD.match(line):
                break

        # Read the sequence record(s)
        while True:
            if GF_RECORD.match(line):
                ident = GF_RECORD.match(line).group(1)[:-3]
                sequence_entry = Sequence(ident, "")
                sequence_file.add(sequence_entry)

            elif GS_RECORD.match(line):
                ident, _, desc = GS_RECORD.match(line).groups()
                sequence_entry = Sequence(ident, "")
                sequence_entry.remark = desc
                sequence_file.add(sequence_entry)

            elif GR_RECORD.match(line):
                pass

            elif len(line.split()) == 2 and line.split()[0] in sequence_file:
                ident, seq = line.replace('.', '-').split()
                sequence_file[ident].seq = sequence_file[ident].seq + seq

            line = f_handle.readline().rstrip()

            # // char in alignment reached
            if END_RECORD.match(line):
{'fraction_non_alphanumeric': 0.13503937007874015, 'fraction_numerical': 0.005511811023622047, 'mean_word_length': 2.0645355850422193, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2452773', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1487, 'n_words': 328}