Search in sources :

Example 1 with FamilyChainsDB

use of algorithms.isomorphism.chains.FamilyChainsDB in project Smiles2Monomers by yoann-dufresne.

the class ChainsFamilyMatching method matchFamilly.

@Override
public Coverage matchFamilly(Family family) {
    if (this.co == null)
        return null;
    this.coverage = new Coverage(co);
    FamilyChainsDB fc = this.chains.getObject(family.getJsonName());
    this.toMatch.addAll(family.getRoots());
    while (!toMatch.isEmpty()) {
        Residue res = toMatch.remove();
        List<MappedChain> mcs = null;
        List<ChainAdd> adds = fc.getAdds(res);
        // If the residue is a root
        if (adds.size() == 0) {
            Chain rootChain = fc.getRootChains().get(res);
            mcs = Isomorphism.searchAChain(rootChain, this.co, this.matchingType);
        } else // From previous mapping
        {
            mcs = new ArrayList<>();
            Residue from = adds.get(0).getFrom();
            for (MappedChain mc : mappings.get(from)) {
                try {
                    MappedChain clone = mc;
                    for (ChainAdd add : adds) clone = add.applyAndClone(mc, this.matchingType);
                    mcs.add(clone);
                } catch (Exception e) {
                }
            }
        }
        // Save results and recursive add
        if (mcs.size() > 0) {
            this.mappings.put(res, mcs);
            this.addToCoverage(family, mcs, res);
            for (Residue child : fc.getFamily().getChildrenOf(res)) {
                if (fc.getAdds(child).get(0).getFrom().equals(res)) {
                    this.toMatch.add(child);
                }
            }
        }
    }
    this.mappings.clear();
    return this.coverage;
}
Also used : MappedChain(algorithms.isomorphism.chains.MappedChain) Chain(algorithms.isomorphism.chains.Chain) MappedChain(algorithms.isomorphism.chains.MappedChain) FamilyChainsDB(algorithms.isomorphism.chains.FamilyChainsDB) Residue(model.Residue) ChainAdd(algorithms.isomorphism.chains.ChainAdd) Coverage(algorithms.utils.Coverage)

Example 2 with FamilyChainsDB

use of algorithms.isomorphism.chains.FamilyChainsDB in project Smiles2Monomers by yoann-dufresne.

the class ChainLearningTests method loadTest.

/**/
@Test
public void loadTest() {
    // Computing
    ChainLearning learning = new ChainLearning(this.learningBase);
    learning.setMarkovianSize(3);
    learning.learn(this.families);
    ChainsDB db = learning.getDb();
    FamilyChainIO io = new FamilyChainIO(this.families);
    io.saveFile(db, "data_tests/chains.json");
    ChainsDB loaded = io.loadFile("data_tests/chains.json");
    if (loaded.getObjects().size() != db.getObjects().size())
        fail("Not the same number of objects");
    FamilyChainsDB fc = db.getObjects().get(0);
    FamilyChainsDB fcLoaded = loaded.getObjects().get(0);
    if (fc.getRootChains().size() != fcLoaded.getRootChains().size())
        fail("Root chain number different");
    for (Residue res : fc.getFamily().getResidues()) {
        if (fc.getAdds(res).size() != fcLoaded.getAdds(res).size())
            fail("Adds of " + res.getName() + "are not correctly loaded");
    }
    Assert.assertTrue(true);
}
Also used : FamilyChainIO(io.loaders.json.FamilyChainIO) FamilyChainsDB(algorithms.isomorphism.chains.FamilyChainsDB) Residue(model.Residue) ChainsDB(algorithms.isomorphism.chains.ChainsDB) FamilyChainsDB(algorithms.isomorphism.chains.FamilyChainsDB) ChainLearning(algorithms.isomorphism.chains.ChainLearning) Test(org.junit.Test)

Example 3 with FamilyChainsDB

use of algorithms.isomorphism.chains.FamilyChainsDB in project Smiles2Monomers by yoann-dufresne.

the class FamilyChainIO method objectFromJson.

@Override
protected FamilyChainsDB objectFromJson(JSONObject jso) {
    // FC creation
    String famName = (String) jso.get("family");
    if (famName.contains("€"))
        famName = famName.split("€")[0];
    Family fam = this.families.getObject(famName);
    FamilyChainsDB fc = new FamilyChainsDB(fam);
    // Roots
    JSONObject jsonRoots = (JSONObject) jso.get("roots");
    for (Object objIdx : jsonRoots.keySet()) {
        String idx = (String) objIdx;
        fc.addRootChain(this.residues.getObject(idx), new Chain((String) jsonRoots.get(objIdx)));
    }
    // Adds
    JSONObject jsonAdds = (JSONObject) jso.get("extensions");
    for (Object objIdx : jsonAdds.keySet()) {
        String idx = (String) objIdx;
        Residue current = this.residues.getObject(idx);
        JSONArray jsonAddsList = (JSONArray) jsonAdds.get(objIdx);
        for (Object obj : jsonAddsList) {
            JSONObject add = (JSONObject) obj;
            Residue from = this.residues.getObject((String) add.get("from"));
            ChainAdd ca = null;
            // TODO : Change it for better usage of genericity.
            switch((String) add.get("type")) {
                case "hydrogen":
                    ca = new HydrogenAdd(from, ((Number) add.get("idx")).intValue(), ((Number) add.get("num")).intValue());
                    break;
                case "extension":
                    ca = new BondAdd(from, new Extension((String) add.get("ext")), ((Number) add.get("idx1")).intValue(), ((Number) add.get("idx2")).intValue());
                    break;
                default:
                    break;
            }
            fc.addAnAdd(current, ca);
        }
    }
    return fc;
}
Also used : Chain(algorithms.isomorphism.chains.Chain) HydrogenAdd(algorithms.isomorphism.chains.HydrogenAdd) JSONArray(org.json.simple.JSONArray) Extension(algorithms.isomorphism.chains.Extension) FamilyChainsDB(algorithms.isomorphism.chains.FamilyChainsDB) JSONObject(org.json.simple.JSONObject) Residue(model.Residue) ChainAdd(algorithms.isomorphism.chains.ChainAdd) Family(model.Family) JSONObject(org.json.simple.JSONObject) BondAdd(algorithms.isomorphism.chains.BondAdd)

Aggregations

FamilyChainsDB (algorithms.isomorphism.chains.FamilyChainsDB)3 Residue (model.Residue)3 Chain (algorithms.isomorphism.chains.Chain)2 ChainAdd (algorithms.isomorphism.chains.ChainAdd)2 BondAdd (algorithms.isomorphism.chains.BondAdd)1 ChainLearning (algorithms.isomorphism.chains.ChainLearning)1 ChainsDB (algorithms.isomorphism.chains.ChainsDB)1 Extension (algorithms.isomorphism.chains.Extension)1 HydrogenAdd (algorithms.isomorphism.chains.HydrogenAdd)1 MappedChain (algorithms.isomorphism.chains.MappedChain)1 Coverage (algorithms.utils.Coverage)1 FamilyChainIO (io.loaders.json.FamilyChainIO)1 Family (model.Family)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1 Test (org.junit.Test)1