# Path: crumbs/seq/alignment_result.py
# def _text_blasts_in_file(fhand):
# def _split_description(string):
# def _text_blast_parser(fhand):
#     def __init__(self, fhand):
#     def __iter__(self):
#     def next(self):
# def _lines_for_every_tab_blast(fhand, line_format):
# def _group_match_parts_by_subject(match_parts):
# def _tabular_blast_parser(fhand, line_format):
#     def __init__(self, fhand, line_format=DEFAULT_TABBLAST_FORMAT):
#     def __iter__(self):
#     def next(self):
#     def __init__(self, fhand, subj_def_as_accesion=None):
#     def __iter__(self):
#     def _create_result_structure(self, bio_result):
#     def _get_blast_metadata(self):
#     def next(self):
#     def __init__(self, fhand):
#     def __iter__(self):
#     def _results_query_from_exonerate(self):
#     def _create_structure_result(query_result):
#     def next(self):
# def _strand_transform(strand):
# def _match_num_if_exists_in_struc(subject_name, struct_dict):
# def get_alignment_parser(kind):
# def get_match_score(match, score_key, query=None, subject=None):
# def get_match_scores(match, score_keys, query, subject):
# def alignment_results_scores(results, scores, filter_same_query_subject=True):
# def build_relations_from_aligment(fhand, query_name, subject_name):
# def _get_match_score(match, score_key, query=None, subject=None):
# def _score_above_threshold(score, min_score, max_score, log_tolerance,
#                            log_best_score):
# def _create_scores_mapper_(score_key, score_tolerance=None,
#                            max_score=None, min_score=None):
#     def map_(alignment):
# def _create_best_scores_mapper(score_key, score_tolerance=None,
#                               max_score=None, min_score=None):
# def _create_scores_mapper(score_key, max_score=None, min_score=None):
# def _create_deepcopy_mapper():
#     def map_(alignment):
# def _create_empty_filter():
#     def filter_(alignment):
# def _fix_match_scores(match, score_keys):
# def _fix_match_start_end(match):
# def _fix_matches(alignment, score_keys=None):
# def _create_fix_matches_mapper():
# def covered_segments_from_match_parts(match_parts, in_query=True,
#                                       merge_segments_closer=1):
# def elongate_match_part_till_global(match_part, query_length, subject_length,
#                                     align_completely):
# def elongate_match_parts_till_global(match_parts, query_length,
#                                      subject_length, align_completely):
# def _match_length(match, length_from_query):
# def _match_part_length(match_part, length_in_query):
# def _match_long_enough(match_length, total_length, min_num_residues,
#                        min_percentage, length_in_query):
# def _create_min_length_mapper(length_in_query, min_num_residues=None,
#                               min_percentage=None, filter_match_parts=False):
#     def map_(alignment):
# def filter_alignments(alignments, config):
# class TextBlastParser(object):
# class TabularBlastParser(object):
# class BlastParser(object):
# class ExonerateParser(object):
# DEFAULT_TABBLAST_FORMAT = ('query', 'subject', 'identity', 'alignment_length',
#                            'mismatches', 'gap_open', 'query_start',
#                            'query_end', 'subject_start', 'subject_end',
#                            'expect', 'score')
# MAPPER = 1
# FILTER = 2
# FILTER_COLLECTION = {'best_scores':
#                                  {'funct_factory': _create_best_scores_mapper,
#                                   'kind': MAPPER},
#                      'score_threshold':
#                                  {'funct_factory': _create_scores_mapper,
#                                  'kind': MAPPER},
#                      'min_length': {'funct_factory': _create_min_length_mapper,
#                                     'kind': MAPPER},
#                      'deepcopy': {'funct_factory': _create_deepcopy_mapper,
#                                   'kind': MAPPER},
#                      'fix_matches':
#                                  {'funct_factory': _create_fix_matches_mapper,
#                                   'kind': MAPPER},
#                      'filter_empty':
#                                  {'funct_factory': _create_empty_filter,
#                                   'kind': FILTER},
#                     }
# 
# Path: crumbs/utils/test_utils.py
# TEST_DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..',
#                                              '..', 'test', 'test_data'))


import unittest
import os
import math
from StringIO import StringIO
from tempfile import NamedTemporaryFile
from crumbs.seq.alignment_result import (BlastParser, TabularBlastParser,
                                         alignment_results_scores,
                                         ExonerateParser,
                                         filter_alignments,
                                         covered_segments_from_match_parts,
                                         elongate_match_parts_till_global,
                                         TextBlastParser, QUERY, SUBJECT)
from crumbs.utils.test_utils import TEST_DATA_DIR
                                 {'query_start':8, 'query_end':27,
                                  'subject_start':182,
                                  'subject_end':201,
                                  'scores':{'expect':    1e-8,
                                            'identity':  100.0}
                                 }
                  ],
                 }
            ],
            },
            {'query':{'name':'primer2'},
            },
            {}, {}
        ]
        n_blasts = 0
        for index, blast in enumerate(parser):
            _check_blast(blast, expected_results[index])
            n_blasts += 1
        assert n_blasts == 2

    def test_blast_no_result(self):
        'It test that the xml output can be and empty string'
        blast_file = NamedTemporaryFile()
        blasts = BlastParser(fhand=blast_file)

        filters = [{'kind': 'best_scores',
                    'score_key': 'expect',
                    'max_score': 1e-4,
                    'score_tolerance': 10
                   }]

# Path: crumbs/seq/alignment_result.py
# def _text_blasts_in_file(fhand):
# def _split_description(string):
# def _text_blast_parser(fhand):
#     def __init__(self, fhand):
#     def __iter__(self):
#     def next(self):
# def _lines_for_every_tab_blast(fhand, line_format):
# def _group_match_parts_by_subject(match_parts):
# def _tabular_blast_parser(fhand, line_format):
#     def __init__(self, fhand, line_format=DEFAULT_TABBLAST_FORMAT):
#     def __iter__(self):
#     def next(self):
#     def __init__(self, fhand, subj_def_as_accesion=None):
#     def __iter__(self):
#     def _create_result_structure(self, bio_result):
#     def _get_blast_metadata(self):
#     def next(self):
#     def __init__(self, fhand):
#     def __iter__(self):
#     def _results_query_from_exonerate(self):
#     def _create_structure_result(query_result):
#     def next(self):
# def _strand_transform(strand):
# def _match_num_if_exists_in_struc(subject_name, struct_dict):
# def get_alignment_parser(kind):
# def get_match_score(match, score_key, query=None, subject=None):
# def get_match_scores(match, score_keys, query, subject):
# def alignment_results_scores(results, scores, filter_same_query_subject=True):
# def build_relations_from_aligment(fhand, query_name, subject_name):
# def _get_match_score(match, score_key, query=None, subject=None):
# def _score_above_threshold(score, min_score, max_score, log_tolerance,
#                            log_best_score):
# def _create_scores_mapper_(score_key, score_tolerance=None,
#                            max_score=None, min_score=None):
#     def map_(alignment):
# def _create_best_scores_mapper(score_key, score_tolerance=None,
#                               max_score=None, min_score=None):
# def _create_scores_mapper(score_key, max_score=None, min_score=None):
# def _create_deepcopy_mapper():
#     def map_(alignment):
# def _create_empty_filter():
#     def filter_(alignment):
# def _fix_match_scores(match, score_keys):
# def _fix_match_start_end(match):
# def _fix_matches(alignment, score_keys=None):
# def _create_fix_matches_mapper():
# def covered_segments_from_match_parts(match_parts, in_query=True,
#                                       merge_segments_closer=1):
# def elongate_match_part_till_global(match_part, query_length, subject_length,
#                                     align_completely):
# def elongate_match_parts_till_global(match_parts, query_length,
#                                      subject_length, align_completely):
# def _match_length(match, length_from_query):
# def _match_part_length(match_part, length_in_query):
# def _match_long_enough(match_length, total_length, min_num_residues,
#                        min_percentage, length_in_query):
# def _create_min_length_mapper(length_in_query, min_num_residues=None,
#                               min_percentage=None, filter_match_parts=False):
#     def map_(alignment):
# def filter_alignments(alignments, config):
# class TextBlastParser(object):
# class TabularBlastParser(object):
# class BlastParser(object):
# class ExonerateParser(object):
# DEFAULT_TABBLAST_FORMAT = ('query', 'subject', 'identity', 'alignment_length',
#                            'mismatches', 'gap_open', 'query_start',
#                            'query_end', 'subject_start', 'subject_end',
#                            'expect', 'score')
# MAPPER = 1
# FILTER = 2
# FILTER_COLLECTION = {'best_scores':
#                                  {'funct_factory': _create_best_scores_mapper,
#                                   'kind': MAPPER},
#                      'score_threshold':
#                                  {'funct_factory': _create_scores_mapper,
#                                  'kind': MAPPER},
#                      'min_length': {'funct_factory': _create_min_length_mapper,
#                                     'kind': MAPPER},
#                      'deepcopy': {'funct_factory': _create_deepcopy_mapper,
#                                   'kind': MAPPER},
#                      'fix_matches':
#                                  {'funct_factory': _create_fix_matches_mapper,
#                                   'kind': MAPPER},
#                      'filter_empty':
#                                  {'funct_factory': _create_empty_filter,
#                                   'kind': FILTER},
#                     }
# 
# Path: crumbs/utils/test_utils.py
# TEST_DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..',
#                                              '..', 'test', 'test_data'))


# Path: test/seq/test_alignment_result.py
import unittest
import os
import math
from StringIO import StringIO
from tempfile import NamedTemporaryFile
from crumbs.seq.alignment_result import (BlastParser, TabularBlastParser,
                                         alignment_results_scores,
                                         ExonerateParser,
                                         filter_alignments,
                                         covered_segments_from_match_parts,
                                         elongate_match_parts_till_global,
                                         TextBlastParser, QUERY, SUBJECT)
from crumbs.utils.test_utils import TEST_DATA_DIR

                                 {'query_start':8, 'query_end':27,
                                  'subject_start':182,
                                  'subject_end':201,
                                  'scores':{'expect':    1e-8,
                                            'identity':  100.0}
                                 }
                  ],
                 }
            ],
            },
            {'query':{'name':'primer2'},
            },
            {}, {}
        ]
        n_blasts = 0
        for index, blast in enumerate(parser):
            _check_blast(blast, expected_results[index])
            n_blasts += 1
        assert n_blasts == 2

    def test_blast_no_result(self):
        'It test that the xml output can be and empty string'
        blast_file = NamedTemporaryFile()
        blasts = BlastParser(fhand=blast_file)

        filters = [{'kind': 'best_scores',
                    'score_key': 'expect',
                    'max_score': 1e-4,
                    'score_tolerance': 10
                   }]

        filt_b = filter_alignments(blasts, config=filters,)