git-svn-id: http://svn.tuebingen.mpg.de/ag-raetsch/projects/QPalma@8653 e1793c9e...
[qpalma.git] / scripts / PipelineHeuristic.py
index 669f5ee..4ea1cb9 100644 (file)
@@ -79,7 +79,7 @@ class PipelineHeuristic:
 
       # when we look for alternative alignments with introns this value is the
       # mean of intron size
-      self.intron_size  = 90
+      self.intron_size  = 250
 
       self.read_size    = 36
    
@@ -178,7 +178,7 @@ class PipelineHeuristic:
             if strand == '-':
                continue
 
-            if ctr == 100:
+            if ctr == 1000:
                break
 
             #if pos > 10000000:
@@ -260,29 +260,51 @@ class PipelineHeuristic:
       print 'True neg / false neg : %d %d'%(self.true_neg,self.false_neg)
 
 
-   def findHighestScoringSpliceSites(self,currentAcc,currentDon):
+   def findHighestScoringSpliceSites(self, currentAcc, currentDon, DNA, max_intron_size, read_size, splice_thresh):
 
-      acc   = []
-      for idx,score in enumerate(currentAcc):
-         if score > -inf:
-            acc.append((idx,score))
-         if idx>self.read_size:
-             break 
+      def signum(a):
+          if a>0: 
+              return 1
+         elif a<0:
+              return -1
+          else:
+              return 0
 
-      acc.sort(lambda x,y: x[0]-y[0]) 
+      proximal_acc   = []
+      for idx in xrange(max_intron_size, max_intron_size+read_size):
+          if currentAcc[idx]>= splice_thresh:
+            proximal_acc.append((idx,currentAcc[idx]))
 
-      don   = []
-      for idx,score in enumerate(currentDon):
-         if score > -inf:
-            don.append((idx,score))
-         if idx>self.read_size:
-             break 
+      proximal_acc.sort(lambda x,y: signum(x[1]-y[1])) 
+      proximal_acc=proximal_acc[-2:]
 
-      don.sort(lambda x,y: x[0]-y[0]) 
+      distal_acc   = []
+      for idx in xrange(max_intron_size+read_size, len(currentAcc)):
+          if currentAcc[idx]>= splice_thresh:
+            distal_acc.append((idx, currentAcc[idx], DNA[idx+1:idx+read_size]))
 
-      return don,acc
+      #distal_acc.sort(lambda x,y: signum(x[1]-y[1])) 
+      #distal_acc=distal_acc[-2:]
 
 
+      proximal_don   = []
+      for idx in xrange(max_intron_size, max_intron_size+read_size):
+         if currentDon[idx] >= splice_thresh:
+            proximal_don.append((idx, currentDon[idx]))
+
+      proximal_don.sort(lambda x,y: signum(x[1]-y[1]))
+      proximal_don=proximal_don[-2:]
+
+      distal_don   = []
+      for idx in xrange(1, max_intron_size):
+         if currentDon[idx] >= splice_thresh:
+            distal_don.append((idx, currentDon[idx], DNA[idx-read_size:idx]))
+
+      #distal_don.sort(lambda x,y: signum(x[1]-y[1]))
+      #distal_don=distal_don[-2:]
+
+      return proximal_acc,proximal_don,distal_acc,distal_don
+
    def calcAlternativeAlignments(self,location):
       """
       Given an alignment proposed by Vmatch this function calculates possible
@@ -291,43 +313,38 @@ class PipelineHeuristic:
       """
 
       run = self.run
+      splice_thresh = 0.01
+      max_intron_size = 2000 
 
       id       = location['id']
       chr      = location['chr']
       pos      = location['pos']
       strand   = location['strand']
-      seq      = location['seq']
-      #orig_seq = location['orig_seq']
-      prb      = location['prb']
+      original_est = location['seq']
+      quality      = location['prb']
       cal_prb  = location['cal_prb']
       
-      orig_seq = self.original_reads[int(id)]
-
-      unb_seq = unbracket_est(seq)
-      effective_len = len(unb_seq)
+      est = unbracket_est(original_est)
+      effective_len = len(est)
 
-      genomicSeq_start  = pos
-      genomicSeq_stop   = pos+self.intron_size*2+self.read_size*2
+      genomicSeq_start  = pos - max_intron_size
+      genomicSeq_stop   = pos + max_intron_size + len(est)
 
       start = cpu()
       currentDNASeq, currentAcc, currentDon = get_seq_and_scores(chr,strand,genomicSeq_start,genomicSeq_stop,run['dna_flat_files'])
       stop = cpu()
       self.get_time += stop-start
-
       dna   = currentDNASeq
 
-      start = cpu()
-      alt_don,alt_acc = self.findHighestScoringSpliceSites(currentAcc,currentDon)
-      stop = cpu()
-      self.splice_site_time = stop-start
-
+      proximal_acc,proximal_don,distal_acc,distal_don = self.findHighestScoringSpliceSites(currentAcc,currentDon, dna, max_intron_size, len(est), splice_thresh)
+       
+      print proximal_acc
+      print proximal_don
+      print distal_acc
+      print distal_don
+      
       alternativeScores = []
-
-      exons          = zeros((2,2),dtype=numpy.int)
-      est            = unb_seq
-      original_est   = seq
-      quality        = prb
-
+      
       # inlined
       h = self.h
       d = self.d
@@ -336,74 +353,65 @@ class PipelineHeuristic:
       qualityPlifs = self.qualityPlifs
       # inlined
 
-      IntronScore = calculatePlif(h, [self.intron_size])[0]
+      # compute dummy scores
+      #IntronScore = calculatePlif(h, [math.fabs(max_acc_pos-30)])[0]
+      #dummyAcceptorScore = calculatePlif(a, [max_acc_score])[0] 
+      IntronScore = calculatePlif(h, [self.intron_size])[0] - 0.5
       dummyAcceptorScore = calculatePlif(a, [0.25])[0] 
       dummyDonorScore = calculatePlif(d, [0.25])[0]
-      print IntronScore,dummyAcceptorScore,dummyDonorScore
       
       _start = cpu()
-      for (don_pos,don_score) in alt_don:
-         start = cpu()
-
-         acc_pos = don_pos + self.intron_size
-         
-         exons[0,0]     = 0
-         exons[0,1]     = don_pos
-         exons[1,0]     = acc_pos+1
-         exons[1,1]     = acc_pos+1+(self.read_size-don_pos)
-
-         _dna = dna[:int(exons[1,1])]
-         _dna = _dna[:exons[1,0]] + est[don_pos:]# only correct if there are no indels!!!
-
-         _currentAcc = currentAcc[:int(exons[1,1])]
-         _currentAcc = [0.25]*len(_currentAcc) 
-
-         _currentDon = currentDon[:int(exons[1,1])]
-         #_currentDon = [0.25]*len(_currentDon)
-
-         currentVMatchAlignment = _dna, exons, est, original_est, quality,\
-         _currentAcc, currentDon_
-
-         stop = cpu()
-         self.array_stuff += stop - start
-
-         #alternativeScore = self.calcAlignmentScore(currentVMatchAlignment)
-         #alternativeScores.append(self.calcAlignmentScore(currentVMatchAlignment))
-
-         # Lets start calculation
-         dna, exons, est, original_est, quality, acc_supp, don_supp =\
-         currentVMatchAlignment
-
-         # Berechne die Parameter des wirklichen Alignments (but with untrained d,a,h ...)    
-         trueSpliceAlign, trueWeightMatch, trueWeightQuality ,dna_calc =\
-         computeSpliceAlignWithQuality(dna, exons, est, original_est,\
-         quality, qualityPlifs, run)
+      for (don_pos,don_score) in proximal_don:
+         # remove mismatching positions in the second exon
+         original_est_cut=''
 
-         stop = cpu()
-         self.computeSpliceAlignWithQualityTime += stop-start
-         start = cpu()
+         est_ptr=0
+         dna_ptr=0
+         ptr=0 
+         while ptr<len(original_est):
+             
+            if original_est[ptr]=='[':
+                dnaletter=original_est[ptr+1]
+                estletter=original_est[ptr+2]
+                if dna_ptr < don_pos:
+                    original_est_cut+=original_est[ptr:ptr+4] 
+                else:
+                    #original_est_cut+=estletter # EST letter
+                    original_est_cut+=dnaletter # DNA letter
+                ptr+=4 
+            else:
+                dnaletter=original_est[ptr]
+                estletter=dnaletter
+                
+                original_est_cut+=estletter # EST letter
+                ptr+=1
 
-         # Calculate the weights
-         trueWeightDon, trueWeightAcc, trueWeightIntron =\
-         computeSpliceWeights(d, a, h, trueSpliceAlign, don_supp, acc_supp)
+            if estletter=='-':
+                dna_ptr+=1 
+            elif dnaletter=='-':
+                est_ptr+=1
+            else:
+                dna_ptr+=1 
+                est_ptr+=1
+                         
+         assert(dna_ptr<=len(dna))
+         assert(est_ptr<=len(est))
 
-         for i in xrange(0,len(trueWeightDon)):
-             trueWeightDon[i]=0.0
-         for i in xrange(0,len(trueWeightAcc)):
-             trueWeightAcc[i]=0.0
-         for i in xrange(0,len(trueWeightIntron)):
-             trueWeightIntron[i]=0.0
+         #print "Donor"
+         DonorScore = calculatePlif(d, [don_score])[0]
+         #print DonorScore,don_score,don_pos
          
-         stop = cpu()
-         self.computeSpliceWeightsTime += stop-start
-
-         start = cpu()
-
-         trueWeight = numpy.vstack([trueWeightIntron, trueWeightDon, trueWeightAcc, trueWeightMatch, trueWeightQuality])
+         score = computeSpliceAlignScoreWithQuality(original_est_cut, quality, qualityPlifs, run, self.currentPhi)
+         score += dummyAcceptorScore + IntronScore + DonorScore
+         
+         #print 'diff %f,%f,%f' % ((trueWeight.T * self.currentPhi)[0,0] - score,(trueWeight.T * self.currentPhi)[0,0], score)
+         alternativeScores.append(score)
 
-         # Calculate w'phi(x,y) the total score of the alignment
-         alternativeScores.append((trueWeight.T * self.currentPhi)[0,0])
+      _stop = cpu()
+      self.alternativeScoresTime += _stop-_start
 
+      _start = cpu()
+      for (acc_pos,acc_score) in alt_acc:
          # remove mismatching positions in the second exon
          original_est_cut=''
 
@@ -415,7 +423,7 @@ class PipelineHeuristic:
             if original_est[ptr]=='[':
                 dnaletter=original_est[ptr+1]
                 estletter=original_est[ptr+2]
-                if est_ptr<=exons[0,1]:
+                if est_ptr>=acc_pos:
                     original_est_cut+=original_est[ptr:ptr+4] 
                 else:
                     original_est_cut+=estletter # EST letter
@@ -438,19 +446,21 @@ class PipelineHeuristic:
          assert(dna_ptr<=len(dna))
          assert(est_ptr<=len(est))
 
-         print original_est, original_est_cut
+         #print original_est,original_est_cut
          
-         # new score
-         DonorScore = calculatePlif(d, [don_score])[0]
-         #print 'don: %f,%f' % (DonorScore, don_score)
+         AcceptorScore = calculatePlif(d, [acc_score])[0]
+         #print "Acceptor"
+         #print AcceptorScore,acc_score,acc_pos
+
+         #if acc_score<0.1:
+         #    print currentAcc[0:50]
+         #    print currentDon[0:50]
          
          score = computeSpliceAlignScoreWithQuality(original_est_cut, quality, qualityPlifs, run, self.currentPhi)
-         #score += dummyAcceptorScore + IntronScore + DonorScore
+         score += AcceptorScore + IntronScore + dummyDonorScore
          
-         print 'diff %f,%f,%f' % ((trueWeight.T * self.currentPhi)[0,0] - score,(trueWeight.T * self.currentPhi)[0,0], score)
-
-         stop = cpu()
-         self.DotProdTime += stop-start
+         #print 'diff %f,%f,%f' % ((trueWeight.T * self.currentPhi)[0,0] - score,(trueWeight.T * self.currentPhi)[0,0], score)
+         alternativeScores.append(score)
 
       _stop = cpu()
       self.alternativeScoresTime += _stop-_start
@@ -493,10 +503,10 @@ if __name__ == '__main__':
    jp = os.path.join
 
    run_fname   = jp(dir,'run_object.pickle')
-   #data_fname = '/fml/ag-raetsch/share/projects/qpalma/solexa/current_data/map.vm_unspliced_flag'
+   #data_fname = '/fml/ag-raetsch/share/projects/qpalma/solexa/current_data/map.vm_unspliced_1k'
 
-   #data_fname  = '/fml/ag-raetsch/share/projects/qpalma/solexa/pipeline_data/map.vm_2k'
-   data_fname  = '/fml/ag-raetsch/share/projects/qpalma/solexa/pipeline_data/map.vm_100'
+   data_fname  = '/fml/ag-raetsch/share/projects/qpalma/solexa/pipeline_data/map.vm_2k'
+   #data_fname  = '/fml/ag-raetsch/share/projects/qpalma/solexa/pipeline_data/map.vm_100'
 
    param_fname = jp(dir,'param_500.pickle')