#!/usr/bin/python
# coding=utf-8
#
# Copyright (C) 2012 Allis Tauri <allista@gmail.com>
# 
# PBS Utils 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 3 of the License, or
# (at your option) any later version.
# 
# indicator_gddccontrol is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
'''
Created on Mar 19, 2014

@author: Allis Tauri <allista@gmail.com>
'''

import os
from collections import Iterable
from PBSUtils.PBSJob import BatchFileJob, SerialJob, job_main

class DegenPrimerJob(SerialJob, BatchFileJob):
    
    _bin         = '/usr/local/bin/degen_primer'
    _name        = 'degen_primer_run'
    _description = 'Launch DegenPrimer as a serial job on a cluster. \
                    Accepts several .cfg files.'
    _extension   = '.cfg'
    
    def _new_job(self, conf_file):
        #get names of fasta nd seq_db files
        files = []
        for line in open(conf_file, 'r'):
            if not line: continue
            words  = line.split('=')
            if len(words) < 2: continue
            option = words[0].strip()
            value  = words[1].strip()
            if not value: continue
            if option == 'fasta_files':
                try: _files = eval(value)
                except: _files = None
                if not isinstance(_files, Iterable):
                    print 'Malformed fasta_files value: %s' % value
                    return None
                files.extend(_files)
            elif option == 'sequence_db':
                files.append(value)
        for stagein_file in files:
            if not os.path.isfile(stagein_file):
                print 'No such file: %s' % stagein_file
                return None
        #create job file
        job_file = self._jf.create_job([self._bin+' %(file1)s'],
                                       name=conf_file.rstrip(self._extension),
                                       walltime=self._walltime,
                                       stagein_files=[conf_file]+files)
        return job_file
    #end def
#end class

if __name__ == '__main__':
    job_main(DegenPrimerJob())