Search in sources :

Example 16 with Residue

use of model.Residue in project Smiles2Monomers by yoann-dufresne.

the class ChainLearning method learn.

/**
 * Learn Sequences from families using the learning base.
 * @param families families to index.
 */
public void learn(FamilyDB families) {
    this.chains.clear();
    this.finalChains.clear();
    this.db = new ChainsDB();
    List<Residue> roots = new ArrayList<>();
    for (Family family : families.getObjects()) {
        for (Residue root : family.getRoots()) {
            roots.add(root);
        }
    }
    // --- Roots ---
    // Init for size 1
    ResidueMappings residueIndex_1 = new ResidueMappings();
    this.initLearn(roots, residueIndex_1);
    // Markovian recursive
    ResidueMappings previous = null;
    ResidueMappings current = residueIndex_1;
    for (int size = 2; size <= this.markovianSize; size++) {
        previous = current;
        current = this.learnMarkovianNext(previous);
    }
    // Greedy recursive
    for (Residue root : current.keySet()) {
        MappedChain bestMarkov = this.getBest(current.get(root));
        MappedChain greedyMB = this.learnGreedy(bestMarkov);
        this.chains.put(greedyMB.getChain().getSmiles(), greedyMB.getChain());
        this.finalChains.put(greedyMB.getChemObject().getId(), greedyMB.getChain());
    }
    // Create index structures all over families.
    for (Family fam : families.getObjects()) {
        FamilyChainsDB fc = new FamilyChainsDB(fam);
        this.db.addObject(fam.getName(), fc);
        this.addAddsToSons(fc, fam);
    }
}
Also used : Residue(model.Residue) ArrayList(java.util.ArrayList) Family(model.Family)

Example 17 with Residue

use of model.Residue in project Smiles2Monomers by yoann-dufresne.

the class PreComputation method main.

public static void main(String[] args) {
    // ----------------- Parameters ---------------------------
    String rulesDBName = "data/rules.json";
    String monosDBName = "data/monomers.json";
    String jsonPolymers = "data/learning.json";
    String serialFolder = "data/serials/";
    String jsonResidues = "data/residues.json";
    String jsonChains = "data/chains.json";
    int markovianSize = 3;
    // Parsing
    for (int idx = 0; idx < args.length; idx++) {
        if (args[idx].startsWith("-")) {
            switch(args[idx]) {
                case "-rul":
                    rulesDBName = args[idx + 1];
                    break;
                case "-mono":
                    monosDBName = args[idx + 1];
                    break;
                case "-poly":
                    jsonPolymers = args[idx + 1];
                    break;
                case "-res":
                    jsonResidues = args[idx + 1];
                    break;
                case "-cha":
                    jsonChains = args[idx + 1];
                    break;
                case "-serial":
                    serialFolder = args[idx + 1];
                    break;
                case "-markovian":
                    markovianSize = new Integer(args[idx + 1]);
                    break;
                default:
                    System.err.println("Wrong option " + args[idx]);
                    System.exit(1);
                    break;
            }
            idx++;
        } else {
            System.err.println("Wrong parameter " + args[idx]);
            System.exit(1);
        }
    }
    // File existence
    File file = new File(rulesDBName);
    if (!file.exists()) {
        System.err.println("No file at " + rulesDBName);
        System.exit(1);
    }
    file = new File(monosDBName);
    if (!file.exists()) {
        System.err.println("No file at " + monosDBName);
        System.exit(1);
    }
    file = new File(jsonPolymers);
    if (!file.exists()) {
        System.err.println("No file at " + jsonPolymers);
        System.exit(1);
    }
    // ------------------- Loadings ------------------------
    System.out.println("--- Loading ---");
    RulesDB rules = RulesJsonLoader.loader.loadFile(rulesDBName);
    MonomersDB monos = new MonomersJsonLoader(true).loadFile(monosDBName);
    PolymersJsonLoader pjl = new PolymersJsonLoader(monos, false);
    PolymersDB learningBase = pjl.loadFile(jsonPolymers);
    // ----------------- Serializations --------------------
    System.out.println("--- Data serialisation ---");
    File folder = new File(serialFolder);
    if (!folder.exists())
        folder.mkdir();
    MonomersSerialization ms = new MonomersSerialization();
    ms.serialize(monos, serialFolder + "monos.serial");
    // ----------------- residues --------------------------
    ResidueCreator rc = new ResidueCreator(rules);
    rc.setVerbose(false);
    System.out.println("--- Residues creation ---");
    FamilyDB families = rc.createResidues(monos);
    System.out.println("--- Saving residues ---");
    ResidueJsonLoader rjl = new ResidueJsonLoader(rules, monos);
    rjl.saveFile(families, jsonResidues);
    // ----------------- chains ----------------------------
    System.out.println("--- Learning chains ---");
    // Adapt residue structures
    for (Residue res : families.getResidues().getObjects()) res.explicitToImplicitHydrogens();
    ChainLearning learning = new ChainLearning(learningBase);
    learning.setMarkovianSize(markovianSize);
    learning.learn(families);
    // --- Save ---
    System.out.println("--- Saving chains ---");
    FamilyChainIO fcio = new FamilyChainIO(families);
    fcio.saveFile(learning.getDb(), jsonChains);
    System.out.println("--- Ended ---");
}
Also used : MonomersJsonLoader(io.loaders.json.MonomersJsonLoader) MonomersDB(db.MonomersDB) ResidueCreator(algorithms.ResidueCreator) FamilyDB(db.FamilyDB) ChainLearning(algorithms.isomorphism.chains.ChainLearning) MonomersSerialization(io.loaders.serialization.MonomersSerialization) RulesDB(db.RulesDB) FamilyChainIO(io.loaders.json.FamilyChainIO) Residue(model.Residue) PolymersDB(db.PolymersDB) PolymersJsonLoader(io.loaders.json.PolymersJsonLoader) ResidueJsonLoader(io.loaders.json.ResidueJsonLoader) File(java.io.File)

Example 18 with Residue

use of model.Residue 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 19 with Residue

use of model.Residue 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)

Example 20 with Residue

use of model.Residue 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)

Aggregations

Residue (model.Residue)35 Family (model.Family)11 ArrayList (java.util.ArrayList)10 IAtom (org.openscience.cdk.interfaces.IAtom)8 JSONArray (org.json.simple.JSONArray)7 JSONObject (org.json.simple.JSONObject)7 IMolecule (org.openscience.cdk.interfaces.IMolecule)7 Monomer (model.Monomer)6 Rule (model.Rule)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 Coverage (algorithms.utils.Coverage)4 Match (algorithms.utils.Match)4 FamilyDB (db.FamilyDB)4 File (java.io.File)4 FamilyChainsDB (algorithms.isomorphism.chains.FamilyChainsDB)3 RulesDB (db.RulesDB)3 ResidueJsonLoader (io.loaders.json.ResidueJsonLoader)3 Color (java.awt.Color)3 Link (model.Family.Link)3