Skip to content

Commit

Permalink
Fixed #789 - Import *.msl, *.msp files - Support Charset
Browse files Browse the repository at this point in the history
  • Loading branch information
eselmeister committed Oct 20, 2021
1 parent 0b40353 commit 212f9a3
Show file tree
Hide file tree
Showing 14 changed files with 344 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Manifest-Version: 1.0
Automatic-Module-Name: org.eclipse.chemclipse.msd.converter.supplier.amdis.ui
Bundle-ManifestVersion: 2
Bundle-Name: Ui
Bundle-Name: AMDIS Converter UI
Bundle-SymbolicName: org.eclipse.chemclipse.msd.converter.supplier.amdis.ui;singleton:=true
Bundle-Version: 0.9.0.qualifier
Bundle-Activator: org.eclipse.chemclipse.msd.converter.supplier.amdis.ui.Activator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

import org.eclipse.chemclipse.msd.converter.supplier.amdis.preferences.PreferenceSupplier;
import org.eclipse.chemclipse.msd.converter.supplier.amdis.ui.Activator;
import org.eclipse.chemclipse.support.text.CharsetNIO;
import org.eclipse.chemclipse.support.ui.preferences.fieldeditors.SpacerFieldEditor;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.ComboFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
Expand All @@ -24,7 +27,8 @@ public PreferencePage() {

super(GRID);
setPreferenceStore(Activator.getDefault().getPreferenceStore());
setDescription("AMDIS Settings.");
setTitle("AMDIS Converter");
setDescription("");
}

/**
Expand All @@ -42,6 +46,12 @@ public void createFieldEditors() {
addField(new BooleanFieldEditor(PreferenceSupplier.P_EXPORT_INTENSITIES_AS_INTEGER, "Export intensities as Integer", getFieldEditorParent()));
addField(new BooleanFieldEditor(PreferenceSupplier.P_PARSE_COMPOUND_INFORMATION, "Parse Compound Information (*.CID)", getFieldEditorParent()));
addField(new BooleanFieldEditor(PreferenceSupplier.P_PARSE_MOL_INFORMATION, "Parse MOL Information (*.MOL)", getFieldEditorParent()));
//
addField(new SpacerFieldEditor(getFieldEditorParent()));
addField(new ComboFieldEditor(PreferenceSupplier.P_CHARSET_IMPORT_MSL, "Charset Import MSL", CharsetNIO.getOptions(), getFieldEditorParent()));
addField(new ComboFieldEditor(PreferenceSupplier.P_CHARSET_IMPORT_MSP, "Charset Import MSP", CharsetNIO.getOptions(), getFieldEditorParent()));
addField(new ComboFieldEditor(PreferenceSupplier.P_CHARSET_IMPORT_FIN, "Charset Import FIN", CharsetNIO.getOptions(), getFieldEditorParent()));
addField(new ComboFieldEditor(PreferenceSupplier.P_CHARSET_IMPORT_ELU, "Charset Import ELU", CharsetNIO.getOptions(), getFieldEditorParent()));
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ds.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
eclipse.preferences.version=1
enabled=true
generateBundleActivationPolicyLazy=true
path=OSGI-INF
validationErrorLevel=error
validationErrorLevel.missingImplicitUnbindMethod=error
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Automatic-Module-Name: org.eclipse.chemclipse.msd.converter.supplier.amdis
Eclipse-RegisterBuddy: org.eclipse.chemclipse.model, org.eclipse.chemclipse.msd.model
Bundle-ManifestVersion: 2
Bundle-Name: Amdis Plug-in
Bundle-Name: AMDIS Converter
Bundle-SymbolicName: org.eclipse.chemclipse.msd.converter.supplier.amdis;singleton:=true
Bundle-Version: 0.9.0.qualifier
Bundle-Activator: org.eclipse.chemclipse.msd.converter.supplier.amdis.Activator
Expand All @@ -15,10 +15,13 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.chemclipse.model;bundle-version="0.8.0",
org.apache.commons.io;bundle-version="2.2.0",
org.eclipse.chemclipse.support;bundle-version="0.8.0",
org.eclipse.chemclipse.numeric;bundle-version="0.8.0"
org.eclipse.chemclipse.numeric;bundle-version="0.8.0",
com.fasterxml.jackson.core.jackson-annotations;bundle-version="2.9.9"
Bundle-ActivationPolicy: lazy
Import-Package: org.osgi.service.component.annotations;version="1.2.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-Vendor: ChemClipse
Export-Package: org.eclipse.chemclipse.msd.converter.supplier.amdis.converter.misc,
org.eclipse.chemclipse.msd.converter.supplier.amdis.model,
org.eclipse.chemclipse.msd.converter.supplier.amdis.preferences
Service-Component: OSGI-INF/org.eclipse.chemclipse.msd.converter.supplier.amdis.system.ConverterProcessor.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.chemclipse.msd.converter.supplier.amdis.system.ConverterProcessor">
<service>
<provide interface="org.eclipse.chemclipse.processing.supplier.IProcessTypeSupplier"/>
</service>
<implementation class="org.eclipse.chemclipse.msd.converter.supplier.amdis.system.ConverterProcessor"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2020 Lablicate GmbH.
* Copyright (c) 2014, 2021 Lablicate GmbH.
*
* All rights reserved.
* This program and the accompanying materials are made available under the
Expand All @@ -15,7 +15,7 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -73,8 +73,9 @@ public class ELUReader implements IPeakReader {
@Override
public IProcessingInfo<IPeaks<?>> read(File file, IProgressMonitor monitor) throws FileNotFoundException, FileIsNotReadableException, FileIsEmptyException, IOException {

Charset charset = PreferenceSupplier.getCharsetImportELU();
IProcessingInfo<IPeaks<?>> processingInfo = new ProcessingInfo<IPeaks<?>>();
String content = FileUtils.readFileToString(file, StandardCharsets.US_ASCII);
String content = FileUtils.readFileToString(file, charset);
int numberOfHits = getNumberOfHits(content);
if(numberOfHits <= 0) {
processingInfo.addErrorMessage("AMDIS ELU Parser", "There seems to be no peak in the file.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 Lablicate GmbH.
* Copyright (c) 2020, 2021 Lablicate GmbH.
*
* All rights reserved.
* This program and the accompanying materials are made available under the
Expand All @@ -13,15 +13,18 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.eclipse.chemclipse.converter.exceptions.FileIsEmptyException;
import org.eclipse.chemclipse.converter.exceptions.FileIsNotReadableException;
import org.eclipse.chemclipse.msd.converter.io.AbstractMassSpectraReader;
import org.eclipse.chemclipse.msd.converter.io.IMassSpectraReader;
import org.eclipse.chemclipse.msd.converter.supplier.amdis.model.IVendorLibraryMassSpectrum;
import org.eclipse.chemclipse.msd.converter.supplier.amdis.preferences.PreferenceSupplier;
import org.eclipse.chemclipse.msd.model.core.IMassSpectra;
import org.eclipse.chemclipse.msd.model.implementation.MassSpectra;
import org.eclipse.core.runtime.IProgressMonitor;
Expand Down Expand Up @@ -64,7 +67,8 @@ private void parse(IMassSpectra massSpectra, File file, IProgressMonitor monitor
* ...
*/
if(file != null && file.exists()) {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
Charset charset = PreferenceSupplier.getCharsetImportFIN();
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset))) {
/*
* Settings and builder.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public class MSLReader extends AbstractMassSpectraReader implements IMassSpectra
private static final Pattern DATA = Pattern.compile("(.*)(Num Peaks:)(\\s*)(\\d*)(.*)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
private static final Pattern IONS = Pattern.compile("([+]?\\d+\\.?\\d*)(\\s+)([+-]?\\d+\\.?\\d*([eE][+-]?\\d+)?)"); // "(\\d+)(\\s+)(\\d+)" or "(\\d+)(\\s+)([+-]?\\d+\\.?\\d*([eE][+-]?\\d+)?)"
//
private static final String CHARSET_US = "US-ASCII";
private static final String RETENTION_INDICES_DELIMITER = ", ";
private static final String LINE_DELIMITER = "\r\n";

Expand Down Expand Up @@ -160,7 +159,7 @@ protected IVendorLibraryMassSpectrum extractMassSpectrum(String massSpectrumData
*/
private List<String> getMassSpectraData(File file) throws IOException {

Charset charset = Charset.forName(CHARSET_US);
Charset charset = PreferenceSupplier.getCharsetImportMSL();
List<String> massSpectraData = new ArrayList<String>();
//
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset))) {
Expand All @@ -186,7 +185,6 @@ private List<String> getMassSpectraData(File file) throws IOException {
* Don't forget to add the last mass spectrum.
*/
addMassSpectrumData(builder, massSpectraData);
bufferedReader.close();
}
//
return massSpectraData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*******************************************************************************/
package org.eclipse.chemclipse.msd.converter.supplier.amdis.io;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -114,39 +113,34 @@ public IMassSpectra read(File file, IProgressMonitor monitor) throws FileNotFoun
*/
private List<String> getMassSpectraData(File file) throws IOException {

Charset charSet = Charset.forName("US-ASCII");
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
InputStreamReader inputStreamReader = new InputStreamReader(bufferedInputStream, charSet);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
Charset charset = PreferenceSupplier.getCharsetImportMSP();
List<String> massSpectraData = new ArrayList<String>();
StringBuilder builder = new StringBuilder();
String line;
while((line = bufferedReader.readLine()) != null) {
//
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset))) {
StringBuilder builder = new StringBuilder();
String line;
while((line = bufferedReader.readLine()) != null) {
/*
* The mass spectra are divided by empty lines. If the builder has
* at least 1 char, then add a new potential mass spectrum to the
* mass spectra data list. Don't forget to build a new
* StringBuilder. In all other cases append the found lines to the
* StringBuilder.
*/
if(line.length() == 0) {
addMassSpectrumData(builder, massSpectraData);
builder = new StringBuilder();
} else {
builder.append(line);
builder.append(LINE_END);
}
}
/*
* The mass spectra are divided by empty lines. If the builder has
* at least 1 char, then add a new potential mass spectrum to the
* mass spectra data list. Don't forget to build a new
* StringBuilder. In all other cases append the found lines to the
* StringBuilder.
* Don't forget to add the last mass spectrum.
*/
if(line.length() == 0) {
addMassSpectrumData(builder, massSpectraData);
builder = new StringBuilder();
} else {
builder.append(line);
builder.append(LINE_END);
}
addMassSpectrumData(builder, massSpectraData);
}
/*
* Don't forget to add the last mass spectrum.
*/
addMassSpectrumData(builder, massSpectraData);
/*
* Close the streams.
*/
bufferedReader.close();
inputStreamReader.close();
bufferedInputStream.close();
//
return massSpectraData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@
*******************************************************************************/
package org.eclipse.chemclipse.msd.converter.supplier.amdis.preferences;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.chemclipse.logging.core.Logger;
import org.eclipse.chemclipse.msd.converter.supplier.amdis.Activator;
import org.eclipse.chemclipse.support.preferences.IPreferenceSupplier;
import org.eclipse.chemclipse.support.text.CharsetNIO;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.osgi.service.prefs.BackingStoreException;

public class PreferenceSupplier implements IPreferenceSupplier {

private static final Logger logger = Logger.getLogger(PreferenceSupplier.class);
//
public static final String P_SPLIT_LIBRARY = "splitLibrary";
public static final boolean DEF_SPLIT_LIBRARY = false;
public static final String P_EXCLUDE_UNCERTAIN_IONS = "excludeUncertainIons";
Expand All @@ -39,6 +45,15 @@ public class PreferenceSupplier implements IPreferenceSupplier {
public static final String P_PARSE_MOL_INFORMATION = "parseMolInformation";
public static final boolean DEF_PARSE_MOL_INFORMATION = true;
//
public static final String P_CHARSET_IMPORT_MSL = "charsetImportMSL";
public static final String DEF_CHARSET_IMPORT_MSL = CharsetNIO.US_ASCII.name();
public static final String P_CHARSET_IMPORT_MSP = "charsetImportMSP";
public static final String DEF_CHARSET_IMPORT_MSP = CharsetNIO.US_ASCII.name();
public static final String P_CHARSET_IMPORT_FIN = "charsetImportFIN";
public static final String DEF_CHARSET_IMPORT_FIN = CharsetNIO.US_ASCII.name();
public static final String P_CHARSET_IMPORT_ELU = "charsetImportELU";
public static final String DEF_CHARSET_IMPORT_ELU = CharsetNIO.US_ASCII.name();
//
private static IPreferenceSupplier preferenceSupplier;

public static IPreferenceSupplier INSTANCE() {
Expand All @@ -65,6 +80,7 @@ public String getPreferenceNode() {
public Map<String, String> getDefaultValues() {

Map<String, String> defaultValues = new HashMap<String, String>();
//
defaultValues.put(P_SPLIT_LIBRARY, Boolean.toString(DEF_SPLIT_LIBRARY));
defaultValues.put(P_EXCLUDE_UNCERTAIN_IONS, Boolean.toString(DEF_EXCLUDE_UNCERTAIN_IONS));
defaultValues.put(P_USE_UNIT_MASS_RESOLUTION, Boolean.toString(DEF_USE_UNIT_MASS_RESOLUTION));
Expand All @@ -73,6 +89,12 @@ public Map<String, String> getDefaultValues() {
defaultValues.put(P_EXPORT_INTENSITIES_AS_INTEGER, Boolean.toString(DEF_EXPORT_INTENSITIES_AS_INTEGER));
defaultValues.put(P_PARSE_COMPOUND_INFORMATION, Boolean.toString(DEF_PARSE_COMPOUND_INFORMATION));
defaultValues.put(P_PARSE_MOL_INFORMATION, Boolean.toString(DEF_PARSE_MOL_INFORMATION));
//
defaultValues.put(P_CHARSET_IMPORT_MSL, DEF_CHARSET_IMPORT_MSL);
defaultValues.put(P_CHARSET_IMPORT_MSP, DEF_CHARSET_IMPORT_MSP);
defaultValues.put(P_CHARSET_IMPORT_FIN, DEF_CHARSET_IMPORT_FIN);
defaultValues.put(P_CHARSET_IMPORT_ELU, DEF_CHARSET_IMPORT_ELU);
//
return defaultValues;
}

Expand Down Expand Up @@ -129,4 +151,65 @@ public static boolean isParseMolInformation() {
IEclipsePreferences preferences = INSTANCE().getPreferences();
return preferences.getBoolean(P_PARSE_MOL_INFORMATION, DEF_PARSE_MOL_INFORMATION);
}
}

public static Charset getCharsetImportMSL() {

return getCharset(P_CHARSET_IMPORT_MSL, DEF_CHARSET_IMPORT_MSL);
}

public static void setCharsetImportMSL(CharsetNIO charsetNIO) {

setCharset(P_CHARSET_IMPORT_MSL, charsetNIO);
}

public static Charset getCharsetImportMSP() {

return getCharset(P_CHARSET_IMPORT_MSP, DEF_CHARSET_IMPORT_MSP);
}

public static void setCharsetImportMSP(CharsetNIO charsetNIO) {

setCharset(P_CHARSET_IMPORT_MSP, charsetNIO);
}

public static Charset getCharsetImportFIN() {

return getCharset(P_CHARSET_IMPORT_FIN, DEF_CHARSET_IMPORT_FIN);
}

public static void setCharsetImportFIN(CharsetNIO charsetNIO) {

setCharset(P_CHARSET_IMPORT_FIN, charsetNIO);
}

public static Charset getCharsetImportELU() {

return getCharset(P_CHARSET_IMPORT_ELU, DEF_CHARSET_IMPORT_ELU);
}

public static void setCharsetImportELU(CharsetNIO charsetNIO) {

setCharset(P_CHARSET_IMPORT_ELU, charsetNIO);
}

private static Charset getCharset(String key, String def) {

IEclipsePreferences preferences = INSTANCE().getPreferences();
try {
return CharsetNIO.valueOf(preferences.get(key, def)).getCharset();
} catch(Exception e) {
return CharsetNIO.US_ASCII.getCharset();
}
}

private static void setCharset(String key, CharsetNIO charsetNIO) {

try {
IEclipsePreferences preferences = PreferenceSupplier.INSTANCE().getPreferences();
preferences.put(key, charsetNIO.name());
preferences.flush();
} catch(BackingStoreException e) {
logger.warn(e);
}
}
}
Loading

0 comments on commit 212f9a3

Please sign in to comment.