Beispiel #1
0
 def _get_all_precursors(self, par, precursor, cursor):
     precursors = []
     R = Residues.Residues("mono")
     pep = precursor.to_old_pep()
     for res in self._get_all_precursors_sub(par, pep, cursor):
         p = Precursor()
         p.initialize(*res)
         if p.included_in_isotopic_range(precursor.q1 - par.q1_window, precursor.q1 + par.q1_window, par):
             precursors.append(p)
     return precursors
Beispiel #2
0
 def _get_all_precursors(self, par, precursor, cursor):
     precursors = []
     R = Residues.Residues('mono')
     pep = precursor.to_old_pep()
     for res in self._get_all_precursors_sub(par, pep, cursor):
         p = Precursor()
         p.initialize(*res)
         if (p.included_in_isotopic_range(precursor.q1 - par.q1_window,
                                          precursor.q1 + par.q1_window,
                                          par)):
             precursors.append(p)
     return precursors
Beispiel #3
0
def runcpp(self, pep, transitions, precursors):
    #first we run the C++ version
    st = time.time()
    pre_obj = [
        Precursor(modified_sequence=p[1],
                  q1_charge=2,
                  q1=p[0],
                  transition_group=p[2],
                  isotopically_modified=0) for p in precursors
    ]
    for kk in range(10):
        tmp = c_getnonuis.calculate_collisions_per_peptide_other_ion_series(
            transitions, pre_obj, self.par, self.q3_low, self.q3_high,
            self.par.q3_window, self.par.ppm, False)
    ctime = (time.time() - st) / 10.0
    return tmp, ctime
Beispiel #4
0
    def setUp(self):
        self.limit = 100
        self.limit_large = 100
        self.limit = 300
        self.limit_large = 600
        try:
            import MySQLdb
            #db_l = MySQLdb.connect(read_default_file="~/.my.cnf.local")
            db = MySQLdb.connect(read_default_file="~/.srm.cnf")
            cursor = db.cursor()
            self.cursor = cursor

            par = test_shared.get_default_setup_parameters()
            par.use_sqlite = True
            par.q1_window = 1.2 / 2.0 
            par.q3_window = 2.0 / 2.0
            par.ssrcalc_window = 4 / 2.0 
            par.ssrcalc_window = 9999 / 2.0 
            par.peptide_tables = ['hroest.srmPeptides_yeast']
            par.transition_table = 'hroest.srmTransitions_yeast'
            par.isotopes_up_to = 3
            self.mycollider = collider.SRMcollider()
            par.select_by = "id"

            self.par = par
            self.min_q1 = 440
            self.max_q1 = 450

            ## For debugging
            ##self.max_q1 = 440.18
            ##par.q1_window = 0.009 / 2.0 
            ##par.q3_window = 2.0 / 2.0
            ##par.isotopes_up_to = 1

            import Residues
            R = Residues.Residues('mono')
            isotope_correction = par.isotopes_up_to * R.mass_diffC13 / min(par.parent_charges)

            start = time.time()
            query =  """
            select modified_sequence, transition_group, parent_id, q1_charge, q1, ssrcalc, isotope_nr, 0, 0
            from %s where q1 between %s and %s
            #and isotope_nr = 0
                           """ % (par.peptide_tables[0], self.min_q1 - par.q1_window, self.max_q1 + par.q1_window) 
            cursor.execute(query)
            self.alltuples =  list(cursor.fetchall() )
            #print "len alltuples", len(self.alltuples)

            query =  """
            select modified_sequence, transition_group, parent_id, q1_charge, q1, ssrcalc, isotope_nr, 0, 0
            from %s where q1 between %s - %s and %s
            and isotope_nr = 0
                           """ % (par.peptide_tables[0], self.min_q1 - par.q1_window, isotope_correction, self.max_q1 + par.q1_window) 
            cursor.execute(query)
            self.alltuples_isotope_correction =  list(cursor.fetchall())
            #print "len alltuples zero", len(self.alltuples_isotope_correction)

            self.myprecursors = Precursors()

            # myprecursors.getFromDB(par, db.cursor(), self.min_q1 - par.q1_window, self.max_q1 + par.q1_window)
            ##### LEGACY getFromDB -- have isotope_nr = 0 in there!

            # Get all precursors from the DB within a window of Q1
            lower_q1 = self.min_q1 - par.q1_window
            upper_q1 =  self.max_q1 + par.q1_window
            self.myprecursors.precursors = []
            isotope_correction = par.isotopes_up_to * R.mass_diffC13 / min(par.parent_charges)
            q =  """
            select modified_sequence, transition_group, parent_id, q1_charge, q1, ssrcalc, modifications, missed_cleavages, isotopically_modified
            from %(peptide_table)s where q1 between %(lowq1)s - %(isotope_correction)s and %(highq1)s
            and isotope_nr = 0
            """ % {'peptide_table' : par.peptide_tables[0],
                          'lowq1'  : lower_q1,  # min_q1 - par.q1_window
                          'highq1' : upper_q1, # max_q1 + par.q1_window,
                          'isotope_correction' : isotope_correction
                  } 
            cursor.execute(q)
            for res in cursor.fetchall():
              p = Precursor()
              p.initialize(*res)
              # Only include those precursors that are actually have isotopes in the specified range
              if(p.included_in_isotopic_range(lower_q1, upper_q1, par) ): 
                self.myprecursors.precursors.append(p)

            ##### END LEGACY getFromDB

            self.precursors_to_evaluate = self.myprecursors.getPrecursorsToEvaluate(self.min_q1, self.max_q1)

        except Exception as inst:
            print "something went wrong"
            print inst
Beispiel #5
0
def do_analysis(input_sequences, seqs, par, wuis, local_cursor, controller):
    """
    ###########################################################################
    # Do analysis
    # 1. Find SSRCalc values for all peptides
    # 2. Iterate through all sequences and calculate the b / y series
    # 3. Find all (potentially) interfering precursors
    # 4. For each precursors, find the list of transitions that interfers
    # 5. For each transition, find the precursors that interfere 
    # 6. Print information
    ###########################################################################
    """

    q3_low, q3_high = par.q3_range
    uis = par.uis
    pepmap = get_ssrcalc_values(seqs, input_sequences, default_ssrcalc, local_cursor, ssrcalc_path)
    toggle_all_str = '<script language="javascript"> function toggleAll(){ '
    mycollider = collider.SRMcollider()

    for seq_id, peptide in enumerate(controller.peptides):
        #
        # Step 2 : find the SSRCalc values for this sequence
        #
        try: ssrcalc = pepmap[filter(str.isalpha, peptide.sequence)]
        except KeyError: ssrcalc = 25
        transitions = [ (f.q3, f.fragment_count) for f in peptide.fragments]
        if len( transitions ) == 0: continue # no transitions in this window

        #
        # Step 3 : find all potentially interfering precursors
        #  Create precursor and use db to find all interfering precursors
        #
        precursor = Precursor(modified_sequence = peptide.get_modified_sequence(), parent_id = -1,
            q1 = peptide.charged_mass, q1_charge = 2, ssrcalc = ssrcalc, transition_group = -1)
        precursor.seq_id = seq_id
        precursors_obj = mycollider._get_all_precursors(par, precursor, local_cursor)

        # 
        # Step 4 and 5: Find interferences per precursor, then find
        # interferences per transition (see the two readouts in the html)
        #
        collisions_per_peptide = \
        c_getnonuis.calculate_collisions_per_peptide_other_ion_series(
            tuple(transitions), precursors_obj, par, q3_low, q3_high,
            par.q3_window, par.ppm, par.chargeCheck) 

        nonunique = c_getnonuis._find_clashes_forall_other_series( 
            tuple(transitions), precursors_obj, par, q3_low, q3_high,
            par.q3_window, par.ppm, peptide.charged_mass - par.q1_window, par.chargeCheck)

        # also add those that have no interference
        for fragment in peptide.fragments: 
            if not fragment.fragment_count in nonunique:
                nonunique[fragment.fragment_count] = []
        nonunique_obj = controller.getNonuniqueObjects(nonunique)

        # 
        # Step 6: printing
        #
        do_all_print(peptide, collisions_per_peptide, 
                 wuis, precursor, par, precursors_obj, nonunique_obj)
        toggle_all_str += "toggleDisplay('col_peptides_%s'); toggleDisplay('col_transitions_%s');\n" % (seq_id,seq_id)

    toggle_all_str += "};</script>"
    print toggle_all_str
    print """
Beispiel #6
0
    def test_mysql_vs_integrated(self):
        """Compare the one table MySQL approach vs the fully integrated Cpp approach
            
            Here we are comparing the old (querying the transitions database as
            well as the precursor database) and the new way (only query the
            precursor database and calculate the transitions on the fly) way of
            calculating the collisions.
            """

        print '\n' * 1
        print "Comparing one table MySQL solution vs integrated solution"
        par = self.par
        cursor = self.cursor

        mypepids = [
            {
                'mod_sequence': r[0],
                'peptide_key': r[1],
                'transition_group': r[1],
                'parent_id': r[2],
                'q1_charge': r[3],
                'q1': r[4],
                'ssrcalc': r[5],
            } for r in self.alltuples if r[3] == 2  #charge is 2
            and r[6] == 0  #isotope is 0
            and r[4] >= self.min_q1 and r[4] < self.max_q1
        ]

        mycollider = collider.SRMcollider()
        #mypepids = _get_unique_pepids(par, cursor)
        self.mycollider.pepids = mypepids
        self.mycollider.calcinner = 0
        shuffle(self.mycollider.pepids)
        self.mycollider.pepids = self.mycollider.pepids[:self.limit]

        import c_rangetree
        r = c_rangetree.ExtendedRangetree_Q1_RT.create()
        r.new_rangetree()
        r.create_tree(tuple(self.alltuples_isotope_correction))
        #c_integrated.create_tree(tuple(self.alltuples_isotope_correction))

        MAX_UIS = 5
        c_newuistime = 0
        oldtime = 0
        c_fromprecursortime = 0
        oldsql = 0
        newsql = 0
        newtime = 0
        oldcalctime = 0
        localsql = 0
        self._cursor = False
        print "i\toldtime\t\tnewtime\t>>\tspeedup"
        for kk, pep in enumerate(self.mycollider.pepids):
            ii = kk + 1
            p_id = pep['parent_id']
            q1 = pep['q1']
            q3_low, q3_high = par.get_q3range_transitions()
            q1_low = q1 - par.q1_window
            q1_high = q1 + par.q1_window
            ssrcalc = pep['ssrcalc']
            peptide_key = pep['peptide_key']

            #correct rounding errors, s.t. we get the same results as before!
            ssrcalc_low = ssrcalc - par.ssrcalc_window + 0.001
            ssrcalc_high = ssrcalc + par.ssrcalc_window - 0.001
            isotope_correction = par.isotopes_up_to * Residues.mass_diffC13 / min(
                par.parent_charges)

            precursor = Precursor(q1=pep['q1'],
                                  transition_group=pep['transition_group'],
                                  parent_id=pep['parent_id'],
                                  modified_sequence=pep['mod_sequence'],
                                  ssrcalc=pep['ssrcalc'])

            transitions = collider.calculate_transitions_ch(
                ((q1, pep['mod_sequence'], p_id), ), [1], q3_low, q3_high)
            #fake some srm_id for the transitions
            transitions = tuple([(t[0], i) for i, t in enumerate(transitions)])
            ##### transitions = self.mycollider._get_all_transitions(par, pep, cursor)
            nr_transitions = len(transitions)
            if nr_transitions == 0: continue  #no transitions in this window

            ###################################
            # Old way to calculate non_uislist
            #  - get all precursors from the SQL database
            #  - calculate collisions per peptide in C++

            par.query2_add = ' and isotope_nr = 0 '  # due to the new handling of isotopes
            mystart = time.time()
            self.mycollider.mysqlnewtime = 0
            precursors = self.mycollider._get_all_precursors(
                par, precursor, cursor)
            newsql += time.time() - mystart

            mystart = time.time()
            q3_low, q3_high = par.get_q3range_transitions()
            collisions_per_peptide = c_getnonuis.calculate_collisions_per_peptide_other_ion_series(
                transitions, precursors, par, q3_low, q3_high, par.q3_window,
                par.ppm, False)
            non_uis_list = [{} for i in range(MAX_UIS + 1)]
            for order in range(1, MAX_UIS + 1):
                non_uis_list[order] = c_getnonuis.get_non_uis(
                    collisions_per_peptide, order)
            c_fromprecursortime += time.time() - mystart

            newl = [len(n) for n in non_uis_list]
            non_uis_list_old_way = [set(kk.keys()) for kk in non_uis_list]
            non_uis_list_len = [len(kk) for kk in non_uis_list_old_way[1:]]

            ###################################
            # New way to calculate non_uislist
            #  - start out from transitions, get non_uislist
            mystart = time.time()
            newresult = c_integrated.wrap_all_bitwise(
                transitions,
                q1_low,
                ssrcalc_low,
                q1_high,
                ssrcalc_high,
                peptide_key,
                min(MAX_UIS, nr_transitions),
                par.q3_window,  #q3_low, q3_high,
                par.ppm,
                par.isotopes_up_to,
                isotope_correction,
                par,
                r)
            newtime += time.time() - mystart

            ###################################
            # Assert equality, print out result
            self.assertEqual(newresult, non_uis_list_len)

            mys =  "%s\t%0.1fms\t\t%0.2fms\t>>>\t%0.1f" % \
             (ii,  #i
             (c_fromprecursortime + newsql)*1000/ii,  # oldtime
             (newtime)*1000/ii, # newtime
             (c_fromprecursortime + newsql) / (newtime), # speedup
            )

            self.ESC = chr(27)
            sys.stdout.write(self.ESC + '[2K')
            if self._cursor:
                sys.stdout.write(self.ESC + '[u')
            self._cursor = True
            sys.stdout.write(self.ESC + '[s')
            sys.stdout.write(mys)
            sys.stdout.flush()
Beispiel #7
0
    def test_two_table_mysql(self):
        """Compare the two table vs the one table MySQL approach
            
            Here we are comparing the old (querying the transitions database as
            well as the precursor database) and the new way (only query the
            precursor database and calculate the transitions on the fly) way of
            calculating the collisions.
            """

        print '\n' * 1
        print "Comparing one vs two table MySQL solution"
        par = self.par
        cursor = self.cursor

        mycollider = collider.SRMcollider()
        mypepids = _get_unique_pepids(par, cursor)
        self.mycollider.pepids = mypepids
        self.mycollider.calcinner = 0
        shuffle(self.mycollider.pepids)
        self.mycollider.pepids = self.mycollider.pepids[:self.limit]

        MAX_UIS = 5
        c_newuistime = 0
        oldtime = 0
        c_fromprecursortime = 0
        oldsql = 0
        newsql = 0
        oldcalctime = 0
        localsql = 0
        self._cursor = False
        print "oldtime = get UIS from collisions and transitions (getting all collisions from the transitions db)"
        print "cuis + oldsql = as oldtime but calculate UIS in C++"
        print "py+newsql = only get the precursors from the db and calculate collisions in Python"
        print "ctime + newsql = only get the precursors from the db and calculate collisions in C++"
        print "new = use fast SQL and C++ code"
        print "old = use slow SQL and Python code"
        print "i\toldtime\tcuis+oldsql\tpy+newsql\tctime+newsql\t>>>\toldsql\tnewsql\t...\t...\tspeedup"
        for kk, pep in enumerate(self.mycollider.pepids):
            ii = kk + 1
            p_id = pep['parent_id']
            q1 = pep['q1']
            q3_low, q3_high = par.get_q3range_transitions()
            precursor = Precursor(q1=pep['q1'],
                                  transition_group=pep['transition_group'],
                                  parent_id=pep['parent_id'],
                                  modified_sequence=pep['mod_sequence'],
                                  ssrcalc=pep['ssrcalc'])
            transitions = collider.calculate_transitions_ch(
                ((q1, pep['mod_sequence'], p_id), ), [1], q3_low, q3_high)
            #fake some srm_id for the transitions
            transitions = tuple([(t[0], i) for i, t in enumerate(transitions)])
            ##### transitions = self.mycollider._get_all_transitions(par, pep, cursor)
            nr_transitions = len(transitions)
            if nr_transitions == 0: continue  #no transitions in this window
            #
            mystart = time.time()
            collisions = list(
                self.mycollider._get_all_collisions_calculate_new(
                    par, precursor, cursor))
            oldcolllen = len(collisions)
            oldcalctime += time.time() - mystart
            #
            mystart = time.time()
            collisions = _get_all_collisions(mycollider,
                                             par,
                                             pep,
                                             cursor,
                                             transitions=transitions)
            oldcsqllen = len(collisions)
            oldsql += time.time() - mystart
            #
            par.query2_add = ' and isotope_nr = 0 '  # due to the new handling of isotopes
            mystart = time.time()
            self.mycollider.mysqlnewtime = 0
            precursors = self.mycollider._get_all_precursors(
                par, precursor, cursor)
            newsql += time.time() - mystart
            #
            mystart = time.time()
            #precursors = self.mycollider._get_all_precursors(par, pep, cursor_l)
            localsql += time.time() - mystart
            par.query2_add = ''  # due to the new handling of isotopes
            #
            mystart = time.time()
            non_uis_list = get_non_UIS_from_transitions(
                transitions, tuple(collisions), par, MAX_UIS)
            cnewuis = non_uis_list
            c_newuistime += time.time() - mystart
            #
            mystart = time.time()
            non_uis_list = get_non_UIS_from_transitions_old(
                transitions, collisions, par, MAX_UIS)
            oldnonuislist = non_uis_list
            oldtime += time.time() - mystart
            #
            mystart = time.time()
            q3_low, q3_high = par.get_q3range_transitions()
            collisions_per_peptide = c_getnonuis.calculate_collisions_per_peptide_other_ion_series(
                transitions, precursors, par, q3_low, q3_high, par.q3_window,
                par.ppm, False)
            non_uis_list = [{} for i in range(MAX_UIS + 1)]
            for order in range(1, MAX_UIS + 1):
                non_uis_list[order] = c_getnonuis.get_non_uis(
                    collisions_per_peptide, order)
            c_fromprecursortime += time.time() - mystart

            newl = [len(n) for n in non_uis_list]
            self.assertEqual(newl, [len(o) for o in cnewuis])
            self.assertEqual(newl, [len(o) for o in oldnonuislist])

            non_uis_list = [set(k.keys()) for k in non_uis_list]
            cnewuis = [set(k.keys()) for k in cnewuis]

            self.assertEqual(non_uis_list, cnewuis)
            self.assertEqual(non_uis_list, oldnonuislist)

            mys =  "%s\t%0.fms\t%0.fms\t\t%0.fms\t\t%0.2fms\t\t>>>\t%0.fms\t%0.2fms\t...\t...\t%0.2f" % \
             (ii,  #i
             (oldtime + oldsql)*1000/ii,  #oldtime
             (c_newuistime+oldsql)*1000/ii, #cuis + oldsql
             (oldcalctime + newsql + oldtime)*1000/ii,  #newsql
             (c_fromprecursortime + newsql)*1000/ii,  #ctime+newsql
             #(c_fromprecursortime + localsql)*1000/ii,

             oldsql*1000/ii, #newsql
             newsql*1000/ii, #oldsql
             #localsql*1000/ii,
             #oldtime / c_newuistime
             (oldtime + oldsql) / (c_fromprecursortime + newsql)
            )

            self.ESC = chr(27)
            sys.stdout.write(self.ESC + '[2K')
            if self._cursor:
                sys.stdout.write(self.ESC + '[u')
            self._cursor = True
            sys.stdout.write(self.ESC + '[s')
            sys.stdout.write(mys)
            sys.stdout.flush()
Beispiel #8
0
    def setUp(self):
        self.limit = 100
        self.limit_large = 100
        self.limit = 300
        self.limit_large = 600
        try:
            import MySQLdb
            #db_l = MySQLdb.connect(read_default_file="~/.my.cnf.local")
            db = MySQLdb.connect(read_default_file="~/.srm.cnf")
            cursor = db.cursor()
            self.cursor = cursor

            par = test_shared.get_default_setup_parameters()
            par.use_sqlite = True
            par.q1_window = 1.2 / 2.0
            par.q3_window = 2.0 / 2.0
            par.ssrcalc_window = 4 / 2.0
            par.ssrcalc_window = 9999 / 2.0
            par.peptide_tables = ['hroest.srmPeptides_yeast']
            par.transition_table = 'hroest.srmTransitions_yeast'
            par.isotopes_up_to = 3
            self.mycollider = collider.SRMcollider()
            par.select_by = "id"

            self.par = par
            self.min_q1 = 440
            self.max_q1 = 450

            ## For debugging
            ##self.max_q1 = 440.18
            ##par.q1_window = 0.009 / 2.0
            ##par.q3_window = 2.0 / 2.0
            ##par.isotopes_up_to = 1

            import Residues
            R = Residues.Residues('mono')
            isotope_correction = par.isotopes_up_to * R.mass_diffC13 / min(
                par.parent_charges)

            start = time.time()
            query = """
            select modified_sequence, transition_group, parent_id, q1_charge, q1, ssrcalc, isotope_nr, 0, 0
            from %s where q1 between %s and %s
            #and isotope_nr = 0
                           """ % (par.peptide_tables[0], self.min_q1 -
                                  par.q1_window, self.max_q1 + par.q1_window)
            cursor.execute(query)
            self.alltuples = list(cursor.fetchall())
            #print "len alltuples", len(self.alltuples)

            query = """
            select modified_sequence, transition_group, parent_id, q1_charge, q1, ssrcalc, isotope_nr, 0, 0
            from %s where q1 between %s - %s and %s
            and isotope_nr = 0
                           """ % (par.peptide_tables[0], self.min_q1 -
                                  par.q1_window, isotope_correction,
                                  self.max_q1 + par.q1_window)
            cursor.execute(query)
            self.alltuples_isotope_correction = list(cursor.fetchall())
            #print "len alltuples zero", len(self.alltuples_isotope_correction)

            self.myprecursors = Precursors()

            # myprecursors.getFromDB(par, db.cursor(), self.min_q1 - par.q1_window, self.max_q1 + par.q1_window)
            ##### LEGACY getFromDB -- have isotope_nr = 0 in there!

            # Get all precursors from the DB within a window of Q1
            lower_q1 = self.min_q1 - par.q1_window
            upper_q1 = self.max_q1 + par.q1_window
            self.myprecursors.precursors = []
            isotope_correction = par.isotopes_up_to * R.mass_diffC13 / min(
                par.parent_charges)
            q = """
            select modified_sequence, transition_group, parent_id, q1_charge, q1, ssrcalc, modifications, missed_cleavages, isotopically_modified
            from %(peptide_table)s where q1 between %(lowq1)s - %(isotope_correction)s and %(highq1)s
            and isotope_nr = 0
            """ % {
                'peptide_table': par.peptide_tables[0],
                'lowq1': lower_q1,  # min_q1 - par.q1_window
                'highq1': upper_q1,  # max_q1 + par.q1_window,
                'isotope_correction': isotope_correction
            }
            cursor.execute(q)
            for res in cursor.fetchall():
                p = Precursor()
                p.initialize(*res)
                # Only include those precursors that are actually have isotopes in the specified range
                if (p.included_in_isotopic_range(lower_q1, upper_q1, par)):
                    self.myprecursors.precursors.append(p)

            ##### END LEGACY getFromDB

            self.precursors_to_evaluate = self.myprecursors.getPrecursorsToEvaluate(
                self.min_q1, self.max_q1)

        except Exception as inst:
            print "something went wrong"
            print inst