use of uk.ac.ebi.pride.jmztab.model.MsRun 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;
}
use of uk.ac.ebi.pride.jmztab.model.MsRun 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);
}
Aggregations