use of org.openscience.cdk.exception.CDKException in project MassBank-web by MassBank.
the class StructureToSvgStringGenerator method structureFromInChI.
public static IAtomContainer structureFromInChI(String inchi) {
IAtomContainer mol = null;
try {
// get atom container
InChIGeneratorFactory inchiFactory = InChIGeneratorFactory.getInstance();
InChIToStructure inchi2structure = inchiFactory.getInChIToStructure(inchi, DefaultChemObjectBuilder.getInstance());
mol = inchi2structure.getAtomContainer();
} catch (CDKException e) {
System.out.println("Warning: " + e.getLocalizedMessage());
}
return mol;
}
use of org.openscience.cdk.exception.CDKException in project Smiles2Monomers by yoann-dufresne.
the class ResidueCreator method getResiduesWithRule.
/*
* Search Residues for one monomer and one rule.
*/
private Set<Residue> getResiduesWithRule(Residue res, Rule rule) {
Set<Residue> residues = new HashSet<>();
IMolecule ruleMol = null;
try {
ruleMol = SmilesConverter.conv.transform(rule.getFormula(), false, false, true);
} catch (InvalidSmilesException e) {
System.err.println("Impossible to parse " + rule.getName() + " rule");
return residues;
}
boolean status = false;
try {
status = UniversalIsomorphismTester.isSubgraph(res.getMolecule(), ruleMol);
} catch (CDKException e) {
e.printStackTrace();
}
// If rule is found
if (status) {
List<List<RMap>> matches = null;
try {
matches = UniversalIsomorphismTester.getSubgraphAtomsMaps(res.getMolecule(), ruleMol);
} catch (CDKException e) {
e.printStackTrace();
}
if (!"".equals(rule.getExclusion()))
for (String exclusion : rule.getExclusion()) this.removeExclusions(matches, exclusion, res.getMolecule());
for (List<RMap> match : matches) {
Set<Residue> residuesByMatch = this.createResidue(match, rule, res);
residues.addAll(residuesByMatch);
}
}
return residues;
}
use of org.openscience.cdk.exception.CDKException in project MassBank-web by MassBank.
the class StructureToSvgStringGenerator method structureFromSMILES.
public static IAtomContainer structureFromSMILES(String smiles) {
IAtomContainer mol = null;
try {
// get atom container
SmilesParser smipar = new SmilesParser(DefaultChemObjectBuilder.getInstance());
mol = smipar.parseSmiles(smiles);
} catch (CDKException e) {
System.out.println("Warning: " + e.getLocalizedMessage());
}
return mol;
}
use of org.openscience.cdk.exception.CDKException in project Smiles2Monomers by yoann-dufresne.
the class ParsePDBe method main.
@SuppressWarnings("unchecked")
public static void main(String[] args) {
File directory = new File("/mnt/data/PDBe/mmcif");
Map<String, JSONObject> names = new HashMap<>();
JSONArray poly = new JSONArray();
JSONArray mono = new JSONArray();
JSONArray nonpoly = new JSONArray();
for (File molfile : directory.listFiles()) {
try {
BufferedReader br = new BufferedReader(new FileReader(molfile));
JSONObject jso = new JSONObject();
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("_chem_comp.pdbx_subcomponent_list")) {
if (line.contains("?")) {
mono.add(jso);
jso.put("desc", jso.get("name"));
jso.put("name", jso.get("id"));
} else {
// Graph spliting
String[] split = line.split("\"")[1].split(" ");
JSONObject graph = new JSONObject();
JSONArray v = new JSONArray();
graph.put("V", v);
JSONArray e = new JSONArray();
graph.put("E", e);
for (int i = 0; i < split.length; i++) {
v.add(split[i]);
if (i > 0) {
JSONArray bond = new JSONArray();
bond.add(i - 1);
bond.add(i);
e.add(bond);
}
}
jso.put("graph", graph);
// Add in polymers
poly.add(jso);
}
} else if (line.startsWith("_chem_comp.id")) {
System.out.println(line);
line = line.replaceAll("_chem_comp.id", "");
line = line.replaceAll(" ", "").replaceAll("\t", "");
jso.put("id", line);
names.put(line, jso);
} else if (line.startsWith("_chem_comp.name")) {
System.out.println(line);
line = line.replaceAll("_chem_comp.name", "");
line = line.replaceAll(" ", "").replaceAll("\t", "");
jso.put("name", line);
jso.put("desc", line);
} else if (jso.containsKey("id") && line.startsWith((String) jso.get("id")) && line.contains("SMILES") && line.contains("ACDLabs")) {
System.out.println(line);
if (line.contains("\"")) {
String[] split = line.split("\"");
line = split[split.length - 2];
} else {
String[] split = line.split(" ");
line = split[split.length - 1];
}
try {
IMolecule mol = SmilesConverter.conv.transform(line);
if (ConnectivityChecker.isConnected(mol)) {
StructureDiagramGenerator sdg = new StructureDiagramGenerator(mol);
sdg.generateCoordinates();
jso.put("smiles", line);
}
} catch (InvalidSmilesException e) {
} catch (CDKException e) {
} catch (IllegalArgumentException e) {
}
} else if (jso.containsKey("id") && line.startsWith((String) jso.get("id")) && line.contains("SMILES") && !jso.containsKey("smiles")) {
if (line.contains("\"")) {
String[] split = line.split("\"");
if (split.length > 1)
line = split[1];
} else {
String[] split = line.split(" ");
line = split[split.length - 1];
}
try {
IMolecule mol = SmilesConverter.conv.transform(line);
if (ConnectivityChecker.isConnected(mol)) {
StructureDiagramGenerator sdg = new StructureDiagramGenerator(mol);
sdg.generateCoordinates();
jso.put("smiles", line);
}
} catch (InvalidSmilesException e) {
} catch (CDKException e) {
} catch (IllegalArgumentException e) {
}
} else if (line.startsWith("_chem_comp.type")) {
if (line.contains("NON-POLYMER"))
nonpoly.add(jso);
}
}
if (!jso.containsKey("smiles") || jso.get("smiles") == null) {
poly.remove(jso);
mono.remove(jso);
nonpoly.remove(jso);
names.remove(jso);
System.err.println(jso.toJSONString());
}
System.out.println();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
HashSet<String> needed = new HashSet<>();
for (Object o : poly) {
JSONObject p = (JSONObject) o;
JSONObject graph = (JSONObject) p.get("graph");
JSONArray v = (JSONArray) graph.get("V");
for (Object o2 : v) {
String name = (String) o2;
needed.add(name);
}
}
JSONArray minMonos = new JSONArray();
for (String name : needed) {
if (names.containsKey(name)) {
minMonos.add(names.get(name));
System.out.println(name + " added");
} else {
System.err.println("Impossible to add " + name);
}
}
System.out.println(poly.size());
System.out.println(mono.size());
System.out.println(nonpoly.size());
System.out.flush();
// Save files
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("data/pdbe_monos.json"));
bw.write(nonpoly.toJSONString());
bw.close();
bw = new BufferedWriter(new FileWriter("data/pdbe_polys.json"));
bw.write(poly.toJSONString());
bw.close();
bw = new BufferedWriter(new FileWriter("data/pdbe_monos_extended.json"));
bw.write(mono.toJSONString());
bw.close();
bw = new BufferedWriter(new FileWriter("data/pdbe_monos_min.json"));
bw.write(minMonos.toJSONString());
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.openscience.cdk.exception.CDKException in project Smiles2Monomers by yoann-dufresne.
the class PictureGenerator method transform.
/**/
public Molecule transform(String smiles) throws InvalidSmilesException {
Molecule imol = null;
imol = new Molecule(this.sp.parseSmiles(smiles));
for (IAtom a : imol.atoms()) a.setImplicitHydrogenCount(0);
StructureDiagramGenerator sdg = new StructureDiagramGenerator(imol);
try {
sdg.generateCoordinates();
} catch (CDKException e) {
System.err.println(smiles);
e.printStackTrace();
}
imol = new Molecule(sdg.getMolecule());
return imol;
}
Aggregations