Search in sources :

Example 1 with Fragment

use of de.ipbhalle.metfragweb.datatype.Fragment in project MetFragRelaunched by ipb-halle.

the class MetFragWebBean method generateFragmentImages.

protected void generateFragmentImages(MetFragResult molecule) throws IOException {
    // first remove last generated fragments
    java.io.File imageFolderFragments = new java.io.File(this.getRootSessionFolder() + Constants.OS_SPECIFIC_FILE_SEPARATOR + "images/fragments");
    if (imageFolderFragments.exists())
        FileUtils.deleteDirectory(imageFolderFragments);
    imageFolderFragments.mkdirs();
    // start creating the fragments
    this.currentFragments = new java.util.Vector<Fragment>();
    String sessionId = this.getSessionId();
    ICandidate candidate = molecule.getRoot().getCandidate();
    try {
        candidate.initialisePrecursorCandidate();
    } catch (Exception e1) {
        System.err.println("error when initialising precursor for fragment generation");
        e1.printStackTrace();
        return;
    }
    for (int i = 0; i < molecule.getMatchList().getNumberElements(); i++) {
        try {
            HighlightSubStructureImageGenerator imageGenerator = new HighlightSubStructureImageGenerator();
            int size = 300;
            if (Double.parseDouble(this.beanSettingsContainer.getNeutralMonoisotopicMass()) > 500)
                size = 400;
            if (Double.parseDouble(this.beanSettingsContainer.getNeutralMonoisotopicMass()) > 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(candidate.getPrecursorMolecule(), molecule.getMatchList().getElement(i).getBestMatchedFragment());
                ImageIO.write(image, "png", imageFile);
            } catch (Exception e) {
                System.err.println("error generating fragment image");
            }
            FragmentMassToPeakMatch match = (FragmentMassToPeakMatch) molecule.getMatchList().getElement(i);
            match.getBestMatchedFragment();
            this.currentFragments.add(new Fragment(match.getModifiedFormulaHtmlStringOfBestMatchedFragment(candidate.getPrecursorMolecule()), MathTools.round(match.getBestMatchFragmentMass()), "/files/" + sessionId + "/images/fragments/fragment_" + i + ".png", match.getMatchedPeak().getMass(), this.currentFragments.size() + 1));
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("error generating fragment image for " + molecule.getIdentifier() + " " + i);
            continue;
        }
    }
    try {
        candidate.resetPrecursorMolecule();
    } catch (Exception e1) {
        System.err.println("error when initialising precursor for fragment generation");
        e1.printStackTrace();
        return;
    }
}
Also used : Fragment(de.ipbhalle.metfragweb.datatype.Fragment) FragmentMassToPeakMatch(de.ipbhalle.metfraglib.match.FragmentMassToPeakMatch) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate) AtomTypeNotKnownFromInputListException(de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) RenderedImage(java.awt.image.RenderedImage) HighlightSubStructureImageGenerator(de.ipbhalle.metfraglib.imagegenerator.HighlightSubStructureImageGenerator)

Aggregations

AtomTypeNotKnownFromInputListException (de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException)1 HighlightSubStructureImageGenerator (de.ipbhalle.metfraglib.imagegenerator.HighlightSubStructureImageGenerator)1 ICandidate (de.ipbhalle.metfraglib.interfaces.ICandidate)1 FragmentMassToPeakMatch (de.ipbhalle.metfraglib.match.FragmentMassToPeakMatch)1 Fragment (de.ipbhalle.metfragweb.datatype.Fragment)1 RenderedImage (java.awt.image.RenderedImage)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1