Search in sources :

Example 6 with RawDataFileWriter

use of net.sf.mzmine.datamodel.RawDataFileWriter in project mzmine2 by mzmine.

the class RawDataImportModule method runModule.

@Override
@Nonnull
public ExitCode runModule(@Nonnull final MZmineProject project, @Nonnull ParameterSet parameters, @Nonnull Collection<Task> tasks) {
    File[] fileNames = parameters.getParameter(RawDataImportParameters.fileNames).getValue();
    // Find common prefix in raw file names if in GUI mode
    String commonPrefix = null;
    if (MZmineCore.getDesktop().getMainWindow() != null && fileNames.length > 1) {
        String fileName = fileNames[0].getName();
        outerloop: for (int x = 0; x < fileName.length(); x++) {
            for (int i = 0; i < fileNames.length; i++) {
                if (!fileName.substring(0, x).equals(fileNames[i].getName().substring(0, x))) {
                    commonPrefix = fileName.substring(0, x - 1);
                    break outerloop;
                }
            }
        }
        if (!Strings.isNullOrEmpty(commonPrefix)) {
            // Show a dialog to allow user to remove common prefix
            Object[] options1 = { "Remove", "Do not remove", "Cancel" };
            JPanel panel = new JPanel();
            panel.add(new JLabel("The files you have chosen have a common prefix."));
            panel.add(new JLabel("Would you like to remove some or all of this prefix to shorten the names?"));
            panel.add(new JLabel(" "));
            panel.add(new JLabel("Prefix to remove:"));
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
            JTextField textField = new JTextField(6);
            textField.setText(commonPrefix);
            panel.add(textField);
            int result = JOptionPane.showOptionDialog(null, panel, "Common prefix", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options1, null);
            // Cancel import if user clicked cancel
            if (result == 2) {
                return ExitCode.ERROR;
            }
            // Only remove if user selected to do so
            if (result == 0) {
                commonPrefix = textField.getText();
            } else {
                commonPrefix = null;
            }
        }
    }
    for (int i = 0; i < fileNames.length; i++) {
        if (fileNames[i] == null) {
            return ExitCode.OK;
        }
        if ((!fileNames[i].exists()) || (!fileNames[i].canRead())) {
            MZmineCore.getDesktop().displayErrorMessage(MZmineCore.getDesktop().getMainWindow(), "Cannot read file " + fileNames[i]);
            logger.warning("Cannot read file " + fileNames[i]);
            return ExitCode.ERROR;
        }
        // Set the new name by removing the common prefix
        String newName;
        if (!Strings.isNullOrEmpty(commonPrefix)) {
            final String regex = "^" + Pattern.quote(commonPrefix);
            newName = fileNames[i].getName().replaceFirst(regex, "");
        } else {
            newName = fileNames[i].getName();
        }
        RawDataFileWriter newMZmineFile;
        try {
            newMZmineFile = MZmineCore.createNewFile(newName);
        } catch (IOException e) {
            MZmineCore.getDesktop().displayErrorMessage(MZmineCore.getDesktop().getMainWindow(), "Could not create a new temporary file " + e);
            logger.log(Level.SEVERE, "Could not create a new temporary file ", e);
            return ExitCode.ERROR;
        }
        RawDataFileType fileType = RawDataFileTypeDetector.detectDataFileType(fileNames[i]);
        logger.finest("File " + fileNames[i] + " type detected as " + fileType);
        if (fileType == null) {
            MZmineCore.getDesktop().displayErrorMessage(MZmineCore.getDesktop().getMainWindow(), "Could not determine the file type of file " + fileNames[i]);
            continue;
        }
        Task newTask = createOpeningTask(fileType, project, fileNames[i], newMZmineFile);
        if (newTask == null) {
            logger.warning("File type " + fileType + " of file " + fileNames[i] + " is not supported.");
            return ExitCode.ERROR;
        }
        tasks.add(newTask);
    }
    return ExitCode.OK;
}
Also used : JPanel(javax.swing.JPanel) MzMLReadTask(net.sf.mzmine.modules.rawdatamethods.rawdataimport.fileformats.MzMLReadTask) MzDataReadTask(net.sf.mzmine.modules.rawdatamethods.rawdataimport.fileformats.MzDataReadTask) CsvReadTask(net.sf.mzmine.modules.rawdatamethods.rawdataimport.fileformats.CsvReadTask) AgilentCsvReadTask(net.sf.mzmine.modules.rawdatamethods.rawdataimport.fileformats.AgilentCsvReadTask) MzXMLReadTask(net.sf.mzmine.modules.rawdatamethods.rawdataimport.fileformats.MzXMLReadTask) ZipReadTask(net.sf.mzmine.modules.rawdatamethods.rawdataimport.fileformats.ZipReadTask) Task(net.sf.mzmine.taskcontrol.Task) NativeFileReadTask(net.sf.mzmine.modules.rawdatamethods.rawdataimport.fileformats.NativeFileReadTask) NetCDFReadTask(net.sf.mzmine.modules.rawdatamethods.rawdataimport.fileformats.NetCDFReadTask) BoxLayout(javax.swing.BoxLayout) JLabel(javax.swing.JLabel) RawDataFileWriter(net.sf.mzmine.datamodel.RawDataFileWriter) IOException(java.io.IOException) JTextField(javax.swing.JTextField) File(java.io.File) Nonnull(javax.annotation.Nonnull)

Example 7 with RawDataFileWriter

use of net.sf.mzmine.datamodel.RawDataFileWriter in project mzmine2 by mzmine.

the class BaselineCorrector method correctDatafile.

public final RawDataFile correctDatafile(final RSessionWrapper rSession, final RawDataFile dataFile, final ParameterSet parameters, final ParameterSet commonParameters) throws IOException, RSessionWrapperException {
    if (isAborted(dataFile) || !rSession.isSessionRunning())
        return null;
    // Get very last information from root module setup
    // this.setGeneralParameters(MZmineCore.getConfiguration().getModuleParameters(BaselineCorrectionModule.class));
    this.collectCommonParameters(commonParameters);
    RawDataFile correctedDataFile = null;
    RawDataFile origDataFile = dataFile;
    // Initialize progress info if not done already.
    if (!progressMap.containsKey(origDataFile))
        progressMap.put(origDataFile, new int[] { 0, 0, 0 });
    // Create a new temporary file to write in.
    RawDataFileWriter rawDataFileWriter = MZmineCore.createNewFile(origDataFile.getName() + ' ' + suffix);
    // Determine number of bins.
    final double mzLen = origDataFile.getDataMZRange().upperEndpoint() - origDataFile.getDataMZRange().lowerEndpoint();
    final int numBins = useBins ? (int) Math.ceil(mzLen / binWidth) : 1;
    // Get MS levels.
    final int[] levels = origDataFile.getMSLevels();
    // Measure progress and find MS-level.
    boolean foundLevel = msLevel == 0;
    // progressMax = 0;
    for (final int level : levels) {
        final boolean isMSLevel = msLevel == level;
        final int numScans = origDataFile.getScanNumbers(level).length;
        foundLevel |= isMSLevel;
        // progressMax += isMSLevel || msLevel == 0 ? 2 * numScans +
        // numBins : numScans;
        progressMap.get(origDataFile)[1] += (isMSLevel || msLevel == 0 ? 2 * numScans + numBins : numScans);
    }
    // Is the specified MS-level present?
    if (!foundLevel) {
        throw new IllegalArgumentException("The data file doesn't contain data for MS-level " + msLevel + '.');
    }
    // Which chromatogram type.
    final boolean useTIC = (chromatogramType == ChromatogramType.TIC);
    // Process each MS level.
    for (final int level : levels) {
        if (!isAborted(origDataFile)) {
            if (level == msLevel || msLevel == 0) {
                // Correct baseline for this MS-level.
                if (useTIC) {
                    correctTICBaselines(rSession, origDataFile, rawDataFileWriter, level, numBins, parameters);
                } else {
                    correctBasePeakBaselines(rSession, origDataFile, rawDataFileWriter, level, numBins, parameters);
                }
            } else {
                // Copy scans for this MS-level.
                copyScansToWriter(origDataFile, rawDataFileWriter, level);
            }
        }
    }
    // If the referring task was canceled, stop processing.
    if (!isAborted(origDataFile)) {
        // Finalize writing.
        correctedDataFile = rawDataFileWriter.finishWriting();
    }
    return correctedDataFile;
}
Also used : RawDataFile(net.sf.mzmine.datamodel.RawDataFile) RawDataFileWriter(net.sf.mzmine.datamodel.RawDataFileWriter) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint)

Example 8 with RawDataFileWriter

use of net.sf.mzmine.datamodel.RawDataFileWriter in project mzmine2 by mzmine.

the class RawFileMergeTask method run.

@Override
public void run() {
    try {
        setStatus(TaskStatus.PROCESSING);
        // total number of scans
        StringBuilder s = new StringBuilder();
        s.append("Merge files: ");
        for (RawDataFile r : raw) {
            s.append(r.getName());
            s.append(", ");
        }
        LOG.info(s.toString());
        // put all in a list and sort by rt
        List<Scan> scans = new ArrayList<>();
        for (RawDataFile r : raw) {
            // some files are only for MS2
            boolean isMS2Only = useMS2Marker && r.getName().contains(ms2Marker);
            int[] snarray = r.getScanNumbers();
            for (int sn : snarray) {
                if (isCanceled())
                    return;
                Scan scan = r.getScan(sn);
                if (!isMS2Only || scan.getMSLevel() > 1) {
                    scans.add(scan);
                }
            }
        }
        // sort by rt
        scans.sort(new Comparator<Scan>() {

            @Override
            public int compare(Scan a, Scan b) {
                return Double.compare(a.getRetentionTime(), b.getRetentionTime());
            }
        });
        // create new file
        RawDataFileWriter rawDataFileWriter = MZmineCore.createNewFile(raw[0].getName() + " " + suffix);
        int i = 0;
        for (Scan scan : scans) {
            if (isCanceled())
                return;
            // copy, reset scan number
            SimpleScan scanCopy = new SimpleScan(scan);
            scanCopy.setScanNumber(i);
            rawDataFileWriter.addScan(scanCopy);
            i++;
        }
        RawDataFile filteredRawDataFile = rawDataFileWriter.finishWriting();
        project.addFile(filteredRawDataFile);
        if (getStatus() == TaskStatus.PROCESSING)
            setStatus(TaskStatus.FINISHED);
    } catch (IOException e) {
        throw new MSDKRuntimeException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) RawDataFileWriter(net.sf.mzmine.datamodel.RawDataFileWriter) IOException(java.io.IOException) SimpleScan(net.sf.mzmine.datamodel.impl.SimpleScan) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) MSDKRuntimeException(io.github.msdk.MSDKRuntimeException) Scan(net.sf.mzmine.datamodel.Scan) SimpleScan(net.sf.mzmine.datamodel.impl.SimpleScan)

Example 9 with RawDataFileWriter

use of net.sf.mzmine.datamodel.RawDataFileWriter in project mzmine2 by mzmine.

the class ScanFilteringTask method run.

/**
 * @see Runnable#run()
 */
@Override
public void run() {
    setStatus(TaskStatus.PROCESSING);
    logger.info("Started filtering scans on " + dataFile);
    scanNumbers = dataFile.getScanNumbers();
    totalScans = scanNumbers.length;
    try {
        // Create new raw data file
        String newName = dataFile.getName() + " " + suffix;
        RawDataFileWriter rawDataFileWriter = MZmineCore.createNewFile(newName);
        for (int i = 0; i < totalScans; i++) {
            if (isCanceled()) {
                return;
            }
            Scan scan = dataFile.getScan(scanNumbers[i]);
            Scan newScan = null;
            if (select.matches(scan))
                newScan = rawDataFilter.getModule().filterScan(scan, rawDataFilter.getParameterSet());
            else
                newScan = scan;
            if (newScan != null) {
                rawDataFileWriter.addScan(newScan);
            }
            processedScans++;
        }
        // Finalize writing
        try {
            filteredRawDataFile = rawDataFileWriter.finishWriting();
            project.addFile(filteredRawDataFile);
            // Remove the original file if requested
            if (removeOriginal) {
                project.removeFile(dataFile);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        setStatus(TaskStatus.FINISHED);
        logger.info("Finished scan filter on " + dataFile);
    } catch (IOException e) {
        setStatus(TaskStatus.ERROR);
        setErrorMessage(e.toString());
        return;
    }
}
Also used : Scan(net.sf.mzmine.datamodel.Scan) RawDataFileWriter(net.sf.mzmine.datamodel.RawDataFileWriter) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

RawDataFileWriter (net.sf.mzmine.datamodel.RawDataFileWriter)9 IOException (java.io.IOException)5 Scan (net.sf.mzmine.datamodel.Scan)5 DataPoint (net.sf.mzmine.datamodel.DataPoint)4 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)4 SimpleScan (net.sf.mzmine.datamodel.impl.SimpleScan)4 File (java.io.File)3 SimpleDataPoint (net.sf.mzmine.datamodel.impl.SimpleDataPoint)3 ArrayList (java.util.ArrayList)2 Task (net.sf.mzmine.taskcontrol.Task)2 MSDKRuntimeException (io.github.msdk.MSDKRuntimeException)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 TreeMap (java.util.TreeMap)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 Nonnull (javax.annotation.Nonnull)1 BoxLayout (javax.swing.BoxLayout)1