+ changed parameter handling a bit (input files command line args and not inside...
[qpalma.git] / scripts / qpalma_pipeline.py
index 3670dd7..362e5f6 100644 (file)
@@ -1,34 +1,46 @@
 #!/usr/bin/env python 
 # -*- coding: utf-8 -*-
 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
 #
-# This file contains the main interface to the QPalma pipeline.
-#
+# Written (W) 2008 Fabio De Bona
+# Copyright (C) 2008 Max-Planck-Society
+
 #
+# This file contains the main interface to the QPalma pipeline.
 #
 
+import os
+import os.path
+import pdb
+import sys
 
-from optparse import OptionParser
-
-
-from qpalma.gridtools import *
+from qpalma.gridtools import ApproximationTask,PreprocessingTask
+from qpalma.gridtools import AlignmentTask,PostprocessingTask
+from qpalma.gridtools import TrainingTask
 
+from qpalma.DatasetUtils import generatePredictionDataset,generateTrainingDataset
 
-def create_option_parser():
-   parser = OptionParser()
+from qpalma.SettingsParser import parseSettings
 
-   #  
-   parser.add_option("-ci", "--check_and_init", help="check configuration and initialize directories")
+from qpalma.utils import logwrite
 
-   #
-   parser.add_option("-r", "--run", help="write report to FILE", metavar="FILE")
-
-   #
-   parser.add_option("-xx", "--clear", action="store_false", dest="verbose", help="cleanup directories delete all created data")
-
-   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:
@@ -41,47 +53,121 @@ class System:
    - setting up the different pipeline modules
    - run the experiment and report the results
 
-   
    """
 
-   # Before creating a candidate spliced read dataset we have to first filter
-   # the matches from the first seed finding run.
+   def __init__(self,filename):
+      """
+      Inititalize the system by loading and parsing the settings file to obtain
+      all parameters.
+      """
 
-   grid_heuristic()
+      self.settings = parseSettings(filename)
+      logwrite('Parsed settings system set up.',self.settings)
 
-   # approx_task = ApproximationTask(...)
-   # 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
+   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)
 
-   createNewDataset
-   
-   # pre_task = PreprocessingTask(...)
-   # pre_task.createJobs()
-   # pre_task.submit()
+      printMessage('Starting dataset generation')
+
+      self.settings['training_data_fn'] = training_data_fn
+
+      # Collect the data and create a pickled training set
+      generateTrainingDataset(self.settings)
 
-   # Now that we have a dataset we can perform the accurate alignments for this
-   # data
+      printMessage('Starting training')
 
-   grid_predict()
+      # Now that we have a dataset we can perform training
+      train_task = TrainingTask(self.settings)
+      train_task.CreateJobs()
+      train_task.Submit()
+      train_task.CheckIfTaskFinished()
 
-   # align_task = AlignmentTask(...)
-   # align_task.createJobs()
-   # align_task.submit()
+      logwrite('End of training.\n',self.settings)
 
-   # The results of the above alignment step can be converted to a data format
-   # needed for further postprocessing
 
-   grid_alignment()
+   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
+      """
 
-   # post_task = PostprocessingTask(...)
-   # post_task.createJobs()
-   # post_task.submit()
+      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()
+      
+      # After filtering combine the filtered matches from the first run and the
+      # found matches from the second run to a full dataset
+
+      printMessage('Starting dataset generation')
+
+      generatePredictionDataset(self.settings)
+
+      printMessage('Starting alignments')
+
+      # 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()
+
+      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()
+
+      logwrite('End of prediction.\n',self.settings)
+   
 
 if __name__ == '__main__':
-   parser = create_option_parser()
-   (options, args) = parser.parse_args()
+   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