Search in sources :

Example 6 with FragmentList

use of de.ipbhalle.metfraglib.list.FragmentList in project MetFragRelaunched by ipb-halle.

the class TopDownFragmenter 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);
    temporaryFragments.add(root);
    generatedFragments.addElement(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 = ((BitArrayPrecursor) 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) {
                    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;
}
Also used : AbstractTopDownBitArrayFragment(de.ipbhalle.metfraglib.fragment.AbstractTopDownBitArrayFragment) AbstractTopDownBitArrayPrecursor(de.ipbhalle.metfraglib.precursor.AbstractTopDownBitArrayPrecursor) AbstractTopDownBitArrayPrecursor(de.ipbhalle.metfraglib.precursor.AbstractTopDownBitArrayPrecursor) BitArrayPrecursor(de.ipbhalle.metfraglib.precursor.BitArrayPrecursor) FragmentList(de.ipbhalle.metfraglib.list.FragmentList) FastBitArray(de.ipbhalle.metfraglib.FastBitArray)

Example 7 with FragmentList

use of de.ipbhalle.metfraglib.list.FragmentList in project MetFragRelaunched by ipb-halle.

the class NewFragmenterLipidScore method calculate.

public void calculate() {
    this.matchList = (MatchList) settings.get(VariableNames.MATCH_LIST_NAME);
    if (this.matchList == null)
        return;
    this.bestFragmentIndeces = new int[this.matchList.getNumberElements()];
    this.optimalValues = new double[this.matchList.getNumberElements()];
    for (int i = 0; i < this.matchList.getNumberElements(); i++) {
        IMatch currentMatch = this.matchList.getElement(i);
        FragmentList currentFragmentList = currentMatch.getMatchedFragmentList();
        double minimumEnergyPerMatch = (double) Integer.MAX_VALUE;
        double peakScore = 0.0;
        try {
            peakScore = Math.pow((currentMatch.getMatchedPeak().getMass() / this.measuredPrecursorMass) * 100.0, this.parameters[0]) * Math.pow(currentMatch.getMatchedPeak().getIntensity(), this.parameters[1]);
        } 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.getNumberOfOverallHydrogensDifferToPeakMass(ii)) * this.WEIGHT_HYDROGEN_PENALTY;
            /*
				 * assign optimal bondenergy and store best fragment
				 */
            this.value += peakScore / Math.pow(energyOfFragment, this.parameters[2]);
            if (energyOfFragment < minimumEnergyPerMatch) {
                minimumEnergyPerMatch = energyOfFragment;
                this.bestFragmentIndeces[i] = ii;
            }
        }
        currentMatch.initialiseBestMatchedFragment(this.bestFragmentIndeces[i]);
        this.optimalValues[i] = minimumEnergyPerMatch;
    }
    this.calculationFinished = true;
}
Also used : IMatch(de.ipbhalle.metfraglib.interfaces.IMatch) FragmentList(de.ipbhalle.metfraglib.list.FragmentList) IFragment(de.ipbhalle.metfraglib.interfaces.IFragment) RelativeIntensityNotDefinedException(de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException)

Example 8 with FragmentList

use of de.ipbhalle.metfraglib.list.FragmentList in project MetFragRelaunched by ipb-halle.

the class NewFragmenterScore method calculate.

public void calculate() {
    this.matchList = (MatchList) settings.get(VariableNames.MATCH_LIST_NAME);
    if (this.matchList == null)
        return;
    this.bestFragmentIndeces = new int[this.matchList.getNumberElements()];
    this.optimalValues = new double[this.matchList.getNumberElements()];
    for (int i = 0; i < this.matchList.getNumberElements(); i++) {
        IMatch currentMatch = this.matchList.getElement(i);
        FragmentList currentFragmentList = currentMatch.getMatchedFragmentList();
        double minimumEnergyPerMatch = (double) Integer.MAX_VALUE;
        double peakScore = 0.0;
        try {
            peakScore = Math.pow((currentMatch.getMatchedPeak().getMass() / this.measuredPrecursorMass) * 100.0, this.parameters[0]) * Math.pow(currentMatch.getMatchedPeak().getIntensity(), this.parameters[1]);
        } 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.getNumberOfOverallHydrogensDifferToPeakMass(ii)) * this.WEIGHT_HYDROGEN_PENALTY;
            /*
				 * assign optimal bondenergy and store best fragment
				 */
            if (energyOfFragment < minimumEnergyPerMatch) {
                minimumEnergyPerMatch = energyOfFragment;
                this.bestFragmentIndeces[i] = ii;
            }
        }
        currentMatch.initialiseBestMatchedFragment(this.bestFragmentIndeces[i]);
        this.value += peakScore / Math.pow(minimumEnergyPerMatch, this.parameters[2]);
        this.optimalValues[i] = minimumEnergyPerMatch;
    }
    this.calculationFinished = true;
}
Also used : IMatch(de.ipbhalle.metfraglib.interfaces.IMatch) FragmentList(de.ipbhalle.metfraglib.list.FragmentList) IFragment(de.ipbhalle.metfraglib.interfaces.IFragment) RelativeIntensityNotDefinedException(de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException)

Example 9 with FragmentList

use of de.ipbhalle.metfraglib.list.FragmentList in project MetFragRelaunched by ipb-halle.

the class NewFragmenterScore 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(((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[] { peakScore / Math.pow(minimumEnergyPerMatch, this.GAMMA), minimumEnergyPerMatch };
}
Also used : HDFragmentMassToPeakMatch(de.ipbhalle.metfraglib.match.HDFragmentMassToPeakMatch) FragmentList(de.ipbhalle.metfraglib.list.FragmentList) IFragment(de.ipbhalle.metfraglib.interfaces.IFragment) RelativeIntensityNotDefinedException(de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException)

Example 10 with FragmentList

use of de.ipbhalle.metfraglib.list.FragmentList 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;
}
Also used : DefaultFragmentToPeakMatch(de.ipbhalle.metfraglib.match.DefaultFragmentToPeakMatch) MatchList(de.ipbhalle.metfraglib.list.MatchList) FragmentFormulaToPeakMatch(de.ipbhalle.metfraglib.match.FragmentFormulaToPeakMatch) FragmentMassToPeakMatch(de.ipbhalle.metfraglib.match.FragmentMassToPeakMatch) RelativeIntensityNotDefinedException(de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException) FragmentList(de.ipbhalle.metfraglib.list.FragmentList) IFragment(de.ipbhalle.metfraglib.interfaces.IFragment)

Aggregations

FragmentList (de.ipbhalle.metfraglib.list.FragmentList)11 RelativeIntensityNotDefinedException (de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException)9 IFragment (de.ipbhalle.metfraglib.interfaces.IFragment)8 DefaultFragmentToPeakMatch (de.ipbhalle.metfraglib.match.DefaultFragmentToPeakMatch)3 FragmentFormulaToPeakMatch (de.ipbhalle.metfraglib.match.FragmentFormulaToPeakMatch)3 FragmentMassToPeakMatch (de.ipbhalle.metfraglib.match.FragmentMassToPeakMatch)3 HDFragmentMassToPeakMatch (de.ipbhalle.metfraglib.match.HDFragmentMassToPeakMatch)3 FastBitArray (de.ipbhalle.metfraglib.FastBitArray)2 AbstractTopDownBitArrayFragment (de.ipbhalle.metfraglib.fragment.AbstractTopDownBitArrayFragment)2 IMatch (de.ipbhalle.metfraglib.interfaces.IMatch)2 AbstractTopDownBitArrayPrecursor (de.ipbhalle.metfraglib.precursor.AbstractTopDownBitArrayPrecursor)2 TopDownPrecursorCandidate (de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate)1 AtomTypeNotKnownFromInputListException (de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException)1 TopDownNeutralLossFragmenter (de.ipbhalle.metfraglib.fragmenter.TopDownNeutralLossFragmenter)1 ICandidate (de.ipbhalle.metfraglib.interfaces.ICandidate)1 MatchList (de.ipbhalle.metfraglib.list.MatchList)1 MergedTandemMassPeak (de.ipbhalle.metfraglib.peak.MergedTandemMassPeak)1 BitArrayPrecursor (de.ipbhalle.metfraglib.precursor.BitArrayPrecursor)1 MetFragGlobalSettings (de.ipbhalle.metfraglib.settings.MetFragGlobalSettings)1 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)1