public void GetMoleculeCycles_Adenine()
        {
            var adenine = new Adenine();
            var cycles  = _moleculeService.GetMoleculeCycles(adenine);

            Assert.AreEqual(2, cycles.Count);
        }
        public Molecule React(Molecule reagent, Molecule substract)
        {
            var reagentCycles   = _moleculeService.GetMoleculeCycles(reagent);
            var substractCycles = _moleculeService.GetMoleculeCycles(substract);

            if (substractCycles.Count >= 0 && substractCycles.Count <= 2)
            {
                var substractOutCycleLinks = _linkService.GetOutCycleLinks(substract, substractCycles);
                var reagentOutCycleLinks   = _linkService.GetOutCycleLinks(reagent, reagentCycles);

                var carbonLinks = GetSubstractLinks(substractOutCycleLinks);
                if (carbonLinks.Count >= 2)
                {
                    var visitedCarbonLink = new List <Link>();
                    foreach (var carbonLink in carbonLinks)
                    {
                        if (!visitedCarbonLink.Contains(carbonLink))
                        {
                            visitedCarbonLink.Add(carbonLink);
                            var sameCarbonLinks = carbonLinks.Where(x => x.From.AtomId == carbonLink.From.AtomId && x.To.AtomId == carbonLink.To.AtomId).ToList();

                            //TODO
                        }
                    }
                }
            }
            return(substract);
        }
Exemplo n.º 3
0
        public Molecule React(Molecule reagent, Molecule substractMolecule)
        {
            var substract = _moleculeService.CloneMolecule(substractMolecule);
            // Get cycles
            var reagentCycles   = _moleculeService.GetMoleculeCycles(reagent);
            var substractCycles = _moleculeService.GetMoleculeCycles(substract);

            if (substractCycles.Count >= 0 && substractCycles.Count <= 2)
            {
                // Get links out cycles
                var substractOutCycleLinks = _linkService.GetOutCycleLinks(substract, substractCycles);
                var reagentOutCycleLinks   = _linkService.GetOutCycleLinks(reagent, reagentCycles);

                // Get links for replacement reaction
                var substractReactionLinks = GetSubstractLinks(substractOutCycleLinks);
                var reagentReactionLinks   = GetReagentLinks(reagentOutCycleLinks);

                if (substractReactionLinks.Count >= 1 && reagentReactionLinks.Count >= 1)
                {
                    // Get first link for reaction
                    var substractReactionLink = substractReactionLinks.FirstOrDefault();
                    var reagentReactionLink   = reagentReactionLinks.FirstOrDefault();

                    // Select atom that will be used in replacement reaction
                    var substractTargetAtom = GetSubstractTargetAtom(substractReactionLink);
                    var reagentTargetAtom   = GetReagentTargetAtom(reagentReactionLink);

                    // Select more eletro positive atom of reagent neighbors for replacement
                    var reagentTargetAtomNeighbors = _linkService.GetLinksFromAtom(reagentTargetAtom, reagent.Links);
                    var positiveLink = GetMoreEletroPositiveLink(reagentTargetAtomNeighbors);

                    // Get rest of links to exclude for substract
                    var linksToExclude = GetTreeLinksToExclude(reagentTargetAtom, positiveLink, reagent);
                    var newLinks       = reagent.Links.ToList();
                    foreach (var exclude in linksToExclude)
                    {
                        newLinks.RemoveAll(x => x.From.AtomId == exclude.From.AtomId && x.To.AtomId == exclude.To.AtomId ||
                                           x.From.AtomId == exclude.To.AtomId && x.To.AtomId == exclude.From.AtomId);
                    }

                    var targetLink = substract.Links.Where(x => x.From.AtomId == substractReactionLink.From.AtomId && x.To.AtomId == substractReactionLink.To.AtomId ||
                                                           x.From.AtomId == substractReactionLink.To.AtomId && x.To.AtomId == substractReactionLink.From.AtomId).FirstOrDefault();

                    var targetAtomId = targetLink.From.AtomId;
                    if (substractReactionLink.To.AtomId == targetLink.To.AtomId)
                    {
                        targetAtomId = targetLink.To.AtomId;
                    }
                    var newAtom = new Atom()
                    {
                        AtomId = targetAtomId,
                        Symbol = reagentTargetAtom.Symbol,
                        Octet  = reagentTargetAtom.Octet
                    };
                    var newLink = new Link(targetLink.From, newAtom);

                    substract.Links.Remove(targetLink);
                    substract.Links.Add(newLink);
                    substract.Atoms.Remove(substractTargetAtom);
                    substract.Atoms.Add(newAtom);

                    var mapper = new Dictionary <int, int>();
                    mapper.Add(reagentTargetAtom.AtomId, substractTargetAtom.AtomId);
                    foreach (var link in newLinks)
                    {
                        if (!mapper.ContainsKey(link.From.AtomId))
                        {
                            var newAtomId = substract.Atoms.Count + 1;
                            mapper.Add(link.From.AtomId, newAtomId);
                            substract.Atoms.Add(new Atom()
                            {
                                AtomId = newAtomId,
                                Symbol = link.From.Symbol,
                                Octet  = link.From.Octet
                            });
                        }
                        if (!mapper.ContainsKey(link.To.AtomId))
                        {
                            var newAtomId = substract.Atoms.Count + 1;
                            mapper.Add(link.To.AtomId, newAtomId);
                            substract.Atoms.Add(new Atom()
                            {
                                AtomId = newAtomId,
                                Symbol = link.To.Symbol,
                                Octet  = link.To.Octet
                            });
                        }
                        substract.Links.Add(
                            new Link(
                                new Atom()
                        {
                            AtomId = mapper[link.From.AtomId],
                            Symbol = link.From.Symbol,
                            Octet  = link.From.Octet
                        },
                                new Atom()
                        {
                            AtomId = mapper[link.To.AtomId],
                            Symbol = link.To.Symbol,
                            Octet  = link.To.Octet
                        }
                                )
                            );
                    }
                }
            }
            return(substract);
        }