Search in sources :

Example 1 with Rule

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;
}
Also used : JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) Residue(model.Residue) Family(model.Family) JSONObject(org.json.simple.JSONObject) Rule(model.Rule) Monomer(model.Monomer) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 2 with Rule

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"));
}
Also used : JSONArray(org.json.simple.JSONArray) Rule(model.Rule)

Example 3 with Rule

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;
}
Also used : RulesDB(db.RulesDB) IMolecule(org.openscience.cdk.interfaces.IMolecule) Residue(model.Residue) Rule(model.Rule) Monomer(model.Monomer) HashSet(java.util.HashSet)

Example 4 with Rule

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");
}
Also used : Residue(model.Residue) Family(model.Family) FamilyDB(db.FamilyDB) Rule(model.Rule) ResidueJsonLoader(io.loaders.json.ResidueJsonLoader) File(java.io.File) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test)

Example 5 with Rule

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");
}
Also used : Residue(model.Residue) Rule(model.Rule) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test)

Aggregations

Rule (model.Rule)8 Residue (model.Residue)6 IAtom (org.openscience.cdk.interfaces.IAtom)6 Family (model.Family)3 IMolecule (org.openscience.cdk.interfaces.IMolecule)3 FamilyDB (db.FamilyDB)2 Monomer (model.Monomer)2 JSONArray (org.json.simple.JSONArray)2 JSONObject (org.json.simple.JSONObject)2 Test (org.junit.Test)2 MappedChain (algorithms.isomorphism.chains.MappedChain)1 Match (algorithms.utils.Match)1 RulesDB (db.RulesDB)1 ResidueJsonLoader (io.loaders.json.ResidueJsonLoader)1 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Molecule (org.openscience.cdk.Molecule)1