use of de.ipbhalle.metfraglib.list.FragmentList in project MetFragRelaunched by ipb-halle.
the class NewFragmenterLipidScore method calculateSingleMatch.
/**
*/
public Double[] calculateSingleMatch(IMatch currentMatch) {
if (currentMatch instanceof HDFragmentMassToPeakMatch)
return new Double[] { 0.0, null };
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();
}
int indexOfBestFragment = -1;
double overallMatchScore = 0.0;
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 = ((AbstractTopDownBitArrayFragment) currentFragment).getBrokenBondsFastBitArray().getSetIndeces();
double energyOfFragment = 0.0;
for (int bondIndex : brokenBondIndeces) {
energyOfFragment += this.bondEnergies[bondIndex];
}
energyOfFragment += Math.abs(currentMatch.getNumberOfOverallHydrogensDifferToPeakMass(ii)) * this.WEIGHT_HYDROGEN_PENALTY;
/*
* assign optimal bondenergy and store best fragment
*/
overallMatchScore += peakScore / Math.pow(energyOfFragment, this.GAMMA);
if (energyOfFragment < minimumEnergyPerMatch) {
minimumEnergyPerMatch = energyOfFragment;
indexOfBestFragment = ii;
}
/*
if(((AbstractTopDownBitArrayFragment)currentFragment).getAtomsFastBitArray().toString().equals("111110001100010001")) {
System.out.println(((AbstractTopDownBitArrayFragment)currentFragment).getAtomsFastBitArray().toString() + " " +
((AbstractTopDownBitArrayFragment)currentFragment).getBondsFastBitArray().toString() + " " +
((AbstractTopDownBitArrayFragment)currentFragment).getBrokenBondsFastBitArray().toString() + " " + currentFragment.getID()
+ " " + energyOfFragment);
}
*/
}
this.calculationFinished = true;
if (indexOfBestFragment != -1)
currentMatch.initialiseBestMatchedFragment(indexOfBestFragment);
return new Double[] { overallMatchScore, minimumEnergyPerMatch };
}
use of de.ipbhalle.metfraglib.list.FragmentList 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.list.FragmentList in project MetFragRelaunched by ipb-halle.
the class HDNewFragmenterScore method calculateSingleMatch.
/**
*/
public Double[] calculateSingleMatch(IMatch currentMatch) {
if (!(currentMatch instanceof HDFragmentMassToPeakMatch))
return new Double[] { 0.0, null };
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();
}
int indexOfBestFragment = -1;
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 = ((AbstractTopDownBitArrayFragment) currentFragment).getBrokenBondsFastBitArray().getSetIndeces();
double energyOfFragment = 0.0;
for (int bondIndex : brokenBondIndeces) {
energyOfFragment += this.bondEnergies[bondIndex];
}
energyOfFragment += Math.abs(currentMatch.getNumberOfOverallHydrogensDifferToPeakMass(ii)) * this.WEIGHT_HYDROGEN_PENALTY;
/*
* assign optimal bondenergy and store best fragment
*/
if (energyOfFragment < minimumEnergyPerMatch) {
minimumEnergyPerMatch = energyOfFragment;
indexOfBestFragment = ii;
}
}
if (indexOfBestFragment != -1)
currentMatch.initialiseBestMatchedFragment(indexOfBestFragment);
return new Double[] { peakScore / Math.pow(minimumEnergyPerMatch, this.GAMMA), minimumEnergyPerMatch };
}
use of de.ipbhalle.metfraglib.list.FragmentList in project MetFragRelaunched by ipb-halle.
the class TopDownNeutralLossFragmenter method generateFragments.
public FragmentList generateFragments() {
FragmentList generatedFragments = new FragmentList();
java.util.Queue<AbstractTopDownBitArrayFragment> temporaryFragments = new java.util.LinkedList<AbstractTopDownBitArrayFragment>();
java.util.Queue<Byte> numberOfFragmentAddedToQueue = new java.util.LinkedList<Byte>();
java.util.Queue<de.ipbhalle.metfraglib.FastBitArray> nextBondIndecesToRemove = new java.util.LinkedList<de.ipbhalle.metfraglib.FastBitArray>();
/*
* set first fragment as root for fragment generation (precursor)
*/
AbstractTopDownBitArrayFragment root = ((AbstractTopDownBitArrayPrecursor) this.scoredCandidate.getPrecursorMolecule()).toFragment();
root.setID(++this.numberOfGeneratedFragments);
root.setWasRingCleavedFragment(false);
generatedFragments.addElement(root);
temporaryFragments.add(root);
numberOfFragmentAddedToQueue.add((byte) 1);
nextBondIndecesToRemove.add(root.getBondsFastBitArray());
for (int k = 1; k <= this.maximumTreeDepth; k++) {
java.util.Queue<AbstractTopDownBitArrayFragment> newTemporaryFragments = new java.util.LinkedList<AbstractTopDownBitArrayFragment>();
java.util.Queue<Byte> newNumberOfFragmentAddedToQueue = new java.util.LinkedList<Byte>();
java.util.Queue<de.ipbhalle.metfraglib.FastBitArray> newNextBondIndecesToRemove = new java.util.LinkedList<de.ipbhalle.metfraglib.FastBitArray>();
while (!temporaryFragments.isEmpty()) {
AbstractTopDownBitArrayFragment nextTopDownFragmentForFragmentation = temporaryFragments.poll();
byte numberOfNextTopDownFragmentForFragmentationAddedToQueue = numberOfFragmentAddedToQueue.poll();
int[] indecesOfSetBondsOfNextTopDownFragment = nextBondIndecesToRemove.poll().getSetIndeces();
for (int i = 0; i < indecesOfSetBondsOfNextTopDownFragment.length; i++) {
short nextBondIndexToRemove = (short) indecesOfSetBondsOfNextTopDownFragment[i];
/*
* if index of selected bond is smaller than the maximum
* index of a cleaved bond of the fragment then select
* another bond prevents generating fragments redundantly
*/
if (nextBondIndexToRemove < nextTopDownFragmentForFragmentation.getMaximalIndexOfRemovedBond() || !nextTopDownFragmentForFragmentation.getBondsFastBitArray().get(nextBondIndexToRemove)) {
continue;
}
short[] indecesOfBondConnectedAtoms = ((AbstractTopDownBitArrayPrecursor) this.scoredCandidate.getPrecursorMolecule()).getConnectedAtomIndecesOfBondIndex(nextBondIndexToRemove);
/*
* getting fragment generated by cleavage of the current
* bond "nextBondIndexToRemove"
*/
AbstractTopDownBitArrayFragment[] newGeneratedTopDownFragments = nextTopDownFragmentForFragmentation.traverseMolecule(this.scoredCandidate.getPrecursorMolecule(), nextBondIndexToRemove, indecesOfBondConnectedAtoms);
/*
* if we got two fragments then save these as valid ones
*/
if (newGeneratedTopDownFragments.length == 2) {
this.checkForNeutralLossesAdaptMolecularFormulas(newGeneratedTopDownFragments, nextBondIndexToRemove);
if (newGeneratedTopDownFragments[0].getMonoisotopicMass(this.scoredCandidate.getPrecursorMolecule()) > this.minimumFragmentMassLimit - this.minimumMassDeviationForFragmentGeneration) {
newGeneratedTopDownFragments[0].setID(++this.numberOfGeneratedFragments);
generatedFragments.addElement(newGeneratedTopDownFragments[0]);
newNextBondIndecesToRemove.add(newGeneratedTopDownFragments[0].getBondsFastBitArray());
newNumberOfFragmentAddedToQueue.add((byte) 1);
newTemporaryFragments.add(newGeneratedTopDownFragments[0]);
}
if (newGeneratedTopDownFragments[1].getMonoisotopicMass(this.scoredCandidate.getPrecursorMolecule()) > this.minimumFragmentMassLimit - this.minimumMassDeviationForFragmentGeneration) {
newGeneratedTopDownFragments[1].setID(++this.numberOfGeneratedFragments);
generatedFragments.addElement(newGeneratedTopDownFragments[1]);
newNextBondIndecesToRemove.add(newGeneratedTopDownFragments[1].getBondsFastBitArray());
newNumberOfFragmentAddedToQueue.add((byte) 1);
newTemporaryFragments.add(newGeneratedTopDownFragments[1]);
}
} else /*
* if just one fragment then we have to cleave once again
*/
{
if (newGeneratedTopDownFragments[0].getMonoisotopicMass(this.scoredCandidate.getPrecursorMolecule()) > this.minimumFragmentMassLimit - this.minimumMassDeviationForFragmentGeneration) {
if (numberOfNextTopDownFragmentForFragmentationAddedToQueue < this.maximumNumberOfAFragmentAddedToQueue) {
temporaryFragments.add(newGeneratedTopDownFragments[0]);
numberOfFragmentAddedToQueue.add((byte) (numberOfNextTopDownFragmentForFragmentationAddedToQueue + 1));
// nextBondIndecesToRemove.add(this.precursorMolecule.getFastBitArrayOfBondsBelongingtoRingLikeBondIndex(nextBondIndexToRemove));
nextBondIndecesToRemove.add(newGeneratedTopDownFragments[0].getBondsFastBitArray());
} else {
newTemporaryFragments.add(newGeneratedTopDownFragments[0]);
newNumberOfFragmentAddedToQueue.add((byte) 1);
newNextBondIndecesToRemove.add(newGeneratedTopDownFragments[0].getBondsFastBitArray());
}
}
}
}
}
temporaryFragments = newTemporaryFragments;
numberOfFragmentAddedToQueue = newNumberOfFragmentAddedToQueue;
nextBondIndecesToRemove = newNextBondIndecesToRemove;
}
temporaryFragments = null;
numberOfFragmentAddedToQueue = null;
nextBondIndecesToRemove = null;
return generatedFragments;
}
use of de.ipbhalle.metfraglib.list.FragmentList in project MetFragRelaunched by ipb-halle.
the class MetfRag method generateAllFragments.
/**
* @param molecule
* @param maximumTreeDepth
* @return
*/
public static IAtomContainer[] generateAllFragments(IAtomContainer molecule, int maximumTreeDepth) {
Logger.getLogger("net.sf.jnati.deploy.artefact.ConfigManager").setLevel(Level.ERROR);
Logger.getLogger("net.sf.jnati.deploy.repository.ClasspathRepository").setLevel(Level.ERROR);
Logger.getLogger("net.sf.jnati.deploy.repository.LocalRepository").setLevel(Level.ERROR);
Logger.getLogger("net.sf.jnati.deploy.artefact.ManifestReader").setLevel(Level.ERROR);
Logger.getLogger("net.sf.jnati.deploy.NativeArtefactLocator").setLevel(Level.ERROR);
Logger.getLogger("net.sf.jnati.deploy.NativeLibraryLoader").setLevel(Level.ERROR);
MetFragGlobalSettings settings = new MetFragGlobalSettings();
MoleculeFunctions.prepareAtomContainer(molecule, true);
ICandidate candidate = null;
try {
candidate = new TopDownPrecursorCandidate(MoleculeFunctions.getInChIInfoFromAtomContainer(molecule)[0], "1");
candidate.initialisePrecursorCandidate();
} catch (AtomTypeNotKnownFromInputListException e1) {
e1.printStackTrace();
return new IAtomContainer[0];
} catch (Exception e1) {
e1.printStackTrace();
return new IAtomContainer[0];
}
settings.set(VariableNames.CANDIDATE_NAME, candidate);
settings.set(VariableNames.MAXIMUM_TREE_DEPTH_NAME, (byte) 2);
settings.set(VariableNames.MINIMUM_FRAGMENT_MASS_LIMIT_NAME, 0.0);
settings.set(VariableNames.MAXIMUM_NUMBER_OF_TOPDOWN_FRAGMENT_ADDED_TO_QUEUE, (byte) maximumTreeDepth);
TopDownNeutralLossFragmenter fragmenter = null;
try {
fragmenter = new TopDownNeutralLossFragmenter(settings);
} catch (Exception e) {
e.printStackTrace();
}
FragmentList fragmentList = fragmenter.generateFragments();
IAtomContainer[] fragments = new IAtomContainer[fragmentList.getNumberElements()];
for (int i = 0; i < fragmentList.getNumberElements(); i++) {
fragments[i] = fragmentList.getElement(i).getStructureAsIAtomContainer(candidate.getPrecursorMolecule());
}
return fragments;
}
Aggregations