11using Chemistry ;
22using MassSpectrometry ;
33using MzLibUtil ;
4- using Proteomics . AminoAcidPolymer ;
54using System ;
6- using System . Collections . Concurrent ;
75using System . Collections . Generic ;
86using System . Linq ;
97using System . Threading . Tasks ;
@@ -76,13 +74,6 @@ protected override MetaMorpheusEngineResults RunSpecific()
7674
7775 var representativeSinglePeptide = identification . BestMatchingBioPolymersWithSetMods . First ( ) . SpecificBioPolymer ;
7876
79- // Get the peptide, don't forget to add the modifications!!!!
80- var SequenceWithChemicalFormulas = representativeSinglePeptide . SequenceWithChemicalFormulas ;
81- if ( SequenceWithChemicalFormulas == null || representativeSinglePeptide . AllModsOneIsNterminus . Any ( b => b . Value . NeutralLosses != null ) )
82- continue ;
83-
84- Peptide coolPeptide = new Peptide ( SequenceWithChemicalFormulas ) ;
85-
8677 List < LabeledDataPoint > ms2tuple = SearchMS2Spectrum ( GoodScans [ matchIndex ] , identification , ProductMassTolerance ) ;
8778
8879 lock ( _ms2Lock )
@@ -91,7 +82,7 @@ protected override MetaMorpheusEngineResults RunSpecific()
9182 }
9283
9384 // Calculate theoretical isotopic distribution of the full peptide
94- var dist = IsotopicDistribution . GetDistribution ( coolPeptide . GetChemicalFormula ( ) , FineResolutionForIsotopeDistCalculation , 0.001 ) ;
85+ var dist = IsotopicDistribution . GetDistribution ( representativeSinglePeptide . ThisChemicalFormula , FineResolutionForIsotopeDistCalculation , 0.001 ) ;
9586
9687 double [ ] theoreticalMasses = dist . Masses . ToArray ( ) ;
9788 double [ ] theoreticalIntensities = dist . Intensities . ToArray ( ) ;
@@ -137,6 +128,7 @@ protected override MetaMorpheusEngineResults RunSpecific()
137128 theIndex = direction == 1 ? ms2spectrumIndex + 1 : identification . PrecursorScanNumber ?? ms2spectrumIndex ;
138129
139130 bool addedAscan = true ;
131+ bool positiveMode = identification . ScanPrecursorCharge > 0 ;
140132
141133 int highestKnownChargeForThisPeptide = peptideCharge ;
142134 while ( theIndex >= 1 && theIndex <= MyMsDataFile . NumSpectra && addedAscan ) //as long as we're finding the peptide in ms1 scans
@@ -157,16 +149,26 @@ protected override MetaMorpheusEngineResults RunSpecific()
157149 break ;
158150
159151 bool startingToAddCharges = false ;
160- int chargeToLookAt = 1 ;
152+ int chargeToLookAt = positiveMode ? 1 : - 1 ;
153+ int chargeLimit = positiveMode ? highestKnownChargeForThisPeptide + 1 : highestKnownChargeForThisPeptide - 1 ;
154+
161155 do
162156 {
163- if ( theoreticalMasses [ 0 ] . ToMz ( chargeToLookAt ) > scanWindowRange . Maximum )
157+ double mz = theoreticalMasses [ 0 ] . ToMz ( chargeToLookAt ) ;
158+
159+ // If m/z is above the scan window, try next charge
160+ if ( mz > scanWindowRange . Maximum )
164161 {
165- chargeToLookAt ++ ;
162+ chargeToLookAt += positiveMode ? 1 : - 1 ;
166163 continue ;
167164 }
168- if ( theoreticalMasses [ 0 ] . ToMz ( chargeToLookAt ) < scanWindowRange . Minimum )
165+
166+ // If m/z is below the scan window, break early, more charge will only make the mz smaller.
167+ if ( mz < scanWindowRange . Minimum )
168+ {
169169 break ;
170+ }
171+
170172 var trainingPointsToAverage = new List < LabeledDataPoint > ( ) ;
171173 foreach ( double a in theoreticalMasses )
172174 {
@@ -187,15 +189,17 @@ protected override MetaMorpheusEngineResults RunSpecific()
187189 var closestPeakIndex = fullMS1spectrum . GetClosestPeakIndex ( theMZ ) ;
188190 var closestPeakMZ = fullMS1spectrum . XArray [ closestPeakIndex ] ;
189191
190- highestKnownChargeForThisPeptide = Math . Max ( highestKnownChargeForThisPeptide , chargeToLookAt ) ;
192+ highestKnownChargeForThisPeptide = positiveMode
193+ ? Math . Max ( highestKnownChargeForThisPeptide , chargeToLookAt )
194+ : Math . Min ( highestKnownChargeForThisPeptide , chargeToLookAt ) ;
191195 trainingPointsToAverage . Add ( new LabeledDataPoint ( closestPeakMZ , - 1 , double . NaN , double . NaN , Math . Log ( fullMS1spectrum . YArray [ closestPeakIndex ] ) , theMZ , null ) ) ;
192196 }
193197 // If started adding and suddenly stopped, go to next one, no need to look at higher charges
194198 if ( trainingPointsToAverage . Count == 0 && startingToAddCharges )
195199 {
196200 break ;
197201 }
198- if ( ( trainingPointsToAverage . Count == 0 || ( trainingPointsToAverage . Count == 1 && theoreticalIntensities [ 0 ] < 0.65 ) ) && ( peptideCharge <= chargeToLookAt ) )
202+ if ( ( trainingPointsToAverage . Count == 0 || ( trainingPointsToAverage . Count == 1 && theoreticalIntensities [ 0 ] < 0.65 ) ) && ( positiveMode ? peptideCharge <= chargeToLookAt : peptideCharge > = chargeToLookAt ) )
199203 {
200204 break ;
201205 }
@@ -213,8 +217,8 @@ protected override MetaMorpheusEngineResults RunSpecific()
213217 trainingPointsToAverage . Select ( b => b . TheoreticalMz ) . Average ( ) ,
214218 identification ) ) ;
215219 }
216- chargeToLookAt ++ ;
217- } while ( chargeToLookAt <= highestKnownChargeForThisPeptide + 1 ) ;
220+ chargeToLookAt += positiveMode ? 1 : - 1 ;
221+ } while ( positiveMode ? chargeToLookAt <= chargeLimit : chargeToLookAt >= chargeLimit ) ;
218222 theIndex += direction ;
219223 }
220224 return ( result , numMs1MassChargeCombinationsConsidered , numMs1MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks ) ;
@@ -240,16 +244,17 @@ private static List<LabeledDataPoint> SearchMS2Spectrum(Ms2ScanWithSpecificMass
240244 if ( envelopesThatMatch . Count == 0 )
241245 continue ;
242246 //only allow one envelope per charge state
243- bool [ ] chargeStateFound = new bool [ envelopesThatMatch . Max ( x => x . Charge ) + 1 ] ;
247+ bool [ ] chargeStateFound = new bool [ envelopesThatMatch . Max ( x => Math . Abs ( x . Charge ) ) + 1 ] ;
244248
245249 foreach ( var envelopeThatMatched in envelopesThatMatch )
246250 {
251+ int charge = Math . Abs ( envelopeThatMatched . Charge ) ;
247252 //if we haven't seen this charge state already
248- if ( ! chargeStateFound [ envelopeThatMatched . Charge ] )
253+ if ( ! chargeStateFound [ charge ] )
249254 {
250- chargeStateFound [ envelopeThatMatched . Charge ] = true ;
255+ chargeStateFound [ charge ] = true ;
251256
252- double exptPeakMz = envelopeThatMatched . MonoisotopicMass . ToMz ( envelopeThatMatched . Charge ) ;
257+ double exptPeakMz = envelopeThatMatched . MonoisotopicMass . ToMz ( charge ) ;
253258 double exptPeakIntensity = envelopeThatMatched . TotalIntensity ;
254259 double injTime = ms2DataScan . TheScan . InjectionTime ?? double . NaN ;
255260
@@ -259,8 +264,8 @@ private static List<LabeledDataPoint> SearchMS2Spectrum(Ms2ScanWithSpecificMass
259264 ms2DataScan . OneBasedScanNumber ,
260265 Math . Log ( ms2DataScan . TotalIonCurrent ) ,
261266 Math . Log ( injTime ) ,
262- Math . Log ( exptPeakIntensity ) ,
263- matchedIon . NeutralTheoreticalProduct . NeutralMass . ToMz ( envelopeThatMatched . Charge ) ,
267+ Math . Log ( exptPeakIntensity ) ,
268+ matchedIon . NeutralTheoreticalProduct . NeutralMass . ToMz ( charge ) ,
264269 identification ) ) ;
265270 }
266271 }
0 commit comments