use of de.ipbhalle.metfraglib.interfaces.IMatch in project MetFragRelaunched by ipb-halle.
the class AutomatedLossFingerprintAnnotationScoreInitialiser method postProcessScoreParameters.
public void postProcessScoreParameters(Settings settings) throws AtomTypeNotKnownFromInputListException, Exception {
CombinedSingleCandidateMetFragProcess[] processes = (CombinedSingleCandidateMetFragProcess[]) settings.get(VariableNames.METFRAG_PROCESSES_NAME);
// fingerprints not seen in training
MassToFingerprintsHashMap lossMassToFingerprints = new MassToFingerprintsHashMap();
MassToFingerprintGroupListCollection lossToFingerprintGroupListCollection = (MassToFingerprintGroupListCollection) settings.get(VariableNames.LOSS_TO_FINGERPRINT_GROUP_LIST_COLLECTION_NAME);
Double mzppm = (Double) settings.get(VariableNames.RELATIVE_MASS_DEVIATION_NAME);
Double mzabs = (Double) settings.get(VariableNames.ABSOLUTE_MASS_DEVIATION_NAME);
int ionmode = (Integer) settings.get(VariableNames.PRECURSOR_ION_MODE_NAME);
boolean ispositive = (Boolean) settings.get(VariableNames.IS_POSITIVE_ION_MODE_NAME);
double adductMass = Constants.getIonisationTypeMassCorrection(Constants.ADDUCT_NOMINAL_MASSES.indexOf(ionmode), ispositive);
double precursorMass = (Double) settings.get(VariableNames.PRECURSOR_NEUTRAL_MASS_NAME);
double ionmass = MathTools.round(precursorMass + adductMass);
Fingerprint fingerprint = new Fingerprint((String) settings.get(VariableNames.FINGERPRINT_TYPE_NAME));
for (CombinedSingleCandidateMetFragProcess scmfp : processes) {
/*
* check whether the single run was successful
*/
if (scmfp.wasSuccessful()) {
ICandidate candidate = scmfp.getScoredPrecursorCandidates()[0];
java.util.ArrayList<MassFingerprintMatch> lossMatchlist = new java.util.ArrayList<MassFingerprintMatch>();
MatchList matchlist = candidate.getMatchList();
if (matchlist != null) {
candidate.initialisePrecursorCandidate();
for (int i = 0; i < matchlist.getNumberElements(); i++) {
IMatch matchI = matchlist.getElement(i);
IFragment fragmentI = matchI.getBestMatchedFragment();
double peakMassI = matchI.getMatchedPeak().getMass();
for (int j = i + 1; j < matchlist.getNumberElements(); j++) {
IMatch matchJ = matchlist.getElement(j);
double peakMassJ = matchJ.getMatchedPeak().getMass();
IFragment fragmentJ = matchJ.getBestMatchedFragment();
if (fragmentJ.isRealSubStructure(fragmentI)) {
double diff = MathTools.round(peakMassJ - peakMassI);
MassToFingerprintGroupList matchingLossToFingerprintGroupList = lossToFingerprintGroupListCollection.getElementByPeak(diff, mzppm, mzabs);
if (matchingLossToFingerprintGroupList == null)
continue;
IFragment diffFragment = fragmentJ.getDifferenceFragment(candidate.getPrecursorMolecule(), fragmentI);
if (diffFragment == null)
continue;
IAtomContainer con = fingerprint.getNormalizedAtomContainer(candidate.getPrecursorMolecule(), diffFragment);
lossMatchlist.add(new MassFingerprintMatch(diff, fingerprint.getNormalizedFastBitArrayFingerprint(con)));
}
}
// do the same for the precursor ion
double diff = MathTools.round(ionmass - peakMassI);
MassToFingerprintGroupList matchingLossToFingerprintGroupList = lossToFingerprintGroupListCollection.getElementByPeak(diff, mzppm, mzabs);
if (matchingLossToFingerprintGroupList == null)
continue;
IFragment diffFragment = fragmentI.getDifferenceFragment(candidate.getPrecursorMolecule());
if (diffFragment == null)
continue;
IAtomContainer con = fingerprint.getNormalizedAtomContainer(candidate.getPrecursorMolecule(), diffFragment);
lossMatchlist.add(new MassFingerprintMatch(diff, fingerprint.getNormalizedFastBitArrayFingerprint(con)));
}
}
// java.util.LinkedList<Double> nonExplainedLosses = this.getNonExplainedLoss(peakList, matchlist);
for (int j = 0; j < lossMatchlist.size(); j++) {
MassFingerprintMatch lossMatch = lossMatchlist.get(j);
MassToFingerprintGroupList lossToFingerprintGroupList = lossToFingerprintGroupListCollection.getElementByPeak(lossMatch.getMass(), mzppm, mzabs);
// if not loss not in our annotation list, there's no need to consider it
if (lossToFingerprintGroupList == null)
continue;
// lossMatch.setMass(lossToFingerprintGroupList.getPeakmz());
FastBitArray currentFingerprint = lossMatch.getFingerprint();
// check whether fingerprint was observed for current peak mass in the training data
if (!lossToFingerprintGroupList.containsFingerprint(currentFingerprint)) {
// if not add the fingerprint to background by addFingerprint function
// addFingerprint checks also whether fingerprint was already added
lossMassToFingerprints.addFingerprint(lossMatch.getMass(), currentFingerprint);
}
}
java.util.LinkedList<?> lossMassesFoundInPeakList = (java.util.LinkedList<?>) settings.get(VariableNames.LOSS_MASSES_FOUND_PEAKLIST_NAME);
// important! now add all losses not assigned by that candidates
// this is to equalize all loss match lists in length over all candidates
this.addNonExplainedLosses(lossMassesFoundInPeakList, lossMatchlist);
candidate.setProperty("LossMatchList", lossMatchlist);
}
}
// alpha
double alpha = (double) settings.get(VariableNames.LOSS_FINGERPRINT_ANNOTATION_ALPHA_VALUE_NAME);
// beta
double beta = (double) settings.get(VariableNames.LOSS_FINGERPRINT_ANNOTATION_BETA_VALUE_NAME);
// f_s
double f_seen_matched = (double) settings.get(VariableNames.LOSS_FINGERPRINT_MATCHED_TUPLE_COUNT_NAME);
// f_s
double f_seen_non_matched = (double) settings.get(VariableNames.LOSS_FINGERPRINT_NON_MATCHED_TUPLE_COUNT_NAME);
// f_u
double f_unseen_matched = lossMassToFingerprints.getOverallMatchedSize();
// f_u
double f_unseen_non_matched = lossMassToFingerprints.getOverallNonMatchedSize();
// \sum_N
double sumFingerprintFrequencies = (double) settings.get(VariableNames.LOSS_FINGERPRINT_DENOMINATOR_COUNT_NAME);
// set value for denominator of P(f,m)
double denominatorValue = sumFingerprintFrequencies + alpha * (f_seen_matched + f_unseen_matched) + beta * (f_seen_non_matched + f_unseen_non_matched);
settings.set(VariableNames.LOSS_FINGERPRINT_DENOMINATOR_VALUE_NAME, denominatorValue);
// P(f,m) F_u
double alphaProbability = alpha / denominatorValue;
// p(f,m) not annotated
double betaProbability = beta / denominatorValue;
for (int i = 0; i < lossToFingerprintGroupListCollection.getNumberElements(); i++) {
MassToFingerprintGroupList groupList = lossToFingerprintGroupListCollection.getElement(i);
// sum_f P(f,m)
// calculate sum of MF_s (including the alpha count) and the joint probabilities
// at this stage getProbability() returns the absolute counts from the annotation files
double sum_f = 0.0;
double sumFsProbabilities = 0.0;
for (int ii = 0; ii < groupList.getNumberElements(); ii++) {
// first calculate P(f,m)
if (groupList.getElement(ii).getFingerprint().getSize() != 1)
groupList.getElement(ii).setJointProbability((groupList.getElement(ii).getProbability() + alpha) / denominatorValue);
else
groupList.getElement(ii).setJointProbability((groupList.getElement(ii).getProbability() + beta) / denominatorValue);
// sum_f P(f,m) -> for F_s
sumFsProbabilities += groupList.getElement(ii).getJointProbability();
}
// calculate the sum of probabilities for un-observed fingerprints for the current mass
double sumFuProbabilities = alphaProbability * lossMassToFingerprints.getSizeMatched(groupList.getPeakmz());
// not needed as it's defined by fingerprint = "0"
// sumFuProbabilities += betaProbability * lossMassToFingerprints.getSizeNonMatched(groupList.getPeakmz());
sumFuProbabilities += betaProbability;
sum_f += sumFsProbabilities;
sum_f += sumFuProbabilities;
for (int ii = 0; ii < groupList.getNumberElements(); ii++) {
// second calculate P(f|m)
groupList.getElement(ii).setConditionalProbability_sp(groupList.getElement(ii).getJointProbability() / sum_f);
}
groupList.setAlphaProb(alphaProbability / sum_f);
groupList.setBetaProb(betaProbability / sum_f);
groupList.setProbabilityToConditionalProbability_sp();
groupList.calculateSumProbabilites();
}
return;
}
use of de.ipbhalle.metfraglib.interfaces.IMatch in project MetFragRelaunched by ipb-halle.
the class HDTopDownFragmenterAssignerScorer method matchFragmentHD.
/**
* @param tempPeakPointer
* @param currentFragmentWrapper
* @param tandemMassPeakList
* @param sortedScoredPeaks
* @param newToProcessFragments
* @param peakIndexToPeakMatch
* @param fragmentIndexToPeakMatch
* @return
*/
protected boolean matchFragmentHD(int tempPeakPointer, HDTopDownBitArrayFragmentWrapper currentFragmentWrapper, SortedTandemMassPeakList tandemMassPeakList, java.util.HashMap<Integer, MatchFragmentList> peakIndexToPeakMatch, java.util.HashMap<Integer, MatchPeakList> fragmentIndexToPeakMatch, int precursorIndex) {
byte matched = -1;
boolean matchedAndAdded = false;
while (matched != 1 && tempPeakPointer >= 0) {
IMatch[] match = new IMatch[1];
TandemMassPeak currentPeak = tandemMassPeakList.getElement(tempPeakPointer);
/*
* calculate match
*/
if (tempPeakPointer >= 0)
matched = currentFragmentWrapper.matchToPeak(this.candidates[0].getPrecursorMolecule(), currentPeak, this.precursorIonTypeIndexHD, this.positiveMode, match);
/*
* check whether match has occurred
*/
if (matched == 0) {
matchedAndAdded = true;
currentFragmentWrapper.getWrappedFragment().setPrecursorFragments(true);
Double[][] currentScores = this.scoreCollection.calculateSingleMatch(match[0]);
/*
* insert fragment into peak's fragment list
*/
/*
* first generate the new fragment node and set the score values
*/
MatchFragmentNode newNode = new MatchFragmentNode(match[0]);
newNode.setScore(currentScores[0][0]);
newNode.setFragmentScores(currentScores[0]);
newNode.setOptimalValues(currentScores[1]);
/*
* find correct location in the fragment list
*/
boolean similarFragmentFound = false;
if (peakIndexToPeakMatch.containsKey(tempPeakPointer)) {
Double[] values = peakIndexToPeakMatch.get(tempPeakPointer).containsByFingerprint(currentFragmentWrapper.getWrappedFragment().getAtomsFastBitArray());
if (values == null) {
peakIndexToPeakMatch.get(tempPeakPointer).insert(newNode);
} else {
if (values[0] < currentScores[0][0]) {
peakIndexToPeakMatch.get(tempPeakPointer).removeElementByID((int) Math.floor(values[1]));
fragmentIndexToPeakMatch.get((int) Math.floor(values[1])).removeElementByID(tempPeakPointer);
if (fragmentIndexToPeakMatch.get((int) Math.floor(values[1])).getRootNode() == null) {
fragmentIndexToPeakMatch.remove((int) Math.floor(values[1]));
}
peakIndexToPeakMatch.get(tempPeakPointer).insert(newNode);
} else
similarFragmentFound = true;
}
} else {
MatchFragmentList newFragmentList = new MatchFragmentList(newNode);
peakIndexToPeakMatch.put(tempPeakPointer, newFragmentList);
}
/*
* insert peak into fragment's peak list
*/
if (!similarFragmentFound) {
if (fragmentIndexToPeakMatch.containsKey(currentFragmentWrapper.getWrappedFragment().getID())) {
fragmentIndexToPeakMatch.get(currentFragmentWrapper.getWrappedFragment().getID()).insert(currentPeak, currentScores[0][0], tempPeakPointer);
} else {
MatchPeakList newPeakList = new MatchPeakList(currentPeak, currentScores[0][0], tempPeakPointer);
fragmentIndexToPeakMatch.put(currentFragmentWrapper.getWrappedFragment().getID(), newPeakList);
}
}
}
/*
* if the mass of the current fragment was greater than the peak mass then assign the current peak ID to the peak IDs of the
* child fragments as they have smaller masses
*/
if (matched == 1 || tempPeakPointer == 0) {
/*
* mark current fragment for further fragmentation
*/
currentFragmentWrapper.setCurrentPeakIndexPointerHD(tempPeakPointer);
}
/*
* if the current fragment has matched to the current peak then set the current peak index to the next peak as the current fragment can
* also match to the next peak
* if the current fragment mass was smaller than that of the current peak then set the current peak index to the next peak (reduce the index)
* as the next peak mass is smaller and could match the current smaller fragment mass
*/
if (matched == 0 || matched == -1)
tempPeakPointer--;
}
return matchedAndAdded;
}
use of de.ipbhalle.metfraglib.interfaces.IMatch in project MetFragRelaunched by ipb-halle.
the class HDTopDownFragmenterAssignerScorer method calculateFragmenterScoresHD.
/**
* @param peakIndexToPeakMatch
* @param singleScores
* @param summedScores
*/
protected void calculateFragmenterScoresHD(java.util.HashMap<Integer, MatchFragmentList> peakIndexToPeakMatchHD, double[][] singleScores, double[] summedScores, ICandidate candidate, int precursorID) {
java.util.Iterator<Integer> it = peakIndexToPeakMatchHD.keySet().iterator();
int index = 0;
String sumFormulasOfFragmentsExplainedPeaks = "";
String smilesOfFragmentsExplainedPeaks = "";
while (it.hasNext()) {
int key = it.next();
MatchFragmentList matchFragmentList = peakIndexToPeakMatchHD.get(key);
MatchFragmentNode bestFragment = matchFragmentList.getRootNode();
IMatch match = bestFragment.getMatch();
sumFormulasOfFragmentsExplainedPeaks += match.getMatchedPeak().getMass() + ":" + match.getModifiedFormulaStringOfBestMatchedFragment(candidate.getPrecursorMolecule()) + ";";
// write out fragment smiles of HDX candidates if extended writer is set
if (this.extendedWriter) {
try {
smilesOfFragmentsExplainedPeaks += match.getMatchedPeak().getMass() + ":" + MoleculeFunctions.getFragmentSmilesHD(this.candidates[precursorID].getPrecursorMolecule(), match.getBestMatchedFragment(), precursorID) + ";";
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Double[] scoreValuesSingleMatch = null;
try {
scoreValuesSingleMatch = bestFragment.getFragmentScores();
} catch (Exception e) {
matchFragmentList.printElements(candidate.getPrecursorMolecule());
System.out.println(candidate.getIdentifier() + " " + key);
return;
}
Double[] optimalValuesSingleMatch = bestFragment.getOptimalValues();
for (int k = 1; k < scoreValuesSingleMatch.length; k++) {
if (optimalValuesSingleMatch[k] != null)
singleScores[k - 1][index] = optimalValuesSingleMatch[k];
summedScores[k - 1] += scoreValuesSingleMatch[k];
}
if (bestFragment != null) {
bestFragment.getFragment().setIsBestMatchedFragment(true);
// match.initialiseBestMatchedFragment(0);
MatchFragmentNode currentFragment = bestFragment;
while (currentFragment.hasNext()) {
MatchFragmentNode node = currentFragment.getNext();
match.addToMatch(node.getMatch());
currentFragment = currentFragment.getNext();
}
}
index++;
}
if (sumFormulasOfFragmentsExplainedPeaks.length() != 0)
sumFormulasOfFragmentsExplainedPeaks = sumFormulasOfFragmentsExplainedPeaks.substring(0, sumFormulasOfFragmentsExplainedPeaks.length() - 1);
if (smilesOfFragmentsExplainedPeaks.length() != 0)
smilesOfFragmentsExplainedPeaks = smilesOfFragmentsExplainedPeaks.substring(0, smilesOfFragmentsExplainedPeaks.length() - 1);
candidate.setProperty("HDSmilesOfExplPeaks", smilesOfFragmentsExplainedPeaks);
candidate.setProperty("HDFormulasOfExplPeaks", sumFormulasOfFragmentsExplainedPeaks);
candidate.setProperty("HDNoExplPeaks", index);
}
use of de.ipbhalle.metfraglib.interfaces.IMatch in project MetFragRelaunched by ipb-halle.
the class HDTopDownFragmenterAssignerScorer method calculateFragmenterScores.
/**
* @param peakIndexToPeakMatch
* @param singleScores
* @param summedScores
*/
protected void calculateFragmenterScores(java.util.HashMap<Integer, MatchFragmentList> peakIndexToPeakMatch, double[][] singleScores, double[] summedScores) {
java.util.Iterator<Integer> it = peakIndexToPeakMatch.keySet().iterator();
int index = 0;
while (it.hasNext()) {
int key = it.next();
MatchFragmentList matchFragmentList = peakIndexToPeakMatch.get(key);
MatchFragmentNode bestFragment = matchFragmentList.getRootNode();
IMatch match = bestFragment.getMatch();
Double[] scoreValuesSingleMatch = null;
try {
scoreValuesSingleMatch = bestFragment.getFragmentScores();
} catch (Exception e) {
matchFragmentList.printElements(this.candidates[0].getPrecursorMolecule());
System.out.println(this.candidates[0].getIdentifier() + " " + key);
return;
}
Double[] optimalValuesSingleMatch = bestFragment.getOptimalValues();
for (int k = 1; k < scoreValuesSingleMatch.length; k++) {
if (optimalValuesSingleMatch[k] != null)
singleScores[k - 1][index] = optimalValuesSingleMatch[k];
summedScores[k - 1] += scoreValuesSingleMatch[k];
}
if (bestFragment != null) {
bestFragment.getFragment().setIsBestMatchedFragment(true);
// match.initialiseBestMatchedFragment(0);
this.matchList.addElementSorted(match);
MatchFragmentNode currentFragment = bestFragment;
while (currentFragment.hasNext()) {
MatchFragmentNode node = currentFragment.getNext();
match.addToMatch(node.getMatch());
currentFragment = currentFragment.getNext();
}
}
index++;
}
}
use of de.ipbhalle.metfraglib.interfaces.IMatch in project MetFragRelaunched by ipb-halle.
the class HDTopDownFragmenterAssignerScorer method matchFragment.
/**
* checks the match of the current fragment and returns the index of the next peak
*
* @param tempPeakPointer
* @param currentFragmentWrapper
* @param tandemMassPeakList
* @param sortedScoredPeaks
* @param newToProcessFragments
* @param peakIndexToPeakMatch
* @param fragmentIndexToPeakMatch
* @return
*/
protected boolean matchFragment(int tempPeakPointer, HDTopDownBitArrayFragmentWrapper currentFragmentWrapper, SortedTandemMassPeakList tandemMassPeakList, java.util.HashMap<Integer, MatchFragmentList> peakIndexToPeakMatch, java.util.HashMap<Integer, MatchPeakList> fragmentIndexToPeakMatch) {
byte matched = -1;
boolean matchedAndAdded = false;
while (matched != 1 && tempPeakPointer >= 0) {
IMatch[] match = new IMatch[1];
TandemMassPeak currentPeak = tandemMassPeakList.getElement(tempPeakPointer);
/*
* calculate match
*/
if (tempPeakPointer >= 0) {
matched = currentFragmentWrapper.getWrappedFragment().matchToPeak(this.candidates[0].getPrecursorMolecule(), currentPeak, this.precursorIonTypeIndex, this.positiveMode, match);
}
/*
* check whether match has occurred
*/
if (matched == 0) {
currentFragmentWrapper.getWrappedFragment().setPrecursorFragments(true);
Double[][] currentScores = this.scoreCollection.calculateSingleMatch(match[0]);
/*
* first generate the new fragment node and set the score values
*/
MatchFragmentNode newNode = new MatchFragmentNode(match[0]);
newNode.setScore(currentScores[0][0]);
newNode.setFragmentScores(currentScores[0]);
newNode.setOptimalValues(currentScores[1]);
/*
* find correct location in the fragment list
*/
boolean similarFragmentFound = false;
if (peakIndexToPeakMatch.containsKey(tempPeakPointer)) {
Double[] values = peakIndexToPeakMatch.get(tempPeakPointer).containsByFingerprint(currentFragmentWrapper.getWrappedFragment().getAtomsFastBitArray());
if (values == null) {
peakIndexToPeakMatch.get(tempPeakPointer).insert(newNode);
} else {
if (values[0] < currentScores[0][0]) {
peakIndexToPeakMatch.get(tempPeakPointer).removeElementByID((int) Math.floor(values[1]));
fragmentIndexToPeakMatch.get((int) Math.floor(values[1])).removeElementByID(tempPeakPointer);
if (fragmentIndexToPeakMatch.get((int) Math.floor(values[1])).getRootNode() == null) {
fragmentIndexToPeakMatch.remove((int) Math.floor(values[1]));
}
peakIndexToPeakMatch.get(tempPeakPointer).insert(newNode);
} else
similarFragmentFound = true;
}
} else {
MatchFragmentList newFragmentList = new MatchFragmentList(newNode);
peakIndexToPeakMatch.put(tempPeakPointer, newFragmentList);
}
/*
* insert peak into fragment's peak list
*/
if (!similarFragmentFound) {
if (fragmentIndexToPeakMatch.containsKey(currentFragmentWrapper.getWrappedFragment().getID())) {
fragmentIndexToPeakMatch.get(currentFragmentWrapper.getWrappedFragment().getID()).insert(currentPeak, currentScores[0][0], tempPeakPointer);
} else {
MatchPeakList newPeakList = new MatchPeakList(currentPeak, currentScores[0][0], tempPeakPointer);
fragmentIndexToPeakMatch.put(currentFragmentWrapper.getWrappedFragment().getID(), newPeakList);
}
}
}
/*
* if the mass of the current fragment was greater than the peak mass then assign the current peak ID to the peak IDs of the
* child fragments as they have smaller masses
*/
if (matched == 1 || tempPeakPointer == 0) {
/*
* mark current fragment for further fragmentation
*/
currentFragmentWrapper.setCurrentPeakIndexPointer(tempPeakPointer);
}
/*
* if the current fragment has matched to the current peak then set the current peak index to the next peak as the current fragment can
* also match to the next peak
* if the current fragment mass was smaller than that of the current peak then set the current peak index to the next peak (reduce the index)
* as the next peak mass is smaller and could match the current smaller fragment mass
*/
if (matched == 0 || matched == -1)
tempPeakPointer--;
}
return matchedAndAdded;
}
Aggregations