import subprocess
import os
def runCmd(cmd):
    res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    sout, serr = res.communicate()
    return res.returncode, sout, serr


def PHAStrigger(thread,phas_info,phas_len,genome_file,miRNA_file,flankLen,output,sRNAminer):
    cmd = f'java -cp {sRNAminer} biocjava.sRNA.Miner.phasiRNA.PHAStriggerQuick --threads ' + str(thread) + \
          " --inPhasInfo "+ phas_info + " --inGenomeSeq " + genome_file + " --phasLen " + str(phas_len) + \
          " --miRNAseq " + miRNA_file + " --flankLen " + str(flankLen) + " --outFile " + output

    returncode, sout, serr = runCmd(cmd)
    if returncode == 0:
        final_result = sout.decode("gbk")
    else:
        final_result = serr.decode("gbk")
    return returncode, final_result

def PHAStrigger_merger_pipeline(thread,phas_info,phas_len,genome_file,miRNA_file,flankLen,output,sRNAminer):
    print("Start...")
    returncode, final_result = PHAStrigger(thread,phas_info,phas_len,genome_file,miRNA_file,flankLen,output,sRNAminer)
    print(final_result)
    print("Finish!")