import os
import luigi
import json
import subprocess

from config import gen_config as gc
from support import merge_fasta as mf

# add parent directory to path
if __name__ == '__main__' and __package__ is None:
    os.sys.path.append(
        os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# ----------------------------------TASKS--------------------------------------


class MergeGenome(luigi.Task):
    """
    Task to merge genome fasta files
    """
    updir = luigi.Parameter()

    def run(self):
        """
        Merge all fasta files in updir
        """
        mf.merge_genome_files(self.updir)

    def output(self):
        """
        Check genome fasta file has been generated
        """
        upid = os.path.split(self.updir)[1]
        genome_fasta = os.path.join(self.updir, upid+'.fa')

        return luigi.LocalTarget(genome_fasta)

# -----------------------------------------------------------------------------


class GenomeMergeEngine(luigi.Task):
    """
    Task to load accessions from file
    """
    project_dir = luigi.Parameter(description="Genome download project directory")
    genome_list = luigi.Parameter(default=None,
                                  description="A list of upids for which to merge the sequence files")
    lsf = luigi.BoolParameter(default=True,
                              description="If used then run on lsf, otherwise run locally")

    def run(self):
        """
        Call load_upid_gca_file to export all upid_gca accession pairs in
        json format.
        """
        id_pairs = None

        upids = []
        if self.genome_list is None:
            upid_gca_file_loc = os.path.join(self.project_dir, "upid_gca_dict.json")
            upid_gca_fp = open(upid_gca_file_loc, 'r')
            accessions = json.load(upid_gca_fp)
            upid_gca_fp.close()

            upids = accessions.keys()

        else:
            upid_list_fp = open(self.genome_list, 'r')
            upids = [x.strip() for x in upid_list_fp]
            upid_list_fp.close()

        cmd = ''
        for upid in upids:

            subdir = os.path.join(self.project_dir, upid[-3:])
            updir = os.path.join(subdir, upid)

            if os.path.exists(updir):
                if self.lsf is True:

                    cmd = """
                          bsub -M %s -R "rusage[mem=%s,tmp=%s]" -o %s -e %s -u %s
                          -Ep "rm -rf luigi"
                          -g %s
                          python %s MergeGenome --updir %s
                          """ % (gc.MEM, gc.MEM, gc.TMP_MEM,
                              os.path.join(updir, "merge.out"),
                              os.path.join(updir, "merge.err"),
                              gc.USER_EMAIL, gc.LSF_GEN_GROUP,
                              os.path.realpath(__file__), updir)

                else:
                    cmd = "python \"{this_file}\" MergeGenome --updir {upid}".format(
                        this_file=os.path.realpath(__file__),
                        updir=updir)

                subprocess.call(cmd, shell=True)

            cmd = ''

# -----------------------------------------------------------------------------


if __name__ == '__main__':

    luigi.run()





@@ -2281,17 +2281,16 @@
 s True:%0A
-%0A
         
@@ -2312,37 +2312,8 @@
  = %22
-%22%22%0A                          
 bsub
@@ -2322,16 +2322,17 @@
 M %25s -R 
+%5C
 %22rusage%5B
@@ -2345,16 +2345,17 @@
 ,tmp=%25s%5D
+%5C
 %22 -o %25s 
@@ -2369,39 +2369,14 @@
 u %25s
-%0A                         
  -Ep 
+%5C
 %22rm 
@@ -2384,17 +2384,22 @@
 rf luigi
-%22
+%5C%22 %22 %5C
 %0A       
@@ -2421,39 +2421,14 @@
     
+%22
 -g %25s
-%0A                         
  pyt
@@ -2460,37 +2460,8 @@
 r %25s
-%0A                          %22%22
 %22 %25 
@@ -2511,32 +2511,72 @@
                 
+                     
+                   
 os.path.join(upd
@@ -2615,32 +2615,72 @@
                 
+                
+                        
 os.path.join(upd
@@ -2727,16 +2727,56 @@
         
+           
+                             
 gc.USER_
@@ -2800,16 +2800,56 @@
 _GROUP,%0A
+                                        
         
@@ -2901,25 +2901,24 @@
 __), updir)%0A
-%0A
             

Fix bsub command