Exemplo n.º 1
0
 def run(self):
     try:
         null_generator(self.procID, self.fam, self.fam_npl,\
                 self.q, self.all_ibd,self.null_ibd,\
                 self.pinv_sall,self.pinv_key_dict,self.pinv_pair_dict,self.sall_flag,self.infer_flag)
     except Exception as e:
         screen_output.err_out("error in: %d" % self.procID)
Exemplo n.º 2
0
def null_generator_trait(procID, fam, queue, null_ibd):
    #generate permutations by shuffling phenotypes
    while True:
        try:
            traits = queue.get()
            if traits is None:
                break
            else:
                #traits is one possible permutation of traits
                #permutations
                new_traits = {}
                for idx, iid in enumerate(fam.all_members):
                    new_traits[iid] = traits[idx]
                n, d = fam.matrix_cal_trait(new_traits)
                null_ibd.append((n, d))
        except Exception as e:
            screen_output.err_out("error in null_generator {}:{}".format(
                procID, e))
            raise TypeError
            break
Exemplo n.º 3
0
 def classify_affect(self):  #mid: marker index
     if self.simple:
         affected_num = len(self.affected)
         for i in range(affected_num):
             for j in range(i + 1,
                            affected_num):  #for each pair of affecteds
                 aid = self.affected[i]
                 bid = self.affected[j]
                 ap = self.fam_dict[aid]['parents']
                 bp = self.fam_dict[bid]['parents']
                 if self.dcousin(ap, bp, self.gpid, self.gmid):
                     # if a and b are cousin
                     self.cousin.append([aid, bid])
                 elif self.dun(aid, bid, ap, bp, self.gpid, self.gmid):
                     # uncle-nephew
                     if bp == [self.gpid, self.gmid]:  #put uncle at first
                         self.un.append([bid, aid])
                     else:
                         self.un.append([aid, bid])
                 elif self.dsib(ap, bp):  # sibpair
                     self.sib.append([aid, bid])
                 elif self.dhsib(ap, bp):
                     self.hsib.append([aid, bid])
                 tmp_gp = self.dgrandpa(aid, bid, ap, bp)
                 if tmp_gp == 1:
                     self.gp.append([aid, bid])
                 elif tmp_gp == 2:
                     self.gp.append([bid, aid])
     miss_persons = []
     for member in self.missing_all:
         offmiss_count = 0
         for tmp_off in self.fam_dict[member]['offspring']:
             if tmp_off in self.missing_all:
                 offmiss_count += 1
         miss_persons.append((member, offmiss_count))
         #sort miss_persons so that person with sequenced offspring can\
         #be inferred first
     self.sorted_miss_persons = sorted(miss_persons,
                                       key=lambda x:
                                       (self.generation(x[0]), x[1]))
     if self.sorted_miss_persons != []:
         self.GT_infer()
     #print self.conditional_prob
     #remove useless individuals
     rm_flag = False
     rm_genotyped_person = False
     if not self.err and self.sorted_miss_persons != []:
         for m_person in self.sorted_miss_persons:
             if m_person[0] not in self.conditional_prob['~combined'][1]:
                 self.remove(m_person[0])
                 rm_flag = True
         try:
             if self.conditional_prob['~combined'][0] == {}:
                 screen_output.err_out('conditional_prob is NULL')
                 self.err = True
         except KeyError:
             screen_output.err_out('conditional_prob no key')
             self.err = True
     if rm_flag:
         self.set_famstruct()
     self.offspring_pairs = []
     for idx, aid in enumerate(self.affected):
         for bid in self.affected[idx + 1:]:
             if aid in self.founder and bid in self.founder or aid in self.fam_dict[
                     bid]['mate']:
                 continue
             dchildren = self.dchildren(aid, bid)
             if dchildren:
                 if dchildren == 1:
                     self.offspring_pairs.append([bid, aid])
                 else:
                     self.offspring_pairs.append([aid, bid])
                 self.affected_pairs.append([aid, bid])
             else:
                 if aid in self.founder or bid in self.founder:
                     if not self.doffspring(aid, bid):
                         continue
                 self.affected_pairs.append([aid, bid])
     if len(self.affected_pairs) == 0:
         screen_output.err_out('no affected relatives pairs')
         self.err = True
Exemplo n.º 4
0
 def run(self):
     try:
         null_generator_trait(self.procID, self.fam, self.q, self.null_ibd)
     except Exception as e:
         screen_output.err_out("error in: %d" % self.procID)
Exemplo n.º 5
0
 def run(self):
     try:
         null_generator(self.procID, self.founderid, self.fam, self.q,
                        self.all_ibd, self.cond_null_ibd, self.null_ibd)
     except Exception as e:
         screen_output.err_out("error in: %d" % self.procID)
Exemplo n.º 6
0
def null_generator(procID, founderid, fam, queue, all_ibd, cond_null_ibd,
                   null_ibd, n_jobs, perfect_max):
    #generate permutations by shuffling founder genotypes
    while True:
        try:
            data = queue.get()
            if data is None:
                break
            else:
                foundergt = {}
                new_alleles = {}
                ibdall = []  #pairwise IBD
                nfnum = len(fam.nonfounder)
                #permutations
                random_gt = None
                if fam.missing_all != []:
                    tmp_dic = fam.conditional_prob['~combined'][0]
                    gt_id = fam.conditional_prob['~combined'][1]
                    r = random.uniform(0, 1)
                    s = 0
                    for gt, prob in tmp_dic.iteritems():
                        s += prob
                        if s >= r:
                            random_gt = gt
                            break
                    if random_gt == None:
                        random_gt = tmp_dic.keys()[0]
                for f in founderid:
                    if f in fam.missing_all:
                        idx = gt_id.index(f)
                        foundergt[f] = [
                            ord(x) - 96 for x in random_gt[2 * idx:2 * idx + 2]
                        ]
                    else:
                        foundergt[f] = fam.fam_dict[f]['gt'][2 * fam.mid:2 *
                                                             fam.mid + 2]
                #Randomly assign an inheritance vector
                v = random.randint(0, pow(2, 2 * nfnum) - 1)
                tmp = bin(v)[2:]  #get binary format
                inv = '0' * (2 * nfnum - len(tmp)
                             ) + tmp  #make tmp complete with bit number
                #print "foundergt"+repr(foundergt)
                new_alleles = fam.getGT(inv, foundergt)
                conditional_prob = {}
                #Assign missing again and re-impute
                if fam.missing_all != []:
                    for person in fam.missing_all:  #fam.sorted_miss_persons:
                        new_alleles[person] = [0, 0]
                allele_key = ''
                all_gt = []
                for person in sorted(new_alleles.keys()):
                    allele_key += ''.join(map(str, new_alleles[person]))
                    all_gt += new_alleles[person]
                if allele_key not in all_ibd.keys():
                    all_ibd[allele_key] = None
                    if fam.missing_all != [] and set(all_gt) != set([0, 1]):
                        fam.GT_infer(new_alleles, conditional_prob)
                        if fam.rvibd:
                            cond_key = conditional_prob['~combined'][0].items(
                            )[0]
                            if cond_key not in cond_null_ibd.keys():
                                cond_null_ibd[cond_key] = None
                                fam.null_permute(n_jobs, perfect_max,
                                                 conditional_prob)
                                cond_null_ibd[cond_key] = [
                                    fam.expect_pair_ibd, fam.prior
                                ]
                            else:
                                while cond_null_ibd[cond_key] is None:
                                    time.sleep(.01)
                                fam.expect_pair_ibd, fam.prior = cond_null_ibd[
                                    cond_key]
                    #print allele_key
                    #print new_alleles
                    #print all_gt
                    if set(all_gt) == set([0, 1]):
                        n, d = None, None
                    else:
                        n, d = fam.matrix_cal(new_alleles, conditional_prob)
                    all_ibd[allele_key] = (n, d)
                    if (n, d) != (None, None):
                        null_ibd.append((n, d))
                    #print n,d
                else:
                    while all_ibd[allele_key] is None:
                        time.sleep(.01)
                    else:
                        n, d = all_ibd[allele_key]
                        if (n, d) != (None, None):
                            null_ibd.append((n, d))
        except Exception as e:
            screen_output.err_out("error in null_generator {}:{}".format(
                procID, e))
            raise TypeError
            break
Exemplo n.º 7
0
def null_generator(procID, fam, fam_npl, queue, all_ibd, null_ibd, pinv_sall,
                   pinv_key_dict, pinv_pair_dict, sall_flag, infer_flag):
    outID = 0
    while True:
        try:
            data = queue.get()
            if data is None:
                break
            else:
                ibd_result = [None, None]
                foundergt_list = []
                foundergt = {}
                new_alleles = {}
                nfnum = len(fam.nonfounder)
                if isinstance(data, tuple):
                    #calculate null ibd by genotypes
                    v, fgt = data
                    for idx, f in enumerate(fam.founder):
                        foundergt[f] = list(fgt[2 * idx:2 * idx + 2])
                else:
                    #permutations
                    random_gt = None
                    if fam.missing_all != []:
                        tmp_dic = fam.conditional_prob['~combined'][0]
                        gt_id = fam.conditional_prob['~combined'][1]
                        r = random.uniform(0, 1)
                        s = 0
                        for gt, prob in tmp_dic.iteritems():
                            s += prob
                            if s >= r:
                                random_gt = gt
                                break
                        if random_gt == None:
                            random_gt = tmp_dic.keys()[0]
                    for f in fam.founder:
                        if f in fam.missing_all:
                            idx = gt_id.index(f)
                            foundergt[f] = [
                                ord(x) - 96
                                for x in random_gt[2 * idx:2 * idx + 2]
                            ]
                        else:
                            foundergt[f] = fam.fam_dict[f]['gt'][2 *
                                                                 fam.mid:2 *
                                                                 fam.mid + 2]
                    #Randomly assign an inheritance vector
                    v = random.randint(0, pow(2, 2 * nfnum) - 1)
                tmp = bin(v)[2:]  #get binary format
                inv = '0' * (2 * nfnum - len(tmp)
                             ) + tmp  #make tmp complete with bit number
                #print "foundergt"+repr(foundergt)
                new_alleles = fam.getGT(
                    inv, foundergt)  #assign fam member genotypes based on inv
                #if there is missing parent, re-impute parental GT based on
                #newly generated offspring GT
                #print "new alleles"+repr(new_alleles)
                conditional_prob = {}
                if fam.missing_all != []:
                    for person in fam.missing_all:  #fam.sorted_miss_persons:
                        new_alleles[person] = [0, 0]
                allele_key = ''
                if fam.identical_sib != []:
                    #affected siblings that are identical in terms of GT imputation
                    for affsib in fam.identical_sib:
                        identical_sib_gt = []
                        for tmpsib in affsib:
                            identical_sib_gt.append(new_alleles[tmpsib])
                        if identical_sib_gt == sorted(identical_sib_gt):
                            continue
                        identical_sib_gt = sorted(identical_sib_gt)
                        for tsib_idx, tmpsib in enumerate(affsib):
                            new_alleles[tmpsib] = identical_sib_gt[tsib_idx]
                for person in sorted(new_alleles.keys()):
                    allele_key += ''.join(map(str, new_alleles[person]))
                if allele_key not in all_ibd.keys(
                ) or sall_flag and all_ibd[allele_key][1] == 0:
                    if fam.missing_all != []:
                        #print "GT_infer start"
                        #print repr(procID)+" new_alleles:"+repr(new_alleles)
                        fam.GT_infer(new_alleles,
                                     conditional_prob,
                                     infer_flag=infer_flag)

#print "GT_infer end"
#print "new conditional_prob"+repr(conditional_prob)
#number of alleles IBD among affecteds under inv
                    ibd_result = fam_npl.cal_ibd(new_alleles, conditional_prob,
                                                 pinv_pair_dict, pinv_sall,
                                                 pinv_key_dict, sall_flag)
                    #print repr(procID)+" IBD finish: "+repr(ibd_result)
                    all_ibd[allele_key] = ibd_result
                    null_ibd.append(ibd_result)
                else:
                    while True:
                        try:
                            ibd_result = all_ibd[allele_key]
                            null_ibd.append(ibd_result)
                            break
                        except:
                            #in case key error, wait for the key:value to be assigned
                            pass
        except Exception as e:
            screen_output.err_out("error in null_generator {}:{}".format(
                procID, e))
            raise TypeError
            break