use of model.Residue in project Smiles2Monomers by yoann-dufresne.
the class IsomorphismFamilyMatcher method matchFamilly.
@Override
public Coverage matchFamilly(Family family) {
// Initialization
Coverage cov = new Coverage(co);
IMolecule mol = co.getMolecule();
Set<Residue> markSet = new HashSet<>();
Stack<Residue> searchSet = new Stack<>();
searchSet.addAll(family.getRoots());
// For all the nodes in the search group.
while (!searchSet.isEmpty()) {
Residue currentRes = searchSet.pop();
// Search the current residue in mol.
try {
this.sqt.setSmarts(currentRes.getSmiles());
} catch (CDKException e) {
e.printStackTrace();
}
boolean isMatching = false;
try {
long time = System.currentTimeMillis();
isMatching = this.sqt.matches(mol);
if (verbose)
System.out.println(" Search for " + currentRes.getName() + " in " + (System.currentTimeMillis() - time));
} catch (CDKException e) {
e.printStackTrace();
}
// If there is at least one occurrence.
if (isMatching) {
// Add matches to the coverage
List<Match> matches = new ArrayList<>();
for (List<Integer> lMatch : sqt.getMatchingAtoms()) {
Match match = new Match(currentRes);
for (int i : lMatch) match.addAtom(i);
matches.add(match);
}
// Change to compare.
cov.addListMatches(currentRes, matches);
// Mark current residue to possibly add children.
markSet.add(currentRes);
// Add children with all parents marked
Set<Residue> children = family.getChildrenOf(currentRes);
for (Residue child : children) {
boolean canBeAdded = true;
for (Link dependance : family.getDepandances()) if (dependance.getTo().equals(child))
if (!markSet.contains(dependance.getFrom())) {
canBeAdded = false;
break;
}
if (canBeAdded)
searchSet.add(child);
}
}
}
return cov;
}
use of model.Residue in project Smiles2Monomers by yoann-dufresne.
the class ChainLearning method initLearn.
// -------------------------------- Initialization ----------------------------------
private void initLearn(List<Residue> roots, ResidueMappings mappings) {
for (Residue res : roots) {
List<MappedChain> resMappings = new ArrayList<>();
for (IBond bond : res.getMolecule().bonds()) {
Extension ext = new Extension(bond);
Chain bloc = new Chain(ext);
String smiles = bloc.getMySmiles();
this.chains.put(smiles, bloc);
this.frequence.put(smiles, 0);
// Creation of residue matchings (sequences of size 1)
for (BondMapping bm : ext.match(bond, MatchingType.EXACT)) {
MappedChain resMap = this.createMappingFromMatch(res, bond, bloc, bm);
resMappings.add(resMap);
}
}
mappings.put(res, resMappings);
}
this.frequencesInit();
}
use of model.Residue in project Smiles2Monomers by yoann-dufresne.
the class ChainLearning method addAddsToSons.
// -------------------------------------- Children extensions -------------------------------------
private void addAddsToSons(FamilyChainsDB fc, Family fam) {
for (Residue root : fam.getRoots()) {
fc.addRootChain(root, this.finalChains.get(root.getId()));
Set<Residue> children = fam.getChildrenOf(root);
this.recurChildrenAdds(fc, root, children);
}
}
use of model.Residue in project Smiles2Monomers by yoann-dufresne.
the class ResidueCreator method createResidue.
// Create residue when it was found
private Set<Residue> createResidue(List<RMap> match, Rule rule, Residue res) {
Set<Residue> residues = new HashSet<>();
// Create index
int[] index = new int[match.size()];
for (RMap rm : match) {
index[rm.getId2()] = rm.getId1();
}
for (Replacement replace : rule.getReplacements()) {
// Clone molecule
Molecule oldMol = res.getMolecule();
Molecule mol = null;
try {
mol = (Molecule) oldMol.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
// Save links atoms
Map<IAtom, IAtom> convesionAtom = new HashMap<>();
for (IAtom a : res.getAtomicLinks().keySet()) {
int idx = oldMol.getAtomNumber(a);
IAtom newA = mol.getAtom(idx);
convesionAtom.put(a, newA);
}
// Prepare deletions
List<IAtom> deletedAtoms = new ArrayList<>();
List<IAtom> linkedAtoms = new ArrayList<>();
for (int i : replace.toDelete) deletedAtoms.add(mol.getAtom(index[i]));
for (int i : replace.toReplace) {
IAtom atom = mol.getAtom(index[i]);
deletedAtoms.add(atom);
for (IAtom neighbor : mol.getConnectedAtomsList(atom)) if (!neighbor.getSymbol().equals("H") && !deletedAtoms.contains(neighbor))
linkedAtoms.add(neighbor);
}
// Delete atoms
for (IAtom a : deletedAtoms) {
for (IBond b : mol.getConnectedBondsList(a)) mol.removeBond(b);
mol.removeAtom(a);
}
String smarts = this.sg.createSMILES(mol);
if (!Residue.existingResidue(smarts, res.getMonoName())) {
Residue residue = Residue.constructResidue(res.getMonoName(), smarts);
residue.setMol(mol);
// Add old links
for (IAtom oldA : convesionAtom.keySet()) {
IAtom newA = convesionAtom.get(oldA);
// int oldIdx = oldMol.getAtomNumber(oldA);
// int newIdx = mol.getAtomNumber(newA);
residue.getAtomicLinks().put(newA, res.getAtomicLinks().get(oldA));
}
// Add new Links
for (IAtom a : linkedAtoms) residue.addLink(a, rule);
residues.add(residue);
} else {
Residue residue = Residue.constructResidue(res.getMonoName(), smarts);
residues.add(residue);
}
}
return residues;
}
use of model.Residue in project Smiles2Monomers by yoann-dufresne.
the class ResidueCreator method createResidues.
/**
* Create all residues from the argument monomers database according to rules entered in paramaters of the constructor.
* @param monosDBName A monomers database.
* @return All possible residues found.
*/
public FamilyDB createResidues(MonomersDB monosDB) {
FamilyDB famDB = new FamilyDB();
famDB.init(monosDB);
for (Family family : famDB.getFamilies()) {
this.residuesFromMonomers(family);
for (Residue res : family.getResidues()) {
IMolecule oldMol = res.getMolecule();
res.setMol(new Molecule(AtomContainerManipulator.removeHydrogens(res.getMolecule())));
IMolecule newMol = res.getMolecule();
Map<IAtom, IAtom> conversion = new HashMap<>();
int idx = 0;
for (IAtom a : oldMol.atoms()) {
if (!"H".equals(a.getSymbol())) {
conversion.put(a, newMol.getAtom(idx));
idx++;
}
}
Map<IAtom, Rule> oldLinks = new HashMap<>(res.getAtomicLinks());
res.getAtomicLinks().clear();
for (IAtom oldA : oldLinks.keySet()) {
Rule rule = oldLinks.get(oldA);
res.addLink(conversion.get(oldA), rule);
}
}
}
return famDB;
}
Aggregations