#!/usr/bin/env python3
#-*- coding: utf-8 -*-
"""
Script to process tmhmm in parallel
"""

from Bio import SeqIO

import argparse
import os
import sys
import multiprocessing

def parse_and_validate(args):
    """
    Parse input args
    """

    parser = argparse.ArgumentParser()

    parser.add_argument("-i", "--input_file",
                        help="input fast file",
                        type=str,
                        action="store")

    parser.add_argument("-o", "--output_dir",
                        help="output directory",
                        type=str,
                        action="store")

    parser.add_argument("-p", "--cpu",
                        help="number of cores",
                        type=int,
                        action="store")

    args = parser.parse_args()

    if not os.path.isfile(args.input_file):
        print("Input file {} does not exist".format(args.input_file))
        sys.exit(1)

    if not os.path.isdir(args.output_dir):
        print("Output directory {} does not exist".format(args.output_dir))
        sys.exit(1)

    return parser.parse_args()

@@ -805,24 +805,25 @@
 rse_args()%0A%0A
+%0A
     if not o
@@ -1096,28 +1096,2263 @@
  return parser.parse_args()%0A
+%0A%0Adef split_input(fasta_fp, input_name):%0A    %22%22%22%0A    Split input fasta into 1000 sequences subset fasta files%0A    return a list of these filepaths to split fasta%0A    %22%22%22%0A%0A    n = 1000%0A%0A    sequences = SeqIO.parse(fasta_fp, 'fasta')%0A%0A    subset_files = %5B%5D%0A%0A    input_name = os.path.splitext(os.path.split(fasta_fp)%5B-1%5D)%5B0%5D%0A%0A    # file number index and sequence range i%0A    for index, i in enumerate(range(0, len(sequences), n)):%0A        subset_seqs = %5Bx for x in sequences%5Bi: i+n%5D%5D%0A%0A        subset_fp = os.path.join('tmp',%0A                                 input_name + '_%7B%7D_subset.fasta'.format(index))%0A%0A        SeqIO.write(subset_seqs, subset_fp, 'fasta')%0A%0A        subset_files.append(subset_fp)%0A%0A    return subset_files%0A%0Adef run_tmhmm(fasta_files, cores):%0A    %22%22%22%0A    Run TMHMM on the subsets using multiprocessing%0A    Return list%0A    %22%22%22%0A%0A    cmds = %5B%5D%0A    output_files = %5B%5D%0A%0A    for file_path in fasta_files:%0A        output_path = file_path + '.tmhmm'%0A        cmds.append('tmhmm %7B%7D %7C grep -v %22PredHel=0%22 %3E %7B%7D'.format(file_path,%0A                                                                 output_path))%0A        output_files.append(output_path)%0A%0A%0A    pool = multiprocessing.Pool(processes = cores)%0A%0A    pool_output = %5Bpool.apply_async(subprocess.call,%0A                                    (str(cmd), ),%0A                                    %7B'stderr': subprocess.PIPE,%0A                                     'shell'; True%7D)%0A                    for cmd in cmds%5D%0A%0A    pool.close()%0A    pool.join()%0A%0A    return output_files%0A%0A%0A%0A%0Adef combine_and_clean(tmhmm_files, , output_dir):%0A    %22%22%22%0A    Combine tmhmm files%0A    %22%22%22%0A%0A    cmd = %22cat %7B%7D %3E %7B%7D%22.format(%22 %22.join(output_files),%0A%0A    exit = subprocess.run(cmd, shell=True)%0A%0A    if exit.returncode == 0:%0A        shutil.rmtree('tmp')%0A%0A%0A%0Aif __name__ == '__main__':%0A%0A    args = parse_and_validate(sys.argv)%0A%0A    if not os.path.isdir('tmp'):%0A        os.mkdir('tmp')%0A%0A    input_name = os.path.splitext(os.path.split(args.input_fasta)%5B-1%5D)%5B0%5D%0A%0A    subset_fps = split_input(args.input_file, input_name)%0A%0A    subset_tmhmm_outputs = run_tmhmm(subset_fps, args.cpu)%0A%0A    output_filename = combine_and_clean(subset_tmhmm_outputs, input_name,%0A                                        args.output_directory)%0A

Add parallel tmhmm script