use of model.Residue 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 model.Residue in project Smiles2Monomers by yoann-dufresne.
the class ChainLearning method calculateMapping.
private List<MappedChain> calculateMapping(FamilyChainsDB fc, Residue res, Residue from, MappedChain mc) {
if (this.finalChains.containsKey(res.getId())) {
Chain chain = this.finalChains.get(res.getId());
List<MappedChain> mappings = Isomorphism.searchAChain(chain, mc.getChemObject(), MatchingType.STRONG);
return mappings;
} else {
Residue ancestor = null;
if (!this.finalChains.containsKey(from.getId())) {
List<ChainAdd> adds = fc.getAdds(from);
ancestor = adds.get(0).getFrom();
}
List<MappedChain> newMcs = this.calculateMapping(fc, from, ancestor, mc);
List<MappedChain> toRemove = new ArrayList<>();
for (MappedChain newMc : newMcs) for (ChainAdd add : fc.getAdds(res)) {
try {
add.apply(newMc, MatchingType.STRONG);
} catch (Exception e) {
toRemove.add(newMc);
break;
}
}
for (MappedChain mcRem : toRemove) newMcs.remove(mcRem);
return newMcs;
}
}
use of model.Residue in project Smiles2Monomers by yoann-dufresne.
the class ChainLearning method learnMarkovianNext.
// --------------------------------- Markovian ------------------------------------
private ResidueMappings learnMarkovianNext(ResidueMappings prevIndex) {
ResidueMappings currentMappings = new ResidueMappings();
// Create all extended blocs
HashSet<Chain> blocs = new HashSet<>();
for (Residue root : prevIndex.keySet()) {
List<MappedChain> prevResIndex = prevIndex.get(root);
List<MappedChain> resIndex = this.createBlocsFromPrevious(prevResIndex);
if (resIndex.size() > 0)
currentMappings.put(root, resIndex);
else {
currentMappings.put(root, prevResIndex);
continue;
}
for (MappedChain mb : resIndex) blocs.add(mb.getChain());
}
// Filtering blocs
for (Chain b : blocs) {
String smiles = b.getMySmiles();
if (!this.chains.containsKey(smiles)) {
this.chains.put(smiles, b);
} else {
Chain rival = this.chains.get(smiles);
double rivalScore = this.computeScore(rival);
double score = this.computeScore(b);
if (score < rivalScore) {
this.chains.put(smiles, b);
for (List<MappedChain> mbs : currentMappings.values()) {
int size = mbs.size();
for (int idx = size - 1; idx >= 0; idx--) if (mbs.get(idx).getChain().equals(rival))
mbs.remove(idx);
}
} else {
for (List<MappedChain> mbs : currentMappings.values()) {
int size = mbs.size();
for (int idx = size - 1; idx >= 0; idx--) if (mbs.get(idx).getChain().equals(b))
mbs.remove(idx);
}
}
}
// Polymer matching and frequency computation for each new chain
this.calculateFrequency(b);
}
return currentMappings;
}
use of model.Residue in project Smiles2Monomers by yoann-dufresne.
the class ChainLearning method recurChildrenAdds.
private void recurChildrenAdds(FamilyChainsDB fc, Residue from, Set<Residue> children) {
for (Residue child : children) {
List<ChainAdd> adds = fc.getAdds(child);
// If extensions already exist, jump to the next son.
if (adds.size() > 0)
continue;
List<MappedChain> mcs = this.calculateMapping(fc, child, from, new MappedChain(child));
// Collections.sort(mcs, new MappedChainComparator());
MappedChain mc = mcs.get(0);
// Hydrogen adds
for (int atomIdx : mc.getAtomsMapping()) {
IAtom a = mc.getChemObject().getMolecule().getAtom(atomIdx);
int chainIdx = mc.getAtomsMapping().indexOf(atomIdx);
Map<Integer, Integer> mapping = mc.getHydrogensMapping();
int mappingHydrogens = mapping.containsKey(atomIdx) ? mapping.get(atomIdx) : 0;
int molHydrogens = a.getImplicitHydrogenCount();
if (mappingHydrogens < molHydrogens)
adds.add(new HydrogenAdd(from, chainIdx, molHydrogens - mappingHydrogens));
}
// Extensions
IMolecule resMol = mc.getChemObject().getMolecule();
for (IBond bond : resMol.bonds()) {
int bondIdx = resMol.getBondNumber(bond);
if (mc.getBondsMapping().contains(bondIdx))
continue;
Extension ext = new Extension(bond);
List<MappedChain> tmpMappings = Isomorphism.searchFromPreviousMapping(mc, ext, MatchingType.EXACT);
for (MappedChain tmp : tmpMappings) if (tmp.getBondsMapping().get(tmp.getBondsMapping().size() - 1) == bondIdx) {
Chain chain = tmp.getChain();
BondAdd ba = new BondAdd(from, ext, chain.getPosition1(), chain.getPosition2());
adds.add(ba);
}
}
/**/
this.recurChildrenAdds(fc, child, fc.getFamily().getChildrenOf(child));
}
}
use of model.Residue in project Smiles2Monomers by yoann-dufresne.
the class Coverage method getCorrectness.
public double getCorrectness(FamilyDB families) {
if (!this.alreadyCalculate)
this.calculateGreedyCoverage();
if (this.corrects == null)
this.calculateCorrectIncorrectNotFound(families);
int pepAtoms = 0;
for (@SuppressWarnings("unused") IAtom a : this.co.getMolecule().atoms()) pepAtoms++;
int corAtoms = 0;
for (Residue res : this.corrects.keySet()) {
int resAtoms = res.getMolecule().getAtomCount();
corAtoms += resAtoms * this.corrects.get(res);
}
double ratio = new Double(corAtoms) / new Double(pepAtoms);
return ratio;
}
Aggregations