Exemple #1
0
def offspring(request):
    if request.method == 'POST':
        parent_form1 = TraitsForm(request.POST, prefix='parent_form1')
        parent_form2 = TraitsForm(request.POST, prefix='parent_form2')
        if parent_form1.is_valid() and parent_form2.is_valid():
            #calculate
            result = {}
            teval = TraitsEvaluator()
            for phenotype_name in TraitsEvaluator.get_trait_names():
                result[phenotype_name] = teval.offspring_probs(phenotype_name, [parent_form1.cleaned_data[phenotype_name]], [parent_form2.cleaned_data[phenotype_name]])

            print result

            #TODO: redirect somewhere
            return render(request, 'traits_web_prototype/offspring.html',
                          {
                              'parent_form1': parent_form1,
                              'parent_form2': parent_form2,
                              'result' : result,
                          })

    else:
            parent_form1 = TraitsForm(prefix='parent_form1')
            parent_form2 = TraitsForm(prefix='parent_form2')


    return render(request, 'traits_web_prototype/offspring.html',
              {
                  'parent_form1': parent_form1,
                  'parent_form2': parent_form2,
              })
Exemple #2
0
    def __init__(self, *args, **kwargs):
        super(TraitsForm, self).__init__(*args, **kwargs)

        for name in TraitsEvaluator.get_trait_names():
            choices = [(p, p)
                       for p in TraitsEvaluator.get_trait_phenotypes(name)]
            choices.append(('unknown', 'unknown'))
            self.fields[name] = forms.ChoiceField(choices=choices)
Exemple #3
0
 def runTest(self):
     for name in TraitsEvaluator.get_trait_names():
         trait = TraitsEvaluator.get_trait(name)
         psquare = ExtendedPunnetSquare(trait)
         prob_map = psquare.get_full_probability_map()
         sum = 0
         for p in prob_map.itervalues():
             sum += p
         self.assertAlmostEqual(
             sum, 1.0, 7,
             "Trait %s. Sum of probabilities of traits must be 1" % (name))
Exemple #4
0
 def runTest(self):
     for name in TraitsEvaluator.get_trait_names():
         trait = TraitsEvaluator.get_trait(name)
         psquare = ExtendedPunnetSquare(trait)
         genotypes = psquare.get_possible_genotypes()
         for genotype in genotypes:
             self.assertIn(
                 genotype,
                 list(chain.from_iterable(trait.trait_map.values())),
                 "Genotype %s is not in the genotype map of trait %s" %
                 (genotype, name))
Exemple #5
0
 def runTest(self):
     for name in TraitsEvaluator.get_trait_names():
         trait = TraitsEvaluator.get_trait(name)
         for phenotype in trait.get_phenotypes():
             phenotype_genotypes = trait.trait_map[phenotype]
             if phenotype in trait.phenotype_probs:
                 prob_genotypes = [
                     g for (g, p) in trait.phenotype_probs[phenotype]
                 ]
                 for pg in prob_genotypes:
                     self.assertIn(
                         pg, phenotype_genotypes,
                         "Genotype %s of probability map is not in the genotype map of trait %s with phenotype %s"
                         % (pg, name, phenotype))
Exemple #6
0
 def runTest(self):
     for name in TraitsEvaluator.get_trait_names():
         trait = TraitsEvaluator.get_trait(name)
         trait_genotypes = list(
             chain.from_iterable(trait.trait_map.values()))
         prob_genotypes = [
             g for (g, p) in list(
                 chain.from_iterable(trait.phenotype_probs.values()))
         ]
         for pg in prob_genotypes:
             self.assertIn(
                 pg, trait_genotypes,
                 "Genotype %s of probability map is not in the genotype map of trait %s"
                 % (pg, name))
Exemple #7
0
 def runTest(self):
     for name in TraitsEvaluator.get_trait_names():
         trait = TraitsEvaluator.get_trait(name)
         sums = {}
         for g, p in list(
                 chain.from_iterable(trait.phenotype_probs.values())):
             if g in sums:
                 sums[g] += p
             else:
                 sums[g] = p
         for g in sums.keys():
             self.assertEqual(
                 sums[g], 1,
                 "Trait %s. Sum of probabilities of genotype %s must be 1" %
                 (name, g))
Exemple #8
0
    def runTest(self):
        trait = 'asdfsadf'
        mothers_traits = ['PLUS']
        fathers_traits = ['MINUS']

        self.assertEqual(
            TraitsEvaluator.offspring_probs(trait, mothers_traits,
                                            fathers_traits), {})
Exemple #9
0
 def __paternity_map(self):
     res = {}
     for trait_name in self.child_traits_map.iterkeys():
         res[trait_name] = TraitsEvaluator.phenotype_probability(
             trait_name, self.father_traits_map.get(trait_name, []),
             self.mother_traits_map.get(trait_name, []),
             self.child_traits_map[trait_name])
     return res
Exemple #10
0
 def __get_offspring_traits(self):
     res = {}
     for trait_name in self.paternity_map.iterkeys():
         probs = TraitsEvaluator.offspring_probs(
             trait_name, self.father_traits_map.get(trait_name, []),
             self.mother_traits_map.get(trait_name, []))
         res[trait_name] = probs
     return res
Exemple #11
0
 def __get_lenghts(self):
     res = []
     for i in range(0, 3):
         for trait_name in self.trait_names:
             number = len(TraitsEvaluator.get_trait_phenotypes(trait_name))
             if self.with_unknown and i != 2:
                 number += 1
             res.append(number)
     return res
Exemple #12
0
 def runTest(self):
     for name in TraitsEvaluator.get_trait_names():
         trait = TraitsEvaluator.get_trait(name)
         prob_genotypes = [
             g for (g, p) in list(
                 chain.from_iterable(trait.phenotype_probs.values()))
         ]
         shared_keys = []
         for phenotype in trait.trait_map.iterkeys():
             for g in trait.trait_map[phenotype]:
                 for phenotype1 in trait.trait_map.iterkeys():
                     if phenotype1 != phenotype:
                         if g in trait.trait_map[phenotype1]:
                             shared_keys.append(g)
         for k in shared_keys:
             self.assertIn(
                 k, prob_genotypes,
                 "Trait %s. Genotype %s is shared and must be in probability map"
                 % (name, k))
Exemple #13
0
 def runTest(self):
     trait = 'BITTER_TASTE'
     mothers_traits = ['STRONG_TASTE']
     fathers_traits = ['ALMOST_CANT_TASTE']
     self.assertEqual(
         TraitsEvaluator.offspring_probs(trait, mothers_traits,
                                         fathers_traits), {
                                             'STRONG_TASTE': 0.6,
                                             'ALMOST_CANT_TASTE': 0.4
                                         })
Exemple #14
0
    def __init__(self, with_unknown=False, trait_names=None):
        self.trait_names = TraitsEvaluator.get_trait_names()
        if trait_names is not None:
            self.trait_names = trait_names

        self.with_unknown = with_unknown

        self.lenghts = self.__get_lenghts()
        self.index = [0] * len(self.lenghts)
        self.first = True
Exemple #15
0
 def runTest(self):
     trait = 'RH_FACTOR'
     mothers_traits = ['PLUS']
     fathers_traits = ['MINUS']
     self.assertEqual(
         TraitsEvaluator.offspring_probs(trait, mothers_traits,
                                         fathers_traits), {
                                             'PLUS': 0.5,
                                             'MINUS': 0.5
                                         })
Exemple #16
0
    def __get_traits(self, index):
        res = {}
        for i in range(0, len(self.trait_names)):
            phenotypes = TraitsEvaluator.get_trait_phenotypes(
                self.trait_names[i])

            if len(phenotypes) == index[i]:
                res[self.trait_names[i]] = phenotypes
            else:
                res[self.trait_names[i]] = [phenotypes[index[i]]]
        return res
Exemple #17
0
 def runTest(self):
     trait = 'EYE_COLOR'
     mothers_traits = ['BLUE']
     fathers_traits = []
     self.assertEqual(
         TraitsEvaluator.offspring_probs(trait, mothers_traits,
                                         fathers_traits), {
                                             'BLUE': 0.25,
                                             'BROWN': 0.5,
                                             'GREEN': 0.25
                                         })
Exemple #18
0
 def runTest(self):
     for name in TraitsEvaluator.get_trait_names():
         trait = TraitsEvaluator.get_trait(name)
         for phenotype in trait.trait_map.keys():
             phenotype_genotypes = trait.trait_map[phenotype]
             if phenotype in trait.phenotype_probs:
                 prob_genotypes = [
                     g for (g, p) in trait.phenotype_probs[phenotype]
                 ]
                 for pg in phenotype_genotypes:
                     if pg not in prob_genotypes:
                         self.assertEqual(
                             trait.get_phenotype_probs(pg, phenotype), 1,
                             "Trait %s. Genotype %s of phenotype %s must have 1 probability"
                             % (name, pg, phenotype))
             else:
                 for pg in phenotype_genotypes:
                     self.assertEqual(
                         trait.get_phenotype_probs(pg, phenotype), 1,
                         "Trait %s. Genotype %s of phenotype %s must have 1 probability"
                         % (name, pg, phenotype))
Exemple #19
0
 def runTest(self):
     trait = 'BLOOD_TYPE'
     mothers_traits = ['I', 'II']
     fathers_traits = ['IV']
     self.assertEqual(
         TraitsEvaluator.offspring_probs(trait, mothers_traits,
                                         fathers_traits), {
                                             'II': 0.5,
                                             'I': 0.0,
                                             'III': 0.25,
                                             'IV': 0.25
                                         })
Exemple #20
0
    def runTest(self):
        powers = {}

        for trait_name in TraitsEvaluator.get_trait_names():
            not_father_counter = 0
            #combinator = TraitsCombinations(with_unknown=True, trait_names=[trait_name])
            combinator = TraitsCombinations(trait_names=[trait_name])

            while combinator.has_next():
                combinator.next()
                tester = PaternityTester(combinator.get_father_traits(),
                                         combinator.get_mother_traits(),
                                         combinator.get_child_traits())
                if not tester.is_parent():
                    not_father_counter += 1
            powers[trait_name] = not_father_counter / float(
                combinator.number_of_combinations())

        print OrderedDict(
            sorted(powers.items(), key=lambda t: t[1], reverse=True))
Exemple #21
0
def grandparent(request):
    if request.method == 'POST':
        parent_form1 = TraitsForm(request.POST, prefix='parent_form1')
        child_form = TraitsForm(request.POST, prefix='child_form')
        if parent_form1.is_valid()  and child_form.is_valid():
            grandparent_map = parent_form1.get_traits_map()
            father_map = {}
            for trait in grandparent_map.keys():
                father_map[trait] = TraitsEvaluator.reduce_traits(trait, grandparent_map[trait], [], [])
            print father_map
            tester = PaternityTester(father_map, None, child_form.get_traits_map())

            no_proof = tester.get_no_proof_traits()
            possible = tester.get_possible_traits()
            is_parent = tester.is_parent()


            #TODO: redirect somewhere
            return render(request, 'traits_web_prototype/grandparent.html',
                          {
                              'parent_form1': parent_form1,
                              'child_form': child_form,
                              'no_proof' : no_proof,
                              'possible' : possible,
                              'is_parent' : is_parent,
                          })

    else:
            parent_form1 = TraitsForm(prefix='parent_form1')
            child_form = TraitsForm(prefix='child_form')


    return render(request, 'traits_web_prototype/grandparent.html',
              {
                  'parent_form1': parent_form1,
                  'child_form': child_form,
              })
Exemple #22
0
 def get_traits_map(self):
     res = {}
     for name in TraitsEvaluator.get_trait_names():
         res[name] = [self.cleaned_data[name]]
     return res
    def runTest(self):
        map = {}
        for name in TraitsEvaluator.get_trait_names():
            map[name] = TraitsEvaluator.get_trait_phenotypes(name)[0]
        father_traits_map = map
        mother_traits_map = map
        child_traits_map = map
        tester = PaternityTester(father_traits_map, mother_traits_map,
                                 child_traits_map)

        self.assertFalse(tester.is_parent())
        self.assertEqual(
            tester.get_possible_traits(), {
                'BITTER_TASTE': {
                    'STRONG_TASTE': 0.8,
                    'ALMOST_CANT_TASTE': 0.2
                },
                'ALCOHOL_FLUSH': {
                    'LITTLE_OR_NO': 0.25,
                    'MODERATE': 0.5,
                    'EXTREME': 0.25
                },
                'EARWAX': {
                    'DRY': 0.25,
                    'WET': 0.75
                },
                'HAIR_WHORL': {
                    'COUNTER_CLOCKWISE': 0.2175,
                    'CLOCKWISE': 0.7825
                },
                'HAIR_COLOR': {
                    'STRAWBERRY_BLOND': 0.125,
                    'BLACK': 0.25,
                    'RED': 0.0625,
                    'BLOND': 0.0625,
                    'BROWN': 0.5
                },
                'CLEFT_CHIN': {
                    'CLEFT': 0.2275,
                    'SMOOTH': 0.7725
                },
                'EYE_COLOR': {
                    'BLUE': 0.0625,
                    'BROWN': 0.75,
                    'GREEN': 0.1875
                },
                'TONGUE_ROLLING': {
                    'ROLL': 0.835,
                    'NO_ROLL': 0.165
                },
                'EARLOBES': {
                    'DISATTACHED': 0.2325,
                    'ATTACHED': 0.7675
                },
                'RH_FACTOR': {
                    'PLUS': 0.75,
                    'MINUS': 0.25
                },
                'LACTOSE_INTOLERANCE': {
                    'INTOLERANT_LIKELY': 0.25,
                    'TOLERANT_LIKELY': 0.75
                },
                'BLOOD_TYPE': {
                    'I': 1.0
                }
            })
        self.assertIn('BLOOD_TYPE', tester.get_no_proof_traits())