Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed SD files not being imported completely #12

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/main/java/de/unijena/cheminf/mortar/model/io/Importer.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ else if(tmpFormat.getFormatName().equalsIgnoreCase(MDLV3000Format.getInstance().
}
//
/**
* Imports an SD file.
* Imports an SD file. If no name can be detected for a structure, the file name extended with the index of the
* structure in the file is used as name of the structure.
*
* @param aFile sdf
* @return the imported molecules in an IAtomContainerSet
Expand All @@ -266,15 +267,32 @@ private IAtomContainerSet importSDFile(File aFile) throws FileNotFoundException
IteratingSDFReader tmpSDFReader = new IteratingSDFReader(new FileInputStream(aFile),
SilentChemObjectBuilder.getInstance());
int tmpCounter = 0;
while(!Thread.currentThread().isInterrupted() && tmpSDFReader.hasNext()){
while(!Thread.currentThread().isInterrupted()){
if (!tmpSDFReader.hasNext()) {
tmpSDFReader.setSkip(true);
if (!tmpSDFReader.hasNext()) {
// there is no next
break;
}
// molecule just could not be read and has therefore been skipped
tmpSDFReader.setSkip(false);
Importer.LOGGER.info("Import failed for structure:\t" + tmpCounter + " (index of structure in file).");
tmpCounter++;
}
IAtomContainer tmpAtomContainer = tmpSDFReader.next();
String tmpName = this.findMoleculeName(tmpAtomContainer);
if(tmpName == null || tmpName.isBlank() || tmpName.isEmpty())
if(tmpName == null || tmpName.isBlank())
// the counter here equals the index of the structure in the file
tmpName = FileUtil.getFileNameWithoutExtension(aFile) + tmpCounter;
tmpAtomContainer.setProperty(Importer.MOLECULE_NAME_PROPERTY_KEY, tmpName);
tmpAtomContainerSet.addAtomContainer(tmpAtomContainer);
tmpCounter++;
}
int tmpFailedImportsCount = tmpCounter - tmpAtomContainerSet.getAtomContainerCount();
if (tmpFailedImportsCount > 0) {
Importer.LOGGER.warning("The import from SD file failed for a total of " + tmpFailedImportsCount +
" structure(s).");
}
return tmpAtomContainerSet;
}
//
Expand Down
Loading