use of ambit2.base.interfaces.IStructureRecord.MOL_TYPE in project ambit-mirror by ideaconsult.
the class MoleculeReader method handleFormat.
protected IAtomContainer handleFormat(MOL_TYPE format, IStructureRecord target) throws AmbitException {
switch(format) {
case SDF:
{
try {
IAtomContainer ac = MoleculeTools.readMolfile(target.getContent());
if ((ac != null) && (ac.getProperties() != null)) {
Object title = ac.getProperty(CDKConstants.TITLE);
if (title != null) {
if (CASProcessor.isValidFormat(title.toString()))
try {
if (casTransformer == null)
casTransformer = new CASProcessor();
ac.setProperty(AmbitCONSTANTS.CASRN, casTransformer.process(title.toString()));
ac.removeProperty(CDKConstants.TITLE);
} catch (Exception x) {
}
if (removeCDKTitle)
ac.removeProperty(CDKConstants.TITLE);
}
// CAS transformation was moved from MyIterating MDLReader
Iterator props = ac.getProperties().entrySet().iterator();
while (props.hasNext()) {
Map.Entry entry = (Map.Entry) props.next();
Object key = entry.getKey();
Object value = entry.getValue();
if (Property.test4CAS(key.toString().toLowerCase())) {
if (casTransformer == null)
casTransformer = new CASProcessor();
ac.setProperty(key, casTransformer.process(value.toString()));
}
}
/* !%&$* PubChem */
Object sid = ac.getProperty("PUBCHEM_SUBSTANCE_ID");
if (sid != null) {
ac.setProperty("PUBCHEM_SID", sid);
ac.removeProperty("PUBCHEM_SUBSTANCE_ID");
}
Object cid = ac.getProperty("PUBCHEM_COMPOUND_CID");
if (cid != null) {
ac.setProperty("PUBCHEM_CID", cid);
ac.removeProperty("PUBCHEM_COMPOUND_CID");
}
Object synonyms = ac.getProperty("PUBCHEM_SUBSTANCE_SYNONYM");
if (synonyms != null) {
BufferedReader reader = new BufferedReader(new StringReader(synonyms.toString()));
String line = null;
while ((line = reader.readLine()) != null) {
String type = "PUBCHEM Name";
String value = line;
if (value.startsWith("DSSTox_RID_")) {
type = "DSSTox_RID";
value = value.substring(11);
} else if (value.startsWith("DSSTox_GSID_")) {
type = "DSSTox_GSID";
value = value.substring(12);
} else if (value.startsWith("DSSTox_CID_")) {
type = "DSSTox_CID";
value = value.substring(11);
} else if (value.startsWith("Tox21_")) {
type = "Tox21";
value = value.substring(6);
} else if (value.startsWith("CAS-")) {
type = "CASRN";
value = value.substring(4);
} else if (value.startsWith("NCGC")) {
type = "NCGC";
} else {
value = value.toLowerCase();
}
ac.setProperty(type, value);
}
reader.close();
ac.removeProperty("PUBCHEM_SUBSTANCE_SYNONYM");
}
ac.removeProperty(CDKConstants.REMARK);
}
if (ac != null) {
if (MoleculeTools.repairBondOrder4(ac)) {
// ok, all set
} else {
try {
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(ac);
} catch (Exception x) {
ac.setProperty("ERROR.atomtypes", String.format("%s\t%s", x.getClass().getName(), x.getMessage()));
}
try {
hadder.addImplicitHydrogens(ac);
} catch (Exception x) {
ac.setProperty("ERROR.implicith", String.format("%s\t%s", x.getClass().getName(), x.getMessage()));
}
}
}
return ac;
} catch (Exception x) {
throw new AmbitException(x);
}
}
case CML:
try {
IAtomContainer ac = MoleculeTools.readCMLMolecule(target.getContent());
if ((ac != null) && (ac.getProperties() != null)) {
Object title = ac.getProperty(CDKConstants.TITLE);
if (title != null) {
if (CASProcessor.isValidFormat(title.toString()))
try {
if (casTransformer == null)
casTransformer = new CASProcessor();
ac.setProperty(AmbitCONSTANTS.CASRN, casTransformer.process(title.toString()));
} catch (Exception x) {
}
ac.removeProperty(CDKConstants.TITLE);
}
ac.removeProperty(CDKConstants.REMARK);
}
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(ac);
hadder.addImplicitHydrogens(ac);
return ac;
} catch (Exception x) {
throw new AmbitException(x);
}
case INC:
try {
if (target.getContent().startsWith("InChI=")) {
if (inchiFactory == null)
inchiFactory = InChIGeneratorFactory.getInstance();
InChIToStructure c = inchiFactory.getInChIToStructure(target.getContent(), SilentChemObjectBuilder.getInstance());
return c.getAtomContainer();
} else {
// smiles
if (smiParser == null)
smiParser = new SmilesParser(SilentChemObjectBuilder.getInstance());
IAtomContainer mol = smiParser.parseSmiles(target.getContent());
// atom typing will drop aromatic flags ...
if (atomtypingonsmiles) {
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
for (IBond b : mol.bonds()) if (b.isAromatic())
for (IAtom a : b.atoms()) a.setFlag(CDKConstants.ISAROMATIC, true);
}
return mol;
}
} catch (Exception x) {
throw new AmbitException(x);
}
case NANO:
try {
Class clazz = FileInputState.class.getClassLoader().loadClass("net.idea.ambit2.rest.nano.MoleculeNanoReader");
Method method = clazz.getMethod("nm2atomcontainer", IStructureRecord.class);
return (IAtomContainer) method.invoke(null, target);
} catch (Exception x) {
if (x instanceof AmbitException)
throw (AmbitException) x;
else
throw new AmbitException(x);
}
case PDB:
try {
IAtomContainer ac = MoleculeTools.readPDBfile(target.getContent());
for (IAtom atom : ac.atoms()) {
if (atom.getImplicitHydrogenCount() == null)
atom.setImplicitHydrogenCount(0);
}
return ac;
} catch (Exception x) {
throw new AmbitException(x);
}
default:
{
throw new AmbitException("Unknown format " + target.getFormat());
}
}
}
use of ambit2.base.interfaces.IStructureRecord.MOL_TYPE in project ambit-mirror by ideaconsult.
the class StructureEditorProcessor method process.
@Override
public String process(IStructureRecord record) throws AmbitException {
try {
MOL_TYPE mtype = MOL_TYPE.valueOf(record.getFormat());
IAtomContainer mol = null;
switch(mtype) {
case SDF:
{
MoleculeReader reader = new MoleculeReader();
mol = reader.process(record);
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
reader = null;
break;
}
case CSV:
{
// smiles
SmilesParserWrapper p = SmilesParserWrapper.getInstance(SMILES_PARSER.CDK);
mol = p.parseSmiles(record.getContent());
break;
}
default:
throw new AmbitException("Unsupported format");
}
// actions
if (mol != null && mol.getAtomCount() > 0)
switch(command) {
case layout:
{
IAtomContainerSet molecules = new AtomContainerSet();
CompoundImageTools cit = new CompoundImageTools();
cit.generate2D(mol, true, molecules);
mol = MoleculeTools.newAtomContainer(mol.getBuilder());
for (IAtomContainer m : molecules.atomContainers()) {
mol.add(m);
}
break;
}
case aromatize:
if (mol != null)
CDKHueckelAromaticityDetector.detectAromaticity(mol);
break;
case dearomatize:
if (mol != null) {
Kekulization.kekulize(mol);
for (IBond bond : mol.bonds()) bond.setFlag(CDKConstants.ISAROMATIC, false);
}
break;
default:
throw new AmbitException("Unknown command");
}
StringWriter w = new StringWriter();
SDFWriter writer = new SDFWriter(w);
writer.write(mol);
writer.close();
return w.toString();
} catch (AmbitException x) {
throw x;
} catch (Exception x) {
throw new AmbitException(x);
}
}
Aggregations