import os
import sys
import subprocess

# -----------------------------------------------------------------------------------------------------------
# TODO expand this to work with project directory and possibly wrap it up in a luigi pipeline for the merge to be executed in parallel

GUNZIP_CMD = "gunzip %s"

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


def merge_genome_files(upid_dir):
    """
    Merge all sequence files of a genome in a single file

    upid_dir: The path to a genome directory

    :return:
    """

    sequence_dir_loc = os.path.join(upid_dir, "sequences")

    seq_dir_contents = os.listdir(sequence_dir_loc)

    upid = os.path.split(upid_dir)[1]

    # open a new sequence file for the genome
    genome_fasta = open(os.path.join(upid_dir, upid + '.fa'), 'w')

    # check if it's divided in subdirs
    if os.path.isfile(os.path.join(sequence_dir_loc, seq_dir_contents[0])):
        for seq_file in seq_dir_contents:
            seq_file_loc = os.path.join(sequence_dir_loc, seq_file)
            # decompress file
            if seq_file.endswith(".gz"):
                subprocess.call(GUNZIP_CMD % seq_file_loc, shell=True)

            seq_file_fp = open(seq_file_loc, 'r')

            for line in seq_file_fp:
                genome_fasta.write(line)

            seq_file_fp.close()

    # work with multiple subdirectories
    else:
        for subdir in seq_dir_contents:
            subdir_loc = os.path.join(sequence_dir_loc, subdir)
            seq_files = os.listdir(subdir_loc)

            for seq_file in seq_files:
                seq_file_loc = os.path.join(subdir_loc, seq_file)

                # decompress file
                if seq_file.endswith(".gz"):
                    subprocess.call(GUNZIP_CMD % seq_file_loc, shell=True)

                seq_file_fp = open(seq_file_loc, 'r')

                for line in seq_file_fp:
                    genome_fasta.write(line)

                seq_file_fp.close()

    genome_fasta.close()

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

if __name__ == '__main__':

    updir = sys.argv[1]

    merge_genome_files(updir)


@@ -14,16 +14,28 @@
 ort sys%0A
+import gzip%0A
 import s
@@ -1088,16 +1088,48 @@
 eq_file)
+%0A%0A            seq_file_fp = None
 %0A       
@@ -1213,63 +1213,73 @@
    s
-ubprocess.call(GUNZIP_CMD %25 seq_file_loc, shell=True)%0A%0A
+eq_file_fp = gzip.open(seq_file_loc, 'rb')%0A            else:%0A    
     
@@ -1771,32 +1771,67 @@
 decompress file%0A
+                seq_file_fp = None%0A
                 
@@ -1884,36 +1884,112 @@
    s
-ubprocess.call(GUNZIP_CMD %25 
+eq_file_fp = gzip.open(seq_file_loc, 'rb')%0A                else:%0A                    seq_file_fp = open(
 seq_
@@ -2002,18 +2002,11 @@
 oc, 
-shell=True
+'r'
 )%0A%0A 

Handle compressed files using gzip