Skip to content

Commit

Permalink
add export project metadata task
Browse files Browse the repository at this point in the history
  • Loading branch information
robinschmid committed Oct 25, 2024
1 parent 436896a commit 6e5eac6
Show file tree
Hide file tree
Showing 14 changed files with 527 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2024 The MZmine Development Team
* Copyright (c) 2004-2024 The mzmine Development Team
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -174,6 +174,7 @@
import io.github.mzmine.modules.visualization.massvoltammogram.MassvoltammogramFromFileModule;
import io.github.mzmine.modules.visualization.msms.MsMsVisualizerModule;
import io.github.mzmine.modules.visualization.network_overview.FeatureNetworkOverviewModule;
import io.github.mzmine.modules.visualization.projectmetadata.io.ProjectMetadataExportModule;
import io.github.mzmine.modules.visualization.projectmetadata.io.ProjectMetadataImportModule;
import io.github.mzmine.modules.visualization.raw_data_summary.RawDataSummaryModule;
import io.github.mzmine.modules.visualization.scan_histogram.CorrelatedFeaturesMzHistogramModule;
Expand Down Expand Up @@ -218,6 +219,7 @@ public class BatchModeModulesList {
* {@link io.github.mzmine.modules.MZmineModuleCategory#PROJECTMETADATA}
*/
ProjectMetadataImportModule.class, //
ProjectMetadataExportModule.class, //

/*
* {@link io.github.mzmine.modules.MZmineModuleCategory.MainCategory#SPECTRAL_DATA}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2024 The MZmine Development Team
* Copyright (c) 2004-2024 The mzmine Development Team
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -83,7 +83,7 @@ The raw data files should always contain a unique identifier that is listed in f
new MetadataGroupingParameter("Unique sample ID metadata", """
Metadata column that defines a unique sample ID.
Go to Project/Metadata to load a metadata sheet and reload this module to select this column.
The values should be a substring found in the filenames.
The values should be a substring found in the filenames.
Make sure to use a prefix or suffix before and after numbers otherwise id1 also matches id10.
Just adding a id1_ will resolve this issue."""));
public static final OptionalParameter<MetadataGroupingParameter> unreactedControls = new OptionalParameter<>(
Expand All @@ -102,7 +102,8 @@ private static void exportExample(File file) {
"unique_substring_contained_in_filenames_better_not_start_or_end_with_number_add_suffix",
"([#6][CX3](=O)O)", "([#6][CX3](=O)O).(OC)>>[#6][CX3](=O)OC.O", 123.45));
try {
file = CSVUtils.ensureTsvOrCsvFormat(file, "csv");
file = CSVUtils.ensureTsvOrCsvFormat(file, "tsv");

CsvWriter.writeToFile(file, examples, OnlineReaction.class, WriterOptions.REPLACE);
} catch (IOException e) {
logger.log(Level.WARNING, "Cannot write example file", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2024 The MZmine Development Team
* Copyright (c) 2004-2024 The mzmine Development Team
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -29,7 +29,6 @@
import io.github.mzmine.modules.visualization.projectmetadata.table.columns.DoubleMetadataColumn;
import io.github.mzmine.modules.visualization.projectmetadata.table.columns.MetadataColumn;
import io.github.mzmine.modules.visualization.projectmetadata.table.columns.StringMetadataColumn;
import io.github.mzmine.parameters.Parameter;
import io.github.mzmine.parameters.impl.SimpleParameterSet;
import io.github.mzmine.parameters.parametertypes.ComboParameter;
import io.github.mzmine.parameters.parametertypes.StringParameter;
Expand Down Expand Up @@ -152,6 +151,6 @@ public MetadataColumn getInstance() {
"Type of the new parameter", AvailableTypes.values(), AvailableTypes.values()[0]);

public ProjectMetadataColumnParameters() {
super(new Parameter[]{title, description, valueType});
super(valueType, title, description);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2004-2024 The mzmine Development Team
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.mzmine.modules.visualization.projectmetadata.io;

import io.github.mzmine.datamodel.MZmineProject;
import io.github.mzmine.modules.MZmineModuleCategory;
import io.github.mzmine.modules.MZmineProcessingModule;
import io.github.mzmine.parameters.ParameterSet;
import io.github.mzmine.taskcontrol.Task;
import io.github.mzmine.util.ExitCode;
import java.time.Instant;
import java.util.Collection;
import java.util.logging.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ProjectMetadataExportModule implements MZmineProcessingModule {

private static final Logger logger = Logger.getLogger(
ProjectMetadataExportModule.class.getName());

private static final String MODULE_NAME = "Project metadata export";
private static final String MODULE_DESCRIPTION = "This module exports metadata to .tsv or .csv-format.";

@Override
public @NotNull String getName() {
return MODULE_NAME;
}

@Override
public @NotNull String getDescription() {
return MODULE_DESCRIPTION;
}

@Override
public @Nullable Class<? extends ParameterSet> getParameterSetClass() {
return ProjectMetadataImportParameters.class;
}

@Override
public @NotNull MZmineModuleCategory getModuleCategory() {
return MZmineModuleCategory.PROJECTMETADATA;
}

@Override
public @NotNull ExitCode runModule(@NotNull MZmineProject project,
@NotNull ParameterSet parameters, @NotNull Collection<Task> tasks,
@NotNull Instant moduleCallDate) {
tasks.add(new ProjectMetadataExportTask(moduleCallDate, parameters));

return ExitCode.OK;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2004-2024 The mzmine Development Team
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.mzmine.modules.visualization.projectmetadata.io;

import io.github.mzmine.parameters.ParameterSet;
import io.github.mzmine.parameters.impl.SimpleParameterSet;
import io.github.mzmine.parameters.parametertypes.ComboParameter;
import io.github.mzmine.parameters.parametertypes.filenames.FileNameParameter;
import io.github.mzmine.parameters.parametertypes.filenames.FileSelectionType;
import io.github.mzmine.util.files.ExtensionFilters;
import java.io.File;

public class ProjectMetadataExportParameters extends SimpleParameterSet {

public enum MetadataFileFormat {
DEFAULT,
/**
* Adds additional headers for data types
*/
MZMINE_INTERNAL,
/**
* GNPS requires Filename and then all other columns with ATTRIBUTE_ prefix
*/
GNPS;

@Override
public String toString() {
return switch (this) {
case DEFAULT -> "Default";
case MZMINE_INTERNAL -> "mzmine internal";
case GNPS -> "GNPS";
};
}
}

public static final FileNameParameter fileName = new FileNameParameter("Metadata file", """
CSV or TSV file to save metadata to.""", ExtensionFilters.CSV_TSV_EXPORT,
FileSelectionType.SAVE);

public static final ComboParameter<MetadataFileFormat> format = new ComboParameter<>("Format",
"Format to export the metadata in", MetadataFileFormat.values(), MetadataFileFormat.DEFAULT);

public ProjectMetadataExportParameters() {
super(fileName, format);
}

public static ParameterSet create(final File file, final MetadataFileFormat mFormat) {
ParameterSet params = new ProjectMetadataExportParameters().cloneParameterSet();
params.setParameter(fileName, file);
params.setParameter(format, mFormat);
return params;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2004-2024 The mzmine Development Team
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.mzmine.modules.visualization.projectmetadata.io;

import io.github.mzmine.modules.visualization.projectmetadata.io.ProjectMetadataExportParameters.MetadataFileFormat;
import io.github.mzmine.modules.visualization.projectmetadata.table.MetadataTable;
import io.github.mzmine.parameters.ParameterSet;
import io.github.mzmine.project.ProjectService;
import io.github.mzmine.taskcontrol.AbstractSimpleToolTask;
import java.io.File;
import java.time.Instant;
import org.jetbrains.annotations.NotNull;

public class ProjectMetadataExportTask extends AbstractSimpleToolTask {

private final File file;
private final MetadataFileFormat format;

/**
* @param moduleCallDate the call date of module to order execution order
*/
public ProjectMetadataExportTask(final @NotNull Instant moduleCallDate,
@NotNull final ParameterSet parameters) {
super(moduleCallDate, parameters);
file = parameters.getValue(ProjectMetadataExportParameters.fileName);
format = parameters.getValue(ProjectMetadataExportParameters.format);
}

@Override
protected void process() {
MetadataTable metadata = ProjectService.getProject().getProjectMetadata();
ProjectMetadataWriter writer = new ProjectMetadataWriter(metadata, format);
if (!writer.exportTo(file)) {
error("Error during metadata file export");
return;
}
}

@Override
public String getTaskDescription() {
return "Exporting project metadata to file";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2022 The MZmine Development Team
* Copyright (c) 2004-2024 The mzmine Development Team
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -43,7 +43,7 @@ public class ProjectMetadataImportModule implements MZmineProcessingModule {
ProjectMetadataImportModule.class.getName());

private static final String MODULE_NAME = "Project metadata import";
private static final String MODULE_DESCRIPTION = "This module imports metadata into the project from .tsv-format files.";
private static final String MODULE_DESCRIPTION = "This module imports metadata into the project from .tsv or .csv-format.";

@Override
public @NotNull String getName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2022 The MZmine Development Team
* Copyright (c) 2004-2024 The mzmine Development Team
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -46,15 +46,20 @@ exporting metadata file (after importing a few data files).""",
"Skip column on error",
"Error during data conversion or parsing will be logged but does not end the import", false);

public static final BooleanParameter removeAttributePrefix = new BooleanParameter(
"Remove attribute prefix", "Remove the ATTRIBUTE_ prefix commonly used by GNPS", false);


public ProjectMetadataImportParameters() {
super(fileName, skipErrorColumns);
super(fileName, skipErrorColumns, removeAttributePrefix);
}

public static ParameterSet create(final File file, final boolean skipErrorCol) {
public static ParameterSet create(final File file, final boolean skipErrorCol,
final boolean removeGnpsAttributePrefix) {
ParameterSet params = new ProjectMetadataImportParameters().cloneParameterSet();
params.setParameter(fileName, file);
params.setParameter(skipErrorColumns, skipErrorCol);
params.setParameter(removeAttributePrefix, removeGnpsAttributePrefix);
return params;
}

Expand Down
Loading

0 comments on commit 6e5eac6

Please sign in to comment.