Search in sources :

Example 1 with IList

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

the class CandidateListWriterExtendedFragmentsXLS method writeFile.

public boolean writeFile(File xlsFile, IList list, Settings settings) throws Exception {
    CandidateList candidateList = null;
    int numberOfPeaksUsed = 0;
    if (list instanceof ScoredCandidateList || list instanceof SortedScoredCandidateList) {
        candidateList = (ScoredCandidateList) list;
        numberOfPeaksUsed = ((ScoredCandidateList) list).getNumberPeaksUsed();
    }
    if (list instanceof CandidateList) {
        candidateList = (CandidateList) list;
    }
    if (candidateList == null)
        return false;
    for (int i = 0; i < candidateList.getNumberElements(); i++) {
        int countExplainedPeaks = 0;
        ICandidate scoredCandidate = candidateList.getElement(i);
        if (settings != null)
            scoredCandidate.setUseSmiles((Boolean) settings.get(VariableNames.USE_SMILES_NAME));
        scoredCandidate.initialisePrecursorCandidate();
        if (scoredCandidate.getMatchList() != null) {
            MatchList matchList = scoredCandidate.getMatchList();
            for (int l = 0; l < matchList.getNumberElements(); l++) {
                try {
                    matchList.getElement(l).getMatchedPeak().getIntensity();
                } catch (RelativeIntensityNotDefinedException e1) {
                    continue;
                }
                countExplainedPeaks++;
            }
        }
        String peaksExplained = "";
        String sumFormulasOfFragmentsExplainedPeaks = "";
        if (scoredCandidate.getMatchList() != null) {
            for (int ii = 0; ii < scoredCandidate.getMatchList().getNumberElements(); ii++) {
                try {
                    double intensity = scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getIntensity();
                    peaksExplained += scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass() + "_" + intensity + ";";
                } catch (RelativeIntensityNotDefinedException e1) {
                    continue;
                }
                String formula = scoredCandidate.getMatchList().getElement(ii).getModifiedFormulaStringOfBestMatchedFragment(scoredCandidate.getPrecursorMolecule());
                sumFormulasOfFragmentsExplainedPeaks += scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass() + ":" + formula + ";";
            }
            if (sumFormulasOfFragmentsExplainedPeaks.length() != 0)
                sumFormulasOfFragmentsExplainedPeaks = sumFormulasOfFragmentsExplainedPeaks.substring(0, sumFormulasOfFragmentsExplainedPeaks.length() - 1);
            if (peaksExplained.length() != 0)
                peaksExplained = peaksExplained.substring(0, peaksExplained.length() - 1);
            if (peaksExplained.length() == 0)
                peaksExplained = "NA";
            if (sumFormulasOfFragmentsExplainedPeaks.length() == 0)
                sumFormulasOfFragmentsExplainedPeaks = "NA";
            scoredCandidate.setProperty("ExplPeaks", peaksExplained);
            scoredCandidate.setProperty("FormulasOfExplPeaks", sumFormulasOfFragmentsExplainedPeaks);
            scoredCandidate.setProperty("NumberPeaksUsed", numberOfPeaksUsed);
            scoredCandidate.setProperty("NoExplPeaks", countExplainedPeaks);
        }
    }
    boolean withImages = true;
    boolean withFragments = true;
    xlsFile.createNewFile();
    WritableWorkbook workbook = Workbook.createWorkbook(xlsFile);
    WritableSheet sheet1 = workbook.createSheet("MetFrag result list", 0);
    WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10);
    WritableCellFormat arial10format = new WritableCellFormat(arial10font);
    arial10font.setBoldStyle(WritableFont.BOLD);
    int numberCells = 0;
    java.util.Map<String, Integer> labels = new java.util.HashMap<String, Integer>();
    int columnWidthAdd = withImages || withFragments ? 3 : 0;
    int rowHeightAdd = withImages || withFragments ? 9 : 1;
    List<RenderedImage> molImages = null;
    List<List<RenderedImage>> molFragmentImages = null;
    if (withImages) {
        molImages = convertMoleculesToImages(candidateList);
        for (int i = 0; i < molImages.size(); i++) {
            File imageFile = File.createTempFile("file" + i, ".png", new File(Constants.OS_TEMP_DIR));
            imageFile.deleteOnExit();
            if (ImageIO.write(molImages.get(i), "png", imageFile)) {
                WritableImage wi = new WritableImage(0, (i * rowHeightAdd) + 1, columnWidthAdd, rowHeightAdd, imageFile);
                sheet1.addImage(wi);
            }
        }
    }
    if (withFragments) {
        molFragmentImages = convertMoleculesAndFragmentToImages(candidateList);
        WritableSheet sheet2 = workbook.createSheet("MetFrag fragment lists", 1);
        sheet2.addCell(new Label(0, 0, "Precursor", arial10format));
        sheet2.addCell(new Label(4, 0, "Identifier", arial10format));
        sheet2.addCell(new Label(5, 0, "Fragments", arial10format));
        for (int i = 0; i < molFragmentImages.size(); i++) {
            File imageFile = File.createTempFile("file" + i, ".png", new File(Constants.OS_TEMP_DIR));
            imageFile.deleteOnExit();
            if (ImageIO.write(molFragmentImages.get(i).get(0), "png", imageFile)) {
                WritableImage wi = new WritableImage(0, (i * rowHeightAdd) + 1, columnWidthAdd, rowHeightAdd, imageFile);
                sheet2.addImage(wi);
            }
            for (int k = molFragmentImages.get(i).size() - 1; k >= 1; k--) {
                File fragmentimageFile = File.createTempFile("file_" + i + "_" + k, ".png", new File(Constants.OS_TEMP_DIR));
                fragmentimageFile.deleteOnExit();
                if (ImageIO.write(molFragmentImages.get(i).get(k), "png", fragmentimageFile)) {
                    WritableImage wi = new WritableImage(((molFragmentImages.get(i).size() - k) * columnWidthAdd) + 2, (i * rowHeightAdd) + 1, columnWidthAdd, rowHeightAdd, fragmentimageFile);
                    sheet2.addImage(wi);
                }
            }
            sheet2.addCell(new Label(4, (i * rowHeightAdd) + 1, ((String) candidateList.getElement(i).getIdentifier()).replaceAll("\\|[0-9]+", "")));
        }
    }
    for (int i = 0; i < candidateList.getNumberElements(); i++) {
        java.util.Hashtable<String, Object> properties = candidateList.getElement(i).getProperties();
        Iterator<String> propNames = properties.keySet().iterator();
        while (propNames.hasNext()) {
            String propName = (String) propNames.next();
            if (!labels.containsKey(propName)) {
                labels.put(propName, Integer.valueOf(numberCells));
                sheet1.addCell(new Label(labels.get(propName) + columnWidthAdd, 0, propName, arial10format));
                numberCells++;
            }
            sheet1.addCell(new Label(labels.get(propName) + columnWidthAdd, (i * rowHeightAdd) + 1, String.valueOf(checkEmptyProperty(properties.get(propName)))));
        }
    }
    workbook.write();
    workbook.close();
    return true;
}
Also used : Label(jxl.write.Label) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) MatchList(de.ipbhalle.metfraglib.list.MatchList) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) ArrayList(java.util.ArrayList) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) List(java.util.List) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) IList(de.ipbhalle.metfraglib.interfaces.IList) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) MatchList(de.ipbhalle.metfraglib.list.MatchList) WritableFont(jxl.write.WritableFont) WritableSheet(jxl.write.WritableSheet) RelativeIntensityNotDefinedException(de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException) WritableCellFormat(jxl.write.WritableCellFormat) WritableWorkbook(jxl.write.WritableWorkbook) WritableImage(jxl.write.WritableImage) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) RenderedImage(java.awt.image.RenderedImage) File(java.io.File)

Example 2 with IList

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

the class CandidateListWriterExtendedXLS method writeFile.

public boolean writeFile(File xlsFile, IList list, Settings settings) throws Exception {
    CandidateList candidateList = null;
    int numberOfPeaksUsed = 0;
    if (list instanceof ScoredCandidateList || list instanceof SortedScoredCandidateList) {
        candidateList = (ScoredCandidateList) list;
        numberOfPeaksUsed = ((ScoredCandidateList) list).getNumberPeaksUsed();
    }
    if (list instanceof CandidateList) {
        candidateList = (CandidateList) list;
    }
    if (candidateList == null)
        return false;
    for (int i = 0; i < candidateList.getNumberElements(); i++) {
        int countExplainedPeaks = 0;
        ICandidate scoredCandidate = candidateList.getElement(i);
        if (settings != null)
            scoredCandidate.setUseSmiles((Boolean) settings.get(VariableNames.USE_SMILES_NAME));
        scoredCandidate.initialisePrecursorCandidate();
        if (scoredCandidate.getMatchList() != null) {
            MatchList matchList = scoredCandidate.getMatchList();
            for (int l = 0; l < matchList.getNumberElements(); l++) {
                try {
                    matchList.getElement(l).getMatchedPeak().getIntensity();
                } catch (RelativeIntensityNotDefinedException e1) {
                    continue;
                }
                countExplainedPeaks++;
            }
        }
        String peaksExplained = "";
        String sumFormulasOfFragmentsExplainedPeaks = "";
        if (scoredCandidate.getMatchList() != null) {
            for (int ii = 0; ii < scoredCandidate.getMatchList().getNumberElements(); ii++) {
                try {
                    double intensity = scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getIntensity();
                    peaksExplained += scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass() + "_" + intensity + ";";
                } catch (RelativeIntensityNotDefinedException e1) {
                    continue;
                }
                String formula = scoredCandidate.getMatchList().getElement(ii).getModifiedFormulaStringOfBestMatchedFragment(scoredCandidate.getPrecursorMolecule());
                sumFormulasOfFragmentsExplainedPeaks += scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass() + ":" + formula + ";";
            }
            if (sumFormulasOfFragmentsExplainedPeaks.length() != 0)
                sumFormulasOfFragmentsExplainedPeaks = sumFormulasOfFragmentsExplainedPeaks.substring(0, sumFormulasOfFragmentsExplainedPeaks.length() - 1);
            if (peaksExplained.length() != 0)
                peaksExplained = peaksExplained.substring(0, peaksExplained.length() - 1);
            if (peaksExplained.length() == 0)
                peaksExplained = "NA";
            if (sumFormulasOfFragmentsExplainedPeaks.length() == 0)
                sumFormulasOfFragmentsExplainedPeaks = "NA";
            scoredCandidate.setProperty("ExplPeaks", peaksExplained);
            scoredCandidate.setProperty("FormulasOfExplPeaks", sumFormulasOfFragmentsExplainedPeaks);
            scoredCandidate.setProperty("NumberPeaksUsed", numberOfPeaksUsed);
            scoredCandidate.setProperty("NoExplPeaks", countExplainedPeaks);
        }
    }
    boolean withImages = true;
    boolean withFragments = false;
    xlsFile.createNewFile();
    WritableWorkbook workbook = Workbook.createWorkbook(xlsFile);
    WritableSheet sheet1 = workbook.createSheet("MetFrag result list", 0);
    WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10);
    WritableCellFormat arial10format = new WritableCellFormat(arial10font);
    arial10font.setBoldStyle(WritableFont.BOLD);
    int numberCells = 0;
    java.util.Map<String, Integer> labels = new java.util.HashMap<String, Integer>();
    int columnWidthAdd = withImages || withFragments ? 3 : 0;
    int rowHeightAdd = withImages || withFragments ? 9 : 1;
    List<RenderedImage> molImages = null;
    List<List<RenderedImage>> molFragmentImages = null;
    if (withImages) {
        molImages = convertMoleculesToImages(candidateList);
        for (int i = 0; i < molImages.size(); i++) {
            // File imageFile = new File(resultspath + fileSep + fileName+
            // "_" +i+".png");
            File imageFile = File.createTempFile("file" + i, ".png", new File(Constants.OS_TEMP_DIR));
            imageFile.deleteOnExit();
            if (ImageIO.write(molImages.get(i), "png", imageFile)) {
                WritableImage wi = new WritableImage(0, (i * rowHeightAdd) + 1, columnWidthAdd, rowHeightAdd, imageFile);
                sheet1.addImage(wi);
            }
        }
    } else if (withFragments) {
        molFragmentImages = convertMoleculesAndFragmentToImages(candidateList);
        WritableSheet sheet2 = workbook.createSheet("MetFrag fragment lists", 1);
        for (int i = 0; i < molFragmentImages.size(); i++) {
            File imageFile = File.createTempFile("file" + i, ".png", new File(Constants.OS_TEMP_DIR));
            imageFile.deleteOnExit();
            if (ImageIO.write(molFragmentImages.get(i).get(0), "png", imageFile)) {
                WritableImage wi = new WritableImage(0, (i * rowHeightAdd) + 1, columnWidthAdd, rowHeightAdd, imageFile);
                sheet2.addImage(wi);
            }
            for (int k = 1; k < molFragmentImages.get(i).size(); k++) {
                File fragmentimageFile = File.createTempFile("file_" + i + "_" + k, ".png", new File(Constants.OS_TEMP_DIR));
                fragmentimageFile.deleteOnExit();
                if (ImageIO.write(molFragmentImages.get(i).get(k), "png", imageFile)) {
                    WritableImage wi = new WritableImage(0, (i * rowHeightAdd) + 1, (k * columnWidthAdd) + 1, rowHeightAdd, fragmentimageFile);
                    sheet2.addImage(wi);
                }
            }
        }
    }
    for (int i = 0; i < candidateList.getNumberElements(); i++) {
        java.util.Hashtable<String, Object> properties = candidateList.getElement(i).getProperties();
        Iterator<String> propNames = properties.keySet().iterator();
        while (propNames.hasNext()) {
            String propName = (String) propNames.next();
            if (!labels.containsKey(propName)) {
                labels.put(propName, Integer.valueOf(numberCells));
                sheet1.addCell(new Label(labels.get(propName) + columnWidthAdd, 0, propName, arial10format));
                numberCells++;
            }
            sheet1.addCell(new Label(labels.get(propName) + columnWidthAdd, (i * rowHeightAdd) + 1, String.valueOf(properties.get(propName))));
        }
    }
    workbook.write();
    workbook.close();
    return true;
}
Also used : Label(jxl.write.Label) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) MatchList(de.ipbhalle.metfraglib.list.MatchList) ArrayList(java.util.ArrayList) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) IList(de.ipbhalle.metfraglib.interfaces.IList) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) List(java.util.List) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) MatchList(de.ipbhalle.metfraglib.list.MatchList) WritableFont(jxl.write.WritableFont) WritableSheet(jxl.write.WritableSheet) RelativeIntensityNotDefinedException(de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException) WritableCellFormat(jxl.write.WritableCellFormat) WritableWorkbook(jxl.write.WritableWorkbook) WritableImage(jxl.write.WritableImage) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) RenderedImage(java.awt.image.RenderedImage) File(java.io.File)

Aggregations

RelativeIntensityNotDefinedException (de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException)2 ICandidate (de.ipbhalle.metfraglib.interfaces.ICandidate)2 IList (de.ipbhalle.metfraglib.interfaces.IList)2 CandidateList (de.ipbhalle.metfraglib.list.CandidateList)2 MatchList (de.ipbhalle.metfraglib.list.MatchList)2 ScoredCandidateList (de.ipbhalle.metfraglib.list.ScoredCandidateList)2 SortedScoredCandidateList (de.ipbhalle.metfraglib.list.SortedScoredCandidateList)2 RenderedImage (java.awt.image.RenderedImage)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Label (jxl.write.Label)2 WritableCellFormat (jxl.write.WritableCellFormat)2 WritableFont (jxl.write.WritableFont)2 WritableImage (jxl.write.WritableImage)2 WritableSheet (jxl.write.WritableSheet)2 WritableWorkbook (jxl.write.WritableWorkbook)2