Skip to content

Commit

Permalink
Initialize reference spectra data only once
Browse files Browse the repository at this point in the history
  • Loading branch information
meier-rene committed Oct 28, 2024
1 parent 3f7ee62 commit d1623d0
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
public class OfflineIndividualMoNASpectralSimilarityInitialiser implements IScoreInitialiser {
private Logger logger = Logger.getLogger(OfflineIndividualMoNASpectralSimilarityInitialiser.class);

private static volatile SpectralPeakListCollection spectralPeakLists = null;
private static final Object lock = new Object();

@Override
public void initScoreParameters(Settings settings) throws Exception {
logger.setLevel(Level.toLevel("info"));
Expand All @@ -30,44 +33,51 @@ public void initScoreParameters(Settings settings) throws Exception {
return;
}

MultipleTandemMassPeakListReader multiplePeakListReader = new MultipleTandemMassPeakListReader(settings);
SpectralPeakListCollection spectralPeakLists = null;
Path offlineSpectralFilePath = null;
if (spectralPeakLists == null) {
synchronized (lock) {
if (spectralPeakLists == null) {
MultipleTandemMassPeakListReader multiplePeakListReader = new MultipleTandemMassPeakListReader(settings);
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());
}
}
offlineSpectralFilePath = Paths.get(uri);
}
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());
}
}
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);
if (Files.isRegularFile(offlineSpectralFilePath)) {
logger.info("Load reference data from file: " + offlineSpectralFilePath);
try (InputStream inStream = Files.newInputStream(offlineSpectralFilePath)) {
spectralPeakLists = multiplePeakListReader.readMultiple(inStream);
}
} 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));
}
}
}
} 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
Expand Up @@ -19,62 +19,71 @@
import de.ipbhalle.metfraglib.settings.Settings;

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

private static volatile SpectralPeakListCollection spectralPeakLists = null;
private static final Object lock = new Object();

@Override
public void initScoreParameters(Settings settings) throws Exception {
logger.setLevel(Level.toLevel("info"));
@Override
public void initScoreParameters(Settings settings) throws Exception {
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;
}
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 (spectralPeakLists == null) {
synchronized (lock) {
if (spectralPeakLists == null) {
MultipleTandemMassPeakListReader multiplePeakListReader = new MultipleTandemMassPeakListReader(settings);
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());
}
}
offlineSpectralFilePath = Paths.get(uri);
}
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());
}
}
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);
}
} 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 (Files.isRegularFile(offlineSpectralFilePath)) {
logger.info("Load reference data from file: " + offlineSpectralFilePath);
try (InputStream inStream = Files.newInputStream(offlineSpectralFilePath)) {
spectralPeakLists = multiplePeakListReader.readMultiple(inStream);
}
} 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);
if (spectralPeakLists != null) {
//check whether MoNA InChIKeys are given as resource
spectralPeakLists.calculateSimilarities((SortedTandemMassPeakList) settings.get(VariableNames.PEAK_LIST_NAME));
}
}
}
}
}
if (spectralPeakLists != null) {
settings.set(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME, spectralPeakLists);
}
}

public void postProcessScoreParameters(Settings settings) {
return;
}

public void postProcessScoreParameters(Settings settings) {
return;
}

}

0 comments on commit d1623d0

Please sign in to comment.