def getTransformation(self, s_from, s_to):

        if s_from == s_to:
            # null smoothing
            return smoothing.SmoothingNull()

        try:
            return self.transformations[s_from][s_to]
        except KeyError:
            return None
示例#2
0
def addDataToTrafo(tr_data,
                   run_0,
                   run_1,
                   spl_aligner,
                   multipeptides,
                   realign_method,
                   max_rt_diff,
                   topN=5,
                   sd_max_data_length=1000):
    id_0 = run_0.get_id()
    id_1 = run_1.get_id()

    if id_0 == id_1:
        null = smoothing.SmoothingNull()
        tr_data.addTrafo(id_0, id_1, null)
        tr_data.addTrafo(id_1, id_0, null)
        return

    # Data
    data_0, data_1 = spl_aligner._getRTData(run_0, run_1, multipeptides)
    tr_data.addData(id_0, data_0, id_1, data_1)

    # import pylab
    # pylab.scatter(data_0, data_1)
    # pylab.savefig('data_%s_%s.pdf' % (run_0, run_1) )
    # pylab.clf()
    # pylab.scatter(data_0, data_1)
    # pylab.xlim(2300, 2600)
    # pylab.ylim(2300, 2600)
    # pylab.savefig('data_%s_%s_zoom.pdf' % (run_0, run_1) )
    # pylab.clf()

    if len(data_0) == 0:
        null = smoothing.SmoothingNull()
        tr_data.addTrafo(id_0, id_1, null)
        tr_data.addTrafo(id_1, id_0, null)
        return

    # Smoothers
    sm_0_1 = smoothing.getSmoothingObj(realign_method,
                                       topN=topN,
                                       max_rt_diff=max_rt_diff,
                                       min_rt_diff=0.1,
                                       removeOutliers=False,
                                       tmpdir=None)
    sm_1_0 = smoothing.getSmoothingObj(realign_method,
                                       topN=topN,
                                       max_rt_diff=max_rt_diff,
                                       min_rt_diff=0.1,
                                       removeOutliers=False,
                                       tmpdir=None)
    # Initialize smoother
    sm_0_1.initialize(data_0, data_1)
    sm_1_0.initialize(data_1, data_0)

    # Compute error for alignment (standard deviation)
    stdev_0_1 = 0.0
    stdev_1_0 = 0.0
    if sd_max_data_length > 0:
        sample_idx = random.sample(xrange(len(data_0)),
                                   min(sd_max_data_length, len(data_0)))
        data_0_s = [data_0[i] for i in sample_idx]
        data_1_s = [data_1[i] for i in sample_idx]
        data0_aligned = sm_0_1.predict(data_0_s)
        stdev_0_1 = numpy.std(
            numpy.array(data_1_s) - numpy.array(data0_aligned))
        data1_aligned = sm_1_0.predict(data_1_s)
        stdev_1_0 = numpy.std(
            numpy.array(data_0_s) - numpy.array(data1_aligned))
        print "stdev for", id_0, id_1, stdev_0_1, " / ", stdev_1_0, "on data length", len(
            data_0_s)

    # Add data
    tr_data.addTrafo(id_0, id_1, sm_0_1, stdev_0_1)
    tr_data.addTrafo(id_1, id_0, sm_1_0, stdev_1_0)
def addDataToTrafo(tr_data,
                   run_0,
                   run_1,
                   spl_aligner,
                   multipeptides,
                   realign_method,
                   max_rt_diff,
                   topN=5,
                   sd_max_data_length=5000,
                   force=False):
    id_0 = run_0.get_id()
    id_1 = run_1.get_id()

    if id_0 == id_1:
        null = smoothing.SmoothingNull()
        tr_data.addTrafo(id_0, id_1, null)
        tr_data.addTrafo(id_1, id_0, null)
        return

    # Data
    data_0, data_1 = spl_aligner._getRTData(run_0, run_1, multipeptides)
    tr_data.addData(id_0, data_0, id_1, data_1)

    # import pylab
    # pylab.scatter(data_0, data_1)
    # pylab.savefig('data_%s_%s.pdf' % (run_0, run_1) )
    # pylab.clf()
    # pylab.scatter(data_0, data_1)
    # pylab.xlim(2300, 2600)
    # pylab.ylim(2300, 2600)
    # pylab.savefig('data_%s_%s_zoom.pdf' % (run_0, run_1) )
    # pylab.clf()

    if len(data_0) == 0:
        print("Warning, zero data!")
        if force:
            null = smoothing.SmoothingNull()
            tr_data.addTrafo(id_0, id_1, null)
            tr_data.addTrafo(id_1, id_0, null)
            return
        else:
            raise Exception("No data available for alignment %s vs %s" %
                            (id_0, id_1))

    # Smoothers
    sm_0_1 = smoothing.getSmoothingObj(realign_method,
                                       topN=topN,
                                       max_rt_diff=max_rt_diff,
                                       min_rt_diff=0.1,
                                       removeOutliers=False,
                                       tmpdir=None)
    sm_1_0 = smoothing.getSmoothingObj(realign_method,
                                       topN=topN,
                                       max_rt_diff=max_rt_diff,
                                       min_rt_diff=0.1,
                                       removeOutliers=False,
                                       tmpdir=None)
    # Initialize smoother
    sm_0_1.initialize(data_0, data_1)
    sm_1_0.initialize(data_1, data_0)

    # Compute error for alignment (standard deviation)
    stdev_0_1 = 0.0
    stdev_1_0 = 0.0
    if sd_max_data_length > 0:
        sample_idx = random.sample(xrange(len(data_0)),
                                   min(sd_max_data_length, len(data_0)))
        data_0_s = [data_0[i] for i in sample_idx]
        data_1_s = [data_1[i] for i in sample_idx]
        data0_aligned = sm_0_1.predict(data_0_s)
        stdev_0_1 = numpy.std(
            numpy.array(data_1_s) - numpy.array(data0_aligned))
        data1_aligned = sm_1_0.predict(data_1_s)
        stdev_1_0 = numpy.std(
            numpy.array(data_0_s) - numpy.array(data1_aligned))
        print("stdev for", id_0, id_1, stdev_0_1, " / ", stdev_1_0,
              "on data length", len(data_0_s))

    # Add data and trafo description.
    # The CyLightTransformationData actually requires to get a specific type of
    # transformation, the CyLinearInterpolateWrapper which may not be directly
    # passed to this function. We will try to recover the underlying linear
    # wrapper and then stick it into the tr_data object. If this fails, we just
    # revert to the regular behavior.
    try:
        sm_0_1_lwp = sm_0_1.internal_interpolation.getLWP()
        sm_1_0_lwp = sm_1_0.internal_interpolation.getLWP()
        tr_data.addTrafo(id_0, id_1, sm_0_1_lwp, stdev_0_1)
        tr_data.addTrafo(id_1, id_0, sm_1_0_lwp, stdev_1_0)
    except Exception:
        tr_data.addTrafo(id_0, id_1, sm_0_1, stdev_0_1)
        tr_data.addTrafo(id_1, id_0, sm_1_0, stdev_1_0)