use of de.ipbhalle.metfraglib.match.DefaultFragmentToPeakMatch in project MetFragRelaunched by ipb-halle.
the class SimulateSpectrumHDX method generateDeuteratedSpectrumNegative.
/**
* @param candidate
* @return
* @throws AtomTypeNotKnownFromInputListException
* @throws Exception
*/
public static double[][] generateDeuteratedSpectrumNegative(ICandidate candidate, int method) throws AtomTypeNotKnownFromInputListException, Exception {
int numberPositions = searchForDeuteriumExchangeablePositions((TopDownBitArrayPrecursor) candidate.getPrecursorMolecule()).length;
HDTopDownBitArrayPrecursor preHDX = new HDTopDownBitArrayPrecursor(candidate.getImplicitHydrogenAtomContainer(), (byte) numberPositions);
preHDX.preprocessPrecursor();
MatchList matchList = candidate.getMatchList();
double[][] spectrumHDX = null;
if (method == 3)
spectrumHDX = new double[matchList.getNumberElements() * 2][2];
else
spectrumHDX = new double[matchList.getNumberElements()][2];
for (int i = 0; i < matchList.getNumberElements(); i++) {
IFragment frag = matchList.getElement(i).getBestMatchedFragment();
IPeak peak = matchList.getElement(i).getMatchedPeak();
int[] setAtoms = ((DefaultBitArrayFragment) frag).getAtomsFastBitArray().getSetIndeces();
int numberDeuteriums = 0;
for (int k = 0; k < setAtoms.length; k++) {
numberDeuteriums += preHDX.getNumberDeuteriumsConnectedToAtomIndex(0, setAtoms[k]);
}
spectrumHDX[i][1] = peak.getAbsoluteIntensity();
// [M-D]-
if (method == 1) {
spectrumHDX[i][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
spectrumHDX[i][0] += Constants.HYDROGEN_MASS - Constants.getMonoisotopicMassOfAtom("D");
} else // [M]-
if (method == 2) {
spectrumHDX[i][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
// nothing to do
} else // [M]- [M-D]-
if (method == 3) {
spectrumHDX[i * 2][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
spectrumHDX[(i * 2) + 1][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
spectrumHDX[(i * 2) + 1][0] += Constants.HYDROGEN_MASS - Constants.getMonoisotopicMassOfAtom("D");
} else // predict based on deuteriums
if (method == 4) {
spectrumHDX[i][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
if (numberDeuteriums != 0)
spectrumHDX[i][0] += Constants.HYDROGEN_MASS - Constants.getMonoisotopicMassOfAtom("D");
} else // predict based on adduct
if (method == 5) {
byte adductTypeIndex = ((DefaultFragmentToPeakMatch) matchList.getElement(i)).getBestMatchedFragmentAdductTypeIndex();
spectrumHDX[i][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
if (adductTypeIndex == 1)
spectrumHDX[i][0] += Constants.getMonoisotopicMassOfAtom("D") - Constants.HYDROGEN_MASS;
}
}
return spectrumHDX;
}
use of de.ipbhalle.metfraglib.match.DefaultFragmentToPeakMatch in project MetFragRelaunched by ipb-halle.
the class NewMergedPeakFragmenterScore method calculate.
public void calculate() {
this.matchList = (MatchList) settings.get(VariableNames.MATCH_LIST_NAME);
this.value = Double.valueOf(0);
this.bestFragmentIndeces = new int[this.matchList.getNumberElements()];
this.optimalValues = new double[this.matchList.getNumberElements()];
for (int i = 0; i < this.matchList.getNumberElements(); i++) {
DefaultFragmentToPeakMatch currentMatch = null;
boolean isMatchOfQuerySpectrum = true;
if (this.matchList.getElement(i) instanceof FragmentMassToPeakMatch)
currentMatch = (FragmentMassToPeakMatch) this.matchList.getElement(i);
else
currentMatch = (FragmentFormulaToPeakMatch) this.matchList.getElement(i);
double intensity = 0.0;
try {
intensity = currentMatch.getMatchedPeak().getIntensity();
} catch (RelativeIntensityNotDefinedException e) {
// if the match is not caused by a peak of the query spectrum
isMatchOfQuerySpectrum = false;
intensity = 0.0;
}
MergedTandemMassPeak matchedPeak = (MergedTandemMassPeak) currentMatch.getMatchedPeak();
FragmentList currentFragmentList = currentMatch.getMatchedFragmentList();
double minimumEnergyPerMatch = (double) Integer.MAX_VALUE;
/*
* get all intensities of random spectra of this peak
*/
java.util.ArrayList<Double> intensities = matchedPeak.getIntensities();
java.util.ArrayList<Integer> spectraIDs = matchedPeak.getSpectraIDs();
double peakScore = Math.pow((matchedPeak.getMass() / this.measuredPrecursorMass) * 10.0, this.ALPHA) * Math.pow(intensity, this.BETA);
double[] peakScores = new double[intensities.size()];
for (int k = 0; k < intensities.size(); k++) {
peakScores[k] = Math.pow((matchedPeak.getMass() / this.measuredPrecursorMass) * 10.0, this.ALPHA) * Math.pow(intensities.get(k), this.BETA);
}
for (int ii = 0; ii < currentFragmentList.getNumberElements(); ii++) {
IFragment currentFragment = currentFragmentList.getElement(ii);
/*
* check if current fragment is valid based on the tree depth
*/
// if(currentFragment.getTreeDepth() > this.maximumTreeDepth) continue;
int[] brokenBondIndeces = currentFragment.getBrokenBondIndeces();
double energyOfFragment = 0.0;
for (int bondIndex : brokenBondIndeces) {
energyOfFragment += this.bondEnergies[bondIndex];
}
energyOfFragment += Math.abs(currentMatch.getNumberOfHydrogensDifferToPeakMass(ii)) * this.WEIGHT_HYDROGEN_PENALTY;
/*
* assign optimal bondenergy and store best fragment
*/
if (energyOfFragment < minimumEnergyPerMatch) {
minimumEnergyPerMatch = energyOfFragment;
if (isMatchOfQuerySpectrum)
this.bestFragmentIndeces[i] = ii;
}
}
if (isMatchOfQuerySpectrum) {
currentMatch.initialiseBestMatchedFragment(this.bestFragmentIndeces[i]);
this.value += peakScore / Math.pow(minimumEnergyPerMatch, this.GAMMA);
this.optimalValues[i] = minimumEnergyPerMatch;
}
for (int k = 0; k < spectraIDs.size(); k++) {
this.scoresRandomSpectra[spectraIDs.get(k)] += peakScores[k] / Math.pow(minimumEnergyPerMatch, this.GAMMA);
}
}
int betterEqualScores = 0;
for (int i = 0; i < this.scoresRandomSpectra.length; i++) {
if (this.scoresRandomSpectra[i] >= this.value)
betterEqualScores++;
}
this.scoredCandidate.setProperty("p-value", (double) betterEqualScores / (double) this.scoresRandomSpectra.length);
this.calculationFinished = true;
}
use of de.ipbhalle.metfraglib.match.DefaultFragmentToPeakMatch in project MetFragRelaunched by ipb-halle.
the class SimulateSpectrumHDX method generateDeuteratedSpectrumPositive.
/**
* @param candidate
* @return
* @throws AtomTypeNotKnownFromInputListException
* @throws Exception
*/
public static double[][] generateDeuteratedSpectrumPositive(ICandidate candidate, int method) throws AtomTypeNotKnownFromInputListException, Exception {
int numberPositions = searchForDeuteriumExchangeablePositions((TopDownBitArrayPrecursor) candidate.getPrecursorMolecule()).length;
HDTopDownBitArrayPrecursor preHDX = new HDTopDownBitArrayPrecursor(candidate.getImplicitHydrogenAtomContainer(), (byte) numberPositions);
preHDX.preprocessPrecursor();
MatchList matchList = candidate.getMatchList();
double[][] spectrumHDX = null;
if (method == 3)
spectrumHDX = new double[matchList.getNumberElements() * 2][2];
else
spectrumHDX = new double[matchList.getNumberElements()][2];
for (int i = 0; i < matchList.getNumberElements(); i++) {
IFragment frag = matchList.getElement(i).getBestMatchedFragment();
IPeak peak = matchList.getElement(i).getMatchedPeak();
int[] setAtoms = ((DefaultBitArrayFragment) frag).getAtomsFastBitArray().getSetIndeces();
int numberDeuteriums = 0;
for (int k = 0; k < setAtoms.length; k++) {
numberDeuteriums += preHDX.getNumberDeuteriumsConnectedToAtomIndex(0, setAtoms[k]);
}
if (method != 3) {
spectrumHDX[i][1] = peak.getAbsoluteIntensity();
} else {
spectrumHDX[i * 2][1] = peak.getAbsoluteIntensity();
spectrumHDX[(i * 2) + 1][1] = peak.getAbsoluteIntensity();
}
// [M+D]+
if (method == 1) {
spectrumHDX[i][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
spectrumHDX[i][0] += Constants.getMonoisotopicMassOfAtom("D") - Constants.HYDROGEN_MASS;
} else // [M]+
if (method == 2) {
spectrumHDX[i][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
// nothing to do
} else // [M]+ [M+D]+
if (method == 3) {
spectrumHDX[i * 2][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
spectrumHDX[(i * 2) + 1][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
spectrumHDX[(i * 2) + 1][0] += Constants.getMonoisotopicMassOfAtom("D") - Constants.HYDROGEN_MASS;
} else // predict based on in silico added deuteriums
if (method == 4) {
spectrumHDX[i][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
if (numberDeuteriums != 0)
spectrumHDX[i][0] += Constants.getMonoisotopicMassOfAtom("D") - Constants.HYDROGEN_MASS;
} else // predict based on adduct
if (method == 5) {
byte adductTypeIndex = ((DefaultFragmentToPeakMatch) matchList.getElement(i)).getBestMatchedFragmentAdductTypeIndex();
spectrumHDX[i][0] = peak.getMass() - (numberDeuteriums) * Constants.HYDROGEN_MASS + (numberDeuteriums) * Constants.getMonoisotopicMassOfAtom("D");
if (adductTypeIndex == 2)
spectrumHDX[i][0] += Constants.getMonoisotopicMassOfAtom("D") - Constants.HYDROGEN_MASS;
}
if (method != 3) {
spectrumHDX[i][0] = MathTools.round(spectrumHDX[i][0]);
} else {
spectrumHDX[i * 2][0] = MathTools.round(spectrumHDX[i * 2][0]);
spectrumHDX[(i * 2) + 1][0] = MathTools.round(spectrumHDX[(i * 2) + 1][0]);
}
}
return spectrumHDX;
}
use of de.ipbhalle.metfraglib.match.DefaultFragmentToPeakMatch in project MetFragRelaunched by ipb-halle.
the class NewFragmenterUniqueFormulaScore method calculate.
public void calculate() {
this.matchList = (MatchList) settings.get(VariableNames.MATCH_LIST_NAME);
this.value = Double.valueOf(0);
java.util.ArrayList<Double> scores = new java.util.ArrayList<Double>();
java.util.ArrayList<Integer> fragmentIds = new java.util.ArrayList<Integer>();
java.util.ArrayList<Integer> fragmentIndeces = new java.util.ArrayList<Integer>();
java.util.ArrayList<IFragment> fragments = new java.util.ArrayList<IFragment>();
java.util.ArrayList<DefaultFragmentToPeakMatch> matches = new java.util.ArrayList<DefaultFragmentToPeakMatch>();
java.util.ArrayList<Byte> hydrogenDifferences = new java.util.ArrayList<Byte>();
for (int i = 0; i < this.matchList.getNumberElements(); i++) {
DefaultFragmentToPeakMatch currentMatch = null;
if (this.matchList.getElement(i) instanceof FragmentMassToPeakMatch)
currentMatch = (FragmentMassToPeakMatch) this.matchList.getElement(i);
else
currentMatch = (FragmentFormulaToPeakMatch) this.matchList.getElement(i);
FragmentList currentFragmentList = currentMatch.getMatchedFragmentList();
double peakScore = 0.0;
try {
peakScore = Math.pow((currentMatch.getMatchedPeak().getMass() / this.measuredPrecursorMass) * 10.0, this.ALPHA) * Math.pow(currentMatch.getMatchedPeak().getIntensity(), this.BETA);
} catch (RelativeIntensityNotDefinedException e) {
e.printStackTrace();
}
for (int ii = 0; ii < currentFragmentList.getNumberElements(); ii++) {
IFragment currentFragment = currentFragmentList.getElement(ii);
// if(currentFragment.getTreeDepth() > this.maximumTreeDepth) continue;
int[] brokenBondIndeces = currentFragment.getBrokenBondIndeces();
double energyOfFragment = 0.0;
for (int bondIndex : brokenBondIndeces) {
energyOfFragment += this.bondEnergies[bondIndex];
}
energyOfFragment += Math.abs(currentMatch.getNumberOfHydrogensDifferToPeakMass(ii)) * this.WEIGHT_HYDROGEN_PENALTY;
double currentScore = peakScore / Math.pow(energyOfFragment, this.GAMMA);
int index = fragmentIds.indexOf(currentFragment.getID());
if (index != -1) {
double lastScore = scores.get(index);
if (lastScore < currentScore) {
scores.remove(index);
fragmentIds.remove(index);
fragments.remove(index);
matches.remove(index);
hydrogenDifferences.remove(index);
fragmentIndeces.remove(index);
index = 0;
while (scores.size() > index && scores.get(index) > currentScore) index++;
scores.add(index, currentScore);
fragmentIds.add(index, currentFragment.getID());
fragments.add(index, currentFragment);
matches.add(index, currentMatch);
hydrogenDifferences.add(index, currentMatch.getNumberOfHydrogensDifferToPeakMass(ii));
fragmentIndeces.add(index, ii);
}
} else {
index = 0;
while (scores.size() > index && scores.get(index) > currentScore) index++;
scores.add(index, currentScore);
fragmentIds.add(index, currentFragment.getID());
fragments.add(index, currentFragment);
matches.add(index, currentMatch);
hydrogenDifferences.add(index, currentMatch.getNumberOfHydrogensDifferToPeakMass(ii));
fragmentIndeces.add(index, ii);
}
}
}
java.util.ArrayList<Integer> validAndBestFragmentMatches = new java.util.ArrayList<Integer>();
for (int i = 0; i < scores.size(); i++) {
boolean validAndBestMatch = true;
for (int k = 0; k < validAndBestFragmentMatches.size(); k++) {
if (fragmentIds.get(validAndBestFragmentMatches.get(k)) == fragmentIds.get(i)) {
validAndBestMatch = false;
}
if (matches.get(validAndBestFragmentMatches.get(k)).getMatchedPeak().getMass() == matches.get(i).getMatchedPeak().getMass()) {
validAndBestMatch = false;
}
}
if (!validAndBestMatch)
continue;
int index = 0;
/*
* insert the valid and best match sorted by mass of the peak
*/
while (index < validAndBestFragmentMatches.size() && matches.get(i).getMatchedPeak().getMass() > matches.get(validAndBestFragmentMatches.get(index)).getMatchedPeak().getMass()) index++;
validAndBestFragmentMatches.add(index, i);
}
MatchList cleanedMatchList = new MatchList();
this.bestFragmentIndeces = new int[validAndBestFragmentMatches.size()];
this.optimalValues = new double[validAndBestFragmentMatches.size()];
for (int i = 0; i < validAndBestFragmentMatches.size(); i++) {
matches.get(validAndBestFragmentMatches.get(i)).initialiseBestMatchedFragment(fragmentIndeces.get(validAndBestFragmentMatches.get(i)));
this.value += scores.get(validAndBestFragmentMatches.get(i));
this.optimalValues[i] = scores.get(validAndBestFragmentMatches.get(i));
cleanedMatchList.addElement(matches.get(validAndBestFragmentMatches.get(i)));
/*
System.out.println(
scores.get(validAndBestFragmentMatches.get(i)) + "\t" +
fragmentIds.get(validAndBestFragmentMatches.get(i)) + "\t" +
matches.get(validAndBestFragmentMatches.get(i)).getMatchedPeak().getMass() + "\t\t" +
fragments.get(validAndBestFragmentMatches.get(i)).getMolecularFormula() + "\t" +
hydrogenDifferences.get(validAndBestFragmentMatches.get(i))
);
*/
}
this.settings.set(VariableNames.MATCH_LIST_NAME, cleanedMatchList);
this.calculationFinished = true;
}
use of de.ipbhalle.metfraglib.match.DefaultFragmentToPeakMatch in project MetFragRelaunched by ipb-halle.
the class NewFragmenterUniqueFormulaScore method calculateSingleMatch.
public Double[] calculateSingleMatch(IMatch match) {
DefaultFragmentToPeakMatch currentMatch = null;
if (match instanceof FragmentMassToPeakMatch)
currentMatch = (FragmentMassToPeakMatch) match;
else
currentMatch = (FragmentFormulaToPeakMatch) match;
FragmentList currentFragmentList = currentMatch.getMatchedFragmentList();
double minimumEnergyPerMatch = (double) Integer.MAX_VALUE;
double peakScore = 0.0;
try {
peakScore = Math.pow((currentMatch.getMatchedPeak().getMass() / this.measuredPrecursorMass) * 10.0, this.ALPHA) * Math.pow(currentMatch.getMatchedPeak().getIntensity(), this.BETA);
} catch (RelativeIntensityNotDefinedException e) {
e.printStackTrace();
}
for (int ii = 0; ii < currentFragmentList.getNumberElements(); ii++) {
IFragment currentFragment = currentFragmentList.getElement(ii);
/*
* check if current fragment is valid based on the tree depth
*/
// if(currentFragment.getTreeDepth() > this.maximumTreeDepth) continue;
int[] brokenBondIndeces = currentFragment.getBrokenBondIndeces();
double energyOfFragment = 0.0;
for (int bondIndex : brokenBondIndeces) {
energyOfFragment += this.bondEnergies[bondIndex];
}
energyOfFragment += Math.abs(currentMatch.getNumberOfHydrogensDifferToPeakMass(ii)) * this.WEIGHT_HYDROGEN_PENALTY;
/*
* assign optimal bondenergy and store best fragment
*/
if (energyOfFragment < minimumEnergyPerMatch) {
minimumEnergyPerMatch = energyOfFragment;
}
}
this.calculationFinished = true;
return new Double[] { peakScore / Math.pow(minimumEnergyPerMatch, this.GAMMA), minimumEnergyPerMatch };
}
Aggregations