Search in sources :

Example 1 with FastBitArray

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

the class CalculateHydrogenDeuteriumScore method getEqualPeakPairs.

/**
 * @param massesHydrogen
 * @param massesDeuterium
 * @param ppm
 * @param abs
 * @return
 */
public static int[][] getEqualPeakPairs(double[] massesHydrogen, double[] massesDeuterium, double ppm, double abs) {
    ArrayList<Integer> equalPeakPairHydrogenIndeces = new ArrayList<Integer>();
    ArrayList<Integer> equalPeakPairDeuteriumIndeces = new ArrayList<Integer>();
    FastBitArray hydrogensAnnotated = new FastBitArray(massesHydrogen.length);
    FastBitArray deuteriumsAnnotated = new FastBitArray(massesDeuterium.length);
    double[] absDeviationHydrogens = new double[massesHydrogen.length];
    double[] absDeviationDeuterium = new double[massesDeuterium.length];
    for (int i = 0; i < massesHydrogen.length; i++) {
        double mzabs = MathTools.calculateAbsoluteDeviation(massesHydrogen[i], ppm) + abs;
        for (int j = 0; j < massesDeuterium.length; j++) {
            double deviation = Math.abs(massesHydrogen[i] - massesDeuterium[j]);
            if (deviation <= mzabs) {
                if (hydrogensAnnotated.get(i) && deuteriumsAnnotated.get(j))
                    continue;
                else if (hydrogensAnnotated.get(i)) {
                    if (deviation < absDeviationHydrogens[i]) {
                        int index = equalPeakPairHydrogenIndeces.indexOf(i);
                        equalPeakPairHydrogenIndeces.remove(index);
                        equalPeakPairDeuteriumIndeces.remove(index);
                        equalPeakPairHydrogenIndeces.add(i);
                        equalPeakPairDeuteriumIndeces.add(j);
                        absDeviationHydrogens[i] = deviation;
                        absDeviationDeuterium[j] = deviation;
                    }
                } else if (deuteriumsAnnotated.get(j)) {
                    if (deviation < absDeviationDeuterium[j]) {
                        int index = equalPeakPairDeuteriumIndeces.indexOf(j);
                        equalPeakPairHydrogenIndeces.remove(index);
                        equalPeakPairDeuteriumIndeces.remove(index);
                        equalPeakPairHydrogenIndeces.add(i);
                        equalPeakPairDeuteriumIndeces.add(j);
                        absDeviationHydrogens[i] = deviation;
                        absDeviationDeuterium[j] = deviation;
                    }
                } else {
                    equalPeakPairHydrogenIndeces.add(i);
                    equalPeakPairDeuteriumIndeces.add(j);
                    absDeviationHydrogens[i] = deviation;
                    absDeviationDeuterium[j] = deviation;
                }
                hydrogensAnnotated.set(i);
                deuteriumsAnnotated.set(j);
            }
        }
    }
    int[][] pairedIndeces = new int[equalPeakPairHydrogenIndeces.size()][2];
    for (int i = 0; i < equalPeakPairHydrogenIndeces.size(); i++) {
        pairedIndeces[i][0] = equalPeakPairHydrogenIndeces.get(i);
        pairedIndeces[i][1] = equalPeakPairDeuteriumIndeces.get(i);
    }
    return pairedIndeces;
}
Also used : ArrayList(java.util.ArrayList) FastBitArray(de.ipbhalle.metfraglib.FastBitArray)

Example 2 with FastBitArray

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

the class InChIDeuteriumGeneration method getAromaticAtoms.

/**
 * @param molecule
 * @return
 */
public static FastBitArray getAromaticAtoms(IAtomContainer molecule) {
    Aromaticity arom = new Aromaticity(ElectronDonation.cdk(), Cycles.cdkAromaticSet());
    FastBitArray aromaticAtoms = new FastBitArray(molecule.getAtomCount());
    try {
        AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule);
        arom.apply(molecule);
        Set<IBond> aromaticBonds = arom.findBonds(molecule);
        Iterator<IBond> it = aromaticBonds.iterator();
        while (it.hasNext()) {
            IBond bond = it.next();
            for (int k = 0; k < bond.getAtomCount(); k++) aromaticAtoms.set(molecule.indexOf(bond.getAtom(k)));
        }
    } catch (CDKException e) {
        e.printStackTrace();
    }
    return aromaticAtoms;
}
Also used : Aromaticity(org.openscience.cdk.aromaticity.Aromaticity) CDKException(org.openscience.cdk.exception.CDKException) IBond(org.openscience.cdk.interfaces.IBond) FastBitArray(de.ipbhalle.metfraglib.FastBitArray)

Example 3 with FastBitArray

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

the class SDFDeuteriumGeneration method getAromaticAtoms.

/**
 * @param molecule
 * @return
 */
public static FastBitArray getAromaticAtoms(IAtomContainer molecule) {
    Aromaticity arom = new Aromaticity(ElectronDonation.cdk(), Cycles.cdkAromaticSet());
    FastBitArray aromaticAtoms = new FastBitArray(molecule.getAtomCount());
    try {
        AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule);
        arom.apply(molecule);
        Set<IBond> aromaticBonds = arom.findBonds(molecule);
        Iterator<IBond> it = aromaticBonds.iterator();
        while (it.hasNext()) {
            IBond bond = it.next();
            for (int k = 0; k < bond.getAtomCount(); k++) aromaticAtoms.set(molecule.indexOf(bond.getAtom(k)));
        }
    } catch (CDKException e) {
        e.printStackTrace();
    }
    return aromaticAtoms;
}
Also used : Aromaticity(org.openscience.cdk.aromaticity.Aromaticity) CDKException(org.openscience.cdk.exception.CDKException) IBond(org.openscience.cdk.interfaces.IBond) FastBitArray(de.ipbhalle.metfraglib.FastBitArray)

Example 4 with FastBitArray

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

the class SmilesDeuteriumGeneration method getAromaticAtoms.

/**
 * @param molecule
 * @return
 */
public static FastBitArray getAromaticAtoms(IAtomContainer molecule) {
    Aromaticity arom = new Aromaticity(ElectronDonation.cdk(), Cycles.cdkAromaticSet());
    FastBitArray aromaticAtoms = new FastBitArray(molecule.getAtomCount());
    try {
        AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule);
        arom.apply(molecule);
        Set<IBond> aromaticBonds = arom.findBonds(molecule);
        Iterator<IBond> it = aromaticBonds.iterator();
        while (it.hasNext()) {
            IBond bond = it.next();
            for (int k = 0; k < bond.getAtomCount(); k++) aromaticAtoms.set(molecule.indexOf(bond.getAtom(k)));
        }
    } catch (CDKException e) {
        e.printStackTrace();
    }
    return aromaticAtoms;
}
Also used : Aromaticity(org.openscience.cdk.aromaticity.Aromaticity) CDKException(org.openscience.cdk.exception.CDKException) IBond(org.openscience.cdk.interfaces.IBond) FastBitArray(de.ipbhalle.metfraglib.FastBitArray)

Example 5 with FastBitArray

use of de.ipbhalle.metfraglib.FastBitArray 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;
}
Also used : IMatch(de.ipbhalle.metfraglib.interfaces.IMatch) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) MassToFingerprintGroupListCollection(de.ipbhalle.metfraglib.substructure.MassToFingerprintGroupListCollection) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate) MassToFingerprintGroupList(de.ipbhalle.metfraglib.substructure.MassToFingerprintGroupList) FastBitArray(de.ipbhalle.metfraglib.FastBitArray) MassFingerprintMatch(de.ipbhalle.metfraglib.match.MassFingerprintMatch) Fingerprint(de.ipbhalle.metfraglib.fingerprint.Fingerprint) MatchList(de.ipbhalle.metfraglib.list.MatchList) CombinedSingleCandidateMetFragProcess(de.ipbhalle.metfraglib.process.CombinedSingleCandidateMetFragProcess) Fingerprint(de.ipbhalle.metfraglib.fingerprint.Fingerprint) MassToFingerprintsHashMap(de.ipbhalle.metfraglib.substructure.MassToFingerprintsHashMap) IFragment(de.ipbhalle.metfraglib.interfaces.IFragment)

Aggregations

FastBitArray (de.ipbhalle.metfraglib.FastBitArray)49 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)10 RenderedImage (java.awt.image.RenderedImage)8 ArrayList (java.util.ArrayList)8 Color (java.awt.Color)7 HighlightSubStructureImageGenerator (de.ipbhalle.metfraglib.imagegenerator.HighlightSubStructureImageGenerator)6 MassToFingerprintGroupList (de.ipbhalle.metfraglib.substructure.MassToFingerprintGroupList)6 MassToFingerprintGroupListCollection (de.ipbhalle.metfraglib.substructure.MassToFingerprintGroupListCollection)6 MassFingerprintMatch (de.ipbhalle.metfraglib.match.MassFingerprintMatch)5 CDKException (org.openscience.cdk.exception.CDKException)5 AbstractTopDownBitArrayFragment (de.ipbhalle.metfraglib.fragment.AbstractTopDownBitArrayFragment)4 ICandidate (de.ipbhalle.metfraglib.interfaces.ICandidate)4 Font (java.awt.Font)4 List (java.util.List)4 Fingerprint (de.ipbhalle.metfraglib.fingerprint.Fingerprint)3 CandidateList (de.ipbhalle.metfraglib.list.CandidateList)3 BitArrayPrecursor (de.ipbhalle.metfraglib.precursor.BitArrayPrecursor)3 FingerprintGroup (de.ipbhalle.metfraglib.substructure.FingerprintGroup)3 Aromaticity (org.openscience.cdk.aromaticity.Aromaticity)3 InvalidSmilesException (org.openscience.cdk.exception.InvalidSmilesException)3