Search in sources :

Example 1 with MZTabFile

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

the class MzTabImportTask method run.

@Override
public void run() {
    setStatus(TaskStatus.PROCESSING);
    try {
        // Prevent MZTabFileParser from writing to console
        OutputStream logStream = ByteStreams.nullOutputStream();
        // Load mzTab file
        MZTabFileParser mzTabFileParser = new MZTabFileParser(inputFile, logStream);
        if (!mzTabFileParser.getErrorList().getErrorList().isEmpty()) {
            setStatus(TaskStatus.ERROR);
            setErrorMessage("Error processing " + inputFile + ":\n" + mzTabFileParser.getErrorList().toString());
            return;
        }
        MZTabFile mzTabFile = mzTabFileParser.getMZTabFile();
        // Let's say the initial parsing took 10% of the time
        finishedPercentage = 0.1;
        // Import raw data files
        SortedMap<Integer, RawDataFile> rawDataFiles = importRawDataFiles(mzTabFile);
        // Check if not canceled
        if (isCanceled())
            return;
        // Create a new feature list
        String peakListName = inputFile.getName().replace(".mzTab", "");
        RawDataFile[] rawDataArray = rawDataFiles.values().toArray(new RawDataFile[0]);
        PeakList newPeakList = new SimplePeakList(peakListName, rawDataArray);
        // Check if not canceled
        if (isCanceled())
            return;
        // Import variables
        importVariables(mzTabFile, rawDataFiles);
        // Check if not canceled
        if (isCanceled())
            return;
        // import small molecules (=feature list rows)
        importSmallMolecules(newPeakList, mzTabFile, rawDataFiles);
        // Check if not canceled
        if (isCanceled())
            return;
        // Add the new feature list to the project
        project.addPeakList(newPeakList);
        // Finish
        setStatus(TaskStatus.FINISHED);
        finishedPercentage = 1.0;
    } catch (Exception e) {
        e.printStackTrace();
        setStatus(TaskStatus.ERROR);
        setErrorMessage("Could not import data from " + inputFile + ": " + e.getMessage());
        return;
    }
}
Also used : MZTabFile(uk.ac.ebi.pride.jmztab.model.MZTabFile) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) OutputStream(java.io.OutputStream) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) SimplePeakList(net.sf.mzmine.datamodel.impl.SimplePeakList) PeakList(net.sf.mzmine.datamodel.PeakList) MZTabFileParser(uk.ac.ebi.pride.jmztab.utils.MZTabFileParser)

Example 2 with MZTabFile

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

the class MzTabImportTask method importRawDataFiles.

private SortedMap<Integer, RawDataFile> importRawDataFiles(MZTabFile mzTabFile) throws Exception {
    SortedMap<Integer, MsRun> msrun = mzTabFile.getMetadata().getMsRunMap();
    SortedMap<Integer, RawDataFile> rawDataFiles = new TreeMap<>();
    // If we are importing files, let's run RawDataImportModule
    if (importRawFiles) {
        List<File> filesToImport = new ArrayList<>();
        for (Entry<Integer, MsRun> entry : msrun.entrySet()) {
            File fileToImport = new File(entry.getValue().getLocation().getPath());
            if (fileToImport.exists() && fileToImport.canRead())
                filesToImport.add(fileToImport);
            else {
                // Check if the raw file exists in the same folder as the mzTab file
                File checkFile = new File(inputFile.getParentFile(), fileToImport.getName());
                if (checkFile.exists() && checkFile.canRead())
                    filesToImport.add(checkFile);
                else {
                    // Append .gz & check again if file exists as a workaround to .gz not getting preserved
                    // when .mzML.gz importing
                    checkFile = new File(inputFile.getParentFile(), fileToImport.getName() + ".gz");
                    if (checkFile.exists() && checkFile.canRead())
                        filesToImport.add(checkFile);
                    else {
                        // One more level of checking, appending .zip & checking as a workaround
                        checkFile = new File(inputFile.getParentFile(), fileToImport.getName() + ".zip");
                        if (checkFile.exists() && checkFile.canRead())
                            filesToImport.add(checkFile);
                    }
                }
            }
        }
        RawDataImportModule RDI = MZmineCore.getModuleInstance(RawDataImportModule.class);
        ParameterSet rdiParameters = RDI.getParameterSetClass().newInstance();
        rdiParameters.getParameter(RawDataImportParameters.fileNames).setValue(filesToImport.toArray(new File[0]));
        synchronized (underlyingTasks) {
            RDI.runModule(project, rdiParameters, underlyingTasks);
        }
        if (underlyingTasks.size() > 0) {
            MZmineCore.getTaskController().addTasks(underlyingTasks.toArray(new Task[0]));
        }
        // Wait until all raw data file imports have completed
        while (true) {
            if (isCanceled())
                return null;
            boolean tasksFinished = true;
            for (Task task : underlyingTasks) {
                if ((task.getStatus() == TaskStatus.WAITING) || (task.getStatus() == TaskStatus.PROCESSING))
                    tasksFinished = false;
            }
            if (tasksFinished)
                break;
            Thread.sleep(1000);
        }
    /*
       * // Sort raw data files based on order in mzTab file MainWindow mainWindow = (MainWindow)
       * MZmineCore.getDesktop(); ProjectTree rawDataTree = mainWindow.getMainPanel()
       * .getRawDataTree(); final RawDataTreeModel treeModel = ((MZmineProjectImpl)
       * project).getRawDataTreeModel(); final DefaultMutableTreeNode rootNode =
       * treeModel.getRoot(); int[] selectedRows = new int[rootNode.getChildCount()]; for (int i =
       * 1; i < rootNode.getChildCount() + 1; i++) { selectedRows[i - 1] = i; } final
       * ArrayList<DefaultMutableTreeNode> selectedNodes = new ArrayList<DefaultMutableTreeNode>();
       * for (int row : selectedRows) { TreePath path = rawDataTree.getPathForRow(row);
       * DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) path
       * .getLastPathComponent(); selectedNodes.add(selectedNode); }
       * 
       * // Reorder the nodes in the tree model based on order in mzTab // file int fileCounter = 0;
       * for (Entry<Integer, MsRun> entry : msrun.entrySet()) { fileCounter++; File f = new
       * File(entry.getValue().getLocation().getPath()); for (DefaultMutableTreeNode node :
       * selectedNodes) { if (node.toString().equals(f.getName())) {
       * treeModel.removeNodeFromParent(node); treeModel.insertNodeInto(node, rootNode, fileCounter
       * - 1); } } }
       */
    } else {
        finishedPercentage = 0.5;
    }
    // Find a matching RawDataFile for each MsRun entry
    for (Entry<Integer, MsRun> entry : msrun.entrySet()) {
        String rawFileName = new File(entry.getValue().getLocation().getPath()).getName();
        RawDataFile rawDataFile = null;
        // Check if we already have a RawDataFile of that name
        for (RawDataFile f : project.getDataFiles()) {
            if (f.getName().equals(rawFileName)) {
                rawDataFile = f;
                break;
            }
        }
        // If no data file of that name exists, create a dummy one
        if (rawDataFile == null) {
            RawDataFileWriter writer = MZmineCore.createNewFile(rawFileName);
            rawDataFile = writer.finishWriting();
            project.addFile(rawDataFile);
        }
        // Save a reference to the new raw data file
        rawDataFiles.put(entry.getKey(), rawDataFile);
    }
    return rawDataFiles;
}
Also used : ParameterSet(net.sf.mzmine.parameters.ParameterSet) Task(net.sf.mzmine.taskcontrol.Task) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) ArrayList(java.util.ArrayList) RawDataFileWriter(net.sf.mzmine.datamodel.RawDataFileWriter) TreeMap(java.util.TreeMap) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) RawDataImportModule(net.sf.mzmine.modules.rawdatamethods.rawdataimport.RawDataImportModule) MsRun(uk.ac.ebi.pride.jmztab.model.MsRun) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) File(java.io.File) MZTabFile(uk.ac.ebi.pride.jmztab.model.MZTabFile)

Aggregations

RawDataFile (net.sf.mzmine.datamodel.RawDataFile)2 MZTabFile (uk.ac.ebi.pride.jmztab.model.MZTabFile)2 File (java.io.File)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 PeakList (net.sf.mzmine.datamodel.PeakList)1 RawDataFileWriter (net.sf.mzmine.datamodel.RawDataFileWriter)1 SimplePeakList (net.sf.mzmine.datamodel.impl.SimplePeakList)1 RawDataImportModule (net.sf.mzmine.modules.rawdatamethods.rawdataimport.RawDataImportModule)1 ParameterSet (net.sf.mzmine.parameters.ParameterSet)1 AbstractTask (net.sf.mzmine.taskcontrol.AbstractTask)1 Task (net.sf.mzmine.taskcontrol.Task)1 MsRun (uk.ac.ebi.pride.jmztab.model.MsRun)1 MZTabFileParser (uk.ac.ebi.pride.jmztab.utils.MZTabFileParser)1