use of algorithms.isomorphism.chains.Chain 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;
}
use of algorithms.isomorphism.chains.Chain in project Smiles2Monomers by yoann-dufresne.
the class Isomorphism method searchFromPreviousMapping.
/**/
public static List<MappedChain> searchFromPreviousMapping(MappedChain mc, Extension ext, int idx1, int idx2, MatchingType type) {
IMolecule mol = mc.getChemObject().getMolecule();
int extIdx = idx1 == -1 ? idx2 : idx1;
List<MappedChain> mappings = new ArrayList<>();
// Connected bonds search
List<IBond> connectedBonds = null;
if (extIdx == -1) {
connectedBonds = new ArrayList<>();
for (IBond bond : mol.bonds()) connectedBonds.add(bond);
} else {
IAtom extAtom = mol.getAtom(mc.getAtomsMapping().get(extIdx));
connectedBonds = mol.getConnectedBondsList(extAtom);
}
for (IBond neighbor : connectedBonds) {
// No need to search bonds already present in mapping.
if (mc.getBondsMapping().contains(mol.getBondNumber(neighbor)))
continue;
List<BondMapping> bms = ext.match(neighbor, type);
for (BondMapping bm : bms) {
// Verification of the compatibility with previous matching
if (idx1 != -1 && mol.getAtomNumber(bm.a0) != mc.getAtomsMapping().get(idx1))
continue;
if (idx2 != -1 && mol.getAtomNumber(bm.a1) != mc.getAtomsMapping().get(idx2))
continue;
// Creation of the new mapping
Chain chain = new Chain(mc.getChain(), ext, idx1, idx2);
List<Integer> atoms = new ArrayList<>(mc.getAtomsMapping());
Map<Integer, Integer> hydrogens = new HashMap<>(mc.getHydrogensMapping());
if (idx1 == -1) {
int an = mol.getAtomNumber(bm.a0);
// To avoid cycles where there is no cycle.
if (mc.getAtomsMapping().contains(an))
continue;
atoms.add(an);
hydrogens.put(an, bm.h0);
}
if (idx2 == -1) {
int an = mol.getAtomNumber(bm.a1);
// To avoid cycles where there is no cycle.
if (mc.getAtomsMapping().contains(an))
continue;
atoms.add(an);
hydrogens.put(an, bm.h1);
}
List<Integer> bonds = new ArrayList<>(mc.getBondsMapping());
bonds.add(mol.getBondNumber(neighbor));
List<MatchingType> matchings = new ArrayList<>(mc.getMatchings());
matchings.add(bm.matchingType);
MappedChain newMc = new MappedChain(mc.getChemObject(), chain, atoms, bonds, matchings, hydrogens);
mappings.add(newMc);
}
}
return mappings;
}
use of algorithms.isomorphism.chains.Chain in project Smiles2Monomers by yoann-dufresne.
the class Isomorphism method searchFromPreviousMapping.
/**
* Search a list of mapped blocs from a smaller mapped bloc
* @param mb Initial mapped bloc
* @param ext Extension to the previous mapped bloc
* @return New mapped blocs.
*/
public static List<MappedChain> searchFromPreviousMapping(MappedChain mb, Extension ext, MatchingType type) {
List<MappedChain> mbs = new ArrayList<>();
ChemicalObject co = mb.getChemObject();
IMolecule mol = co.getMolecule();
List<Integer> neighbors = mb.getNeighborsBonds(mol);
// Create a new bloc for each neighbor
for (int idx : neighbors) {
// Create bloc
IBond nb = mol.getBond(idx);
List<BondMapping> matchings = ext.match(nb, type);
for (BondMapping bm : matchings) {
int atomIdx0 = mol.getAtomNumber(bm.a0);
int atomIdx1 = mol.getAtomNumber(bm.a1);
int blocPosition0 = mb.getMappingIdx(atomIdx0);
int blocPosition1 = mb.getMappingIdx(atomIdx1);
int hydrogen0 = bm.h0;
int hydrogen1 = bm.h1;
Chain bloc = new Chain(mb.getChain(), ext, blocPosition0, blocPosition1);
List<Integer> atoms = new ArrayList<>(mb.getAtomsMapping());
if (blocPosition0 == -1)
atoms.add(atomIdx0);
if (blocPosition1 == -1)
atoms.add(atomIdx1);
List<Integer> bonds = new ArrayList<>(mb.getBondsMapping());
bonds.add(mol.getBondNumber(nb));
List<MatchingType> matchingTypes = new ArrayList<>(mb.getMatchings());
matchingTypes.add(bm.matchingType);
Map<Integer, Integer> hydrogens = new HashMap<>(mb.getHydrogensMapping());
if (!hydrogens.containsKey(atomIdx0) || hydrogens.get(atomIdx0) <= hydrogen0)
hydrogens.put(atomIdx0, hydrogen0);
if (!hydrogens.containsKey(atomIdx1) || hydrogens.get(atomIdx1) <= hydrogen1)
hydrogens.put(atomIdx1, hydrogen1);
MappedChain newMb = new MappedChain(co, bloc, atoms, bonds, matchingTypes, hydrogens);
if (!mbs.contains(newMb))
mbs.add(newMb);
}
}
return mbs;
}
use of algorithms.isomorphism.chains.Chain in project Smiles2Monomers by yoann-dufresne.
the class BlocsTests method serialsTest.
@Test
public void serialsTest() {
Chain b0 = new Chain(this.ext3);
Chain b00 = new Chain(b0, ext1, 0, -1);
Chain b01 = new Chain(b0, ext1, 1, -1);
Assert.assertNotEquals(b00.getSerial(), b01.getSerial());
}
use of algorithms.isomorphism.chains.Chain in project Smiles2Monomers by yoann-dufresne.
the class ChainLearningTests method generateSize3Test.
@Test
public void generateSize3Test() {
// Computing
ChainLearning learning = new ChainLearning(this.learningBase);
learning.setMarkovianSize(3);
learning.learn(this.families);
FrequencesIndex frequences = learning.getFrequence();
ChainsIndex chains = learning.getAllChains();
// Verification
for (String pattern : chains.keySet()) {
Chain chain = chains.get(pattern);
if (chain.getSize() != 3)
continue;
if (!this.size3Frequencies.containsKey(pattern) || this.size3Frequencies.get(pattern) != frequences.get(pattern) || !this.size3Blocs.get(pattern).equals(chain.getSerial())) {
System.out.println(pattern);
System.out.println(chain.getSerial());
fail(pattern + " : " + frequences.get(pattern) + " -> " + this.size3Blocs.get(pattern));
}
}
Assert.assertTrue(frequences.size() == 19);
}
Aggregations