Esempio n. 1
0
 private void AddIndividualSet(int generationalLevel, PedigreeIndividualSet individualSet)
 {
     if (!individualSets.ContainsKey(generationalLevel))
     {
         individualSets[generationalLevel] = new List <PedigreeIndividualSet>();
     }
     individualSets[generationalLevel].Add(individualSet);
 }
Esempio n. 2
0
        public PedigreeModel(FamilyHistory p_familyHistory)
        {
            familyHistory = p_familyHistory;

            //Store the individuals as PedigreeIndividuals
            lock (familyHistory.Relatives)
            {
                foreach (Person Person in familyHistory.Relatives)
                {
                    PedigreeIndividual pedigreeIndividual = new PedigreeIndividual(Person);
                    individuals.Add(pedigreeIndividual);
                    individualsDictionary[Person.relativeID] = pedigreeIndividual;
                    points.Add(pedigreeIndividual.point);
                }
            }
            //here keys are the couple ids, values are the couples to which they map
            couplesDictionary = new Dictionary <CoupleID, PedigreeCouple>();

            //Link PedigreeIndividuals with their parents in the object model, derive couples
            foreach (PedigreeIndividual child in individuals)
            {
                //determine whether or not each parent exists in the data
                bool     hasMother = individualsDictionary.ContainsKey(child.HraPerson.motherID);
                bool     hasFather = individualsDictionary.ContainsKey(child.HraPerson.fatherID);
                CoupleID coupleId  = new CoupleID(child.HraPerson.fatherID, child.HraPerson.motherID);
                if (hasMother || hasFather)
                {
                    //link individuals with their parents in the object model, forming a pointer graph
                    if (hasMother)
                    {
                        child.Mother = individualsDictionary[child.HraPerson.motherID];
                    }
                    if (hasFather)
                    {
                        child.Father = individualsDictionary[child.HraPerson.fatherID];
                    }

                    //derive couples and store them
                    {
                        //if this individual has parents, set it's "parents" pointer to the
                        //couple representing it's parents (deriving couples from the data as needed).

                        //get the parents couple
                        PedigreeCouple parents = null;
                        {
                            //check if the parents have already been stored as a couple

                            if (couplesDictionary.ContainsKey(coupleId))
                            {
                                //if so, then use the previously stored couple.
                                parents = couplesDictionary[coupleId];
                            }
                            else
                            {
                                //if not store parents as a couple if they haven't been already
                                parents = new PedigreeCouple(child.Mother, child.Father);
                                couplesDictionary[coupleId] = parents;
                                couples.Add(parents);
                                points.Add(parents.point);



                                //link participating individuals with the new couple
                                if (child.Mother != null)
                                {
                                    if (child.Mother.spouseCouples != null)
                                    {
                                        child.Mother.spouseCouples.Add(parents);
                                    }
                                }

                                if (child.Father != null)
                                {
                                    if (child.Father.spouseCouples != null)
                                    {
                                        child.Father.spouseCouples.Add(parents);
                                    }
                                }
                            }
                        }
                        //set this individual's "parents" pointer to the parents couple
                        child.Parents = parents;

                        //add the child to the children of the parents couple
                        parents.children.Add(child);
                    }
                }
            }

            // derive the intergenerational edges of the couples graph
            foreach (PedigreeIndividual parent in individuals)
            {
                bool parentHasGrandparents = parent.Parents != null;
                bool parentIsPartOfACouple = parent.spouseCouples.Count != 0;

                if (parentHasGrandparents && parentIsPartOfACouple)
                {
                    foreach (PedigreeCouple parents in parent.spouseCouples)
                    {
                        PedigreeCouple grandparents      = parent.Parents;
                        bool           intergenerational = true;
                        coupleEdges.Add(new PedigreeCoupleEdge(grandparents, parents, intergenerational));
                    }
                }
            }

            // derive the intragenerational edges of the couples graph
            // (from half sibling relationships)
            foreach (PedigreeIndividual parent in individuals)
            {
                if (parent.spouseCouples.Count == 2)
                {
                    PedigreeCouple u = parent.spouseCouples[0];
                    PedigreeCouple v = parent.spouseCouples[1];
                    bool           intergenerational = false;
                    coupleEdges.Add(new PedigreeCoupleEdge(u, v, intergenerational));
                }
                //else if (parent.spouseCouples.Count > 2)
                //    throw new Exception("Pedigree not drawable: individual " + parent.relativeID + " has more than two spouses");
            }

            //derive generational levels
            bool undefinedLevelsRemain = true;
            int  minGenerationalLevel  = 0;
            int  maxGenerationalLevel  = 0;

            if (couples.Count > 0)
            {
                //assign the seed level
                couples[0].GenerationalLevel = 0;

                //propagate the seed level through the graph
                int NULL = PedigreeCouple.UNDEFINED_GENERATION;
                while (undefinedLevelsRemain)
                {
                    undefinedLevelsRemain = false;
                    foreach (PedigreeCoupleEdge e in coupleEdges)
                    {
                        if (e.intergenerational)
                        {
                            PedigreeCouple grandparents = e.u;
                            PedigreeCouple parents      = e.v;

                            bool parentsHaveLevel      = parents.GenerationalLevel != NULL;
                            bool grandparentsHaveLevel = grandparents.GenerationalLevel != NULL;

                            if (!parentsHaveLevel || !grandparentsHaveLevel)
                            {
                                undefinedLevelsRemain = true;
                            }

                            if (!parentsHaveLevel && !grandparentsHaveLevel)
                            {
                                undefinedLevelsRemain = false;
                            }

                            if (parentsHaveLevel && !grandparentsHaveLevel)
                            {
                                grandparents.GenerationalLevel = parents.GenerationalLevel - 1;
                                if (grandparents.GenerationalLevel < minGenerationalLevel)
                                {
                                    minGenerationalLevel = grandparents.GenerationalLevel;
                                }
                            }
                            else if (!parentsHaveLevel && grandparentsHaveLevel)
                            {
                                parents.GenerationalLevel = grandparents.GenerationalLevel + 1;
                                if (parents.GenerationalLevel > maxGenerationalLevel)
                                {
                                    maxGenerationalLevel = parents.GenerationalLevel;
                                }
                            }
                        }
                        else
                        {
                            //propagate levels through intragenerational edges (half siblings)
                            if (e.u.GenerationalLevel == NULL)
                            {
                                e.u.GenerationalLevel = e.v.GenerationalLevel;
                            }
                            else if (e.v.GenerationalLevel == NULL)
                            {
                                e.v.GenerationalLevel = e.u.GenerationalLevel;
                            }
                        }
                    }
                }
            }

            //normalize the levels
            foreach (PedigreeCouple couple in couples)
            {
                couple.GenerationalLevel -= minGenerationalLevel;
            }

            //store the (normalized) max level in the model
            this.maxGenerationalLevel = maxGenerationalLevel - minGenerationalLevel;

            //when a parent set of half siblings (father, mother, father)
            //is detedted, the mother id is added to this list. If a
            //couple involving this mother is detected later, this
            //list is checked to see if she has already been counted.
            List <int> halfSiblingParentsMotherIds = new List <int>();

            //derive individual sets
            foreach (PedigreeCouple couple in couples)
            {
                //add the [mother,father] individual sets
                if (couple.mother != null && couple.father != null)
                {
                    if (couple.mother.Parents == null && couple.father.Parents == null)
                    {
                        //if the mother has a single spouse
                        if (couple.mother.spouseCouples.Count == 1)
                        {
                            PedigreeIndividualSet parentsIndividualSet = new PedigreeIndividualSet(couple);
                            parentsIndividualSet.Add(couple.mother);
                            parentsIndividualSet.Add(couple.father);
                            AddIndividualSet(couple.GenerationalLevel, parentsIndividualSet);
                        }
                        //if the mother has a two spouses
                        else if (couple.mother.spouseCouples.Count == 2)
                        {
                            //collapse parents of half siblings into a single individual set
                            if (!halfSiblingParentsMotherIds.Contains(couple.mother.HraPerson.relativeID))
                            {
                                halfSiblingParentsMotherIds.Add(couple.mother.HraPerson.relativeID);
                                PedigreeIndividualSet parentsIndividualSet = new PedigreeIndividualSet(couple);
                                parentsIndividualSet.Add(couple.mother);
                                parentsIndividualSet.Add(couple.mother.spouseCouples[0].father);
                                parentsIndividualSet.Add(couple.mother.spouseCouples[1].father);
                                AddIndividualSet(couple.GenerationalLevel, parentsIndividualSet);
                            }
                        }
                    }

                    try
                    {
                        //add the children individual sets
                        PedigreeIndividualSet childrenIndividualSet = new PedigreeIndividualSet(couple);
                        foreach (PedigreeIndividual child in couple.children)
                        {
                            childrenIndividualSet.Add(child);
                            foreach (PedigreeCouple pc in child.spouseCouples)
                            {
                                if (pc.mother.HraPerson.relativeID == child.HraPerson.relativeID)
                                {
                                    childrenIndividualSet.Add(pc.father);
                                }
                                else
                                {
                                    childrenIndividualSet.Add(pc.mother);
                                }
                            }
                        }
                        AddIndividualSet(couple.GenerationalLevel + 1, childrenIndividualSet);
                    }
                    catch (Exception e)
                    {
                        Logger.Instance.WriteToLog(e.ToString());
                    }
                }
            }

            if (individualsDictionary.ContainsKey(1))
            {
                SetBloodRelatives(individualsDictionary[1]);
            }

            foreach (PedigreeIndividual pi in individuals)
            {
                if (pi.bloodRelative == false)
                {
                    if (pi.spouseCouples.Count == 0)
                    {
                        pi.bloodRelative = true;
                    }
                    else
                    {
                        bool bloodFound = false;
                        foreach (PedigreeCouple pc in pi.spouseCouples)
                        {
                            if ((pc.mother != null) && (pc.father != null))
                            {
                                if (pc.mother.bloodRelative == true || pc.father.bloodRelative == true)
                                {
                                    bloodFound = true;
                                }
                            }
                        }
                        if (bloodFound == false)
                        {
                            foreach (PedigreeCouple pc in pi.spouseCouples)
                            {
                                if (pc.mother != null)
                                {
                                    pc.mother.bloodRelative = true;
                                }
                                if (pc.father != null)
                                {
                                    pc.father.bloodRelative = true;
                                }
                            }
                        }
                    }
                }
            }
            this.FamilialVariants = p_familyHistory.ReloadFamilialVariants();
        }