Search in sources :

Example 1 with ChainLearning

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

the class ChainLearningTests method generateGreedyTest.

/**/
@Test
public void generateGreedyTest() {
    // Computing
    ChainLearning learning = new ChainLearning(this.learningBase);
    learning.setMarkovianSize(3);
    learning.learn(this.families);
    ChainsIndex chains = learning.getFinalChains();
    // Verification
    for (String idx : chains.keySet()) {
        Chain chain = chains.get(idx);
        if (!this.greedyPatterns.containsKey(idx) || !this.greedyPatterns.get(idx).equals(chain.getSerial()))
            fail(idx + " not found");
    /**/
    }
    Assert.assertTrue(chains.size() == 3);
}
Also used : Chain(algorithms.isomorphism.chains.Chain) ChainLearning(algorithms.isomorphism.chains.ChainLearning) ChainsIndex(algorithms.isomorphism.chains.ChainLearning.ChainsIndex) Test(org.junit.Test)

Example 2 with ChainLearning

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

the class ChainLearningTests method saveTest.

/**/
@Test
public void saveTest() {
    String line = null;
    String expected = "[{\"roots\":{\"86\":\"C,0,C,1,2,-1,-1;C,0,N,1,1,0,-1;C,0,C,0,1,-1,0;C,1,O,0,0,3,-1\",\"87\":\"C,0,O,2,0,-1,-1;C,0,C,1,2,-1,0;C,0,C,0,1,-1,2;C,0,N,1,1,2,-1;C,1,O,0,0,3,-1\"},\"family\":\"D-Ser\",\"extensions\":{\"79\":[{\"idx2\":-1,\"idx1\":3,\"from\":\"85\",\"type\":\"extension\",\"ext\":\"C,0,O,0,1\"}],\"78\":[{\"idx2\":-1,\"idx1\":3,\"from\":\"84\",\"type\":\"extension\",\"ext\":\"C,0,O,0,1\"}],\"77\":[{\"num\":1,\"idx\":1,\"from\":\"84\",\"type\":\"hydrogen\"}],\"82\":[{\"idx2\":-1,\"idx1\":3,\"from\":\"87\",\"type\":\"extension\",\"ext\":\"C,0,O,0,1\"}],\"83\":[{\"idx2\":-1,\"idx1\":3,\"from\":\"86\",\"type\":\"extension\",\"ext\":\"C,0,O,0,1\"}],\"80\":[{\"idx2\":-1,\"idx1\":3,\"from\":\"81\",\"type\":\"extension\",\"ext\":\"C,0,O,0,1\"}],\"81\":[{\"num\":1,\"idx\":1,\"from\":\"87\",\"type\":\"hydrogen\"}],\"86\":[],\"87\":[],\"84\":[{\"num\":1,\"idx\":4,\"from\":\"87\",\"type\":\"hydrogen\"}],\"85\":[{\"num\":1,\"idx\":2,\"from\":\"86\",\"type\":\"hydrogen\"}]}},{\"roots\":{\"95\":\"C,0,N,2,1,-1,-1;C,0,C,1,2,-1,0;C,0,N,1,1,2,-1;C,0,C,0,1,-1,2;C,1,O,0,0,4,-1\"},\"family\":\"Dpr\",\"extensions\":{\"95\":[],\"94\":[{\"idx2\":-1,\"idx1\":4,\"from\":\"95\",\"type\":\"extension\",\"ext\":\"C,0,O,0,1\"}],\"93\":[{\"num\":1,\"idx\":3,\"from\":\"95\",\"type\":\"hydrogen\"}],\"92\":[{\"num\":1,\"idx\":1,\"from\":\"95\",\"type\":\"hydrogen\"}],\"91\":[{\"idx2\":-1,\"idx1\":4,\"from\":\"93\",\"type\":\"extension\",\"ext\":\"C,0,O,0,1\"}],\"90\":[{\"idx2\":-1,\"idx1\":4,\"from\":\"92\",\"type\":\"extension\",\"ext\":\"C,0,O,0,1\"}],\"89\":[{\"num\":1,\"idx\":1,\"from\":\"93\",\"type\":\"hydrogen\"}]}}]";
    // 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");
    try {
        BufferedReader br = new BufferedReader(new FileReader(new File("data_tests/chains.json")));
        line = br.readLine();
        br.close();
    } catch (IOException e) {
        fail(e.getMessage());
    }
    Assert.assertTrue(line.equals(expected));
}
Also used : FamilyChainIO(io.loaders.json.FamilyChainIO) ChainsDB(algorithms.isomorphism.chains.ChainsDB) FamilyChainsDB(algorithms.isomorphism.chains.FamilyChainsDB) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) ChainLearning(algorithms.isomorphism.chains.ChainLearning) File(java.io.File) Test(org.junit.Test)

Example 3 with ChainLearning

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

the class ChainLearningTests method generateMarkov6Test.

/**/
@Test
public void generateMarkov6Test() {
    // Computing
    ChainLearning learning = new ChainLearning(this.learningBase);
    learning.setMarkovianSize(6);
    learning.learn(this.families);
    ChainsIndex chains = learning.getFinalChains();
    // Verification
    for (String pattern : chains.keySet()) {
        Chain chain = chains.get(pattern);
        if (!this.markovPatterns.containsKey(pattern) || !this.markovPatterns.get(pattern).equals(chain.getSerial()))
            fail(pattern + " not found");
    /**/
    }
    Assert.assertTrue(chains.size() == 3);
}
Also used : Chain(algorithms.isomorphism.chains.Chain) ChainLearning(algorithms.isomorphism.chains.ChainLearning) ChainsIndex(algorithms.isomorphism.chains.ChainLearning.ChainsIndex) Test(org.junit.Test)

Example 4 with ChainLearning

use of algorithms.isomorphism.chains.ChainLearning 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 5 with ChainLearning

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

the class ChainLearningTests method generateSize1Test.

@Test
public void generateSize1Test() {
    // Computing
    ChainLearning learning = new ChainLearning(this.learningBase);
    learning.setMarkovianSize(1);
    learning.learn(this.families);
    FrequencesIndex frequences = learning.getFrequence();
    // Verification
    for (String pattern : frequences.keySet()) {
        if (!this.size1Frequencies.containsKey(pattern) || this.size1Frequencies.get(pattern) != frequences.get(pattern))
            fail(pattern + " : " + frequences.get(pattern));
    }
    Assert.assertTrue(frequences.size() == 6);
}
Also used : FrequencesIndex(algorithms.isomorphism.chains.ChainLearning.FrequencesIndex) ChainLearning(algorithms.isomorphism.chains.ChainLearning) Test(org.junit.Test)

Aggregations

ChainLearning (algorithms.isomorphism.chains.ChainLearning)8 Test (org.junit.Test)6 Chain (algorithms.isomorphism.chains.Chain)3 ChainsIndex (algorithms.isomorphism.chains.ChainLearning.ChainsIndex)3 ChainsDB (algorithms.isomorphism.chains.ChainsDB)3 FamilyChainIO (io.loaders.json.FamilyChainIO)3 ResidueCreator (algorithms.ResidueCreator)2 FrequencesIndex (algorithms.isomorphism.chains.ChainLearning.FrequencesIndex)2 FamilyChainsDB (algorithms.isomorphism.chains.FamilyChainsDB)2 FamilyDB (db.FamilyDB)2 MonomersDB (db.MonomersDB)2 PolymersDB (db.PolymersDB)2 RulesDB (db.RulesDB)2 MonomersJsonLoader (io.loaders.json.MonomersJsonLoader)2 PolymersJsonLoader (io.loaders.json.PolymersJsonLoader)2 File (java.io.File)2 Residue (model.Residue)2 MonomericSpliting (algorithms.MonomericSpliting)1 ResidueJsonLoader (io.loaders.json.ResidueJsonLoader)1 MonomersSerialization (io.loaders.serialization.MonomersSerialization)1