use of de.ipbhalle.metfraglib.match.FragmentFormulaToPeakMatch 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.FragmentFormulaToPeakMatch in project MetFragRelaunched by ipb-halle.
the class AnalyseAnnotatedMetFragSiriusTree method detectReplacementRearrangement.
public boolean detectReplacementRearrangement(MatchList matchList) {
java.util.ArrayList<SiriusNodePeak> leaves = this.peakList.getLeaves();
for (int i = 0; i < leaves.size(); i++) {
SiriusNodePeak currentNode = leaves.get(i);
boolean hasMatch = false;
while (!currentNode.isRoot()) {
FragmentFormulaToPeakMatch peakMatch = getMatchToPeak(matchList, currentNode);
if (peakMatch != null) {
hasMatch = true;
break;
}
currentNode = currentNode.getFather();
}
if (!hasMatch)
return true;
}
return false;
}
use of de.ipbhalle.metfraglib.match.FragmentFormulaToPeakMatch in project MetFragRelaunched by ipb-halle.
the class AnalyseAnnotatedMetFragSiriusTree method getAnnotatedDotTree.
/**
* @param candidateNumber
* @return
*/
public String getAnnotatedDotTree(ICandidate candidate, MatchList matchList) {
String dotString = "strict digraph {\n";
for (int i = 0; i < this.peakList.getNumberElements(); i++) {
SiriusNodePeak currentPeak = (SiriusNodePeak) this.peakList.getElement(i);
FragmentFormulaToPeakMatch peakMatch = getMatchToPeak(matchList, currentPeak);
if (peakMatch != null) {
try {
dotString += currentPeak.getDotEntry("\\nInt: " + currentPeak.getIntensity() + "\\nMetFrag:\\n" + peakMatch.getBestMatchedFragment().getMolecularFormula(candidate.getPrecursorMolecule()) + "\\n" + peakMatch.getBestMatchedFragment().getSmiles(candidate.getPrecursorMolecule()), "red", "grey") + "\n";
} catch (RelativeIntensityNotDefinedException e) {
e.printStackTrace();
}
} else {
try {
dotString += currentPeak.getDotEntry("Int: " + currentPeak.getIntensity()) + "\n";
} catch (RelativeIntensityNotDefinedException e) {
e.printStackTrace();
}
}
}
dotString += "}\n";
return dotString;
}
use of de.ipbhalle.metfraglib.match.FragmentFormulaToPeakMatch 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.FragmentFormulaToPeakMatch 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