Search in sources :

Example 1 with HighlightSubStructureImageGenerator

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

the class CompareSpectraD3JSBean method generateFragmentsForPeakList.

protected java.util.Vector<PeakFragmentImage> generateFragmentsForPeakList(String peaklist, String smiles, String subpath, String randomString) throws Exception {
    String inchi = MoleculeFunctions.getInChIFromSmiles(smiles);
    ICandidate precursorCandidate = new TopDownPrecursorCandidate(inchi, "1");
    precursorCandidate.initialisePrecursorCandidate();
    precursorCandidate.setProperty(VariableNames.MOLECULAR_FORMULA_NAME, inchi.split("/")[1]);
    precursorCandidate.setProperty(VariableNames.SMILES_NAME, smiles);
    precursorCandidate.initialisePrecursorCandidate();
    MetFragGlobalSettings settings = new MetFragGlobalSettings();
    double monoisotopicmass = precursorCandidate.getMolecularFormula().getMonoisotopicMass();
    settings.set(VariableNames.PRECURSOR_NEUTRAL_MASS_NAME, monoisotopicmass);
    settings.set(VariableNames.PEAK_LIST_STRING_NAME, peaklist);
    settings.set(VariableNames.PEAK_LIST_NAME, new FilteredStringTandemMassPeakListReader(settings).read());
    precursorCandidate.setUseSmiles(true);
    settings.set(VariableNames.CANDIDATE_NAME, precursorCandidate);
    settings.set(VariableNames.BOND_ENERGY_OBJECT_NAME, new BondEnergies());
    settings.set(VariableNames.RELATIVE_MASS_DEVIATION_NAME, Double.parseDouble(this.relativeMassDeviation));
    settings.set(VariableNames.ABSOLUTE_MASS_DEVIATION_NAME, Double.parseDouble(this.absoluteMassDeviation));
    AbstractFragmenterAssignerScorer fas = (AbstractFragmenterAssignerScorer) Class.forName((String) settings.get(VariableNames.METFRAG_ASSIGNER_SCORER_NAME)).getConstructor(Settings.class, ICandidate.class).newInstance(settings, precursorCandidate);
    fas.setCandidate(precursorCandidate);
    fas.initialise();
    fas.calculate();
    fas.assignInterimScoresResults();
    ICandidate scoredCandidate = fas.getCandidates()[0];
    // generate fragments
    java.io.File imageFolderFragments = new java.io.File(this.getRootSessionFolder() + Constants.OS_SPECIFIC_FILE_SEPARATOR + "comparespectra" + Constants.OS_SPECIFIC_FILE_SEPARATOR + "images" + Constants.OS_SPECIFIC_FILE_SEPARATOR + "fragments" + Constants.OS_SPECIFIC_FILE_SEPARATOR + subpath);
    if (imageFolderFragments.exists())
        FileUtils.deleteDirectory(imageFolderFragments);
    imageFolderFragments.mkdirs();
    String sessionId = this.getSessionId();
    java.util.Vector<PeakFragmentImage> imagePathVector = new java.util.Vector<PeakFragmentImage>();
    for (int i = 0; i < scoredCandidate.getMatchList().getNumberElements(); i++) {
        HighlightSubStructureImageGenerator imageGenerator = new HighlightSubStructureImageGenerator();
        int size = 300;
        if (monoisotopicmass > 500)
            size = 400;
        if (monoisotopicmass > 700)
            size = 500;
        imageGenerator.setImageHeight(size);
        imageGenerator.setImageWidth(size);
        RenderedImage image;
        java.io.File imageFile = new java.io.File(imageFolderFragments.getAbsolutePath() + Constants.OS_SPECIFIC_FILE_SEPARATOR + "fragment_" + i + ".png");
        try {
            image = imageGenerator.generateImage(scoredCandidate.getPrecursorMolecule(), scoredCandidate.getMatchList().getElement(i).getBestMatchedFragment());
            ImageIO.write(image, "png", imageFile);
        } catch (Exception e) {
            System.err.println("error generating fragment image");
        }
        imagePathVector.add(new PeakFragmentImage(scoredCandidate.getMatchList().getElement(i).getMatchedPeak().getMass(), this.getURL() + "/files/" + sessionId + "/comparespectra/images/fragments/" + subpath + "/fragment_" + i + ".png?dummy=" + randomString, MathTools.round(((FragmentMassToPeakMatch) scoredCandidate.getMatchList().getElement(i)).getBestMatchFragmentMass())));
    }
    return imagePathVector;
}
Also used : MetFragGlobalSettings(de.ipbhalle.metfraglib.settings.MetFragGlobalSettings) BondEnergies(de.ipbhalle.metfraglib.additionals.BondEnergies) TopDownPrecursorCandidate(de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate) CDKException(org.openscience.cdk.exception.CDKException) IOException(java.io.IOException) AtomTypeNotKnownFromInputListException(de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException) FilteredStringTandemMassPeakListReader(de.ipbhalle.metfraglib.peaklistreader.FilteredStringTandemMassPeakListReader) AbstractFragmenterAssignerScorer(de.ipbhalle.metfraglib.fragmenterassignerscorer.AbstractFragmenterAssignerScorer) RenderedImage(java.awt.image.RenderedImage) HighlightSubStructureImageGenerator(de.ipbhalle.metfraglib.imagegenerator.HighlightSubStructureImageGenerator)

Example 2 with HighlightSubStructureImageGenerator

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

the class PFAS method saveHighlightedBondsImage.

public void saveHighlightedBondsImage(List<Integer> bondIndexes, String filename) throws Exception {
    IAtomContainer con = this.pfasStructure.getImplicitHydrogenAtomContainer();
    HighlightSubStructureImageGenerator imageGen = new HighlightSubStructureImageGenerator();
    imageGen.setBackgroundColor(new Color(1.0f, 1.0f, 1.0f, 1.0f));
    FastBitArray atoms = new FastBitArray(con.getAtomCount());
    FastBitArray bonds = new FastBitArray(con.getBondCount());
    for (int i = 0; i < bondIndexes.size(); i++) bonds.set(bondIndexes.get(i));
    imageGen.setImageHeight(1500);
    imageGen.setImageWidth(1500);
    imageGen.setStrokeRation(1.2);
    RenderedImage img = imageGen.generateImage(atoms, bonds, con);
    ImageIO.write((RenderedImage) img, "PNG", new java.io.File(filename));
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) Color(java.awt.Color) FastBitArray(de.ipbhalle.metfraglib.FastBitArray) RenderedImage(java.awt.image.RenderedImage) HighlightSubStructureImageGenerator(de.ipbhalle.metfraglib.imagegenerator.HighlightSubStructureImageGenerator)

Example 3 with HighlightSubStructureImageGenerator

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

the class PFAS method findEndChainCarbons.

/**
 * @param endChainCarbonSm
 * @param debugFolder
 * @return
 * @throws Exception
 */
public int[] findEndChainCarbons(String endChainCarbonSm, String debugFolder) throws Exception {
    IAtomContainer con = this.pfasStructure.getImplicitHydrogenAtomContainer();
    List<List<Integer>> matchAtomIndeces = this.getMatchesBySmarts(endChainCarbonSm, con);
    // generate images with highlighted matches
    if (debugFolder != null) {
        System.out.println("Found " + matchAtomIndeces.size() + " match(es) using end chain SMARTS (pacs)");
        HighlightSubStructureImageGenerator s = new HighlightSubStructureImageGenerator(new Font("Verdana", Font.BOLD, 18));
        for (int i = 0; i < matchAtomIndeces.size(); i++) {
            FastBitArray bitArrayAtoms = this.generateAndSetBitString(con.getAtomCount(), matchAtomIndeces.get(i));
            FastBitArray bitArrayBonds = new FastBitArray(con.getBondCount());
            s.setHighlightColor(new Color(0x6495ED));
            s.setImageHeight(1500);
            s.setImageWidth(1500);
            s.setStrokeRation(1.2);
            RenderedImage img = s.generateImage(bitArrayAtoms, bitArrayBonds, con);
            ImageIO.write((RenderedImage) img, "PNG", new java.io.File(debugFolder + Constants.OS_SPECIFIC_FILE_SEPARATOR + "01a-pacs-match-" + (i + 1) + ".png"));
        }
    }
    int[] matches = new int[matchAtomIndeces.size()];
    for (int i = 0; i < matches.length; i++) matches[i] = this.findEndChainCarbonFromMatch(matchAtomIndeces.get(i), con);
    // count matches after filtering
    if (debugFolder != null) {
        int validMatches = 0;
        for (int i = 0; i < matches.length; i++) if (matches[i] != -1)
            validMatches++;
        System.out.println(validMatches + " match(es) remain after filtering");
    }
    // generate images with highlighted matches after filtering
    if (debugFolder != null) {
        HighlightSubStructureImageGenerator s = new HighlightSubStructureImageGenerator(new Font("Verdana", Font.BOLD, 18));
        for (int i = 0; i < matches.length; i++) {
            if (matches[i] == -1)
                continue;
            // store end chain carbon image
            FastBitArray bitArrayAtoms = this.generateAndSetBitString(con.getAtomCount(), new int[] { matches[i] });
            FastBitArray bitArrayBonds = new FastBitArray(con.getBondCount());
            s.setHighlightColor(new Color(0x6495ED));
            s.setImageHeight(1500);
            s.setImageWidth(1500);
            s.setStrokeRation(1.2);
            RenderedImage img = s.generateImage(bitArrayAtoms, bitArrayBonds, con);
            ImageIO.write((RenderedImage) img, "PNG", new java.io.File(debugFolder + Constants.OS_SPECIFIC_FILE_SEPARATOR + "01b-pfas-alpha-carbon-" + (i + 1) + ".png"));
        }
    }
    return matches;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) Color(java.awt.Color) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) FastBitArray(de.ipbhalle.metfraglib.FastBitArray) RenderedImage(java.awt.image.RenderedImage) HighlightSubStructureImageGenerator(de.ipbhalle.metfraglib.imagegenerator.HighlightSubStructureImageGenerator) Font(java.awt.Font)

Example 4 with HighlightSubStructureImageGenerator

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

the class PFAS method saveHighlightedAtomsImage.

public void saveHighlightedAtomsImage(List<Integer> toHighlightAtoms, String filename) throws Exception {
    IAtomContainer con = this.pfasStructure.getImplicitHydrogenAtomContainer();
    HighlightSubStructureImageGenerator imageGen = new HighlightSubStructureImageGenerator();
    imageGen.setBackgroundColor(new Color(1.0f, 1.0f, 1.0f, 1.0f));
    FastBitArray atoms = new FastBitArray(con.getAtomCount());
    FastBitArray bonds = new FastBitArray(con.getBondCount());
    for (int i = 0; i < toHighlightAtoms.size(); i++) atoms.set(toHighlightAtoms.get(i));
    imageGen.setImageHeight(1500);
    imageGen.setImageWidth(1500);
    imageGen.setStrokeRation(1.2);
    RenderedImage img = imageGen.generateImage(atoms, bonds, con);
    ImageIO.write((RenderedImage) img, "PNG", new java.io.File(filename));
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) Color(java.awt.Color) FastBitArray(de.ipbhalle.metfraglib.FastBitArray) RenderedImage(java.awt.image.RenderedImage) HighlightSubStructureImageGenerator(de.ipbhalle.metfraglib.imagegenerator.HighlightSubStructureImageGenerator)

Example 5 with HighlightSubStructureImageGenerator

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

the class PFAS method saveHighlightedBondsImage.

public void saveHighlightedBondsImage(int[] bondIndexes, String filename) throws Exception {
    IAtomContainer con = this.pfasStructure.getImplicitHydrogenAtomContainer();
    HighlightSubStructureImageGenerator imageGen = new HighlightSubStructureImageGenerator();
    imageGen.setBackgroundColor(new Color(1.0f, 1.0f, 1.0f, 1.0f));
    FastBitArray atoms = new FastBitArray(con.getAtomCount());
    FastBitArray bonds = new FastBitArray(con.getBondCount());
    for (int i = 0; i < bondIndexes.length; i++) atoms.set(bondIndexes[i]);
    imageGen.setImageHeight(1500);
    imageGen.setImageWidth(1500);
    imageGen.setStrokeRation(1.2);
    RenderedImage img = imageGen.generateImage(atoms, bonds, con);
    ImageIO.write((RenderedImage) img, "PNG", new java.io.File(filename));
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) Color(java.awt.Color) FastBitArray(de.ipbhalle.metfraglib.FastBitArray) RenderedImage(java.awt.image.RenderedImage) HighlightSubStructureImageGenerator(de.ipbhalle.metfraglib.imagegenerator.HighlightSubStructureImageGenerator)

Aggregations

HighlightSubStructureImageGenerator (de.ipbhalle.metfraglib.imagegenerator.HighlightSubStructureImageGenerator)8 RenderedImage (java.awt.image.RenderedImage)8 FastBitArray (de.ipbhalle.metfraglib.FastBitArray)6 Color (java.awt.Color)6 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)6 Font (java.awt.Font)3 List (java.util.List)3 AtomTypeNotKnownFromInputListException (de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException)2 ICandidate (de.ipbhalle.metfraglib.interfaces.ICandidate)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 BondEnergies (de.ipbhalle.metfraglib.additionals.BondEnergies)1 TopDownPrecursorCandidate (de.ipbhalle.metfraglib.candidate.TopDownPrecursorCandidate)1 AbstractFragmenterAssignerScorer (de.ipbhalle.metfraglib.fragmenterassignerscorer.AbstractFragmenterAssignerScorer)1 FragmentMassToPeakMatch (de.ipbhalle.metfraglib.match.FragmentMassToPeakMatch)1 FilteredStringTandemMassPeakListReader (de.ipbhalle.metfraglib.peaklistreader.FilteredStringTandemMassPeakListReader)1 MetFragGlobalSettings (de.ipbhalle.metfraglib.settings.MetFragGlobalSettings)1 Fragment (de.ipbhalle.metfragweb.datatype.Fragment)1 UnknownHostException (java.net.UnknownHostException)1