public static async Task <TreeNode <NodeData> > GenerateTreeAsync(Species species, AncestryTreeGenerationFlags flags) { // Start by finding the earliest ancestor of this species. List <long> ancestor_ids = new List <long>(); if (!flags.HasFlag(AncestryTreeGenerationFlags.DescendantsOnly)) { ancestor_ids.AddRange(await SpeciesUtils.GetAncestorIdsAsync(species.Id)); } ancestor_ids.Add(species.Id); // Starting from the earliest ancestor, generate all tiers, down to the latest descendant. TreeNode <NodeData> root = new TreeNode <NodeData> { Value = new NodeData { Species = await SpeciesUtils.GetSpeciesAsync(ancestor_ids.First()), IsAncestor = true } }; Queue <TreeNode <NodeData> > queue = new Queue <TreeNode <NodeData> >(); queue.Enqueue(root); while (queue.Count() > 0) { Species[] descendants = await SpeciesUtils.GetDirectDescendantsAsync(queue.First().Value.Species); foreach (Species descendant in descendants) { TreeNode <NodeData> node = new TreeNode <NodeData> { Value = new NodeData { Species = descendant, IsAncestor = ancestor_ids.Contains(descendant.Id) } }; if (!flags.HasFlag(AncestryTreeGenerationFlags.AncestorsOnly) || node.Value.IsAncestor) { queue.First().AddChild(node); queue.Enqueue(node); } } queue.Dequeue(); } return(root); }
public static async Task <Species[]> GetSpeciesAsync(Taxon taxon) { List <Species> species = new List <Species>(); if (taxon.type == TaxonRank.Species) { // Return all species with the same name as the taxon. species.AddRange(await SpeciesUtils.GetSpeciesAsync("", taxon.name)); } else if (taxon.type == TaxonRank.Genus) { // Return all species within this genus (rather than recursively calling this function for each species). using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Species WHERE genus_id = $genus_id")) { cmd.Parameters.AddWithValue("$genus_id", taxon.id); using (DataTable table = await Database.GetRowsAsync(cmd)) foreach (DataRow row in table.Rows) { species.Add(await SpeciesUtils.SpeciesFromDataRow(row)); } } } else { // Get all subtaxa and call this function recursively to get the species from each of them. Taxon[] subtaxa = await GetSubtaxaAsync(taxon); foreach (Taxon t in subtaxa) { species.AddRange(await GetSpeciesAsync(t)); } } return(species.ToArray()); }