public static IChemModel getChemModelFromReader(ISimpleChemObjectReader cor,AbstractJChemPaintPanel panel)
throws CDKException {
panel.get2DHub().setRGroupHandler(null);
String error = null;
ChemModel chemModel = null;
IChemFile chemFile = null;
if (cor.accepts(IChemFile.class) && chemModel==null) {
// try to read a ChemFile
try {
chemFile = (IChemFile) cor.read((IChemObject) new ChemFile());
if (chemFile == null) {
error = "The object chemFile was empty unexpectedly!";
}
} catch (Exception exception) {
error = "Error while reading file: " + exception.getMessage();
exception.printStackTrace();
}
}
if (error != null) {
throw new CDKException(error);
}
if (chemModel == null && chemFile != null) {
chemModel = (ChemModel) chemFile.getChemSequence(0).getChemModel(0);
}
if (cor.accepts(ChemModel.class) && chemModel==null) {
// try to read a ChemModel
try {
chemModel = (ChemModel) cor.read((IChemObject) new ChemModel());
if (chemModel == null) {
error = "The object chemModel was empty unexpectedly!";
}
} catch (Exception exception) {
error = "Error while reading file: " + exception.getMessage();
exception.printStackTrace();
}
}
// Smiles reading
if (cor.accepts(IAtomContainerSet.class) && chemModel==null) {
// try to read a Molecule set
try {
IAtomContainerSet som = (AtomContainerSet) cor.read(new AtomContainerSet());
chemModel = new ChemModel();
chemModel.setMoleculeSet(som);
if (chemModel == null) {
error = "The object chemModel was empty unexpectedly!";
}
} catch (Exception exception) {
error = "Error while reading file: " + exception.getMessage();
exception.printStackTrace();
}
}
// MDLV3000 reading
if (cor.accepts(IAtomContainer.class) && chemModel==null) {
// try to read a Molecule
IAtomContainer mol = (AtomContainer) cor.read(new AtomContainer());
if(mol!=null )
try{
IAtomContainerSet newSet = new AtomContainerSet();
newSet.addAtomContainer(mol);
chemModel = new ChemModel();
chemModel.setMoleculeSet(newSet);
if (chemModel == null) {
error = "The object chemModel was empty unexpectedly!";
}
} catch (Exception exception) {
error = "Error while reading file: " + exception.getMessage();
exception.printStackTrace();
}
}
// RGroupQuery reading
if (cor.accepts(RGroupQuery.class) && chemModel==null) {
IRGroupQuery rgroupQuery = (RGroupQuery) cor.read(new RGroupQuery(DefaultChemObjectBuilder.getInstance()));
if(rgroupQuery!=null )
try{
chemModel = new ChemModel();
RGroupHandler rgHandler = new RGroupHandler(rgroupQuery, panel);
panel.get2DHub().setRGroupHandler(rgHandler);
chemModel.setMoleculeSet(rgHandler.getMoleculeSet(chemModel));
rgHandler.layoutRgroup();
} catch (Exception exception) {
error = "Error while reading file: " + exception.getMessage();
exception.printStackTrace();
}
}
if (error != null) {
throw new CDKException(error);
}
if (chemModel == null && chemFile != null) {
chemModel = (ChemModel) chemFile.getChemSequence(0).getChemModel(0);
}
//SmilesParser sets valencies, switch off by default.
if(cor instanceof SMILESReader){
IAtomContainer allinone = JChemPaintPanel.getAllAtomContainersInOne(chemModel);