/// <summary> /// Parses a haplo string in the form of <comma delimited string of names>/<description>/<name of parent> /// </summary> /// <returns>A KeyValuePair of the parsed yhaplo and a string giving its parent, if any</returns> /// <param name="record">The record to parse</param> private KeyValuePair <YHaplo, string> ParseHaploString(string record) { string[] recordElements = record.Split('/'); if (null == recordElements) { throw new FileLoadException("Could not parse record. The data found was " + record); } if (3 != recordElements.Count()) { //Read error throw new FileLoadException("Record does not have three slash delimited sections. The record found was " + record); } string[] names = recordElements[0].Split(','); string description = recordElements[1]; string parent = recordElements[2]; YHaplo newHaplo = new YHaplo(names, description); return(new KeyValuePair <YHaplo, string> (newHaplo, parent)); }
/// <summary> /// Populates the Children of this node with more than two children via the use of dummy intermediary YHaplo instances. /// </summary> /// <param name="children">The children to populate</param> public void PopulateNonBinaryChildrenWithDummies(IEnumerable <YHaplo> children) { YHaplo[] childArray = children.ToArray(); int childCount = childArray.Count(); if (0 == childCount) { //Nothing to add. return; } if (1 == childCount) { Left = childArray [0]; return; } if (2 == childCount) { Left = childArray [0]; Right = childArray [1]; return; } //There are more than 2 nodes. We will need dummies. //Store the first child Left = childArray[0]; //Create a dummy to store the rest YHaplo right = new YHaplo() { IsDummy = true }; //Recurse //This would be easier with C :) List <YHaplo> rest = new List <YHaplo>(childArray); rest.Remove(rest[0]); Right = right; right.PopulateNonBinaryChildrenWithDummies(rest); }
/// <summary> /// Loads a collection of YHaplos, builds them into a tree, and returns the root /// </summary> /// <returns>The root of the collection. If no root (record with no parent) is found, returns null</returns> public YHaplo Load() { //Read the raw records. KeyValuePair <YHaplo, string>[] rawRecords = ReadHaplos(dataSource).ToArray(); //Collate them into a relationship tree. YHaplo root = null; foreach (KeyValuePair <YHaplo, string> rawRecord in rawRecords) { YHaplo current = rawRecord.Key; string parent = rawRecord.Value; if ("null" == parent) { //This is the root root = current; } //Find all children var children = from KeyValuePair <YHaplo, string> potentialChild in rawRecords where potentialChild.Value == current.PrimaryName select potentialChild.Key; var childrenArray = children.ToArray(); current.PopulateNonBinaryChildrenWithDummies(childrenArray); } //return from KeyValuePair<YHaplo,string> validRecord in rawRecords where true select validRecord.Key; return(root); }
/// <summary> /// Searches for a haplogroup with a specified name anywhere in the current YHaplo's family, including the haplo itself, its descendants, its ancestors, and cousins if requested. /// </summary> /// <returns>The haplogroup, if any</returns> /// <param name="name">The name to search for</param> /// <param name="includeCousins">Whether to include cousins</param> public YHaplo FindInFamily(string name, bool includeCousins) { return(YHaplo.SearchForNameInHaplogroups(GetEntireFamilyAsYHaplos(includeCousins), name)); }