Пример #1
0
def testGetPrefixPeaks():
    ion1 = Peak(1, 274.112, 40.1)  #
    ion2 = Peak(1, 361.121, 80.1)
    spec = Spectrum()
    spec.addPeak(ion1)
    spec.addPeak(ion2)

    seq = 'SWR'
    acids = []
    acids += [AminoAcid(s, '', aa_table[s]) for s in seq]
    peptide = Peptide(acids)
    spec.setAnnotation(peptide)

    pg = PeakGenerator(spec)
    ppeaks = pg.getTheoreticalPrefixPeaks(True, 1)
    print 'theoretical prefix peaks are: ---------'
    for peak in ppeaks:
        print peak.getMz(), ':', peak.getPosition()
    print 'theoretical suffix peaks are:----------'
    speaks = pg.getTheoreticalPrefixPeaks(False, 1)
    for peak in speaks:
        print peak.getMz(), ':', peak.getPosition()
    print 'theoretical precursor peak is : -------'
    rpeak = pg.getTheoreticalPrecursorPeak(1)
    print rpeak.getMz(), ':', rpeak.getPosition()

    compared_peaks = []
    compared_peaks += [
        peak for peak in spec.getPeaks() if peak.getIntensity() > 0
    ]
    print 'compared peaks are: ---------'
    for peak in compared_peaks:
        print peak
Пример #2
0
	def matches( self, protein, random_samples=None, frequency_matcher=None ):
		if len(self)==0: return
		freq=list(self[0].freq)
		for w,d in zip(self.freq,self.dims):
			freq[d]=w
		peak=Peak(freq, self[0].rule)
		for match in peak.matches( protein, random_samples, frequency_matcher, match_mask=self.mask ):
			yield StripMatch( match.peak_match, self )
Пример #3
0
def getComplementaryPeak(peak, charge, pm_or_spec):
    '''gets the complementary peak from a parent mass or a spectrum.'''
    if isinstance(pm_or_spec, float):
        return Peak(
            charge,
            (pm_or_spec / charge - peak.getMz() + 2 * Composition.PROTON),
            virtual_peak_intensity)
    elif isinstance(pm_or_spec, Spectrum):
        return Peak(charge, (float(pm_or_spec.getParentMass()) / charge -
                             peak.getMz() + 2 * Composition.PROTON),
                    virtual_peak_intensity)
Пример #4
0
		def crosspeak_to_peak(cp, rule, filter,list_name):
			import chemical
			freq=tuple([cp._freq[i] for i in filter])
			peak=Peak( freq, rule, cp._id,list_name )
			if cp._assignments:
				peak.hard_assignments=[]
				for assignment in cp._assignments:
					new_ass=[]
					for i in filter:
						a=assignment._atoms[i]
						new_ass.append(chemical.Atom(a._name,a._resid) )
					peak.hard_assignments.append(tuple(new_ass))
			return peak
Пример #5
0
    def find_peaks(self):
        new_peaks = find_positive_peaks(self.datalist,
                                        threshold=float(
                                            self.thresholdValueBox.text()),
                                        min_dist=self.minDistSlider.value())

        for peak in self.peaks:
            if not peak.manual:
                try:
                    peak.scatter_point.remove()
                except (AttributeError, ValueError):
                    pass

        manual_peaks = [p for p in self.peaks if p.manual]
        self.peaks = [p for p in self.peaks if p.manual]
        for i, row in enumerate(new_peaks):
            parameter = self.parameters[i]
            for peak_position in row:
                for p in manual_peaks:
                    if parameter == p.parameter:
                        if abs(p.position - peak_position) < int(
                                self.minDistValueBox.text()):
                            break
                else:
                    self.peaks.append(
                        Peak(peak_position, INITIAL_PEAK_AREA,
                             INITIAL_PEAK_FWHM, parameter))
        self.plot_peak_positions()
Пример #6
0
def extrema_to_peaks(minimaE, maximaE, minimaI, maximaI):
    """
	INPUT:
		minimaE -- A 2d numpy array. Each row is an instance (currently, that means a single snapshot). Each column is the energy value of a relative minima for that instance
		maximaE, minimaI, maximaI -- Same format. I indicates relative intensity maxima or minima
	OUTPUT:
		peaks -- A 2d numpy array. Each row is an instance. Each column is a Peak instance. Peaks are defined by the energy, intensity values at the maxima and the energies of the left and right minimum
	PURPOSE:
		Converting extrema of energies and intensities into peaks for the peak_tracking(*params) method to use
	IMPORTANT POINTS:
		I am assuming that the first and last extrema are minima. If not, indexing the minima based on j will be off.
		Yeah, indexing is causing problems. Especially with the min_row_E[j+1] snippet
	"""
    peaks = []
    for i in range(len(maximaE)):
        max_row_E = maximaE[i]
        min_row_E = minimaE[i]
        max_row_I = maximaI[i]
        row_peaks = []
        for j in range(len(max_row_E)):
            curr_max_E = max_row_E[j]
            curr_max_I = max_row_I[j]
            left_min_E, right_min_E = find_left_right(minimaE[i], curr_max_E)
            row_peaks.append(
                Peak(curr_max_E, curr_max_I, left_min_E, right_min_E))
        peaks.append(row_peaks)
    return peaks
Пример #7
0
 def getTheoreticalPrefixPeaks(self, is_prefix, charge, offset=0):
     ''' returns theoretical prefix peaks.
       args: is_prefix: Ture if prefix otherwise suffix.'''
     peaks = []
     if self.getPeptide() == None:
         return None
     masses = self.getPeptide().getPrefixMasses(is_prefix)
     for m, pos in masses.items():
         peaks.append(
             Peak(charge, (m + offset) / charge, virtual_peak_intensity,
                  pos))
     return peaks
Пример #8
0
 def onclick(self, event):
     if event.button == 1 and event.dblclick:
         parameter = self.parameters[np.abs(self.parameters -
                                            event.ydata).argmin()]
         peak = Peak(event.xdata,
                     INITIAL_PEAK_AREA,
                     INITIAL_PEAK_FWHM,
                     parameter,
                     manual=True)
         self.peaks.append(peak)
         self.plot_peak_positions()
         self.show_initial_parameters()
Пример #9
0
 def createPeaks(self):
     for i in range(0, len(self.mz)):
         mz = self.mz[i]
         intensity = self.intensity[i]
         rank = self.ranks[i]
         counterpartMass = self.MH - mz + 1
         counterpartIndex = util.find_nearest(counterpartMass, self.mz)
         counterpartDifference = abs(self.mz[counterpartIndex] - mz)
         if counterpartDifference < self.mzTolerance:
             hasCounterpart = True
         else:
             hasCounterpart = False
         self.peakList.append(Peak(mz, intensity, hasCounterpart, rank))
Пример #10
0
    def readSpectrum(self, input_file):
        '''parses a spectrum file with mgf format.
           created on Aug 31, 2015 by mht.'''
        #   spec = None
        #  title = None
        is_parser = False
        with open(input_file) as input_data:
            for line in input_data:
                if len(line) == 0: continue
                #                if line.startswith('MASS'): continue
                if line.startswith('BEGIN IONS'):
                    is_parser = True
                    spec = Spectrum()
                elif (is_parser):
                    if line.startswith('TITLE'):
                        title = line[line.index('=') + 1:].strip()
                        spec.setTitle(title)
                    elif line.startswith('SEQ'):
                        annotation = line[line.index('=') + 1:].strip()
                        if spec.getAnnotation() is None:
                            spec.setAnnotation(annotation)
                    elif line.startswith('PEPMASS'):
                        pep_str = line[line.index('=') + 1:].strip()
                        pep_str = pep_str.split(' ')
                        pre_mass = float(pep_str[0])
                    elif line.startswith('CHARGE'):
                        charge = line[line.index('=') +
                                      1:line.index('+')].strip()
                        pre_charge = int(charge)
                    elif line[0].isdigit():
                        mass, intensity = map(float, line.split(' '))
                        spec.addPeak(Peak(1, mass, intensity))

                    elif line.startswith('END IONS'):
                        assert (spec is not None)
                        spec.setPrecursor(
                            Peak(pre_charge, pre_mass * pre_charge, 1))
                        # spec.sortPeaks()
                        yield spec  # return spec, replace return
Пример #11
0
		def read_peak_match( fast_peaks, molecule, line, last_peak ):
			line=line.replace('SCORES:','|')
			if last_peak and not ':' in line:
				peak=last_peak
				match_str=line.split('|')[0]
			else:
				tags=line.split(':')
				read_peak=Peak.from_string(tags[0])
				peak=fast_peaks[read_peak]
				match_str=tags[1].split('|')[0]
			peak_match=tuple(read_matched_atoms( molecule, match_str ) )
			rule_match=peak.rule.translate_peak_match_to_full_match( peak_match, molecule )
			return PeakMatch( rule_match, peak_match, peak ), peak
Пример #12
0
 def release_add_peak(self, event):
     self.canvas.mpl_disconnect(self.peak_drawing_binding)
     fwhm = abs(self.add_peak_event.xdata - event.xdata)
     height = (event.ydata -
               self.add_peak_event.ydata) * self.Window.stackscale
     area = height * fwhm * 1.0645889
     if not ALLOW_NEGATIVE_PEAKS:
         area = abs(area)
     parameter = self.Window.parameters[np.abs(
         self.Window.parameters - self.add_peak_event.ydata).argmin()]
     peak = Peak(self.add_peak_event.xdata,
                 area,
                 fwhm,
                 parameter,
                 manual=True)
     self.Window.peaks.append(peak)
     self.draw_fit_line.remove()
     self.Window.plot_peak_positions()
     self.Window.show_initial_parameters()
     self.Window.canvas.draw()
    def find_peaks_valleys(self, map_valley={}):
        self.calculate_histogram()
        #detect peaks
        len_histogram = len(self.histogram)

        for i in range(1, len_histogram - 1):
            left_val = self.histogram[i - 1]
            centre_val = self.histogram[i]
            right_val = self.histogram[i + 1]
            #peak detection
            if centre_val >= left_val and centre_val >= right_val:
                # Try to get the largest peak in same region.
                if len(self.peaks) != 0 and i - self.peaks[
                        -1].position <= self.avg_height // 2 and centre_val >= self.peaks[
                            -1].value:
                    self.peaks[-1].position = i
                    self.peaks[-1].value = centre_val
                elif len(self.peaks) > 0 and i - self.peaks[
                        -1].position <= self.avg_height // 2 and centre_val < self.peaks[
                            -1].value:
                    abc = 0
                else:
                    self.peaks.append(Peak(position=i, value=centre_val))
        #
        peaks_average_values = 0
        new_peaks = []  # Peak type
        for p in self.peaks:
            peaks_average_values += p.value
        peaks_average_values //= max(1, int(len(self.peaks)))

        for p in self.peaks:
            if p.value >= peaks_average_values / 4:
                new_peaks.append(p)

        self.lines_count = int(len(new_peaks))

        self.peaks = new_peaks
        #sort peaks by max value and remove the outliers (the ones with less foreground pixels)
        self.peaks.sort(key=Peak().get_value)
        #resize self.peaks
        if self.lines_count + 1 <= len(self.peaks):
            self.peaks = self.peaks[:self.lines_count + 1]
        else:
            self.peaks = self.peaks[:len(self.peaks)]
        self.peaks.sort(key=Peak().get_row_position)

        #search for valleys between 2 peaks
        for i in range(1, len(self.peaks)):
            min_pos = (self.peaks[i - 1].position + self.peaks[i].position) / 2
            min_value = self.histogram[int(min_pos)]

            start = self.peaks[i - 1].position + self.avg_height / 2
            end = 0
            if i == len(self.peaks):
                end = self.thresh_img.shape[0]  #rows
            else:
                end = self.peaks[i].position - self.avg_height - 30

            for j in range(int(start), int(end)):
                valley_black_count = 0
                for l in range(self.thresh_img.shape[1]):  #cols
                    if self.thresh_img[j][l] == 0:
                        valley_black_count += 1

                if i == len(self.peaks) and valley_black_count <= min_value:
                    min_value = valley_black_count
                    min_pos = j
                    if min_value == 0:
                        min_pos = min(self.thresh_img.shape[0] - 10,
                                      min_pos + self.avg_height)
                        j = self.thresh_img.shape[0]
                elif min_value != 0 and valley_black_count <= min_value:
                    min_value = valley_black_count
                    min_pos = j

            new_valley = Valley(chunk_index=self.index, position=min_pos)
            self.valleys.append(new_valley)

            # map valley
            map_valley[new_valley.valley_id] = new_valley
        return int(math.ceil(self.avg_height))
Пример #14
0
        return sig_ions


def updateFeatures(feature_dict, features):
    if features == None: return
    for gof in features:
        if gof in feature_dict:
            freq = feature_dict[gof]
        else:
            freq = 0
        freq += 1
        feature_dict[gof] = freq


if __name__ == '__main__':
    ion1 = Peak(2, 274.112, 40.1)  #
    ion2 = Peak(2, 361.121, 80.1)
    spec = Spectrum()
    spec.addPeak(ion1)
    spec.addPeak(ion2)

    seq = 'SWR'
    acids = []
    for s in seq:
        acids.append(AminoAcid(s, '', aa_table[s]))
    pep = Peptide(acids)
    spec.setAnnotation(pep)
    specs = []
    specs.append(spec)
    ist = IonSelector(0.5, 10)
    sig_ions = ist.findSigIons(1, specs)
Пример #15
0
      args:
        feature_freqs- feature frequences, dict{RelationBetweenPeaks,int}
        threshold - a float threshold to select features
    '''
    
    num_peaks = 100 # max number of peaks considered
    fff_peaks = [] # FeatureFrequencyPeak list
    if feature_freqs is None or len(feature_freqs) == 0:
        return fff_peaks

    fff_peaks_tmp = []
    for feature in feature_freqs:
        numbers = (int)(feature_freqs[feature])
        fff_peaks_tmp.append(FeatureFrequencyPeak(feature,numbers))
    fff_peaks_tmp.reverse()
    
    for i in range(min(len(fff_peaks_tmp), num_peaks)):
        fff_peaks.append(fff_peaks_tmp[i])
    return fff_peaks

if __name__ == '__main__':
    peak = Peak(1,100.1, 50.1)
    peak1 = Peak(1,100.3,50.1)
    peak2 = Peak(1,110.2,80.1)
    peaks = [peak1,peak2]
    features =  getFeaturesBetweenPeaks(peak, peaks,False)
    for relation in features: print relation


    
Пример #16
0
    def getPeaksByMass(self, mass, tol=0.5):
        '''returns a list of peaks that match the target mass within the
        tolernace value. The absolute distance between mass and a returned peak
        is less or equal the tolerance value.'''
        return self.getPeaksByMassRange(mass - tol, mass + tol)

    def getPeaksByMassRange(self, min_mass, max_mass):
        '''returns a list of peaks that match the mass within the specified
        range. Assuming spectrum is sorted by mass.'''
        matches = []
        peaks = sorted(self.getPeaks())

        for peak in peaks:
            if peak.getMz() < min_mass: break
            if peak.getMz() > max_mass: break
            else:
                matches.append(peak)
        return matches


if __name__ == '__main__':
    p1 = Peak(1, 274.112, 40.1)
    p2 = Peak(1, 361.121, 80.1)
    peak = Peak(2, 448.225, 80.1)
    spec = Spectrum(peak)
    spec.addPeak(p1)
    print spec.getAnnotation()
    for peak in spec.getPeaks():
        print peak.getCharge()
Пример #17
0
 def __init__(self, precursor_peak=Peak(), title=''):
     self.peaks = []
     self.annotation = None  # peptide annotation
     self.precursor_peak = precursor_peak
     self.title = title
     self.peaks.append(precursor_peak)
Пример #18
0
 def getTheoreticalPrecursorPeak(self, charge, offset=0):
     if self.peptide == None: return None
     return Peak(charge, (self.peptide.getParentMass() + offset) / charge +
                 Composition.PROTON, virtual_peak_intensity,
                 len(self.peptide.getAcids()))
Пример #19
0
        ret_val = (total_delete_atom_cost,
                   split[1] + ["delete ATOM[{0}]".format(j)])
    #RETURN OPTIMAL COST AND INSTRUCTIONS
    return ret_val


if __name__ == '__main__':
    #THIS METHOD IS FOR TESTING

    #TEST 0
    #energies = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
    #intensities = np.array([[2, 1, 5, 10, 5, 8, 5, 1, 2, 3], [3, 2, 1, 5, 10, 5, 8, 5, 1, 2]])
    #minimaE, maximaE, minimaI, maximaI = get_extrema(energies, intensities)
    #peaks = extrema_to_peaks(minimaE, maximaE, minimaI, maximaI)

    #Test 1
    avg_peaks = [Peak(1, 10, 0, 2), Peak(5, 10, 4, 6)]
    atom_peaks = [Peak(1, 10, 0, 2), Peak(3, 10, 2, 4), Peak(5, 10, 4, 6)]

    #Test 2
    avg_peaks = [Peak(1, 10, 0, 2), Peak(3, 10, 2, 4)]
    atom_peaks = [Peak(1, 10, 0, 2), Peak(3, 10, 2, 4), Peak(5, 10, 4, 6)]

    #Test 3
    avg_peaks = [Peak(1, 10, 0, 2), Peak(4, 10, 3, 5)]
    atom_peaks = [Peak(1, 10, 0, 2), Peak(3, 10, 2, 4), Peak(5, 10, 4, 6)]

    peaks = np.array([avg_peaks, atom_peaks])
    edit_path = peak_tracking(peaks[0], peaks[1])
    print edit_path
Пример #20
0
import PeakGenerator
from Spectrum import Spectrum

class RelationBetweenPeaks(object):
    ''' represents the relation between two peaks considering m/z offset and
    complementary relation.
    This is used to define offsetFeature and for training.
      created on Sep 10, 2015 by mht.
    '''
    def __init__(self,base_charge,offset,is_complementary,pos=0):
        self.base_charge = base_charge
        self.offset = offset # m/z offset
        self.is_complementary = is_complementary
        self.pos = pos

    def __str__(self):
        return 'MzOff: %g Comp:%s ' % (self.offset, self.is_complementary)

    def getBaseCharge(self): return self.base_charge
    def getOffset(self): return self.offset
    def isComplementary(self): return self.is_complementary
    def getPosition(self): return self.pos

if __name__ == '__main__':
    relation = RelationBetweenPeaks(1,0.1,True)
    peak = Peak(1,87.1,1.2) # residule-S
    print peak.getMz()
    precursor_peak = Peak(1, 429.1,1.2)


Пример #21
0
    This is used to define offsetFeature and for training.
      created on Sep 10, 2015 by mht.
    '''
    def __init__(self, base_charge, offset, is_complementary, pos=0):
        self.base_charge = base_charge
        self.offset = offset  # m/z offset
        self.is_complementary = is_complementary
        self.pos = pos

    def __str__(self):
        return 'MzOff: %g Comp:%s ' % (self.offset, self.is_complementary)

    def getBaseCharge(self):
        return self.base_charge

    def getOffset(self):
        return self.offset

    def isComplementary(self):
        return self.is_complementary

    def getPosition(self):
        return self.pos


if __name__ == '__main__':
    relation = RelationBetweenPeaks(1, 0.1, True)
    peak = Peak(1, 87.1, 1.2)  # residule-S
    print peak.getMz()
    precursor_peak = Peak(1, 429.1, 1.2)