use of model.Rule in project Smiles2Monomers by yoann-dufresne.
the class ResidueJsonLoader method objectFromJson.
@Override
protected Family objectFromJson(JSONObject obj) {
Residue res = new Residue((String) obj.get("mono"), (String) obj.get("smarts"), true);
res.setIdx(((Number) obj.get("id")).intValue());
JSONArray array = (JSONArray) obj.get("links");
for (Object o : array) {
JSONObject jso = (JSONObject) o;
String name = (String) jso.get("name");
Rule rule = null;
try {
rule = this.rules.getObject(name);
} catch (NullPointerException e) {
System.err.println("Unknown link " + name);
}
int idx = ((Number) jso.get("atom")).intValue();
IAtom ia = res.getMolecule().getAtom(idx);
res.addLink(ia, rule);
}
// Family construction
Family fam = new Family();
try {
for (String name : ((String) obj.get("family")).split("€")) {
Monomer m = this.monos.getObject(name);
fam.addMonomer(m);
}
} catch (NullPointerException e) {
System.err.println("Unloaded residue " + res.getMonoName());
}
fam.addResidue(res);
for (Object jso : (JSONArray) obj.get("depandances")) {
int idx = ((Number) jso).intValue();
fam.addDependance(idx, new Integer(res.getId()));
}
return fam;
}
use of model.Rule in project Smiles2Monomers by yoann-dufresne.
the class RulesJsonLoader method objectFromJson.
@Override
protected Rule objectFromJson(JSONObject obj) {
JSONArray sWeights = (JSONArray) obj.get("weights");
int[] weights = new int[sWeights.size()];
for (int i = 0; i < weights.length; i++) weights[i] = ((Number) sWeights.get(i)).intValue();
JSONArray array = (JSONArray) obj.get("transformations");
String[] transformations = new String[array.size()];
for (int i = 0; i < transformations.length; i++) transformations[i] = (String) array.get(i);
array = (JSONArray) obj.get("exclusion");
String[] exclusions = new String[array.size()];
for (int i = 0; i < array.size(); i++) exclusions[i] = (String) array.get(i);
return new Rule((String) obj.get("name"), (String) obj.get("formula"), transformations, weights, exclusions, (Boolean) obj.get("alone"));
}
use of model.Rule in project Smiles2Monomers by yoann-dufresne.
the class ResidueCreator method residuesFromMonomers.
/*
* Create residues from a monomer according to the rules database.
*/
private Set<Residue> residuesFromMonomers(Family family) {
Monomer mono = family.getMonomers().get(0);
Set<Residue> residues = new HashSet<>();
RulesDB rules = new RulesDB();
rules.addDB(this.dbAlone);
// List of residues in witch we search other residues.
Set<Residue> searchResidues = new HashSet<>();
IMolecule m = null;
try {
m = mono.getMolecule().clone();
AtomContainerManipulator.convertImplicitToExplicitHydrogens(m);
} catch (CloneNotSupportedException e1) {
e1.printStackTrace();
}
searchResidues.add(new Residue(mono.getCode(), this.sg.createSMILES(m), true));
boolean firstTime = true;
while (searchResidues.size() != 0) {
Set<Residue> nextLevelResidues = new HashSet<>();
for (Residue res : searchResidues) {
res.setExplicitHydrogens(true);
Set<Residue> newResidues = new HashSet<>();
for (Rule rule : rules.getObjects()) {
Set<Residue> ruleResidues = this.getResiduesWithRule(res, rule);
newResidues.addAll(ruleResidues);
for (Residue r : ruleResidues) nextLevelResidues.add(r);
}
for (Residue newRes : newResidues) {
family.addResidue(newRes);
if (family.containsMonomer(res.getMonoName()) && res.getAtomicLinks().size() > 0)
family.addDependance(newRes, res);
}
res.setExplicitHydrogens(false);
}
searchResidues = nextLevelResidues;
residues.addAll(searchResidues);
if (firstTime) {
firstTime = false;
rules.addDB(this.dbNotAlone);
}
}
if (residues.size() == 0) {
String smiles = this.sg.createSMILES(m);
Residue res = Residue.constructResidue(mono.getName(), smiles);
family.addResidue(res);
residues.add(res);
}
if (this.verbose) {
System.out.println("Nb residues of " + mono.getCode() + " : " + residues.size());
for (Residue res : residues) System.out.println(" " + res.getSmiles());
System.out.println();
}
return residues;
}
use of model.Rule in project Smiles2Monomers by yoann-dufresne.
the class ResidueCreatorTests method linksLoadingTest.
@Test
public void linksLoadingTest() {
ResidueJsonLoader rjl = new ResidueJsonLoader(this.rules, this.monos);
rjl.saveFile(this.families, "tmp.json");
Residue.resetResidues();
FamilyDB loaded = rjl.loadFile("tmp.json");
new File("tmp.json").delete();
Family famTyr = null;
try {
famTyr = loaded.getObject("Tyr");
} catch (NullPointerException e) {
e.printStackTrace();
}
Residue tyrN = null;
for (Residue res : famTyr.getResidues()) if ("Tyr_pepN".equals(res.getName())) {
tyrN = res;
break;
}
Entry<IAtom, Rule> entry = tyrN.getAtomicLinks().entrySet().iterator().next();
IAtom a = entry.getKey();
Assert.assertEquals(a.getSymbol(), "N");
}
use of model.Rule in project Smiles2Monomers by yoann-dufresne.
the class ResidueCreatorTests method linksCreationTest.
@Test
public void linksCreationTest() {
Residue tyrN = null;
for (Residue res : this.residues) if ("Tyr_pepN".equals(res.getName())) {
tyrN = res;
break;
}
Entry<IAtom, Rule> entry = tyrN.getAtomicLinks().entrySet().iterator().next();
IAtom a = entry.getKey();
Assert.assertEquals(a.getSymbol(), "N");
}
Aggregations