Search in sources :

Example 1 with BitArrayPrecursor

use of de.ipbhalle.metfraglib.precursor.BitArrayPrecursor in project MetFragRelaunched by ipb-halle.

the class FragmentListWriterSDF method writeSingleMatchList.

private boolean writeSingleMatchList(ICandidate candidate, MatchList matchList, String completeFilePath) {
    IAtomContainerSet set = new AtomContainerSet();
    for (int i = 0; i < matchList.getNumberElements(); i++) {
        IAtomContainer fragmentAtomContainer = matchList.getElement(i).getBestMatchedFragment().getStructureAsIAtomContainer(candidate.getPrecursorMolecule());
        fragmentAtomContainer.setProperty("MolecularFormula", matchList.getElement(i).getModifiedFormulaStringOfBestMatchedFragment(candidate.getPrecursorMolecule()).toString());
        String brokenBonds = "";
        int[] brokenBondIndeces = matchList.getElement(i).getBestMatchedFragment().getBrokenBondIndeces();
        String bondStrings = "";
        for (int index : brokenBondIndeces) {
            brokenBonds += index + ";";
            bondStrings += ((BitArrayPrecursor) candidate.getPrecursorMolecule()).getBondAsString((short) index) + ";";
        }
        if (bondStrings.length() != 0)
            bondStrings = bondStrings.substring(0, bondStrings.length() - 1);
        if (brokenBonds.length() != 0)
            brokenBonds = brokenBonds.substring(0, brokenBonds.length() - 1);
        fragmentAtomContainer.setProperty("BrokenBondsStrings", bondStrings);
        fragmentAtomContainer.setProperty("BrokenBonds", brokenBonds);
        fragmentAtomContainer.setProperty("Match", matchList.getElement(i).toString());
        set.addAtomContainer(fragmentAtomContainer);
    }
    try {
        SDFWriter sdfWriter = new SDFWriter(new FileWriter(new File(completeFilePath)));
        sdfWriter.write(set);
        sdfWriter.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (CDKException e) {
        e.printStackTrace();
        return false;
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) CDKException(org.openscience.cdk.exception.CDKException) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) FileWriter(java.io.FileWriter) SDFWriter(org.openscience.cdk.io.SDFWriter) IOException(java.io.IOException) File(java.io.File) AtomContainerSet(org.openscience.cdk.AtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) BitArrayPrecursor(de.ipbhalle.metfraglib.precursor.BitArrayPrecursor)

Example 2 with BitArrayPrecursor

use of de.ipbhalle.metfraglib.precursor.BitArrayPrecursor 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;
}
Also used : AbstractTopDownBitArrayFragment(de.ipbhalle.metfraglib.fragment.AbstractTopDownBitArrayFragment) ArrayList(java.util.ArrayList) AbstractTopDownBitArrayPrecursor(de.ipbhalle.metfraglib.precursor.AbstractTopDownBitArrayPrecursor) BitArrayPrecursor(de.ipbhalle.metfraglib.precursor.BitArrayPrecursor) FastBitArray(de.ipbhalle.metfraglib.FastBitArray)

Example 3 with BitArrayPrecursor

use of de.ipbhalle.metfraglib.precursor.BitArrayPrecursor in project MetFragRelaunched by ipb-halle.

the class DefaultBitArrayFragment method isConnected.

public boolean isConnected(IMolecularStructure precursorMolecule) {
    if (this.atomsFastBitArray.cardinality() == 1)
        return true;
    BitArrayPrecursor pre = (BitArrayPrecursor) precursorMolecule;
    FastBitArray foundAtoms = new FastBitArray(this.atomsFastBitArray.getSize(), false);
    java.util.LinkedList<Integer> toCheck = new java.util.LinkedList<Integer>();
    toCheck.add(this.atomsFastBitArray.getFirstSetBit());
    while (toCheck.size() != 0) {
        int currentAtomIndex = toCheck.poll();
        short[] neighbors = pre.getConnectedAtomIndecesOfAtomIndex((short) currentAtomIndex);
        for (int k = 0; k < neighbors.length; k++) {
            if (this.atomsFastBitArray.get(neighbors[k]) && !foundAtoms.get(neighbors[k])) {
                foundAtoms.set(neighbors[k]);
                toCheck.add((int) neighbors[k]);
            }
        }
    }
    if (foundAtoms.equals(this.atomsFastBitArray))
        return true;
    return false;
}
Also used : FastBitArray(de.ipbhalle.metfraglib.FastBitArray) BitArrayPrecursor(de.ipbhalle.metfraglib.precursor.BitArrayPrecursor)

Example 4 with BitArrayPrecursor

use of de.ipbhalle.metfraglib.precursor.BitArrayPrecursor 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;
}
Also used : AbstractTopDownBitArrayFragment(de.ipbhalle.metfraglib.fragment.AbstractTopDownBitArrayFragment) AbstractTopDownBitArrayPrecursor(de.ipbhalle.metfraglib.precursor.AbstractTopDownBitArrayPrecursor) BitArrayPrecursor(de.ipbhalle.metfraglib.precursor.BitArrayPrecursor)

Example 5 with BitArrayPrecursor

use of de.ipbhalle.metfraglib.precursor.BitArrayPrecursor 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)

Aggregations

BitArrayPrecursor (de.ipbhalle.metfraglib.precursor.BitArrayPrecursor)8 FastBitArray (de.ipbhalle.metfraglib.FastBitArray)4 AbstractTopDownBitArrayFragment (de.ipbhalle.metfraglib.fragment.AbstractTopDownBitArrayFragment)4 AbstractTopDownBitArrayPrecursor (de.ipbhalle.metfraglib.precursor.AbstractTopDownBitArrayPrecursor)4 ArrayList (java.util.ArrayList)2 FragmentList (de.ipbhalle.metfraglib.list.FragmentList)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 AtomContainerSet (org.openscience.cdk.AtomContainerSet)1 CDKException (org.openscience.cdk.exception.CDKException)1 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)1 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)1 SDFWriter (org.openscience.cdk.io.SDFWriter)1