+ changed parameter handling a bit (input files command line args and not inside...
[qpalma.git] / scripts / qpalma_pipeline.py
index f7614f8..362e5f6 100644 (file)
 #
 # This file contains the main interface to the QPalma pipeline.
 #
-#
-#
 
-
-from optparse import OptionParser
+import os
+import os.path
+import pdb
+import sys
 
 from qpalma.gridtools import ApproximationTask,PreprocessingTask
 from qpalma.gridtools import AlignmentTask,PostprocessingTask
+from qpalma.gridtools import TrainingTask
 
-def create_option_parser():
-   parser = OptionParser()
-
-   #  
-   parser.add_option("-ci", "--check_and_init", help="check configuration and initialize directories")
+from qpalma.DatasetUtils import generatePredictionDataset,generateTrainingDataset
 
-   #
-   parser.add_option("-r", "--run", help="write report to FILE", metavar="FILE")
+from qpalma.SettingsParser import parseSettings
 
-   #
-   parser.add_option("-xx", "--clear", action="store_false", dest="verbose", help="cleanup directories delete all created data")
+from qpalma.utils import logwrite
 
-   return parser
+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:
@@ -50,53 +55,119 @@ class System:
 
    """
 
-   def __init__(self):
+   def __init__(self,filename):
       """
+      Inititalize the system by loading and parsing the settings file to obtain
+      all parameters.
       """
-      parser = create_option_parser()
-      (options, args) = parser.parse_args()
 
+      self.settings = parseSettings(filename)
+      logwrite('Parsed settings system set up.',self.settings)
 
-   def run(self):
 
-      # Before creating a candidate spliced read dataset we have to first filter
-      # the matches from the first seed finding run.
+   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)
 
-      grid_heuristic()
+      printMessage('Starting dataset generation')
 
-      # approx_task = ApproximationTask(...)
-      # approx_task.createJobs()
-      # approx_task.submit()
-      # approx_task.checkIfTaskFinished()
+      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()
+
+      logwrite('End of training.\n',self.settings)
 
-      # After filtering combine the filtered matches from the first run and the
-      # found matches from the second run to a full dataset
 
-      createNewDataset
+   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.settings)
+      approx_task.CreateJobs()
+      approx_task.Submit()
+      approx_task.CheckIfTaskFinished()
       
-      # pre_task = PreprocessingTask(...)
-      # pre_task.createJobs()
-      # pre_task.submit()
+      # After filtering combine the filtered matches from the first run and the
+      # found matches from the second run to a full dataset
 
-      # Now that we have a dataset we can perform the accurate alignments for this
-      # data
+      printMessage('Starting dataset generation')
 
-      grid_predict()
+      generatePredictionDataset(self.settings)
 
-      # align_task = AlignmentTask(...)
-      # align_task.createJobs()
-      # align_task.submit()
+      printMessage('Starting alignments')
 
-      # The results of the above alignment step can be converted to a data format
-      # needed for further postprocessing
+      # 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()
 
-      grid_alignment()
+      printMessage('Postprocessing')
 
-      # post_task = PostprocessingTask(...)
-      # post_task.createJobs()
-      # post_task.submit()
+      # 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()
 
+      logwrite('End of prediction.\n',self.settings)
+   
 
 if __name__ == '__main__':
-   system_obj = System() 
-   system_obj.run()
+   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)
+
+   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