+ added alignment reconstruction function
[qpalma.git] / scripts / createAlignmentFileFromPrediction.py
index e089626..922dc70 100644 (file)
 
 import cPickle
 import sys
-import pydb
 import pdb
 import os
 import os.path
 import math
+import numpy
 
-def prediction_on(filename):
-    
-   allPredictions = cPickle.load(open(filename))
+import qparser
 
-   allPositions = {}
+from Utils import pprint_alignment
 
+import palma.palma_utils as pu
+from palma.output_formating import print_results
 
-   for current_example_pred in allPredictions:
-      gt_example = current_example_pred[0]
 
-      exampleId = gt_example['id']
+def alignment_reconstruct(current_prediction,num_exons):
+   """
+   We reconstruct the exact alignment to infer the positions of indels and the
+   sizes of the respective exons.
+   """
 
-      for elem_nr,current_pred in enumerate(current_example_pred[1:]):
-         exampleId   = current_pred['id']
-         chr         = current_pred['chr']
-         strand      = current_pred['strand']
-         true_cut    = current_pred['true_cut']
-         start_pos   = current_pred['alternative_start_pos']
+   SpliceAlign = current_prediction['spliceAlign']
+   estAlign    = current_prediction['estAlign']
 
-         predExons = current_pred['predExons']
-         trueExons = current_pred['trueExons']
-         predPositions = [elem + current_pred['alternative_start_pos'] for elem in predExons]
-         truePositions = [elem + current_pred['start_pos'] for elem in trueExons.flatten().tolist()[0]]
+   dna_array   = current_prediction['dna_array']
+   est_array   = current_prediction['est_array']
 
-         if len(predExons) == 4:
-            p1 = predExons[0]
-            p2 = predExons[1]
-            p3 = predExons[2]
-            p4 = predExons[3]
+   dna_array   = "".join(map(lambda x: str(x),dna_array))
+   est_array   = "".join(map(lambda x: str(x),est_array))
 
-            #line = '%d\t%d\t%d\t%d\t%d\n' % (exampleId,p1,p2,p3,p4)
-            #print line
-            allPositions[exampleId] = (chr,strand,start_pos,true_cut,p1,p2,p3,p4)
+   translation = '-ACGTN_'    #how aligned est and dna sequence is displayed
+                              #(gap_char, 5 nucleotides, intron_char) 
 
+   # this array hold a number for each exons indicating its number of matches
+   exonIdentities = [0]*num_exons
+   exonGaps = [0]*num_exons
 
-   return allPositions
+   est_array = map(lambda x: translation[int(x)],est_array)
+   dna_array = map(lambda x: translation[int(x)],dna_array)
 
+   gaps = 0
+   identity = 0
+   idx = 0
 
-def writePredictions(fname,allPositions):
+   #if num_exons > 1:
+   #   pdb.set_trace()
 
-   out_fh = open(fname,'w+')
+   for ridx in range(len(est_array)):
+      read_elem = est_array[ridx]
+      dna_elem = dna_array[ridx]
 
-   allEntries = {}
+      if ridx > 0 and est_array[ridx-1] != '_' and est_array[ridx] == '_':
+         idx += 1
+         gaps = 0
+         identity = 0
+         
+      if read_elem == '_':
+         continue
 
-   for line in open('/fml/ag-raetsch/share/projects/qpalma/solexa/allReads.pipeline'):
-      line = line.strip()
-      id,seq,q1,q2,q3 = line.split()
-      id = int(id)
+      if read_elem == dna_elem:
+         identity += 1
+
+      if read_elem == '-':
+         gaps += 1
       
-      allEntries[id] = (seq,q1,q2,q3)
+      exonIdentities[idx] = identity
+      exonGaps[idx] = gaps
 
+   return exonIdentities,exonGaps
 
-   for id,elems in allPositions.items():
-      seq,q1,q2,q3 = allEntries[1000000000000+id]
-      chr,strand,start_pos,true_cut,p1,p2,p3,p4 = elems
 
-      p1 += start_pos
-      p2 += start_pos
-      p3 += start_pos
-      p4 += start_pos
+def create_alignment_file(current_loc,out_fname):
+   import numpy
+   #qparser.parse_reads(filtered_reads)
 
-      new_line = '%d\t%d\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n' %\
-      (id,chr,strand,seq,q1,q2,q3,true_cut,p1,p2,p3,p4)
+   out_fh = open(out_fname,'w+')
 
-      out_fh.write(new_line)
+   allPredictions = cPickle.load(open(current_loc))
+   #allPredictions = current_loc
 
-   out_fh.close()
+   allPositions = {}
 
-def collect_prediction(current_dir):
-   """
-   Given the toplevel directory this function takes care that for each distinct
-   experiment the training and test predictions are evaluated.
+   for current_prediction in allPredictions:
+      id = current_prediction['id']
 
-   """
-   train_suffix   = '_allPredictions_TRAIN'
-   test_suffix    = '_allPredictions_TEST'
+      #current_ground_truth = qparser.fetch_read(id)
+      #true_cut = current_ground_truth['true_cut']
+      #seq = current_ground_truth['seq']
+      #q1 = current_ground_truth['prb']
+
+      seq         = current_prediction['est']
+      dna         = current_prediction['dna']
+
+      # CHECK !!!
+      q1          = 'zzz'
+
+      chromo      = current_prediction['chr']
+      strand      = current_prediction['strand']
+      start_pos   = current_prediction['start_pos']
+      alignment   = current_prediction['alignment']
+
+      DPScores   = current_prediction['DPScores']
+
+      predExons = current_prediction['predExons']
+      predExons = [e+start_pos for e in predExons]
+
+      (qStart, qEnd, tStart, tEnd, num_exons, qExonSizes, qStarts, qEnds,\
+      tExonSizes,tStarts, tEnds) = alignment 
 
-   run_name = 'run_+_quality_+_splicesignals_+_intron_len_1'
+      EST2GFF = False
+      new_line = ''
 
-   jp = os.path.join
-   b2s = ['-','+']
+      if EST2GFF:
+         predExons = numpy.mat(predExons).reshape(len(predExons)/2,2)
 
-   currentRun = cPickle.load(open(jp(current_dir,'run_object_1.pickle')))
-   QFlag    = currentRun['enable_quality_scores']
-   SSFlag   = currentRun['enable_splice_signals']
-   ILFlag   = currentRun['enable_intron_length']
-   currentRunId = '%s%s%s' % (b2s[QFlag],b2s[SSFlag],b2s[ILFlag])
-   
-   #filename =  jp(current_dir,run_name)+train_suffix
-   #print 'Prediction on: %s' % filename
-   #prediction_on(filename)
+         match = predExons[0,1] - predExons[0,0] 
+         if predExons.shape == (2,2):
+            match += predExons[1,1] - predExons[1,0] 
 
-   filename =  jp(current_dir,run_name)+test_suffix
-   print 'Prediction on: %s' % filename
-   test_result = prediction_on(filename)
+         mismatch = 0
+         repmatch = 0
+         Ns = 0
+         Qgapcnt = 0
+         Qgapbs = 0
 
-   fname = 'predictions.txt'
-   writePredictions(fname,test_result)
+         Tgapcnt = 0
+         Tgapbs = 0
 
-   #return train_result,test_result,currentRunId
+         if predExons.shape == (2,2):
+            Tgapbs += predExons[1,0] - predExons[0,1]
+            Tgapcnt = 1
+
+         # &strand, Qname, &Qsize, 
+         # &Qstart, &Qend, Tname, &Tsize,
+         # &Tstart, &Tend, &blockcnt,exonsizes_str,Qstarts_str,Tstarts_str
+         #
+         # ATTENTION
+         #  
+         # we enforce equal exons sizes for q and t because we are missing indel
+         # positions of the alignment
+
+         if qExonSizes[0] != tExonSizes[0]:
+            continue
+
+         Qname = '%d(%2.2f)'%(id,DPScores.tolist()[0][0])
+         Qsize = len(seq)
+         Qstart = qStart
+         Qend = qEnd
+         Tname = 'CHR%d'%chromo
+
+         start_pos -= 2
+
+         Tsize = tEnd+1 + start_pos
+         Tstart =  tStart + start_pos
+         Tend = tEnd + start_pos
+         blockcnt = Tgapcnt+1
+         exonsizes_str = str(tExonSizes)[1:-1].replace(' ','')
+         Qstarts_str = str(qStarts)[1:-1].replace(' ','')
+         Tstarts_str = str(map(lambda x: x+start_pos,tStarts))[1:-1].replace(' ','')
+
+         new_line = "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%c\t%s\t%d\t%d\t%d\t%s\t%d\t%d\t%d\t%d\t%s\t%s\t%s\n"\
+         % (match, mismatch, repmatch, Ns, Qgapcnt, Qgapbs,\
+         Tgapcnt, Tgapbs, strand, Qname, Qsize, Qstart, Qend,\
+         Tname, Tsize, Tstart, Tend,\
+         blockcnt,exonsizes_str,Qstarts_str,Tstarts_str) #str(predExons))
+
+      else:
+
+         num_exons = len(qExonSizes)
+         exonIdentities,exonGaps = alignment_reconstruct(current_prediction,num_exons)
+
+         pp = lambda x : str(x)[1:-1].replace(' ','')
+
+         new_line = '%d\t%d\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' %\
+         (id,chromo,strand,seq,str(q1)[1:-1],start_pos,qStart,qEnd,tStart,tEnd,num_exons,\
+         str(qExonSizes)[1:-1].replace(' ',''),str(qStarts)[1:-1].replace(' ',''),\
+         str(qEnds)[1:-1].replace(' ',''),str(tExonSizes)[1:-1].replace(' ',''),\
+         str(tStarts)[1:-1].replace(' ',''),str(tEnds)[1:-1].replace(' ',''),\
+         str(exonIdentities)[1:-1].replace(' ',''),str(exonGaps)[1:-1].replace(' ',''))
+
+      out_fh.write(new_line)
+
+   return allPositions
 
 
 if __name__ == '__main__':
-   dir = sys.argv[1]
-   #run_name = sys.argv[2]
-   collect_prediction(dir)
+   current_dir = sys.argv[1]
+   out_fh = sys.argv[2]
+   create_alignment_file(current_dir,out_fh)