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);
}
}
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 ---");
}
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");
}
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");
}
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);
}
Aggregations