# 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
        match_part2 = {'scores': {'similarity': 80.0},
                       'query_start': 10,
                       'query_end': 20,
                       'query_strand': 1,
                       'subject_start': 0,
                       'subject_end': 10,
                       'subject_strand': 1
                      }
        result1 = {'query': query,
                   'matches':
                        [{'subject': subject1,
                          'start': 10, 'end': 20,
                          'scores': {'expect': 0.01},
                          'match_parts': [match_part1]
                         },
                         {'subject': subject2,
                          'start': 10, 'end': 20,
                          'scores': {'expect': 0.01},
                          'match_parts': [match_part2]
                         }]}
        results = [result1]
        #filtering the query-subj with the same names
        scores = alignment_results_scores(results, ['similarity'])
        assert scores == [90.0]
        #without the filtering
        scores = alignment_results_scores(results, ['similarity'],
                                          filter_same_query_subject=False)
        assert scores == [90.0, 80.0]

        #we can ask for several scores at once

# 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

        match_part2 = {'scores': {'similarity': 80.0},
                       'query_start': 10,
                       'query_end': 20,
                       'query_strand': 1,
                       'subject_start': 0,
                       'subject_end': 10,
                       'subject_strand': 1
                      }
        result1 = {'query': query,
                   'matches':
                        [{'subject': subject1,
                          'start': 10, 'end': 20,
                          'scores': {'expect': 0.01},
                          'match_parts': [match_part1]
                         },
                         {'subject': subject2,
                          'start': 10, 'end': 20,
                          'scores': {'expect': 0.01},
                          'match_parts': [match_part2]
                         }]}
        results = [result1]
        #filtering the query-subj with the same names
        scores = alignment_results_scores(results, ['similarity'])
        assert scores == [90.0]
        #without the filtering
        scores = alignment_results_scores(results, ['similarity'],
                                          filter_same_query_subject=False)
        assert scores == [90.0, 80.0]

        #we can ask for several scores at once

        scores = alignment_results_scores(results,