Search in sources :

Example 1 with ScoredCandidateList

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

the class CandidateListWriterXLS 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;
    java.util.ArrayList<Integer> correctIndeces = new java.util.ArrayList<Integer>();
    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));
        try {
            scoredCandidate.initialisePrecursorCandidate();
        } catch (Exception e) {
            continue;
        }
        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);
        }
        scoredCandidate.resetPrecursorMolecule();
        correctIndeces.add(i);
    }
    boolean withImages = false;
    xlsFile.createNewFile();
    WritableWorkbook workbook = Workbook.createWorkbook(xlsFile);
    WritableSheet sheet = workbook.createSheet("MetFrag result list", 0);
    WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10);
    WritableCellFormat arial10format = new WritableCellFormat(arial10font);
    try {
        arial10font.setBoldStyle(WritableFont.BOLD);
    } catch (WriteException e1) {
        System.out.println("Warning: Could not set WritableFont");
    }
    int numberCells = 0;
    java.util.Map<String, Integer> labels = new java.util.HashMap<String, Integer>();
    int columnWidthAdd = withImages ? 3 : 0;
    int rowHeightAdd = withImages ? 9 : 1;
    List<RenderedImage> molImages = 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);
                sheet.addImage(wi);
            }
        }
    }
    for (int i = 0; i < correctIndeces.size(); i++) {
        java.util.Hashtable<String, Object> properties = candidateList.getElement(correctIndeces.get(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));
                try {
                    sheet.addCell(new Label(labels.get(propName) + columnWidthAdd, 0, propName, arial10format));
                } catch (RowsExceededException e) {
                    e.printStackTrace();
                } catch (WriteException e) {
                    e.printStackTrace();
                }
                numberCells++;
            }
            try {
                String prop = String.valueOf(properties.get(propName));
                if (prop.trim().length() == 0)
                    prop = "NA";
                if (propName.equals(VariableNames.IDENTIFIER_NAME))
                    prop = prop.replaceAll("\\|[0-9]+", "");
                sheet.addCell(new Label(labels.get(propName) + columnWidthAdd, (i * rowHeightAdd) + 1, prop));
            } catch (RowsExceededException e) {
                e.printStackTrace();
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    }
    workbook.write();
    workbook.close();
    return true;
}
Also used : ArrayList(java.util.ArrayList) 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) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) WriteException(jxl.write.WriteException) MatchList(de.ipbhalle.metfraglib.list.MatchList) WritableFont(jxl.write.WritableFont) WritableSheet(jxl.write.WritableSheet) RelativeIntensityNotDefinedException(de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException) WritableCellFormat(jxl.write.WritableCellFormat) WriteException(jxl.write.WriteException) RelativeIntensityNotDefinedException(de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException) RowsExceededException(jxl.write.biff.RowsExceededException) RowsExceededException(jxl.write.biff.RowsExceededException) 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 ScoredCandidateList

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

the class CandidateListWriterZippedPSV method writeFile.

public boolean writeFile(File file, 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;
    if (candidateList == null || candidateList.getNumberElements() == 0) {
        writeDefaultHeader(file);
        return false;
    }
    String[] lines = new String[candidateList.getNumberElements()];
    String heading = "";
    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(VariableNames.EXPLAINED_PEAKS_COLUMN, peaksExplained);
            scoredCandidate.setProperty(VariableNames.FORMULAS_OF_PEAKS_EXPLAINED_COLUMN, sumFormulasOfFragmentsExplainedPeaks);
            scoredCandidate.setProperty(VariableNames.NUMBER_PEAKS_USED_COLUMN, numberOfPeaksUsed);
            scoredCandidate.setProperty(VariableNames.NUMBER_EXPLAINED_PEAKS_COLUMN, countExplainedPeaks);
        }
        java.util.Enumeration<String> keys = scoredCandidate.getProperties().keys();
        if (keys.hasMoreElements()) {
            String key = keys.nextElement();
            if (i == 0)
                heading += key;
            lines[i] = "" + checkEmptyProperty(scoredCandidate.getProperty(key));
        }
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            if (i == 0)
                heading += "|" + key;
            lines[i] += "|" + checkEmptyProperty(scoredCandidate.getProperty(key));
        }
    }
    java.io.BufferedWriter bwriter;
    bwriter = new java.io.BufferedWriter(new FileWriter(file));
    bwriter.write(heading);
    bwriter.newLine();
    for (int i = 0; i < lines.length; i++) {
        bwriter.write(lines[i]);
        bwriter.newLine();
    }
    bwriter.close();
    this.zipFile(file);
    file.delete();
    return true;
}
Also used : MatchList(de.ipbhalle.metfraglib.list.MatchList) FileWriter(java.io.FileWriter) RelativeIntensityNotDefinedException(de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList)

Example 3 with ScoredCandidateList

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

the class CandidateWriterXLS method writeFile.

@Override
public boolean writeFile(File xlsFile, IList list, Settings settings) throws Exception {
    CandidateList candidateList = null;
    boolean isScoredCandidate = false;
    if (list instanceof ScoredCandidateList || list instanceof SortedScoredCandidateList) {
        candidateList = (ScoredCandidateList) list;
        isScoredCandidate = true;
    }
    if (list instanceof CandidateList) {
        candidateList = (CandidateList) list;
    }
    if (candidateList == null || candidateList.getNumberElements() == 0)
        return false;
    ICandidate candidate = candidateList.getElement(0);
    if (settings != null)
        candidate.setUseSmiles((Boolean) settings.get(VariableNames.USE_SMILES_NAME));
    candidate.initialisePrecursorCandidate();
    xlsFile.createNewFile();
    WritableWorkbook workbook = Workbook.createWorkbook(xlsFile);
    WritableSheet sheet1 = workbook.createSheet("MetFrag Candidate Result", 0);
    WritableFont arial10fontBold = new WritableFont(WritableFont.ARIAL, 10);
    arial10fontBold.setBoldStyle(WritableFont.BOLD);
    WritableCellFormat arial10formatBold = new WritableCellFormat(arial10fontBold);
    WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10);
    arial10font.setBoldStyle(WritableFont.NO_BOLD);
    WritableCellFormat arial10format = new WritableCellFormat(arial10font);
    sheet1.addCell(new Label(0, 0, VariableNames.IDENTIFIER_NAME, arial10formatBold));
    sheet1.addCell(new Label(1, 0, candidate.getIdentifier().replaceAll("\\|[0-9]+", ""), arial10format));
    java.util.Hashtable<String, Object> properties = candidate.getProperties();
    java.util.Enumeration<?> keys = properties.keys();
    int propertyRow = 1;
    while (keys.hasMoreElements()) {
        String currentKey = (String) keys.nextElement();
        sheet1.addCell(new Label(0, propertyRow, currentKey, arial10formatBold));
        String value = "";
        Object obj = properties.get(currentKey);
        if (obj instanceof java.lang.Double)
            value = String.valueOf((Double) obj);
        else if (obj instanceof java.lang.Integer)
            value = String.valueOf((Integer) obj);
        else if (obj instanceof java.lang.String)
            value = (String) obj;
        sheet1.addCell(new Label(1, propertyRow, value, arial10format));
        propertyRow++;
    }
    java.util.List<RenderedImage> molFragmentImages = null;
    RenderedImage molImage = this.convertMoleculeToImages(candidate);
    File imageFile = File.createTempFile("file", ".png", new File(Constants.OS_TEMP_DIR));
    imageFile.deleteOnExit();
    int rowHeightImage = 10;
    int colWidthImage = 3;
    int fragmentColumns = 1;
    sheet1.addCell(new Label(0, propertyRow + 1, "Precursor", arial10formatBold));
    if (ImageIO.write(molImage, "png", imageFile)) {
        WritableImage wi = new WritableImage(0, propertyRow + 2, colWidthImage, rowHeightImage, imageFile);
        sheet1.addImage(wi);
    }
    if (isScoredCandidate) {
        molFragmentImages = this.convertMoleculeFragmentsToImages(candidate);
        if (molFragmentImages.size() > 0) {
            sheet1.addCell(new Label(0, (propertyRow + 4) + rowHeightImage, "Fragments", arial10formatBold));
        }
        int imagesWritten = 1;
        int fragmentRow = 0;
        for (int i = 0; i < molFragmentImages.size(); i++) {
            File imageFileFragment = File.createTempFile("file" + i, ".png", new File(Constants.OS_TEMP_DIR));
            imageFileFragment.deleteOnExit();
            if (ImageIO.write(molFragmentImages.get(i), "png", imageFileFragment)) {
                int column = (((imagesWritten - 1) % fragmentColumns)) * (colWidthImage + 3);
                if ((imagesWritten - 1) % fragmentColumns == 0)
                    fragmentRow++;
                WritableImage wi = new WritableImage(column, (propertyRow + 5) + (rowHeightImage * fragmentRow), colWidthImage, rowHeightImage, imageFileFragment);
                sheet1.addImage(wi);
                FragmentMassToPeakMatch match = (FragmentMassToPeakMatch) candidate.getMatchList().getElement(imagesWritten - 1);
                sheet1.addCell(new Label(column + colWidthImage, (propertyRow + 5) + (rowHeightImage * fragmentRow), "Fragment " + imagesWritten, arial10formatBold));
                sheet1.addCell(new Label(column + colWidthImage, (propertyRow + 5) + (rowHeightImage * fragmentRow) + 2, "Formula", arial10formatBold));
                sheet1.addCell(new Label(column + colWidthImage + 1, (propertyRow + 5) + (rowHeightImage * fragmentRow) + 2, match.getModifiedFormulaStringOfBestMatchedFragment(candidate.getPrecursorMolecule()), arial10format));
                sheet1.addCell(new Label(column + colWidthImage, (propertyRow + 5) + (rowHeightImage * fragmentRow) + 3, "Mass", arial10formatBold));
                sheet1.addCell(new Label(column + colWidthImage + 1, (propertyRow + 5) + (rowHeightImage * fragmentRow) + 3, String.valueOf(MathTools.round(match.getBestMatchFragmentMass())), arial10format));
                sheet1.addCell(new Label(column + colWidthImage, (propertyRow + 5) + (rowHeightImage * fragmentRow) + 4, "Peak m/z", arial10formatBold));
                sheet1.addCell(new Label(column + colWidthImage + 1, (propertyRow + 5) + (rowHeightImage * fragmentRow) + 4, String.valueOf(match.getMatchedPeak().getMass()), arial10format));
                imagesWritten++;
            }
        }
    }
    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) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) WritableFont(jxl.write.WritableFont) WritableSheet(jxl.write.WritableSheet) WritableCellFormat(jxl.write.WritableCellFormat) FragmentMassToPeakMatch(de.ipbhalle.metfraglib.match.FragmentMassToPeakMatch) WritableWorkbook(jxl.write.WritableWorkbook) WritableImage(jxl.write.WritableImage) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) RenderedImage(java.awt.image.RenderedImage) File(java.io.File)

Example 4 with ScoredCandidateList

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

the class HDCandidateListWriterPSV method writeFile.

public boolean writeFile(File file, 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(VariableNames.EXPLAINED_PEAKS_COLUMN, peaksExplained);
            scoredCandidate.setProperty(VariableNames.FORMULAS_OF_PEAKS_EXPLAINED_COLUMN, sumFormulasOfFragmentsExplainedPeaks);
            scoredCandidate.setProperty(VariableNames.NUMBER_PEAKS_USED_COLUMN, numberOfPeaksUsed);
            scoredCandidate.setProperty(VariableNames.NUMBER_EXPLAINED_PEAKS_COLUMN, countExplainedPeaks);
        }
    }
    java.util.Hashtable<String, java.util.ArrayList<ICandidate>> hdGroupedCandidates = new java.util.Hashtable<String, java.util.ArrayList<ICandidate>>();
    String[] lines = new String[candidateList.getNumberElements()];
    String heading = "";
    for (int i = 0; i < candidateList.getNumberElements(); i++) {
        ICandidate candidate = candidateList.getElement(i);
        if (hdGroupedCandidates.containsKey((String) candidate.getProperty(VariableNames.HD_GROUP_FLAG_NAME)))
            ((java.util.ArrayList<ICandidate>) hdGroupedCandidates.get((String) candidate.getProperty(VariableNames.HD_GROUP_FLAG_NAME))).add(candidate);
        else {
            java.util.ArrayList<ICandidate> vec = new java.util.ArrayList<ICandidate>();
            vec.add(candidate);
            hdGroupedCandidates.put((String) candidate.getProperty(VariableNames.HD_GROUP_FLAG_NAME), vec);
        }
        candidate.getProperties().remove(VariableNames.HD_GROUP_FLAG_NAME);
    }
    java.util.ArrayList<String> propertyNames = new java.util.ArrayList<String>();
    java.util.Iterator<?> it = (java.util.Iterator<?>) hdGroupedCandidates.keys();
    while (it.hasNext()) {
        String currentGroup = (String) it.next();
        java.util.ArrayList<ICandidate> vec = hdGroupedCandidates.get(currentGroup);
        int originalCandidate = -1;
        int maxPropertySize = 0;
        // find original candidate properties
        for (int i = 0; i < vec.size(); i++) {
            if (vec.get(i).getProperties().size() > maxPropertySize) {
                originalCandidate = i;
                maxPropertySize = vec.get(i).getProperties().size();
            }
        }
        // fill in missing properties
        java.util.Hashtable<String, Object> properties = vec.get(originalCandidate).getProperties();
        for (int i = 0; i < vec.size(); i++) {
            ICandidate currentCandidate = vec.get(i);
            java.util.Iterator<?> prop_it = (java.util.Iterator<?>) properties.keys();
            while (prop_it.hasNext()) {
                String key = (String) prop_it.next();
                if (!propertyNames.contains(key))
                    propertyNames.add(key);
                if (currentCandidate.getProperty(key) == null)
                    currentCandidate.setProperty(key, properties.get(key));
            }
        }
    }
    for (int i = 0; i < candidateList.getNumberElements(); i++) {
        ICandidate scoredCandidate = candidateList.getElement(i);
        if (propertyNames.size() >= 1) {
            String key = propertyNames.get(0);
            if (i == 0)
                heading += key;
            lines[i] = "" + checkEmptyProperty(scoredCandidate.getProperty(key), key);
        }
        for (int k = 1; k < propertyNames.size(); k++) {
            String key = propertyNames.get(k);
            if (i == 0)
                heading += "|" + key;
            String prop = checkEmptyProperty(scoredCandidate.getProperty(key), key).toString();
            lines[i] += "|" + prop;
        }
    }
    java.io.BufferedWriter bwriter = new java.io.BufferedWriter(new FileWriter(file));
    bwriter.write(heading);
    bwriter.newLine();
    for (int i = 0; i < lines.length; i++) {
        bwriter.write(lines[i]);
        bwriter.newLine();
    }
    bwriter.close();
    return true;
}
Also used : FileWriter(java.io.FileWriter) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) MatchList(de.ipbhalle.metfraglib.list.MatchList) RelativeIntensityNotDefinedException(de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList)

Example 5 with ScoredCandidateList

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

the class CandidateListWriterLossFragmentSmilesPSV method writeFile.

public boolean writeFile(File file, 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;
    StringBuilder heading = new StringBuilder();
    this.removeCandidateProperties(candidateList.getElement(0));
    java.util.Enumeration<String> keys = candidateList.getElement(0).getProperties().keys();
    if (keys.hasMoreElements()) {
        String key = keys.nextElement();
        heading.append(key);
    }
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        heading.append("|");
        heading.append(key);
    }
    heading.append("|");
    heading.append("ExplPeaks");
    heading.append("|");
    heading.append("FormulasOfExplPeaks");
    heading.append("|");
    heading.append("FragmentFingerprintOfExplPeaks");
    heading.append("|");
    heading.append("NumberPeaksUsed");
    heading.append("|");
    heading.append("NoExplPeaks");
    heading.append("|");
    heading.append("LossFingerprintOfExplPeaks");
    java.io.BufferedWriter bwriter = new java.io.BufferedWriter(new FileWriter(file));
    bwriter.write(heading.toString());
    bwriter.newLine();
    Fingerprint fingerprint = new Fingerprint((String) settings.get(VariableNames.FINGERPRINT_TYPE_NAME));
    for (int i = 0; i < candidateList.getNumberElements(); i++) {
        StringBuilder line = new StringBuilder();
        int countExplainedPeaks = 0;
        ICandidate scoredCandidate = candidateList.getElement(i);
        this.removeCandidateProperties(scoredCandidate);
        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++;
            }
        }
        keys = scoredCandidate.getProperties().keys();
        if (keys.hasMoreElements()) {
            String key = keys.nextElement();
            line.append(scoredCandidate.getProperty(key));
        }
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            line.append("|");
            line.append(scoredCandidate.getProperty(key));
        }
        StringBuilder peaksExplained = new StringBuilder();
        StringBuilder sumFormulasOfFragmentsExplainedPeaks = new StringBuilder();
        StringBuilder fingerprintOfFragmentsExplainedPeaks = new StringBuilder();
        if (scoredCandidate.getMatchList() != null) {
            String[] matchedFormulas = new String[scoredCandidate.getMatchList().getNumberElements()];
            double[] correctedMasses = new double[scoredCandidate.getMatchList().getNumberElements()];
            for (int ii = 0; ii < scoredCandidate.getMatchList().getNumberElements(); ii++) {
                try {
                    double intensity = scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getIntensity();
                    peaksExplained.append(scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass());
                    peaksExplained.append("_");
                    peaksExplained.append(intensity);
                    peaksExplained.append(";");
                } catch (RelativeIntensityNotDefinedException e1) {
                    continue;
                }
                String formula = scoredCandidate.getMatchList().getElement(ii).getModifiedFormulaStringOfBestMatchedFragment(scoredCandidate.getPrecursorMolecule());
                double mass = scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass();
                if ((Boolean) settings.get(VariableNames.CORRECT_MASSES_FOR_FINGERPRINT_ANNOTATION_NAME)) {
                    matchedFormulas[ii] = formula;
                    correctedMasses[ii] = MathTools.round(calculateMassOfFormula(formula));
                    mass = correctedMasses[ii];
                }
                sumFormulasOfFragmentsExplainedPeaks.append(scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass());
                sumFormulasOfFragmentsExplainedPeaks.append(":");
                sumFormulasOfFragmentsExplainedPeaks.append(formula);
                sumFormulasOfFragmentsExplainedPeaks.append(";");
                // get fragment of explained peak
                IFragment frag = scoredCandidate.getMatchList().getElement(ii).getBestMatchedFragment();
                String fp = null;
                try {
                    IAtomContainer con = fingerprint.getNormalizedAtomContainer(scoredCandidate.getPrecursorMolecule(), frag);
                    fp = fingerprint.getNormalizedFingerprint(con);
                } catch (Exception e) {
                    continue;
                }
                fingerprintOfFragmentsExplainedPeaks.append(mass);
                fingerprintOfFragmentsExplainedPeaks.append(":");
                fingerprintOfFragmentsExplainedPeaks.append(fp);
                fingerprintOfFragmentsExplainedPeaks.append(";");
            }
            line.append("|");
            line.append(peaksExplained.length() == 0 ? "NA" : peaksExplained.substring(0, peaksExplained.length() - 1));
            line.append("|");
            line.append(sumFormulasOfFragmentsExplainedPeaks.length() == 0 ? "NA" : sumFormulasOfFragmentsExplainedPeaks.substring(0, sumFormulasOfFragmentsExplainedPeaks.length() - 1));
            line.append("|");
            line.append(fingerprintOfFragmentsExplainedPeaks.length() == 0 ? "NA" : fingerprintOfFragmentsExplainedPeaks.substring(0, fingerprintOfFragmentsExplainedPeaks.length() - 1));
            line.append("|");
            line.append(numberOfPeaksUsed);
            line.append("|");
            line.append(countExplainedPeaks);
            // add loss information
            if (settings != null) {
                String lossesFingerprints = createLossAnnotations(scoredCandidate.getPrecursorMolecule(), scoredCandidate.getMatchList(), settings, correctedMasses, fingerprint);
                line.append("|");
                line.append(lossesFingerprints);
            }
        }
        bwriter.write(line.toString());
        bwriter.newLine();
    }
    bwriter.close();
    return true;
}
Also used : Fingerprint(de.ipbhalle.metfraglib.fingerprint.Fingerprint) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) MatchList(de.ipbhalle.metfraglib.list.MatchList) FileWriter(java.io.FileWriter) RelativeIntensityNotDefinedException(de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException) Fingerprint(de.ipbhalle.metfraglib.fingerprint.Fingerprint) ICandidate(de.ipbhalle.metfraglib.interfaces.ICandidate) AtomTypeNotKnownFromInputListException(de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException) RelativeIntensityNotDefinedException(de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) CandidateList(de.ipbhalle.metfraglib.list.CandidateList) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList) IFragment(de.ipbhalle.metfraglib.interfaces.IFragment) ScoredCandidateList(de.ipbhalle.metfraglib.list.ScoredCandidateList) SortedScoredCandidateList(de.ipbhalle.metfraglib.list.SortedScoredCandidateList)

Aggregations

ScoredCandidateList (de.ipbhalle.metfraglib.list.ScoredCandidateList)20 ICandidate (de.ipbhalle.metfraglib.interfaces.ICandidate)18 SortedScoredCandidateList (de.ipbhalle.metfraglib.list.SortedScoredCandidateList)17 CandidateList (de.ipbhalle.metfraglib.list.CandidateList)15 RelativeIntensityNotDefinedException (de.ipbhalle.metfraglib.exceptions.RelativeIntensityNotDefinedException)14 MatchList (de.ipbhalle.metfraglib.list.MatchList)14 FileWriter (java.io.FileWriter)11 File (java.io.File)6 AtomTypeNotKnownFromInputListException (de.ipbhalle.metfraglib.exceptions.AtomTypeNotKnownFromInputListException)4 RenderedImage (java.awt.image.RenderedImage)4 IOException (java.io.IOException)4 Label (jxl.write.Label)4 WritableCellFormat (jxl.write.WritableCellFormat)4 WritableFont (jxl.write.WritableFont)4 WritableImage (jxl.write.WritableImage)4 WritableSheet (jxl.write.WritableSheet)4 WritableWorkbook (jxl.write.WritableWorkbook)4 IFragment (de.ipbhalle.metfraglib.interfaces.IFragment)3 ArrayList (java.util.ArrayList)3 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)3