예제 #1
0
파일: MLP.py 프로젝트: poneill/gini_project
 def detect_correlations(self,site):
     """Does the MLP exhibit any correlations of the type
     demonstrated by Man & Stormo (NAR 2001)?"""
     bases = "ATCG" # follow ordering defined in Recognizer class
     w = len(site)
     all_dependent = True
     dependencies = 0
     discrepancy_dict = {}
     for (i,j) in choose2(range(w)):
         print (i,j)
         base1 = site[i]
         base2 = site[j]
         base_index1 = Recognizer.base_index[base1]
         base_index2 = Recognizer.base_index[base2]
         default_energy = self.binding_energy(site)
         alt_sites = [[subst(subst(site,b1,i),b2,j)
                       for b2 in bases]
                      for b1 in bases]
         alt_energies = mmap(self.binding_energy,alt_sites)
         best_energy = min(map(min,alt_energies))
         norm_energies = mmap(lambda x:x/best_energy,alt_energies)
         norm_default_energy = default_energy/best_energy
         independent = all((sorted_indices(alt_energy_row) ==
                            sorted_indices(alt_energies[0]))
                           for alt_energy_row in alt_energies)
         
         #independent = False
         if independent:
             all_dependent = False
         else:
             dependencies += 1
             ###Print motif
             print (i,j)
             print "     "
             for c2 in bases:
                 print "    ",c2,
             print
             
             for b_i1,c1 in enumerate(bases):
                 print c1,
                 for b_i2 in range(4):
                     print "%1.4f" % norm_energies[b_i1][b_i2],
                 print
             ###Finish printing motif
             base_1_mutant_energies = [(b,row[base_index2])
                                       for (b,row) in zip(bases,norm_energies)]
             print "base 1 mutants:",base_1_mutant_energies
             best_responses = [min(zip(bases,row),
                                   key=lambda (b,x):abs(norm_default_energy - x))
                                    for row in norm_energies]
             print "best responses:",best_responses
             discrepancy = max(zip(base_1_mutant_energies, best_responses),
                               key = lambda ((b1,x),(b2,y)): abs(x-y))
             print "actual bases:",base1,base2,(base_index1,base_index2)
             print "norm default energy:",norm_default_energy
             (b1,v1),(b2,v2) = discrepancy
             print "discrepancy:",b1,b2,v1,v2,v2-v1
             discrepancy_dict[(i,j)] = v2-v1
             
     print "all dependent:",all_dependent
     print "# dependencies:", dependencies, "out of:",len(choose2(range(w)))
     return discrepancy_dict
예제 #2
0
파일: MLP.py 프로젝트: poneill/gini_project
 def detect_correlations2(self,site):
     """Does the MLP exhibit any correlations of the type
     demonstrated by Man & Stormo (NAR 2001)?"""
     bases = "ATCG" # follow ordering defined in Recognizer class
     w = len(site)
     all_dependent = True
     dependencies = 0
     discrepancy_dict = {}
     for (i,j) in choose2(range(w)):
         print (i,j)
         base1 = site[i]
         base2 = site[j]
         base_index1 = Recognizer.base_index[base1]
         base_index2 = Recognizer.base_index[base2]
         default_energy = self.binding_energy(site)
         alt_sites = [[subst(subst(site,b1,i),b2,j)
                       for b2 in bases]
                      for b1 in bases]
         alt_energies = mmap(self.binding_energy,alt_sites)
         best_energy = min(map(min,alt_energies))
         norm_energies = mmap(lambda x:x/best_energy,alt_energies)
         norm_default_energy = default_energy/best_energy
         independent = all((sorted_indices(alt_energy_row) ==
                            sorted_indices(alt_energies[0]))
                           for alt_energy_row in alt_energies)
         
         #independent = False
         if independent:
             all_dependent = False
         else:
             dependencies += 1
             ###Print motif
             print (i,j)
             print "     "
             for c2 in bases:
                 print "    ",c2,
             print
             
             for b_i1,c1 in enumerate(bases):
                 print c1,
                 for b_i2 in range(4):
                     print "%1.4f" % norm_energies[b_i1][b_i2],
                 print
             print "actual bases:",base1,base2,(base_index1,base_index2)
             ###Finish printing motif
             for base,row in zip(bases,norm_energies):
                 if (norm_energies[base_index1][base_index2] > row[base_index2] and
                     (all(norm_energies[base_index1][b] < row[b]
                          for b in range(4) if b != base_index1))):
                     print "discrepancy:", (norm_energies[base_index1][base_index2] -
                                           row[base_index2])
                     print "consider row:",base,row
             tr_norm_energies = transpose(norm_energies)
             for base,col in zip(bases,tr_norm_energies):
                 if (norm_energies[base_index1][base_index2] > col[base_index1]
                     and
                     (all(norm_energies[b][base_index2] < col[b]
                          for b in range(4) if b != base_index1))):
                     print "discrepancy:",(norm_energies[base_index1][base_index2] -
                                           col[base_index1])
                     print "consider col:",base,col
                   
             
     print "all dependent:",all_dependent
     print "# dependencies:", dependencies, "out of:",len(choose2(range(w)))
     return discrepancy_dict