Beispiel #1
0
 def choose_best(self, phylogenetics):
     # Get all the phylo_groups present.
     phylo_groups = self._get_present_phylo_groups(
         phylogenetics["phylo_group"])
     phylogenetics["phylo_group"] = phylo_groups
     sub_complexes = self._get_present_phylo_groups(
         phylogenetics["sub_complex"], mix_threshold=90)
     phylogenetics["sub_complex"] = sub_complexes
     # for each phylo_group, get the best species in the phylo_group (using
     # sub_complex info where possible)
     species = {}
     for pg in phylo_groups.keys():
         if self.hierarchy:
             allowed_species = flatten([
                 self.hierarchy.dict[pg]["children"][subc]
                 ["children"].keys()
                 for subc in self.hierarchy.dict[pg]["children"].keys()
                 if subc is not "Unknown"
             ])
             species_to_consider = {
                 k: phylogenetics["species"].get(k, {"percent_coverage": 0})
                 for k in allowed_species
             }
         else:
             species_to_consider = phylogenetics["species"]
         best_species = self._get_present_phylo_groups(species_to_consider,
                                                       mix_threshold=90)
         species.update(best_species)
     phylogenetics["species"] = species
     # For each species, get the best sub species where applicable
     sub_species = {}
     for s in species.keys():
         if self.hierarchy:
             allowed_sub_species = self.hierarchy.get_children(s)
             sub_species_to_consider = {
                 k: phylogenetics["lineage"].get(k, {"percent_coverage": 0})
                 for k in allowed_sub_species
             }
         else:
             sub_species_to_consider = phylogenetics.get("lineage", {})
         best_sub_species = self._get_best_coverage_dict(
             sub_species_to_consider)
         sub_species.update(best_sub_species)
     phylogenetics["lineage"] = sub_species
     return phylogenetics
Beispiel #2
0
def seen_together(variants):
    # Takes a list of variants.
    # Returns a list of variants that appear together (in the same variant set)
    variant_id_to_samples = {}
    for variant in variants:
        variant_id_to_samples[variant.var_hash] = variant.seen_in_samples()

    samples_counter = Counter(flatten(variant_id_to_samples.values()))
    samples_seen_more_than_once = [
        k for k, v in samples_counter.items() if v > 1
    ]
    contexts = []

    for sample in samples_seen_more_than_once:
        vars_together = []

        for variant_id, samples in variant_id_to_samples.items():
            if sample in samples:
                vars_together.append(variant_id)

        if vars_together not in contexts:
            contexts.append(vars_together)
            variants = [
                var for var in variants if var.var_hash not in vars_together
            ]

    for var in variants:
        contexts.append([var.var_hash])

    new_contexts = []
    for context in contexts:
        new_context = []
        for variant_id in context:
            new_context.append(Variant.objects.get(var_hash=variant_id))
        new_contexts.append(new_context)
    return new_contexts + [[]]
Beispiel #3
0
 def _get_drug_list_from_variant_to_resistance_drug(self):
     return unique(
         flatten(self.variant_or_gene_name_to_resistance_drug.values()))
Beispiel #4
0
 def _get_drug_list_from_variant_to_resistance_drug(self):
     return unique(flatten(self.variant_or_gene_name_to_resistance_drug.values()))