Skip to content

Commit

Permalink
Load default reference data from resource directory if it is a direct…
Browse files Browse the repository at this point in the history
…ory or from file if it is a file.
  • Loading branch information
meier-rene committed Oct 25, 2024
1 parent 2bd0391 commit a81be39
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package de.ipbhalle.metfraglib.scoreinitialisation;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.net.URI;
import java.nio.file.*;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import de.ipbhalle.metfraglib.collection.SpectralPeakListCollection;
Expand All @@ -17,48 +18,57 @@
import de.ipbhalle.metfraglib.peaklistreader.MultipleTandemMassPeakListReader;
import de.ipbhalle.metfraglib.settings.Settings;


public class OfflineIndividualMoNASpectralSimilarityInitialiser implements IScoreInitialiser {
private Logger logger = Logger.getLogger(OfflineIndividualMoNASpectralSimilarityInitialiser.class);

@Override
public void initScoreParameters(Settings settings) throws Exception {
if(!settings.containsKey(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME) || settings.get(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME) == null) {
if(settings.containsKey(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME) && settings.get(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME) != null) return;
MultipleTandemMassPeakListReader multiplePeakListReader = new MultipleTandemMassPeakListReader(settings);
SpectralPeakListCollection spectralPeakLists = null;
Path offlineSpectralFilePath = null;
if(settings.containsKey(VariableNames.OFFLINE_SPECTRAL_DATABASE_FILE_NAME) && settings.get(VariableNames.OFFLINE_SPECTRAL_DATABASE_FILE_NAME) != null) {
offlineSpectralFilePath = Paths.get((String)settings.get(VariableNames.OFFLINE_SPECTRAL_DATABASE_FILE_NAME));
}
if (offlineSpectralFilePath !=null && Files.isRegularFile(offlineSpectralFilePath)) {
logger.info("Load reference data from file: " +offlineSpectralFilePath );
List<Path> libFiles = new ArrayList<Path>();
libFiles.add(offlineSpectralFilePath);
InputStream inStream = Files.newInputStream(offlineSpectralFilePath);
spectralPeakLists = multiplePeakListReader.readMultiple(inStream);
} else if (offlineSpectralFilePath !=null && Files.isDirectory(offlineSpectralFilePath)) {
logger.info("Load reference data from directory: " +offlineSpectralFilePath );
List<Path> libFiles = Files.walk(offlineSpectralFilePath)
.filter(Files::isRegularFile)
.filter(p -> p.getFileName().toString().endsWith(".mb"))
.collect(Collectors.toList());
spectralPeakLists = multiplePeakListReader.readMultiple(libFiles);
logger.setLevel(Level.toLevel("info"));

if (settings.containsKey(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME) &&
settings.get(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME) != null) {
return;
}

MultipleTandemMassPeakListReader multiplePeakListReader = new MultipleTandemMassPeakListReader(settings);
SpectralPeakListCollection spectralPeakLists = null;
Path offlineSpectralFilePath = null;

if (settings.containsKey(VariableNames.OFFLINE_SPECTRAL_DATABASE_FILE_NAME) &&
settings.get(VariableNames.OFFLINE_SPECTRAL_DATABASE_FILE_NAME) != null) {
offlineSpectralFilePath = Paths.get((String) settings.get(VariableNames.OFFLINE_SPECTRAL_DATABASE_FILE_NAME));
} else {
logger.info("Load reference data from default resource \"MoNA-export-LC-MS.mb\".");
URI uri = Objects.requireNonNull(getClass().getClassLoader().getResource("/MoNA-export-LC-MS.mb")).toURI();
if ("jar".equals(uri.getScheme())) {
try {
FileSystems.getFileSystem(uri);
} catch (FileSystemNotFoundException e) {
FileSystems.newFileSystem(uri, Collections.emptyMap());
}
}
else {
logger.info("Load reference data from resource file \"MoNA-export-LC-MS.mb\".");
InputStream inStream = null;
inStream = OfflineIndividualMoNASpectralSimilarityInitialiser.class.getResourceAsStream("/MoNA-export-LC-MS.mb");
offlineSpectralFilePath = Paths.get(uri);
}

if (Files.isRegularFile(offlineSpectralFilePath)) {
logger.info("Load reference data from file: " + offlineSpectralFilePath);
try (InputStream inStream = Files.newInputStream(offlineSpectralFilePath)) {
spectralPeakLists = multiplePeakListReader.readMultiple(inStream);
inStream.close();
}
//check whether MoNA InChIKeys are given as resource
spectralPeakLists.calculateSimilarities((SortedTandemMassPeakList)settings.get(VariableNames.PEAK_LIST_NAME));
} else if (Files.isDirectory(offlineSpectralFilePath)) {
logger.info("Load reference data from directory: " + offlineSpectralFilePath);
List<Path> libFiles = Files.walk(offlineSpectralFilePath)
.filter(Files::isRegularFile)
.filter(p -> p.getFileName().toString().endsWith(".mb"))
.collect(Collectors.toList());
logger.info("Reference data files found: " + libFiles);
spectralPeakLists = multiplePeakListReader.readMultiple(libFiles);
}

if (spectralPeakLists != null) {
//check whether MoNA InChIKeys are given as resource
spectralPeakLists.calculateSimilarities((SortedTandemMassPeakList) settings.get(VariableNames.PEAK_LIST_NAME));
settings.set(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME, spectralPeakLists);

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package de.ipbhalle.metfraglib.scoreinitialisation;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.net.URI;
import java.nio.file.*;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import de.ipbhalle.metfraglib.collection.SpectralPeakListCollection;
Expand All @@ -23,42 +24,53 @@ public class OfflineMetFusionSpectralSimilarityScoreInitialiser implements IScor

@Override
public void initScoreParameters(Settings settings) throws Exception {
if(!settings.containsKey(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME) || settings.get(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME) == null) {
if(settings.containsKey(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME) && settings.get(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME) != null) return;
MultipleTandemMassPeakListReader multiplePeakListReader = new MultipleTandemMassPeakListReader(settings);
SpectralPeakListCollection spectralPeakLists = null;
Path offlineSpectralFilePath = null;
if(settings.containsKey(VariableNames.OFFLINE_SPECTRAL_DATABASE_FILE_NAME) && settings.get(VariableNames.OFFLINE_SPECTRAL_DATABASE_FILE_NAME) != null) {
offlineSpectralFilePath = Paths.get((String)settings.get(VariableNames.OFFLINE_SPECTRAL_DATABASE_FILE_NAME));
}
if (offlineSpectralFilePath !=null && Files.isRegularFile(offlineSpectralFilePath)) {
logger.info("Load reference data from file: " +offlineSpectralFilePath );
List<Path> libFiles = new ArrayList<Path>();
libFiles.add(offlineSpectralFilePath);
InputStream inStream = Files.newInputStream(offlineSpectralFilePath);
spectralPeakLists = multiplePeakListReader.readMultiple(inStream);
} else if (offlineSpectralFilePath !=null && Files.isDirectory(offlineSpectralFilePath)) {
logger.info("Load reference data from directory: " +offlineSpectralFilePath );
List<Path> libFiles = Files.walk(offlineSpectralFilePath)
.filter(Files::isRegularFile)
.filter(p -> p.getFileName().toString().endsWith(".mb"))
.collect(Collectors.toList());
spectralPeakLists = multiplePeakListReader.readMultiple(libFiles);
logger.setLevel(Level.toLevel("info"));

if (settings.containsKey(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME) &&
settings.get(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME) != null) {
return;
}

MultipleTandemMassPeakListReader multiplePeakListReader = new MultipleTandemMassPeakListReader(settings);
SpectralPeakListCollection spectralPeakLists = null;
Path offlineSpectralFilePath = null;

if (settings.containsKey(VariableNames.OFFLINE_SPECTRAL_DATABASE_FILE_NAME) &&
settings.get(VariableNames.OFFLINE_SPECTRAL_DATABASE_FILE_NAME) != null) {
offlineSpectralFilePath = Paths.get((String) settings.get(VariableNames.OFFLINE_SPECTRAL_DATABASE_FILE_NAME));
} else {
logger.info("Load reference data from default resource \"MoNA-export-LC-MS.mb\".");
URI uri = Objects.requireNonNull(getClass().getClassLoader().getResource("/MoNA-export-LC-MS.mb")).toURI();
if ("jar".equals(uri.getScheme())) {
try {
FileSystems.getFileSystem(uri);
} catch (FileSystemNotFoundException e) {
FileSystems.newFileSystem(uri, Collections.emptyMap());
}
}
else {
logger.info("Load reference data from resource file \"MoNA-export-LC-MS.mb\".");
InputStream inStream = null;
inStream = OfflineMetFusionSpectralSimilarityScoreInitialiser.class.getResourceAsStream("/MoNA-export-LC-MS.mb");
offlineSpectralFilePath = Paths.get(uri);
}

if (Files.isRegularFile(offlineSpectralFilePath)) {
logger.info("Load reference data from file: " + offlineSpectralFilePath);
try (InputStream inStream = Files.newInputStream(offlineSpectralFilePath)) {
spectralPeakLists = multiplePeakListReader.readMultiple(inStream);
inStream.close();
}
//check whether MoNA InChIKeys are given as resource
spectralPeakLists.calculateSimilarities((SortedTandemMassPeakList)settings.get(VariableNames.PEAK_LIST_NAME));

settings.set(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME, spectralPeakLists);
} else if (Files.isDirectory(offlineSpectralFilePath)) {
logger.info("Load reference data from directory: " + offlineSpectralFilePath);
List<Path> libFiles = Files.walk(offlineSpectralFilePath)
.filter(Files::isRegularFile)
.filter(p -> p.getFileName().toString().endsWith(".mb"))
.collect(Collectors.toList());
logger.info("Reference data files found: " + libFiles);
spectralPeakLists = multiplePeakListReader.readMultiple(libFiles);
}

if (spectralPeakLists != null) {
//check whether MoNA InChIKeys are given as resource
spectralPeakLists.calculateSimilarities((SortedTandemMassPeakList) settings.get(VariableNames.PEAK_LIST_NAME));
settings.set(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME, spectralPeakLists);
}
}

public void postProcessScoreParameters(Settings settings) {
Expand Down

0 comments on commit a81be39

Please sign in to comment.