Skip to content

Commit

Permalink
Merge pull request mzmine#583 from robinschmid/waters_raw
Browse files Browse the repository at this point in the history
Fix Waters raw import
  • Loading branch information
robinschmid authored Mar 9, 2022
2 parents 57a1ce9 + 09b9a16 commit cbbf204
Show file tree
Hide file tree
Showing 19 changed files with 92 additions and 1,433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import io.github.mzmine.util.ExitCode;
import io.github.mzmine.util.MemoryMapStorage;
import io.github.mzmine.util.RawDataFileUtils;
import io.github.mzmine.util.files.FileAndPathUtil;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
Expand All @@ -44,8 +46,8 @@
*/
public class WatersRawImportModule implements MZmineProcessingModule {

private Logger logger = Logger.getLogger(this.getClass().getName());

private static final Logger logger = Logger.getLogger(WatersRawImportModule.class.getName());
private static final String MODULE_NAME = "Waters RAW file import";
private static final String MODULE_DESCRIPTION = "This module imports raw data into the project.";

Expand Down Expand Up @@ -74,7 +76,16 @@ public class WatersRawImportModule implements MZmineProcessingModule {
public ExitCode runModule(final @NotNull MZmineProject project, @NotNull ParameterSet parameters,
@NotNull Collection<Task> tasks, @NotNull Instant moduleCallDate) {

File fileNames[] = parameters.getParameter(WatersRawImportParameters.fileNames).getValue();
File dir = parameters.getParameter(WatersRawImportParameters.fileNames).getValue()[0];
File fileNames[] = Arrays.stream(FileAndPathUtil.getSubDirectories(dir))
.filter(Objects::nonNull)
.filter(f -> f.isDirectory() && f.getName().toLowerCase().endsWith(".raw"))
.toArray(File[]::new);

if (fileNames.length <= 0) {
logger.warning("Select .raw folder or parent folder for waters import");
return ExitCode.ERROR;
}

if (Arrays.asList(fileNames).contains(null)) {
logger.warning("List of filenames contains null");
Expand Down Expand Up @@ -105,7 +116,8 @@ public ExitCode runModule(final @NotNull MZmineProject project, @NotNull Paramet
}

try {
RawDataFile newMZmineFile = MZmineCore.createNewFile(newName, fileNames[i].getAbsolutePath(), storage);
RawDataFile newMZmineFile = MZmineCore.createNewFile(newName,
fileNames[i].getAbsolutePath(), storage);
Task newTask = new WatersRawImportTask(project, fileNames[i], newMZmineFile,
WatersRawImportModule.class, parameters, moduleCallDate);
tasks.add(newTask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,22 @@
import io.github.mzmine.taskcontrol.TaskStatus;
import io.github.mzmine.util.ExceptionUtils;
import io.github.mzmine.util.TextUtils;
import io.github.mzmine.util.ZipUtils;
import io.github.mzmine.util.scans.ScanUtils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.file.Files;
import java.time.Instant;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -54,18 +58,14 @@
*/
public class WatersRawImportTask extends AbstractTask {

private Logger logger = Logger.getLogger(this.getClass().getName());

public static final Logger logger = Logger.getLogger(WatersRawImportTask.class.getName());
private final ParameterSet parameters;
private final Class<? extends MZmineModule> module;
private File file;
private MZmineProject project;
private RawDataFile newMZmineFile;
private final ParameterSet parameters;
private final Class<? extends MZmineModule> module;

private Process dumper = null;

private int totalScans = 0, parsedScans = 0;

/*
* These variables are used during parsing of the RAW dump.
*/
Expand All @@ -78,7 +78,8 @@ public class WatersRawImportTask extends AbstractTask {
private double precursorMZ = 0;

public WatersRawImportTask(MZmineProject project, File fileToOpen, RawDataFile newMZmineFile,
@NotNull final Class<? extends MZmineModule> module, @NotNull final ParameterSet parameters, @NotNull Instant moduleCallDate) {
@NotNull final Class<? extends MZmineModule> module, @NotNull final ParameterSet parameters,
@NotNull Instant moduleCallDate) {
super(null, moduleCallDate); // storage in raw data file
this.project = project;
this.file = fileToOpen;
Expand All @@ -87,6 +88,50 @@ public WatersRawImportTask(MZmineProject project, File fileToOpen, RawDataFile n
this.module = module;
}

public static File unzipWatersParser() throws IOException {
final String tmpPath = System.getProperty("java.io.tmpdir");
File watersRawFileParserFolder = new File(tmpPath, "mzmine_waters_raw_parser");
final File watersRawFileParserExe = new File(watersRawFileParserFolder, "WatersRawDump.exe");

// Check if it has already been unzipped
if (watersRawFileParserFolder.exists() && watersRawFileParserFolder.isDirectory()
&& watersRawFileParserFolder.canRead() && watersRawFileParserExe.exists()
&& watersRawFileParserExe.isFile() && watersRawFileParserExe.canExecute()) {
logger.finest("Waters RawFileParser found in folder " + watersRawFileParserFolder);
return watersRawFileParserFolder;
}
synchronized (WatersRawImportTask.class) {
// double checked
if (watersRawFileParserFolder.exists() && watersRawFileParserFolder.isDirectory()
&& watersRawFileParserFolder.canRead() && watersRawFileParserExe.exists()
&& watersRawFileParserExe.isFile() && watersRawFileParserExe.canExecute()) {
logger.finest("Waters RawFileParser found in folder " + watersRawFileParserFolder);
return watersRawFileParserFolder;
}

// In case the folder already exists, unzip to a different folder
if (watersRawFileParserFolder.exists()) {
logger.finest("Folder " + watersRawFileParserFolder + " exists, creating a new one");
watersRawFileParserFolder = Files.createTempDirectory("mzmine_waters_raw_parser").toFile();
}

logger.finest("Unpacking Waters RawFileParser to folder " + watersRawFileParserFolder);
InputStream zipStream = WatersRawImportTask.class.getResourceAsStream(
"/vendorlib/waters/waters.zip");
if (zipStream == null) {
throw new IOException("Failed to open the resource /vendorlib/waters/waters.zip");
}
ZipInputStream zipInputStream = new ZipInputStream(zipStream);
ZipUtils.unzipStream(zipInputStream, watersRawFileParserFolder);
zipInputStream.close();

// Delete the temporary folder on application exit
FileUtils.forceDeleteOnExit(watersRawFileParserFolder);

return watersRawFileParserFolder;
}
}

/**
* @see io.github.mzmine.taskcontrol.Task#getFinishedPercentage()
*/
Expand All @@ -95,9 +140,6 @@ public double getFinishedPercentage() {
return totalScans == 0 ? 0 : (double) parsedScans / totalScans;
}

/**
* @see java.lang.Runnable#run()
*/
@Override
public void run() {

Expand All @@ -106,15 +148,25 @@ public void run() {

// Check the OS we are running
String osName = System.getProperty("os.name").toUpperCase();
String rawDumpPath =
System.getProperty("user.dir") + File.separator + "lib" + File.separator + "vendor_lib"
+ File.separator + "waters" + File.separator + "WatersRawDump.exe";
String rawDumpPath = "";

try {
final File folder = unzipWatersParser();
rawDumpPath = new File(folder, "WatersRawDump.exe").getPath();
} catch (IOException e) {
final String msg = "Error while reading waters raw library. " + e.getMessage();
logger.log(Level.WARNING, msg, e);
setErrorMessage(msg);
setStatus(TaskStatus.ERROR);
return;
}

String cmdLine[];

if (osName.toUpperCase().contains("WINDOWS")) {
cmdLine = new String[]{rawDumpPath, file.getPath()};
cmdLine = new String[]{rawDumpPath, this.file.getPath()};
} else {
cmdLine = new String[]{"wine", rawDumpPath, file.getPath()};
cmdLine = new String[]{"wine", rawDumpPath, this.file.getPath()};
}

try {
Expand Down Expand Up @@ -147,7 +199,8 @@ public void run() {
+ totalScans + ")"));
}

newMZmineFile.getAppliedMethods().add(new SimpleFeatureListAppliedMethod(module, parameters, getModuleCallDate()));
newMZmineFile.getAppliedMethods()
.add(new SimpleFeatureListAppliedMethod(module, parameters, getModuleCallDate()));
project.addFile(newMZmineFile);

} catch (Throwable e) {
Expand All @@ -166,7 +219,7 @@ public void run() {
return;
}

logger.info("Finished parsing " + file + ", parsed " + parsedScans + " scans");
logger.info("Finished parsing " + this.file + ", parsed " + parsedScans + " scans");
setStatus(TaskStatus.FINISHED);

}
Expand Down Expand Up @@ -194,7 +247,7 @@ private void readRAWDump(InputStream dumpStream) throws IOException {
}

if (line.startsWith("ERROR: ")) {
throw (new IOException(line.substring("ERROR: ".length())));
throw (new IOException(file.getAbsolutePath() + line.substring("ERROR: ".length())));
}

if (line.startsWith("NUMBER OF SCANS: ")) {
Expand Down Expand Up @@ -280,8 +333,8 @@ private void readRAWDump(InputStream dumpStream) throws IOException {
// VALUES
if (numOfDataPoints != Integer.parseInt(m.group(1))) {
throw new IOException(
"Scan " + scanNumber + " contained " + numOfDataPoints + " mass values, but " + m
.group(1) + " intensity values");
"Scan " + scanNumber + " contained " + numOfDataPoints + " mass values, but "
+ m.group(1) + " intensity values");
}
final int byteSize = Integer.parseInt(m.group(2));

Expand Down Expand Up @@ -321,9 +374,8 @@ private void readRAWDump(InputStream dumpStream) throws IOException {
msLevel != 1 && precursorMZ != 0d ? new DDAMsMsInfoImpl(precursorMZ, precursorCharge,
null, null, null, msLevel, ActivationMethod.UNKNOWN, null) : null;

SimpleScan newScan = new SimpleScan(newMZmineFile, scanNumber, msLevel, retentionTime,
info, mzValues, intensityValues, spectrumType, polarity, scanId,
mzRange);
SimpleScan newScan = new SimpleScan(newMZmineFile, scanNumber, msLevel, retentionTime, info,
mzValues, intensityValues, spectrumType, polarity, scanId, mzRange);
newMZmineFile.addScan(newScan);

parsedScans++;
Expand Down Expand Up @@ -354,5 +406,4 @@ public void cancel() {
dumper.destroy();
}
}

}
Binary file removed src/main/resources/vendorlib/waters/MassLynxRaw.dll
Binary file not shown.
Binary file removed src/main/resources/vendorlib/waters/MassLynxRaw.lib
Binary file not shown.
8 changes: 0 additions & 8 deletions src/main/resources/vendorlib/waters/README.txt

This file was deleted.

Loading

0 comments on commit cbbf204

Please sign in to comment.