Search in sources :

Example 1 with SmallMolecule

use of uk.ac.ebi.pride.jmztab.model.SmallMolecule in project mzmine2 by mzmine.

the class MzTabImportTask method importSmallMolecules.

private void importSmallMolecules(PeakList newPeakList, MZTabFile mzTabFile, Map<Integer, RawDataFile> rawDataFiles) {
    SortedMap<Integer, Assay> assayMap = mzTabFile.getMetadata().getAssayMap();
    Collection<SmallMolecule> smallMolecules = mzTabFile.getSmallMolecules();
    // Loop through SML data
    String formula, description, database, url = "";
    double mzExp = 0, abundance = 0, peak_mz = 0, peak_rt = 0, peak_height = 0, rtValue = 0;
    // int charge = 0;
    int rowCounter = 0;
    for (SmallMolecule smallMolecule : smallMolecules) {
        // Stop the process if cancel() was called
        if (isCanceled())
            return;
        rowCounter++;
        formula = smallMolecule.getChemicalFormula();
        // smile = smallMolecule.getSmiles();
        // inchiKey = smallMolecule.getInchiKey();
        description = smallMolecule.getDescription();
        // species = smallMolecule.getSpecies();
        database = smallMolecule.getDatabase();
        if (smallMolecule.getURI() != null) {
            url = smallMolecule.getURI().toString();
        }
        String identifier = smallMolecule.getIdentifier().toString();
        SplitList<Double> rt = smallMolecule.getRetentionTime();
        if (smallMolecule.getExpMassToCharge() != null) {
            mzExp = smallMolecule.getExpMassToCharge();
        }
        // Calculate average RT if multiple values are available
        if (rt != null && !rt.isEmpty()) {
            rtValue = DoubleMath.mean(rt);
        }
        if ((url != null) && (url.equals("null"))) {
            url = null;
        }
        if (identifier.equals("null")) {
            identifier = null;
        }
        if (description == null && identifier != null) {
            description = identifier;
        }
        // Add shared information to row
        SimplePeakListRow newRow = new SimplePeakListRow(rowCounter);
        newRow.setAverageMZ(mzExp);
        newRow.setAverageRT(rtValue);
        if (description != null) {
            SimplePeakIdentity newIdentity = new SimplePeakIdentity(description, formula, database, identifier, url);
            newRow.addPeakIdentity(newIdentity, false);
        }
        // Add raw data file entries to row
        for (Entry<Integer, RawDataFile> rawDataEntry : rawDataFiles.entrySet()) {
            RawDataFile rawData = rawDataEntry.getValue();
            Assay dataFileAssay = assayMap.get(rawDataEntry.getKey());
            abundance = 0;
            peak_mz = 0;
            peak_rt = 0;
            peak_height = 0;
            if (smallMolecule.getAbundanceColumnValue(dataFileAssay) != null) {
                abundance = smallMolecule.getAbundanceColumnValue(dataFileAssay);
            }
            if (smallMolecule.getOptionColumnValue(dataFileAssay, "peak_mz") != null) {
                peak_mz = Double.parseDouble(smallMolecule.getOptionColumnValue(dataFileAssay, "peak_mz"));
            } else {
                peak_mz = mzExp;
            }
            if (smallMolecule.getOptionColumnValue(dataFileAssay, "peak_rt") != null) {
                peak_rt = Double.parseDouble(smallMolecule.getOptionColumnValue(dataFileAssay, "peak_rt"));
            } else {
                peak_rt = rtValue;
            }
            if (smallMolecule.getOptionColumnValue(dataFileAssay, "peak_height") != null) {
                peak_height = Double.parseDouble(smallMolecule.getOptionColumnValue(dataFileAssay, "peak_height"));
            } else {
                peak_height = 0.0;
            }
            int[] scanNumbers = {};
            DataPoint[] finalDataPoint = new DataPoint[1];
            finalDataPoint[0] = new SimpleDataPoint(peak_mz, peak_height);
            int representativeScan = 0;
            int fragmentScan = 0;
            int[] allFragmentScans = new int[] { 0 };
            Range<Double> finalRTRange = Range.singleton(peak_rt);
            Range<Double> finalMZRange = Range.singleton(peak_mz);
            Range<Double> finalIntensityRange = Range.singleton(peak_height);
            FeatureStatus status = FeatureStatus.DETECTED;
            Feature peak = new SimpleFeature(rawData, peak_mz, peak_rt, peak_height, abundance, scanNumbers, finalDataPoint, status, representativeScan, fragmentScan, allFragmentScans, finalRTRange, finalMZRange, finalIntensityRange);
            if (abundance > 0) {
                newRow.addPeak(rawData, peak);
            }
        }
        // Add row to feature list
        newPeakList.addRow(newRow);
    }
}
Also used : FeatureStatus(net.sf.mzmine.datamodel.Feature.FeatureStatus) SimplePeakIdentity(net.sf.mzmine.datamodel.impl.SimplePeakIdentity) SimplePeakListRow(net.sf.mzmine.datamodel.impl.SimplePeakListRow) Feature(net.sf.mzmine.datamodel.Feature) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) SimpleFeature(net.sf.mzmine.datamodel.impl.SimpleFeature) Assay(uk.ac.ebi.pride.jmztab.model.Assay) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) SmallMolecule(uk.ac.ebi.pride.jmztab.model.SmallMolecule) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint)

Example 2 with SmallMolecule

use of uk.ac.ebi.pride.jmztab.model.SmallMolecule in project mzmine2 by mzmine.

the class MzTabExportTask method run.

public void run() {
    setStatus(TaskStatus.PROCESSING);
    // Shall export several files?
    boolean substitute = fileName.getPath().contains(plNamePattern);
    // Total number of rows
    for (PeakList peakList : peakLists) {
        totalRows += peakList.getNumberOfRows();
    }
    // Process feature lists
    for (PeakList peakList : peakLists) {
        File curFile = fileName;
        try {
            // Filename
            if (substitute) {
                // Cleanup from illegal filename characters
                String cleanPlName = peakList.getName().replaceAll("[^a-zA-Z0-9.-]", "_");
                // Substitute
                String newFilename = fileName.getPath().replaceAll(Pattern.quote(plNamePattern), cleanPlName);
                curFile = new File(newFilename);
            }
            // Open file
            FileWriter writer;
            try {
                writer = new FileWriter(curFile);
            } catch (Exception e) {
                setStatus(TaskStatus.ERROR);
                setErrorMessage("Could not open file " + curFile + " for writing.");
                return;
            }
            // Metadata
            Metadata mtd = new Metadata();
            mtd.setMZTabMode(MZTabDescription.Mode.Summary);
            mtd.setMZTabType(MZTabDescription.Type.Quantification);
            mtd.setDescription(peakList.getName());
            mtd.addSoftwareParam(1, new CVParam("MS", "MS:1002342", "MZmine", MZmineCore.getMZmineVersion()));
            mtd.setSmallMoleculeQuantificationUnit(new CVParam("PRIDE", "PRIDE:0000330", "Arbitrary quantification unit", null));
            mtd.addSmallMoleculeSearchEngineScoreParam(1, new CVParam("MS", "MS:1001153", "search engine specific score", null));
            mtd.addFixedModParam(1, new CVParam("MS", "MS:1002453", "No fixed modifications searched", null));
            mtd.addVariableModParam(1, new CVParam("MS", "MS:1002454", "No variable modifications searched", null));
            // Create stable columns
            MZTabColumnFactory factory = MZTabColumnFactory.getInstance(Section.Small_Molecule);
            factory.addDefaultStableColumns();
            // Add optional columns which have stable order
            factory.addURIOptionalColumn();
            factory.addBestSearchEngineScoreOptionalColumn(SmallMoleculeColumn.BEST_SEARCH_ENGINE_SCORE, 1);
            final RawDataFile[] rawDataFiles = peakList.getRawDataFiles();
            int fileCounter = 0;
            for (RawDataFile file : rawDataFiles) {
                fileCounter++;
                /**
                 * TO DO: Add path to original imported raw file to MZmine and write it out here instead
                 */
                // MS run location
                MsRun msRun = new MsRun(fileCounter);
                msRun.setLocation(new URL("file:///" + file.getName()));
                mtd.addMsRun(msRun);
                mtd.addAssayMsRun(fileCounter, msRun);
                // Add samples to study variable assay
                for (UserParameter<?, ?> p : project.getParameters()) {
                    Assay assay = mtd.getAssayMap().get(fileCounter);
                    for (StudyVariable studyVariable : mtd.getStudyVariableMap().values()) {
                        if (studyVariable.getDescription().equals(String.valueOf(p) + ": " + String.valueOf(project.getParameterValue(p, file)))) {
                            mtd.addStudyVariableAssay(studyVariable.getId(), assay);
                        }
                    }
                }
                // Additional columns
                factory.addAbundanceOptionalColumn(new Assay(fileCounter));
                factory.addOptionalColumn(new Assay(fileCounter), "peak_mz", String.class);
                factory.addOptionalColumn(new Assay(fileCounter), "peak_rt", String.class);
                factory.addOptionalColumn(new Assay(fileCounter), "peak_height", String.class);
            }
            // Variable descriptions
            int parameterCounter = 0;
            for (UserParameter<?, ?> p : project.getParameters()) {
                for (Object e : ((ComboParameter<?>) p).getChoices()) {
                    parameterCounter++;
                    mtd.addStudyVariableDescription(parameterCounter, String.valueOf(p) + ": " + String.valueOf(e));
                    StudyVariable studyVariable = new StudyVariable(parameterCounter);
                    factory.addAbundanceOptionalColumn(studyVariable);
                }
            }
            // Write to file
            BufferedWriter out = new BufferedWriter(writer);
            out.write(mtd.toString());
            out.write(newLine);
            out.write(factory.toString());
            out.write(newLine);
            // Write data rows
            for (PeakListRow peakListRow : peakList.getRows()) {
                // Cancel?
                if (isCanceled()) {
                    return;
                }
                PeakIdentity peakIdentity = peakListRow.getPreferredPeakIdentity();
                if (exportall || peakIdentity != null) {
                    SmallMolecule sm = new SmallMolecule(factory, mtd);
                    if (peakIdentity != null) {
                        // Identity information
                        String identifier = escapeString(peakIdentity.getPropertyValue("ID"));
                        String database = peakIdentity.getPropertyValue("Identification method");
                        String formula = peakIdentity.getPropertyValue("Molecular formula");
                        String description = escapeString(peakIdentity.getPropertyValue("Name"));
                        String url = peakIdentity.getPropertyValue("URL");
                        if (identifier != null) {
                            sm.setIdentifier(identifier);
                        }
                        if (database != null) {
                            sm.setDatabase(database);
                        }
                        if (formula != null) {
                            sm.setChemicalFormula(formula);
                        }
                        if (description != null) {
                            sm.setDescription(description);
                        }
                        if (url != null) {
                            sm.setURI(url);
                        }
                    }
                    Double rowMZ = peakListRow.getAverageMZ();
                    int rowCharge = peakListRow.getRowCharge();
                    String rowRT = String.valueOf(peakListRow.getAverageRT());
                    if (rowMZ != null) {
                        sm.setExpMassToCharge(rowMZ);
                    }
                    if (rowCharge > 0) {
                        sm.setCharge(rowCharge);
                    }
                    if (rowRT != null) {
                        sm.setRetentionTime(rowRT);
                    }
                    int dataFileCount = 0;
                    for (RawDataFile dataFile : rawDataFiles) {
                        dataFileCount++;
                        Feature peak = peakListRow.getPeak(dataFile);
                        if (peak != null) {
                            String peakMZ = String.valueOf(peak.getMZ());
                            String peakRT = String.valueOf(String.valueOf(peak.getRT()));
                            String peakHeight = String.valueOf(peak.getHeight());
                            Double peakArea = peak.getArea();
                            sm.setOptionColumnValue(new Assay(dataFileCount), "peak_mz", peakMZ);
                            sm.setOptionColumnValue(new Assay(dataFileCount), "peak_rt", peakRT);
                            sm.setOptionColumnValue(new Assay(dataFileCount), "peak_height", peakHeight);
                            sm.setAbundanceColumnValue(new Assay(dataFileCount), peakArea);
                        }
                    }
                    out.write(sm.toString());
                    out.write(newLine);
                }
            }
            out.flush();
            out.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            setStatus(TaskStatus.ERROR);
            setErrorMessage("Could not export feature list to file " + curFile + ": " + e.getMessage());
            return;
        }
    }
    if (getStatus() == TaskStatus.PROCESSING)
        setStatus(TaskStatus.FINISHED);
}
Also used : FileWriter(java.io.FileWriter) Metadata(uk.ac.ebi.pride.jmztab.model.Metadata) CVParam(uk.ac.ebi.pride.jmztab.model.CVParam) Feature(net.sf.mzmine.datamodel.Feature) URL(java.net.URL) ComboParameter(net.sf.mzmine.parameters.parametertypes.ComboParameter) BufferedWriter(java.io.BufferedWriter) Assay(uk.ac.ebi.pride.jmztab.model.Assay) PeakListRow(net.sf.mzmine.datamodel.PeakListRow) MsRun(uk.ac.ebi.pride.jmztab.model.MsRun) StudyVariable(uk.ac.ebi.pride.jmztab.model.StudyVariable) MZTabColumnFactory(uk.ac.ebi.pride.jmztab.model.MZTabColumnFactory) PeakIdentity(net.sf.mzmine.datamodel.PeakIdentity) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) SmallMolecule(uk.ac.ebi.pride.jmztab.model.SmallMolecule) PeakList(net.sf.mzmine.datamodel.PeakList) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) File(java.io.File)

Aggregations

Feature (net.sf.mzmine.datamodel.Feature)2 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)2 Assay (uk.ac.ebi.pride.jmztab.model.Assay)2 SmallMolecule (uk.ac.ebi.pride.jmztab.model.SmallMolecule)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 URL (java.net.URL)1 DataPoint (net.sf.mzmine.datamodel.DataPoint)1 FeatureStatus (net.sf.mzmine.datamodel.Feature.FeatureStatus)1 PeakIdentity (net.sf.mzmine.datamodel.PeakIdentity)1 PeakList (net.sf.mzmine.datamodel.PeakList)1 PeakListRow (net.sf.mzmine.datamodel.PeakListRow)1 SimpleDataPoint (net.sf.mzmine.datamodel.impl.SimpleDataPoint)1 SimpleFeature (net.sf.mzmine.datamodel.impl.SimpleFeature)1 SimplePeakIdentity (net.sf.mzmine.datamodel.impl.SimplePeakIdentity)1 SimplePeakListRow (net.sf.mzmine.datamodel.impl.SimplePeakListRow)1 ComboParameter (net.sf.mzmine.parameters.parametertypes.ComboParameter)1 CVParam (uk.ac.ebi.pride.jmztab.model.CVParam)1 MZTabColumnFactory (uk.ac.ebi.pride.jmztab.model.MZTabColumnFactory)1