<commit_before>import os

import readers
import filtering
import writers


class BaseDriver(object):
    def __init__(self, fns, outdir, **kwargs):
        self.fns = fns
        self.outdir = outdir
        if 'outsuffix' in kwargs:
            self.outsuffix = kwargs['outsuffix']
        if 'targetsuffix' in kwargs:
            self.targetsuffix = kwargs['targetsuffix']
        if 'decoysuffix' in kwargs:
            self.decoysuffix = kwargs['decoysuffix']

    def prepare_percolator_output(self, fn):
        """Returns namespace and static xml from percolator output file"""
        ns = readers.get_namespace(fn)
        static = readers.get_percolator_static_xml(fn, ns)
        return ns, static

    def create_outfilepath(self, fn, suffix=None):
        basefn = os.path.basename(fn)
        outfn = basefn + suffix
        return os.path.join(self.outdir, outfn)


def merge_multiple_fractions(fns):
    """Performs the work to merge parallelized percolator fractions.
    Target/decoy split, filtering unique peptides, running qvality on resulting
    score distributions for psms and peptides and setting values."""
    pass

class SplitDriver(BaseDriver):
    def split(self): #targetsuffix='_target.xml', decoysuffix='_decoy.xml'):
        """ Calls splitter to split percolator output into target/decoy elements.
            Writes two new xml files with features. Currently only psms and
            peptides. Proteins not here, since one cannot do protein inference
            before having merged and remapped multifraction data anyway.
        """
        for fn in self.fns:
            namespace, static_xml = self.prepare_percolator_output(fn)
            split_elements = filtering.split_target_decoy(fn, namespace)
            targetfn = self.create_outfilepath(fn, self.outdir, self.targetsuffix)
            decoyfn = self.create_outfilepath(fn, self.outdir, self.decoysuffix)
            writers.write_percolator_xml(static_xml, split_elements['target'], 
                                            targetfn)
            writers.write_percolator_xml(static_xml, split_elements['decoy'],
                                            decoyfn)



class MergeDriver(BaseDriver):
    """Base class for merging multiple percolator fractions under different
    sorts of filtering. It writes a single percolator out xml from multiple fractions.
    Namespace and static xml come from first percolator file. 
    Make sure fractions are from same percolator run."""

    def __init__(self, fns, outdir, **kwargs):
        super(MergeDriver, self).__init__(fns, outdir, kwargs)
        if 'score' in kwargs:
            self.score = kwargs['score']
        else:
            self.score = 'svm'

        if 'searchspace' in kwargs:
            self.searchspace = kwargs['searchspace']

    def run(self):
        self.prepare_merge()
        self.merge()<commit_msg>Better option handling in driver code<commit_after>import os

import readers
import filtering
import writers


class BaseDriver(object):
    def __init__(self, fns, outdir, **kwargs):
        self.fns = fns
        self.outdir = outdir
        self.outsuffix = kwargs.get('outsuffix', '.xml')

    def prepare_percolator_output(self, fn):
        """Returns namespace and static xml from percolator output file"""
        ns = readers.get_namespace(fn)
        static = readers.get_percolator_static_xml(fn, ns)
        return ns, static

    def create_outfilepath(self, fn, suffix=None):
        basefn = os.path.basename(fn)
        outfn = basefn + suffix
        return os.path.join(self.outdir, outfn)


def merge_multiple_fractions(fns):
    """Performs the work to merge parallelized percolator fractions.
    Target/decoy split, filtering unique peptides, running qvality on resulting
    score distributions for psms and peptides and setting values."""
    pass

class SplitDriver(BaseDriver):
    def __init__(self, fns, outdir, kwargs):
        super(MergeDriver, self).__init__(fns, outdir, kwargs)
        self.targetsuffix = kwargs.get('targetsuffix', '_target.xml')
        self.decoysuffix = kwargs.get('decoysuffix', '_decoy.xml')

    def run(self):
        self.split()

    def split(self): #targetsuffix='_target.xml', decoysuffix='_decoy.xml'):
        """ Calls splitter to split percolator output into target/decoy elements.
            Writes two new xml files with features. Currently only psms and
            peptides. Proteins not here, since one cannot do protein inference
            before having merged and remapped multifraction data anyway.
        """
        for fn in self.fns:
            namespace, static_xml = self.prepare_percolator_output(fn)
            split_elements = filtering.split_target_decoy(fn, namespace)
            targetfn = self.create_outfilepath(fn, self.outdir, self.targetsuffix)
            decoyfn = self.create_outfilepath(fn, self.outdir, self.decoysuffix)
            writers.write_percolator_xml(static_xml, split_elements['target'], 
                                            targetfn)
            writers.write_percolator_xml(static_xml, split_elements['decoy'],
                                            decoyfn)



class MergeDriver(BaseDriver):
    """Base class for merging multiple percolator fractions under different
    sorts of filtering. It writes a single percolator out xml from multiple fractions.
    Namespace and static xml come from first percolator file. 
    Make sure fractions are from same percolator run."""

    def __init__(self, fns, outdir, **kwargs):
        super(MergeDriver, self).__init__(fns, outdir, kwargs)
        self.outsuffix = kwargs.get('outsuffix', '_merged.xml')
        self.score = kwargs.get('score', 'svm')

        if 'searchspace' in kwargs:
            self.searchspace = kwargs['searchspace']

    def run(self):
        self.prepare_merge()
        self.merge()
{'fraction_non_alphanumeric': 0.08554421768707483, 'fraction_numerical': 0.0, 'mean_word_length': 2.6964173475801383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 28}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '281574', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1583, 'n_words': 476}