<commit_before>#! /usr/bin/local/python
# -*- coding: utf-8 -*-

import breakmer.utils as utils

__author__ = "<NAME>"
__copyright__ = "Copyright 2015, <NAME>"
__email__ = "<EMAIL>"
__license__ = "MIT"


class Filter:
    """
    """

    def __init(self):
        self.name = None
        self.svType = None
        self.breakpoints = None
        self.description = None

    def set_values(self, values):
        name, svType, breakpoints, description = values
        self.name = name.lower()
        self.svType = svType
        self.breakpoints = self.parse_breakpoints(breakpoints)
        self.description = description

    def parse_breakpoints(self, breakpoints):
        """ """
        breakpointList = breakpoints.split(',')
        brkpts = []
        for breakpoint in breakpointList:
            chrom, bps = breakpoint.split(':')
            bpSplit = bps.split('-')
            brkptList = [chrom]
            for bp in bpSplit:
                brkptList.append(bp)
            brkpts.append(brkptList)
        return brkpts


class ResultFilter:
    """
    """

    def __init__(self, filterFn, params):
        self.loggingName = 'breakmer.caller.filter'
        self.filterFn = filterFn
        self.filters = []
        self.params = params
        self.setup()

    def setup(self):
        if self.filterFn:
            for line in open(self.filterFn, 'rU'):
                line = line.strip()
                print line.split('\t')
                resultFilter = Filter()
                resultFilter.set_values(line.split('\t'))
                self.filters.append(resultFilter)

# Filters for events
# 1. Contig complexity
        # avg_comp, comp_vec = calc_contig_complexity(self.contig_seq)
                # brkpt_rep_filt = False
                    # brkpt_rep_filt = brkpt_rep_filt or (comp_vec[qb[0]] < (avg_comp / 2))
                # brkpt_rep_filt = brkpt_rep_filt or (len(filter(lambda x: x, brkpts['f'])) > 0)

    def check_filters(self, svEvent):
        """
        """
        if len(self.filters) > 1:
            # Check if event is in the pre-defined filters
            self.check_defined_filters(svEvent)

        if svEvent.svType == 'indel':
            self.filter_indel(svEvent)
        elif svEvent.svType == 'trl':
            self.filter_trl(svEvent)
        elif svEvent.svType == 'rearr':
            self.filter_rearr(svEvent)

    def check_defined_filters(self, svEvent):
        """The event must match
            1. target name
            2. SV type (indel, trl, rearrangement_inversion, rearrangement_tandem_dup)
            3. breakpoints
            4. description
        """
        print 'Checking defined filters'
        for SVFilter in self.filters:
            nameMatch = svEvent.get_target_name().lower() == SVFilter.name
            typeMatch = svEvent.svType == SVFilter.svType
            eventBrkpts = svEvents.get_brkpts()
            bpMatches = True
            for eventBrkpt in eventBrkpts:
                match = False
                for filterBrkpt in SVFilter.breakpoints:
                    if len(eventBrkpt) == len(filterBrkpt):
                        bpMatch = True
                        for v1, v2 in zip(eventBrkpt, filterBrkpt):
                            if str(v1) != str(v2):
                                bpMatch = False
                                break
                        if bpMatch:
                            match = True
                            break
                boMatches = bpMatches and match
            if nameMatch and typeMatch and bpMatches:
                svEvent.set_filtered('Matched input filter variant')

    def filter_indel(self, svEvent):
        """ """
        indelSizeThresh = int(self.params.get_param('indel_size'))
        utils.log(self.loggingName, 'info', 'Checking if blat result contains an indel variant')
        blatResult = svEvent.blatResults[0][1]
        keep_br = blatResult.valid and blatResult.meanCov < 2 and blatResult.in_target and (blatResult.indel_maxevent_size[0] >= indelSizeThresh)
        utils.log(self.loggingName, 'debug', 'Keep blat result %r' % keep_br)

        # Determine the uniqueness of the realignment.
        svFilterValues = svEvent.resultValues.filterValues
        uniqRealignment = svFilterValues.resultMeanHitFreq < 2
        indelSize = svFilterValues.maxEventSize >= indelSizeThresh
        brkptCoverages = svFilterValues.brkptCoverages[0] >= self.params.get_sr_thresh('indel')<commit_msg>Bug fix<commit_after>#! /usr/bin/local/python
# -*- coding: utf-8 -*-

import sys
import breakmer.utils as utils

__author__ = "<NAME>"
__copyright__ = "Copyright 2015, <NAME>"
__email__ = "<EMAIL>"
__license__ = "MIT"


class Filter:
    """
    """

    def __init(self):
        self.name = None
        self.svType = None
        self.breakpoints = None
        self.description = None

    def set_values(self, values):
        name, svType, breakpoints, description = values
        self.name = name.lower()
        self.svType = svType
        self.breakpoints = self.parse_breakpoints(breakpoints)
        self.description = description

    def parse_breakpoints(self, breakpoints):
        """ """
        breakpointList = breakpoints.split(',')
        brkpts = []
        for breakpoint in breakpointList:
            chrom, bps = breakpoint.split(':')
            bpSplit = bps.split('-')
            brkptList = [chrom]
            for bp in bpSplit:
                brkptList.append(bp)
            brkpts.append(brkptList)
        return brkpts


class ResultFilter:
    """
    """

    def __init__(self, filterFn, params):
        self.loggingName = 'breakmer.caller.filter'
        self.filterFn = filterFn
        self.filters = []
        self.params = params
        self.setup()

    def setup(self):
        if self.filterFn:
            for line in open(self.filterFn, 'rU'):
                line = line.strip()
                print line.split('\t')
                resultFilter = Filter()
                resultFilter.set_values(line.split('\t'))
                self.filters.append(resultFilter)

# Filters for events
# 1. Contig complexity
        # avg_comp, comp_vec = calc_contig_complexity(self.contig_seq)
                # brkpt_rep_filt = False
                    # brkpt_rep_filt = brkpt_rep_filt or (comp_vec[qb[0]] < (avg_comp / 2))
                # brkpt_rep_filt = brkpt_rep_filt or (len(filter(lambda x: x, brkpts['f'])) > 0)

    def check_filters(self, svEvent):
        """
        """
        if len(self.filters) > 1:
            # Check if event is in the pre-defined filters
            self.check_defined_filters(svEvent)

        if svEvent.svType == 'indel':
            self.filter_indel(svEvent)
        elif svEvent.svType == 'trl':
            self.filter_trl(svEvent)
        elif svEvent.svType == 'rearr':
            self.filter_rearr(svEvent)

    def check_defined_filters(self, svEvent):
        """The event must match
            1. target name
            2. SV type (indel, trl, rearrangement_inversion, rearrangement_tandem_dup)
            3. breakpoints
            4. description
        """
        print 'Checking defined filters'
        for SVFilter in self.filters:
            print 'Names', svEvent.get_target_name().lower(), SVFilter.name
            print 'Types', svEvent.svType, SVFilter.svType
            nameMatch = svEvent.get_target_name().lower() == SVFilter.name
            typeMatch = svEvent.svType == SVFilter.svType
            eventBrkpts = svEvents.get_brkpts()
            bpMatches = True
            for eventBrkpt in eventBrkpts:
                match = False
                for filterBrkpt in SVFilter.breakpoints:
                    print 'lens', len(eventBrkpt), len(filterBrkpt)
                    if len(eventBrkpt) == len(filterBrkpt):
                        bpMatch = True
                        for v1, v2 in zip(eventBrkpt, filterBrkpt):
                            print 'bps', v1, v2
                            if str(v1) != str(v2):
                                bpMatch = False
                                break
                        if bpMatch:
                            match = True
                            break
                boMatches = bpMatches and match
            print nameMatch, typeMatch, bpMatches
            if nameMatch and typeMatch and bpMatches:
                svEvent.set_filtered('Matched input filter variant')
        sys.exit()

    def filter_indel(self, svEvent):
        """ """
        indelSizeThresh = int(self.params.get_param('indel_size'))
        utils.log(self.loggingName, 'info', 'Checking if blat result contains an indel variant')
        blatResult = svEvent.blatResults[0][1]
        keep_br = blatResult.valid and blatResult.meanCov < 2 and blatResult.in_target and (blatResult.indel_maxevent_size[0] >= indelSizeThresh)
        utils.log(self.loggingName, 'debug', 'Keep blat result %r' % keep_br)

        # Determine the uniqueness of the realignment.
        svFilterValues = svEvent.resultValues.filterValues
        uniqRealignment = svFilterValues.resultMeanHitFreq < 2
        indelSize = svFilterValues.maxEventSize >= indelSizeThresh
        brkptCoverages = svFilterValues.brkptCoverages[0] >= self.params.get_sr_thresh('indel')
{'fraction_non_alphanumeric': 0.09726247987117552, 'fraction_numerical': 0.005367686527106817, 'mean_word_length': 1.862058371735791, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, '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': '4593204', 'n_tokens_mistral': 2802, 'n_tokens_neox': 2662, 'n_words': 603}