+ changed parameter handling a bit (input files command line args and not inside...
[qpalma.git] / scripts / qpalma_pipeline.py
index a90c8ea..362e5f6 100644 (file)
@@ -20,11 +20,27 @@ import sys
 
 from qpalma.gridtools import ApproximationTask,PreprocessingTask
 from qpalma.gridtools import AlignmentTask,PostprocessingTask
+from qpalma.gridtools import TrainingTask
 
-from qpalma.settingsParser import parseSettings
+from qpalma.DatasetUtils import generatePredictionDataset,generateTrainingDataset
 
+from qpalma.SettingsParser import parseSettings
 
-Errormsg = """Usage is: python qpalma_pipeline.py <config filename>"""
+from qpalma.utils import logwrite
+
+Errormsg = """Usage is:  
+                  
+                  python qpalma_pipeline.py train <config filename> <training data filename>
+              or
+                  python qpalma_pipeline.py predict <config filename> <parameter filename> <putative unspliced reads filename> <putative spliced reads filename>
+                  
+           """
+
+
+def printMessage(mes):
+   print '#'*80
+   print '\t\t\t%s...\n'%mes
+   print '#'*80
 
 
 class System:
@@ -45,72 +61,113 @@ class System:
       all parameters.
       """
 
-      self.global_settings = parseSettings(filename)
-      pdb.set_trace()
+      self.settings = parseSettings(filename)
+      logwrite('Parsed settings system set up.',self.settings)
 
 
-   def training(self):
+   def training(self, training_data_fn):
       """
       This function is responsible for the whole training process. It first
       converts the data to the right format needed by QPalma for the training
       algorithm.
       """
+      logwrite('Begin of training.\n',self.settings)
 
-      pre_task = TrainingPreprocessingTask(self.global_settings)
-      pre_task.createJobs()
-      pre_task.submit() 
-      while pre_task.checkIfTaskFinished() == False:
-         sleep(20)
-      
+      printMessage('Starting dataset generation')
+
+      self.settings['training_data_fn'] = training_data_fn
+
+      # Collect the data and create a pickled training set
+      generateTrainingDataset(self.settings)
+
+      printMessage('Starting training')
+
+      # Now that we have a dataset we can perform training
+      train_task = TrainingTask(self.settings)
+      train_task.CreateJobs()
+      train_task.Submit()
+      train_task.CheckIfTaskFinished()
 
-   def prediction(self):
+      logwrite('End of training.\n',self.settings)
+
+
+   def prediction(self, param_fn, unspliced_reads_fn, spliced_reads_fn):
       """
       This function encapsulates all steps needed to perform a prediction. Given
       the parameter of the training and paths to a prediction set it will
       generate several output files containing the spliced alignments
       """
 
+      logwrite('Begin of prediction.\n',self.settings)
+
+      printMessage('Starting approximation')
+
+      self.settings['prediction_param_fn'] = param_fn
+      self.settings['unspliced_reads_fn']  = unspliced_reads_fn
+      self.settings['spliced_reads_fn']    = spliced_reads_fn
+
       # Before creating a candidate spliced read dataset we have to first filter
       # the matches from the first seed finding run.
 
-      approx_task = ApproximationTask(self.global_settings)
-      approx_task.createJobs()
-      approx_task.submit()
-      approx_task.checkIfTaskFinished()
+      approx_task = ApproximationTask(self.settings)
+      approx_task.CreateJobs()
+      approx_task.Submit()
+      approx_task.CheckIfTaskFinished()
       
       # After filtering combine the filtered matches from the first run and the
       # found matches from the second run to a full dataset
 
-      sys.exit(0)
+      printMessage('Starting dataset generation')
+
+      generatePredictionDataset(self.settings)
 
-      pre_task = PreprocessingTask(self.global_settings)
-      pre_task.createJobs()
-      pre_task.submit() 
-      pre_task.checkIfTaskFinished()
+      printMessage('Starting alignments')
 
-      # Now that we have a dataset we can perform the accurate alignments for this
-      # data
+      # Now that we have a dataset we can perform accurate alignments
+      align_task = AlignmentTask(self.settings)
+      align_task.CreateJobs()
+      align_task.Submit()
+      align_task.CheckIfTaskFinished()
 
-      align_task = AlignmentTask(self.global_settings)
-      align_task.createJobs()
-      align_task.submit()
-      align_task.checkIfTaskFinished()
+      printMessage('Postprocessing')
 
       # The results of the above alignment step can be converted to a data format
       # needed for further postprocessing.
+      post_task = PostprocessingTask(self.settings)
+      post_task.CreateJobs()
+      post_task.Submit()
+      post_task.CheckIfTaskFinished()
 
-      post_task = PostprocessingTask(self.global_settings)
-      post_task.createJobs()
-      post_task.submit()
-      post_task.checkIfTaskFinished()
-
-      print "Success!"
+      logwrite('End of prediction.\n',self.settings)
    
 
 if __name__ == '__main__':
-   filename = sys.argv[1]
-   assert os.path.exists(filename), Errormsg
+   if len(sys.argv) != 4 and len(sys.argv) != 6:
+      print 'Invalid number of arguments!'
+      print len(sys.argv)
+      print Errormsg
+      sys.exit(1)
+
+   mode     = sys.argv[1]
+   if not mode in ['predict','train']:
+      print Errormsg
+      sys.exit(1)
+
+   filename = sys.argv[2]
+   if not os.path.exists(filename):
+      print Errormsg
+      sys.exit(1)
+
+   # creating system object
    system_obj = System(filename)
-   system_obj.prediction()
 
-   #system_obj.training()
+   if mode == 'predict':
+      param_fn           = sys.argv[3]
+      unspliced_reads_fn = sys.argv[4]
+      spliced_reads_fn   = sys.argv[5]
+      system_obj.prediction(param_fn,unspliced_reads_fn, spliced_reads_fn)
+   elif mode == 'train':
+      training_data_fn   = sys.argv[3]
+      system_obj.training(training_data_fn)
+   else:
+      assert False