use of de.ipbhalle.metfraglib.fragment.AbstractTopDownBitArrayFragment in project MetFragRelaunched by ipb-halle.
the class TopDownFragmenter method getFragmentsOfNextTreeDepth.
/**
* generates all fragments of the given precursor fragment to reach the new tree depth
*/
@Override
public ArrayList<AbstractTopDownBitArrayFragment> getFragmentsOfNextTreeDepth(AbstractTopDownBitArrayFragment precursorFragment) {
FastBitArray ringBonds = new FastBitArray(precursorFragment.getBondsFastBitArray().getSize(), false);
java.util.Queue<AbstractTopDownBitArrayFragment> ringBondCuttedFragments = new java.util.LinkedList<AbstractTopDownBitArrayFragment>();
java.util.Queue<Short> lastCuttedBondOfRing = new java.util.LinkedList<Short>();
ArrayList<AbstractTopDownBitArrayFragment> fragmentsOfNextTreeDepth = new ArrayList<AbstractTopDownBitArrayFragment>();
/*
* generate fragments of skipped bonds
*/
if (this.ringBondsInitialised)
this.generateFragmentsOfSkippedBonds(fragmentsOfNextTreeDepth, precursorFragment);
/*
* get the last bond index that was removed; from there on the next bonds will be removed
*/
short nextBrokenIndexBondIndexToRemove = (short) (precursorFragment.getMaximalIndexOfRemovedBond() + 1);
/*
* start from the last broken bond index
*/
for (short i = nextBrokenIndexBondIndexToRemove; i < precursorFragment.getBondsFastBitArray().getSize(); i++) {
if (!precursorFragment.getBondsFastBitArray().get(i))
continue;
short[] indecesOfBondConnectedAtoms = ((BitArrayPrecursor) this.scoredCandidate.getPrecursorMolecule()).getConnectedAtomIndecesOfBondIndex(i);
/*
* try to generate at most two fragments by the removal of the given bond
*/
AbstractTopDownBitArrayFragment[] newGeneratedTopDownFragments = precursorFragment.traverseMolecule(this.scoredCandidate.getPrecursorMolecule(), i, indecesOfBondConnectedAtoms);
/*
* in case the precursor wasn't splitted try to cleave an additional bond until
*
* 1. two fragments are generated or
* 2. the maximum number of trials have been reached
* 3. no further bond can be removed
*/
if (newGeneratedTopDownFragments.length == 1) {
ringBonds.set(i, true);
ringBondCuttedFragments.add(newGeneratedTopDownFragments[0]);
lastCuttedBondOfRing.add(i);
if (!this.ringBondsInitialised)
this.ringBondFastBitArray.set(i);
}
/*
* pre-processing of the generated fragment/s
*/
this.processGeneratedFragments(newGeneratedTopDownFragments);
/*
* if two new fragments have been generated set them as valid
*/
if (newGeneratedTopDownFragments.length == 2) {
newGeneratedTopDownFragments[0].setAsValidFragment();
newGeneratedTopDownFragments[1].setAsValidFragment();
}
/*
* add fragment/s to vector after setting the proper precursor
*/
for (int k = 0; k < newGeneratedTopDownFragments.length; k++) {
// precursorFragment.addChild(newGeneratedTopDownFragments[k]);
if (newGeneratedTopDownFragments.length == 2)
fragmentsOfNextTreeDepth.add(newGeneratedTopDownFragments[k]);
/*
if(precursorFragment.isValidFragment()) {
newGeneratedTopDownFragments[k].setPrecursorFragment(precursorFragment);
}
else {
newGeneratedTopDownFragments[k].setPrecursorFragment(precursorFragment.hasPrecursorFragment() ? precursorFragment.getPrecursorFragment(): precursorFragment);
}
*/
}
}
/*
* create fragments by ring bond cleavage and store them in the given vector
*/
this.createRingBondCleavedFragments(fragmentsOfNextTreeDepth, precursorFragment, ringBondCuttedFragments, ringBonds, lastCuttedBondOfRing);
this.ringBondsInitialised = true;
return fragmentsOfNextTreeDepth;
}
use of de.ipbhalle.metfraglib.fragment.AbstractTopDownBitArrayFragment 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.fragment.AbstractTopDownBitArrayFragment in project MetFragRelaunched by ipb-halle.
the class HDFragmentMassToPeakMatch method addToMatch.
public void addToMatch(IMatch match) {
HDFragmentMassToPeakMatch currentMatch = (HDFragmentMassToPeakMatch) match;
FastBitArray atomsFastBitArrayOfCurrentFragment = ((AbstractTopDownBitArrayFragment) currentMatch.getMatchedFragmentList().getElement(0)).getAtomsFastBitArray();
for (int i = 0; i < this.matchedFragmentsList.getNumberElements(); i++) {
AbstractTopDownBitArrayFragment tmpFragment = (AbstractTopDownBitArrayFragment) this.matchedFragmentsList.getElement(i);
if (tmpFragment.getAtomsFastBitArray().equals(atomsFastBitArrayOfCurrentFragment))
return;
}
if (match.getBestMatchedFragment() != null) {
this.addMatchedFragment(currentMatch.getBestMatchedFragment(), currentMatch.getBestMatchedFragmentHydrogenDifference(), currentMatch.getBestMatchedFragmentDeuteriumDifference(), currentMatch.getBestMatchFragmentMass(), currentMatch.getBestMatchedFragmentAdductTypeIndex(), currentMatch.getVariableDeuteriumDifferenceOfBestFragment(), currentMatch.getNumberOfDeuteriumsOfBestFragment());
} else {
this.addMatchedFragment(currentMatch.getMatchedFragmentList().getElement(0), currentMatch.getNumberOfHydrogensDifferToPeakMass(0), currentMatch.getNumberOfDeuteriumDifferToPeakMass(0), currentMatch.getMatchedFragmentMassToTandemMassPeak(0), currentMatch.getFragmentsAdductTypeIndeces().get(0), currentMatch.getNumberOfVariableDeuteriumDifferToPeakMass(0), currentMatch.getNumberOfDeuteriumsOfMatchedFragment(0));
}
}
use of de.ipbhalle.metfraglib.fragment.AbstractTopDownBitArrayFragment in project MetFragRelaunched by ipb-halle.
the class PFAS method getSplitResult.
public FragmentPFAS getSplitResult(List<Integer> bondIndexes, int[] endChainCarbonIndexes) {
LinkedList<AbstractTopDownBitArrayFragment> abstractTopDownBitArrayFragments = new LinkedList<AbstractTopDownBitArrayFragment>();
LinkedList<AbstractTopDownBitArrayFragment> newAbstractTopDownBitArrayFragments = null;
AbstractTopDownBitArrayFragment root = ((AbstractTopDownBitArrayPrecursor) this.pfasStructure.getPrecursorMolecule()).toFragment();
abstractTopDownBitArrayFragments.add(root);
int bondsBroken = 0;
List<AbstractTopDownBitArrayFragment> createdFragments = new ArrayList<AbstractTopDownBitArrayFragment>();
// store atoms
List<short[]> indecesOfBondConnectedAtomsList = new ArrayList<short[]>();
for (int i = 0; i < bondIndexes.size(); i++) {
int bondIndex = bondIndexes.get(i);
newAbstractTopDownBitArrayFragments = new LinkedList<AbstractTopDownBitArrayFragment>();
while (!abstractTopDownBitArrayFragments.isEmpty()) {
AbstractTopDownBitArrayFragment currentFragment = abstractTopDownBitArrayFragments.remove();
if (currentFragment.getBondsFastBitArray().get(bondIndex)) {
short[] indecesOfBondConnectedAtoms = new short[2];
AbstractTopDownBitArrayFragment[] fragments = this.split((short) bondIndex, currentFragment, indecesOfBondConnectedAtoms);
if (fragments.length != 1)
bondsBroken++;
for (int ii = 0; ii < fragments.length; ii++) {
newAbstractTopDownBitArrayFragments.add(fragments[ii]);
createdFragments.add(fragments[ii]);
indecesOfBondConnectedAtomsList.add(indecesOfBondConnectedAtoms);
}
}
}
abstractTopDownBitArrayFragments = newAbstractTopDownBitArrayFragments;
}
List<short[]> cleanedIndecesOfBondConnectedAtomsList = new ArrayList<short[]>();
List<AbstractTopDownBitArrayFragment> cleanedCreatedFragments = new ArrayList<AbstractTopDownBitArrayFragment>();
for (int i = 0; i < createdFragments.size(); i++) {
boolean bondFound = false;
for (int j = 0; j < bondIndexes.size(); j++) {
if (createdFragments.get(i).getBondsFastBitArray().get(bondIndexes.get(j))) {
bondFound = true;
}
}
if (!bondFound) {
cleanedCreatedFragments.add(createdFragments.get(i));
cleanedIndecesOfBondConnectedAtomsList.add(indecesOfBondConnectedAtomsList.get(i));
}
}
boolean[] containsEndChainCarbon = new boolean[cleanedCreatedFragments.size()];
for (int i = 0; i < cleanedCreatedFragments.size(); i++) {
for (int ii = 0; ii < endChainCarbonIndexes.length; ii++) {
if (cleanedCreatedFragments.get(i).getAtomsFastBitArray().get(endChainCarbonIndexes[ii]))
containsEndChainCarbon[i] = true;
}
}
int fragmentsWithEndChainCarbonCount = 0;
for (boolean fragmentsWithEndChainCarbon : containsEndChainCarbon) if (fragmentsWithEndChainCarbon)
fragmentsWithEndChainCarbonCount++;
if (fragmentsWithEndChainCarbonCount == 0) {
System.err.println("Error: Problem occured. Check input molecule. No PFAS left after split.");
return null;
}
for (int i = 0; i < containsEndChainCarbon.length; i++) if (!containsEndChainCarbon[i])
return new FragmentPFAS(cleanedCreatedFragments, cleanedIndecesOfBondConnectedAtomsList, containsEndChainCarbon, bondsBroken, this.pfasStructure);
return null;
}
use of de.ipbhalle.metfraglib.fragment.AbstractTopDownBitArrayFragment in project MetFragRelaunched by ipb-halle.
the class TopDownFragmenter method createRingBondCleavedFragments.
/**
* @param newGeneratedTopDownFragments
* @param precursorFragment
* @param toProcess
* @param ringBondFastBitArray
* @param lastCuttedRingBond
* @return
*/
protected ArrayList<AbstractTopDownBitArrayFragment> createRingBondCleavedFragments(ArrayList<AbstractTopDownBitArrayFragment> newGeneratedTopDownFragments, AbstractTopDownBitArrayFragment precursorFragment, java.util.Queue<AbstractTopDownBitArrayFragment> toProcess, FastBitArray ringBondFastBitArray, java.util.Queue<Short> lastCuttedRingBond) {
/*
* process all fragments that have been cutted in a ring without generating
* a new one
*/
while (!toProcess.isEmpty() && lastCuttedRingBond.size() != 0) {
/*
*
*/
AbstractTopDownBitArrayFragment currentFragment = toProcess.poll();
short nextRingBondToCut = (short) (lastCuttedRingBond.poll() + 1);
/*
*
*/
for (short currentBond = nextRingBondToCut; currentBond < ringBondFastBitArray.getSize(); currentBond++) {
if (!ringBondFastBitArray.get(currentBond))
continue;
if (currentFragment.getBrokenBondsFastBitArray().get(currentBond))
continue;
AbstractTopDownBitArrayFragment[] newFragments = { currentFragment };
short[] connectedAtomIndeces = ((BitArrayPrecursor) this.scoredCandidate.getPrecursorMolecule()).getConnectedAtomIndecesOfBondIndex((short) currentBond);
newFragments = currentFragment.traverseMolecule(this.scoredCandidate.getPrecursorMolecule(), (short) currentBond, connectedAtomIndeces);
//
// pre-processing of the generated fragment/s
//
this.processGeneratedFragments(newFragments);
//
if (newFragments.length == 2) {
newFragments[0].setAsValidFragment();
newFragments[1].setAsValidFragment();
newFragments[0].setLastSkippedBond(currentFragment.getLastSkippedBond());
newFragments[1].setLastSkippedBond(currentFragment.getLastSkippedBond());
}
//
for (int k = 0; k < newFragments.length; k++) {
/*
if(precursorFragment.isValidFragment())
newFragments[k].setPrecursorFragment(precursorFragment);
else
newFragments[k].setPrecursorFragment(precursorFragment.hasPrecursorFragment() ? precursorFragment.getPrecursorFragment(): precursorFragment);
*/
if (newFragments.length == 2) {
newGeneratedTopDownFragments.add(newFragments[k]);
}
}
if (newFragments.length == 1) {
if (newFragments[0].getAddedToQueueCounts() < this.maximumNumberOfAFragmentAddedToQueue) {
toProcess.add(newFragments[0]);
lastCuttedRingBond.add(currentBond);
} else {
newGeneratedTopDownFragments.add(newFragments[0]);
}
}
}
}
return newGeneratedTopDownFragments;
}
Aggregations