import subprocess
import os
import pandas as pd
import traceback
import argparse
from miRNA_part import miRNA_identification_without_align
from miRNA_part import miRNA_expression
from Alignment_Part import alignment
from Alignment_Part import Bowtie_to_sRNAminer
from PHAS_Part import PHAS_identification_one_step
from PHAS_Part import PHAS_Expression
from HcsiRNA_Part import hcsiRNA_identification
from Data_Preprocessing import Adapter_treat
from Data_Preprocessing import Adapter_or_not
from Data_Preprocessing import fastq2fasta
from Data_Preprocessing import Quality_control
from Data_Preprocessing import Collapse_seq
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 dir_build(dir_path):
    cmd = f'mkdir -p {dir_path}'
    returncode, sout, serr = runCmd(cmd)
    if returncode == 0:
        final_result = "Build Dir."
    else:
        final_result = serr.decode("gbk")
    return returncode, final_result
def One_step_workflow(miRNA_mode,genome_file,input_file_1,locate,mismatch,thread,minMirLen,maxMirLen,Batch_mode,dir_path,sRNAminer,sRNAminer_database,
                      PHAS21_mode, lib_num, phas_score, pvalue, PHAS24_mode,hc_siRNA_mode,minRegionGap,minRepeatRatio,rRNA_file,organelle_file):
    print("Start...")
    if hc_siRNA_mode:
        PHAS24_mode = True
    print("Start Data Pre-processing...")
    final_result = ""
    returncode = 0
    n = 0

    if not dir_path.endswith("/") or dir_path.endswith("\\"):
        dir_path = dir_path + "/"
    final_dir_path = dir_path + "One_step_sRNAminer/"
    while os.path.exists(final_dir_path):
        n += 1
        final_dir_path = dir_path + "One_step_sRNAminer_" + str(n) + "/"
    dir_path = final_dir_path
    returncode, final_result = dir_build(dir_path)
    print(final_result)
    if returncode == 0:
        log_file = dir_path + "One_Step_sRNAminer.log"
        log_file_content = open(log_file, "w+")
        output_adapter_list = dir_path + "adapter.list"
        output_adapter_list_content = open(output_adapter_list, "w+", encoding='utf-8')
        if returncode == 0:
            returncode, final_result = alignment.build_index_bowtie(genome_file)
            print(final_result, file=log_file_content)
            if returncode == 0:
                print(final_result)
                returncode, final_result = alignment.build_index_TBtools(genome_file, sRNAminer)
                print(final_result, file=log_file_content)
        print(final_result)
        if returncode == 0:
            if Batch_mode:
                input_file_content = open(input_file_1, "r")
                file_list = [i.strip() for i in input_file_content.readlines()]
                new_file_list = []
                input_file_content.close()
                for input_file in file_list:
                    if Adapter_or_not.check_Adapter(input_file):
                        returncode, final_result = Adapter_treat.adapter_predict_for_use(input_file, sRNAminer)
                        if returncode == 0:
                            print(final_result)
                            output_adapter_list_content.write(final_result)
                            adapter = final_result.rstrip().split("\t")[1]
                            output_file = dir_path + os.path.basename(input_file) + ".trimmed.fa"
                            returncode, final_result = Adapter_treat.adapter_remove_for_use(input_file, output_file,
                                                                                            adapter, sRNAminer)
                            print(final_result, file=log_file_content)
                        else:
                            break
                    else:
                        returncode, final_result = fastq2fasta.fq2fa(input_file, dir_path)
                        print(final_result, file=log_file_content)
                    if returncode == 0:
                        print(final_result)
                        input_file = dir_path + os.path.basename(input_file) + ".trimmed.fa"
                        returncode, final_result = Collapse_seq.seq_collapse(input_file, dir_path, sRNAminer)
                        print(final_result, file=log_file_content)
                    else:
                        break
                    if returncode == 0:
                        print(final_result)
                        prefix, _ = os.path.splitext(input_file)
                        input_file = dir_path + os.path.basename(prefix) + ".mc.fa"
                        returncode, final_result = Quality_control.remove_rRNA(input_file, thread, dir_path, rRNA_file)
                        print(final_result, file=log_file_content)
                    else:
                        break
                    if returncode == 0:
                        print(final_result)
                        prefix, _ = os.path.splitext(input_file)
                        input_file = dir_path + os.path.basename(prefix) + ".no_rRNA.fa"
                        returncode, final_result = Quality_control.remove_Organelle(input_file, thread, dir_path,
                                                                                    organelle_file)
                        print(final_result, file=log_file_content)
                    else:
                        break

                    if returncode == 0:
                        prefix, _ = os.path.splitext(input_file)
                        input_file = dir_path + os.path.basename(prefix) + ".no_ol.fa"
                        returncode, final_result = alignment.align(genome_file, input_file, thread, locate, mismatch,
                                                                   dir_path)  # .bowtie
                        print(final_result, file=log_file_content)
                        if returncode == 0:
                            print(final_result)
                    else:
                        break
                    if returncode == 0:
                        input_file = dir_path + os.path.basename(input_file) + ".bowtie"

                    else:
                        break
                    if returncode == 0:
                        input_file = dir_path + os.path.basename(input_file) + ".aln"

                        input_file = dir_path + os.path.basename(input_file) + ".sorted"
                        new_file_list.append(input_file)
                    else:
                        break
                if returncode == 0:
                    returncode, final_result = alignment.bowtie_format_change(dir_path, thread, sRNAminer)  # .aln
                    print(final_result, file=log_file_content)
                if returncode == 0:
                    print(final_result)
                    returncode, final_result = alignment.sort_bowtie(dir_path, thread, sRNAminer)  # .aln
                    print(final_result, file=log_file_content)
                print(final_result)
                if returncode == 0:
                    if miRNA_mode:
                        print("Start miRNAminer workflow...")
                        new_file_list_1 = []
                        if returncode == 0:
                            returncode, final_result = miRNA_identification_without_align.miRNAminerCLI(genome_file,
                                                                                                        minMirLen,
                                                                                                        maxMirLen,
                                                                                                        dir_path,
                                                                                                        thread,
                                                                                                        sRNAminer)
                            print(final_result, file=log_file_content)
                        if returncode == 0:
                            print(final_result)
                            returncode, final_result = miRNA_identification_without_align.DetectKnownMIR(sRNAminer,
                                                                                                         dir_path,
                                                                                                         thread,
                                                                                                         sRNAminer_database)
                            print(final_result, file=log_file_content)
                        if returncode == 0:
                            print(final_result)
                            for input_file in new_file_list:
                                input_file = dir_path + os.path.basename(input_file) + ".tab.known"
                                returncode, final_result = miRNA_identification_without_align.filter(input_file)
                                input_file = dir_path + os.path.basename(input_file) + ".filter"
                                new_file_list_1.append(input_file)

                                if returncode != 0:
                                    print(final_result, file=log_file_content)
                                    break
                        if returncode == 0:
                            print(final_result)
                            print(final_result, file=log_file_content)
                            check_num_miRNA_signal = 0
                            all_miRNA_list_tmp = open(dir_path + "all_miRNA.list", "w+")
                            for i in new_file_list_1:
                                print(i, file=all_miRNA_list_tmp)
                                with open(i, "r") as f:
                                    file_content = f.readlines()
                                if len(file_content) == 1:
                                    check_num_miRNA_signal += 1
                            all_miRNA_list_tmp.close()
                            if check_num_miRNA_signal == len(new_file_list_1):
                                returncode += 1
                                final_result = "No miRNA is found in all Libs."
                                print(final_result, file=log_file_content)
                            if returncode == 0:
                                returncode, final_result = miRNA_identification_without_align.MIRmerger(dir_path,
                                                                                                        sRNAminer)
                                print(final_result, file=log_file_content)
                            if returncode == 0:
                                print(final_result)
                                returncode, final_result = miRNA_identification_without_align.DetectKnownMIR_2(dir_path,
                                                                                                               sRNAminer,
                                                                                                               sRNAminer_database)
                                print(final_result, file=log_file_content)
                                if returncode == 0:
                                    print(final_result)
                                    returncode, final_result = miRNA_identification_without_align.MIRnamer(dir_path,
                                                                                                           sRNAminer)
                                    print(final_result, file=log_file_content)
                                    if returncode == 0:
                                        print(final_result)
                                        returncode, final_result = miRNA_identification_without_align.for_miRNAinfo(
                                            dir_path)
                                        print(final_result, file=log_file_content)
                                        if returncode == 0:
                                            print(final_result)
                                            returncode, final_result = miRNA_identification_without_align.MIRinfo2GFF(
                                                dir_path, genome_file, sRNAminer)
                                            print(final_result, file=log_file_content)
                        if returncode == 0:
                            print(final_result)
                            gff_file = dir_path + "miRNA.gff3"
                            returncode, final_result = miRNA_expression.feature_region(gff_file, dir_path)
                            print(final_result, file=log_file_content)
                            if returncode == 0:
                                for input_file in new_file_list:
                                    returncode, final_result = miRNA_expression.sRNAQuantify(input_file, dir_path,
                                                                                             sRNAminer)
                                    print(final_result, file=log_file_content)
                                    if returncode != 0:
                                        break
                                    else:
                                        print(final_result)
                            if returncode == 0:
                                returncode, final_result = miRNA_expression.Merge_Exp(dir_path, new_file_list)
                                print(final_result, file=log_file_content)
                        print(final_result)
                    if returncode == 0:
                        if PHAS21_mode:
                            print("Start 21nt-phasiRNAminer workflow....")
                            new_file_list_2 = []
                            for input_file in new_file_list:
                                new_file_list_2.append(dir_path + os.path.basename(input_file) + ".21.PHAS.tab")
                            returncode, final_result = PHAS_identification_one_step.PHASMinerCLI("21",
                                                                                                 dir_path, thread,
                                                                                                 sRNAminer)  # .tab
                            print(final_result, file=log_file_content)

                            if returncode == 0:
                                print(final_result)
                                check_num_PHAS_signal = 0
                                all_phas_list_tmp = open(dir_path + "all.21.PHAS.list", "w+")
                                for i in new_file_list_2:
                                    print(i, file=all_phas_list_tmp)
                                    with open(i, "r") as f:
                                        file_content = f.readlines()
                                    if len(file_content) == 1:
                                        check_num_PHAS_signal += 1
                                all_phas_list_tmp.close()
                                if check_num_PHAS_signal == len(new_file_list_2):
                                    returncode += 1
                                    final_result = "No PHAS locus is found in all Libs."
                                    print(final_result, file=log_file_content)
                                if returncode == 0:
                                    returncode, final_result = PHAS_identification_one_step.PHASMerger(dir_path, "21",
                                                                                                       sRNAminer)
                                    print(final_result, file=log_file_content)
                                if returncode == 0:
                                    print(final_result)
                                    returncode, final_result = PHAS_identification_one_step.filter_phasiRNA_identification(
                                        dir_path, "21", lib_num,
                                        phas_score, pvalue)
                                    print(final_result, file=log_file_content)
                                    if returncode == 0:
                                        print(final_result)
                                        returncode, final_result = PHAS_identification_one_step.PHASMergedListToGFF(
                                            dir_path, genome_file, "21", sRNAminer)
                                        print(final_result, file=log_file_content)

                            if returncode == 0:
                                print(final_result)
                                for input_file in new_file_list:
                                    returncode, final_result = PHAS_Expression.phasiRNAsQuantify(input_file,
                                                                                                 dir_path + "merged.21.PHAS.list.filter.xls",
                                                                                                 "21", dir_path,
                                                                                                 sRNAminer)
                                    print(final_result, file=log_file_content)
                                    if returncode != 0:
                                        break
                                    else:
                                        print(final_result)
                                if returncode == 0:
                                    returncode, final_result = PHAS_Expression.Merge_Exp(new_file_list, "21", dir_path)
                                    print(final_result, file=log_file_content)
                            print(final_result)

                    if returncode == 0:
                        if PHAS24_mode:
                            print("Start 24nt-phasiRNAminer workflow....")
                            new_file_list_3 = []
                            for input_file in new_file_list:
                                new_file_list_3.append(dir_path + os.path.basename(input_file) + ".24.PHAS.tab")
                            returncode, final_result = PHAS_identification_one_step.PHASMinerCLI("24",
                                                                                                 dir_path, thread,
                                                                                                 sRNAminer)  # .tab
                            print(final_result, file=log_file_content)

                            if returncode == 0:
                                print(final_result)
                                check_num_PHAS_signal = 0
                                all_phas_list_tmp = open(dir_path + "all.24.PHAS.list", "w+")
                                for i in new_file_list_3:
                                    print(i, file=all_phas_list_tmp)
                                    with open(i, "r") as f:
                                        file_content = f.readlines()
                                    if len(file_content) == 1:
                                        check_num_PHAS_signal += 1
                                all_phas_list_tmp.close()
                                if check_num_PHAS_signal == len(new_file_list_3):
                                    returncode += 1
                                    final_result = "No PHAS locus is found in all Libs."
                                    print(final_result, file=log_file_content)
                                if returncode == 0:
                                    returncode, final_result = PHAS_identification_one_step.PHASMerger(dir_path, "24",
                                                                                                       sRNAminer)
                                    print(final_result, file=log_file_content)
                                if returncode == 0:
                                    print(final_result)
                                    returncode, final_result = PHAS_identification_one_step.filter_phasiRNA_identification(
                                        dir_path, "24", lib_num,
                                        phas_score, pvalue)
                                    print(final_result, file=log_file_content)
                                    if returncode == 0:
                                        print(final_result)
                                        returncode, final_result = PHAS_identification_one_step.PHASMergedListToGFF(
                                            dir_path, genome_file, "24", sRNAminer)
                                        print(final_result, file=log_file_content)

                            if returncode == 0:
                                print(final_result)
                                for input_file in new_file_list:
                                    returncode, final_result = PHAS_Expression.phasiRNAsQuantify(input_file,
                                                                                                 dir_path + "merged.24.PHAS.list.filter.xls",
                                                                                                 "24", dir_path,
                                                                                                 sRNAminer)
                                    print(final_result, file=log_file_content)
                                    if returncode != 0:
                                        break
                                    else:
                                        print(final_result)
                                if returncode == 0:
                                    returncode, final_result = PHAS_Expression.Merge_Exp(new_file_list, "24", dir_path)
                                    print(final_result, file=log_file_content)
                            print(final_result)

                    if returncode == 0:
                        if hc_siRNA_mode:
                            print("Start hc-siRNAminer workflow....")
                            new_file_list_4 = []
                            for input_file in file_list:
                                input_file = dir_path + os.path.basename(input_file) + ".trimmed.mc.no_rRNA.no_ol.fa"
                                returncode, final_result = hcsiRNA_identification.align(genome_file, input_file, thread,
                                                                                        "0", dir_path)  # .bowtie
                                print(final_result, file=log_file_content)
                                if returncode == 0:
                                    print(final_result)
                            if returncode == 0:
                                returncode, final_result = hcsiRNA_identification.bowtie_format_change(dir_path, thread,
                                                                                                       sRNAminer)  # .aln
                                print(final_result, file=log_file_content)
                            if returncode == 0:
                                print(final_result)
                                returncode, final_result = hcsiRNA_identification.sort_bowtie(dir_path, thread,
                                                                                              sRNAminer)  # .aln
                                print(final_result, file=log_file_content)
                            print(final_result)
                            if returncode == 0:
                                print("Preparing for Bowtie Files(sRNAminer Format) [Finish]")
                                for input_file in file_list:
                                    input_file = dir_path + os.path.basename(
                                        input_file) + ".trimmed.mc.no_rRNA.no_ol.fa.hcsiRNA.bowtie.aln.sorted"
                                    new_file_list_4.append(
                                        dir_path + os.path.basename(input_file) + ".hcsiRNA")
                            if returncode == 0:
                                returncode, final_result = hcsiRNA_identification.hcsiRNAMinerCLI(minRegionGap,
                                                                                                  minRepeatRatio,
                                                                                                  dir_path, thread,
                                                                                                  sRNAminer)  # .hcsiRNA
                                print(final_result, file=log_file_content)

                            if returncode == 0:
                                print(final_result)
                                check_num_hcsiRNA_signal = 0
                                all_hcsiRNA_list_tmp = open(dir_path + "hcsiRNA.list", "w+")
                                for i in new_file_list_4:
                                    print(i, file=all_hcsiRNA_list_tmp)
                                    with open(i, "r") as f:
                                        file_content = f.readlines()
                                    if len(file_content) == 0:
                                        check_num_hcsiRNA_signal += 1
                                all_hcsiRNA_list_tmp.close()
                                if check_num_hcsiRNA_signal == len(new_file_list_4):
                                    returncode += 1
                                    final_result = "No hc-siRNA is found in all Libs."
                                    print(final_result, file=log_file_content)
                                if returncode == 0:
                                    returncode, final_result = hcsiRNA_identification.HcsiRNAmerger_2(dir_path,
                                                                                                      sRNAminer)
                                    print(final_result, file=log_file_content)
                                if returncode == 0:
                                    print(final_result)
                                    input_hc_file = dir_path + "merged.hcsiRNA.info"
                                    returncode, final_result = hcsiRNA_identification.filter_24_PHAS(input_hc_file,
                                                                                                     dir_path + "merged.24.PHAS.list.filter.xls")
                                    print(final_result, file=log_file_content)
                                    if returncode == 0:
                                        print(final_result)
                                        input_for_gff3 = dir_path + "final.hcsiRNA.loci.xls"
                                        returncode, final_result = hcsiRNA_identification.hcsiRNAToGff(input_for_gff3)
                                        print(final_result, file=log_file_content)
                            print(final_result)

                log_file_content.close()
                output_adapter_list_content.close()
                output_readme = open(dir_path + "README_One_Step_sRNAminer.txt", "w+")
                print("*.trimmed.mc.no_rRNA.no_ol.fa\tThe clean data of raw data (after collapsing)",
                      "*.trimmed.mc.no_rRNA.no_ol.fa.bowtie.aln.sorted\tBowtie Results treated by sRNAminer",
                      "*.trimmed.mc.fa.rfam.log\tShow the proportion of rRNA in the raw data",
                      "*.trimmed.mc.no_rRNA.fa.organelle.log\tShow the proportion of organelle in the data (rRNA have been cleaned)",
                      "*.trimmed.mc.no_rRNA.no_ol.fa.bowtie.log\tShow the proportion of reads that map with the genome",
                      "*.trimmed.mc.no_rRNA.no_ol.fa.bowtie.aln.sorted\tThe bowtie results in sRNAminer format",
                      "*.adapter.list\tThe adapter of the raw data",
                      "merged.miRNA.list.anno.rename.xls\tThe list of miRNA identified from the data",
                      "miRNA.gff3\tThe list of miRNA identified from the data (in gff3 Format)",
                      "merged.miRNA.exp.xls	The expression level of miRNA (RP10M [Reads per 10 million])",
                      "merged.21.PHAS.list.xls\tThe list of identified PHAS21 loci",
                      "merged.21.PHAS.list.filter.xls\tThe list of identified PHAS21 loci being filtered (Criteria: Phasing score >= 10, P-value <= 0.001)",
                      "merged.PHAS21.gff3\tThe list of identified PHAS21 loci (in gff3 Format)",
                      "merged.21.PHAS.exp.xls\tThe expression level of PHAS21 loci",
                      "merged.24.PHAS.list.xls\tThe list of identified PHAS24 loci",
                      "merged.24.PHAS.list.filter.xls\tThe list of identified PHAS24 loci being filtered (Criteria: Phasing score >= 10, P-value <= 0.001)",
                      "merged.PHAS24.gff3\tThe list of identified PHAS24 loci (in gff3 Format)",
                      "merged.24.PHAS.exp.xls\tThe expression level of PHAS24 loci",
                      "final.hcsiRNA.loci.xls\tThe list of identified hcsiRNA",
                      "merged.hcsiRNA.gff3\tThe list of identified hcsiRNA (in gff3 Format)",
                      "*.trimmed.mc.no_rRNA.no_ol.fa.hcsiRNA.bowtie.log\tShow the proportion of reads that map with the genome (mapping method with changed to fit for the hcsiRNA identification)",
                      sep="\n", file=output_readme)
                output_readme.close()
            else:
                file_list = str(input_file_1).split(",")
                new_file_list = []
                for input_file in file_list:
                    if Adapter_or_not.check_Adapter(input_file):
                        returncode, final_result = Adapter_treat.adapter_predict_for_use(input_file, sRNAminer)
                        if returncode == 0:
                            print(final_result)
                            output_adapter_list_content.write(final_result)
                            adapter = final_result.rstrip().split("\t")[1]
                            output_file = dir_path + os.path.basename(input_file) + ".trimmed.fa"
                            returncode, final_result = Adapter_treat.adapter_remove_for_use(input_file, output_file,
                                                                                            adapter, sRNAminer)
                            print(final_result, file=log_file_content)
                        else:
                            break
                    else:
                        returncode, final_result = fastq2fasta.fq2fa(input_file, dir_path)
                        print(final_result, file=log_file_content)
                    if returncode == 0:
                        print(final_result)
                        input_file = dir_path + os.path.basename(input_file) + ".trimmed.fa"
                        returncode, final_result = Collapse_seq.seq_collapse(input_file, dir_path, sRNAminer)
                        print(final_result, file=log_file_content)
                    else:
                        break
                    if returncode == 0:
                        print(final_result)
                        prefix, _ = os.path.splitext(input_file)
                        input_file = dir_path + os.path.basename(prefix) + ".mc.fa"
                        returncode, final_result = Quality_control.remove_rRNA(input_file, thread, dir_path, rRNA_file)
                        print(final_result, file=log_file_content)
                    else:
                        break
                    if returncode == 0:
                        print(final_result)
                        prefix, _ = os.path.splitext(input_file)
                        input_file = dir_path + os.path.basename(prefix) + ".no_rRNA.fa"
                        returncode, final_result = Quality_control.remove_Organelle(input_file, thread, dir_path,
                                                                                    organelle_file)
                        print(final_result, file=log_file_content)
                    else:
                        break

                    if returncode == 0:
                        prefix, _ = os.path.splitext(input_file)
                        input_file = dir_path + os.path.basename(prefix) + ".no_ol.fa"
                        returncode, final_result = alignment.align(genome_file, input_file, thread, locate, mismatch,
                                                                   dir_path)  # .bowtie
                        print(final_result, file=log_file_content)
                        if returncode == 0:
                            print(final_result)
                    else:
                        break
                    if returncode == 0:
                        input_file = dir_path + os.path.basename(input_file) + ".bowtie"

                    else:
                        break
                    if returncode == 0:
                        input_file = dir_path + os.path.basename(input_file) + ".aln"

                        input_file = dir_path + os.path.basename(input_file) + ".sorted"
                        new_file_list.append(input_file)
                    else:
                        break
                if returncode == 0:
                    returncode, final_result = alignment.bowtie_format_change(dir_path, thread, sRNAminer)  # .aln
                    print(final_result, file=log_file_content)
                if returncode == 0:
                    print(final_result)
                    returncode, final_result = alignment.sort_bowtie(dir_path, thread, sRNAminer)  # .aln
                    print(final_result, file=log_file_content)
                print(final_result)
                if returncode == 0:
                    if miRNA_mode:
                        print("Start miRNAminer workflow...")
                        new_file_list_1 = []
                        if returncode == 0:
                            returncode, final_result = miRNA_identification_without_align.miRNAminerCLI(genome_file,
                                                                                                        minMirLen,
                                                                                                        maxMirLen,
                                                                                                        dir_path,
                                                                                                        thread,
                                                                                                        sRNAminer)
                            print(final_result, file=log_file_content)
                        if returncode == 0:
                            print(final_result)
                            returncode, final_result = miRNA_identification_without_align.DetectKnownMIR(sRNAminer,
                                                                                                         dir_path,
                                                                                                         thread,
                                                                                                         sRNAminer_database)
                            print(final_result, file=log_file_content)
                        if returncode == 0:
                            print(final_result)
                            for input_file in new_file_list:
                                input_file = dir_path + os.path.basename(input_file) + ".tab.known"
                                returncode, final_result = miRNA_identification_without_align.filter(input_file)
                                input_file = dir_path + os.path.basename(input_file) + ".filter"
                                new_file_list_1.append(input_file)

                                if returncode != 0:
                                    print(final_result, file=log_file_content)
                                    break
                        if returncode == 0:
                            print(final_result)
                            print(final_result, file=log_file_content)
                            check_num_miRNA_signal = 0
                            all_miRNA_list_tmp = open(dir_path + "all_miRNA.list", "w+")
                            for i in new_file_list_1:
                                print(i, file=all_miRNA_list_tmp)
                                with open(i, "r") as f:
                                    file_content = f.readlines()
                                if len(file_content) == 1:
                                    check_num_miRNA_signal += 1
                            all_miRNA_list_tmp.close()
                            if check_num_miRNA_signal == len(new_file_list_1):
                                returncode += 1
                                final_result = "No miRNA is found in all Libs."
                                print(final_result, file=log_file_content)
                            if returncode == 0:
                                returncode, final_result = miRNA_identification_without_align.MIRmerger(dir_path,
                                                                                                        sRNAminer)
                                print(final_result, file=log_file_content)
                            if returncode == 0:
                                print(final_result)
                                returncode, final_result = miRNA_identification_without_align.DetectKnownMIR_2(dir_path,
                                                                                                               sRNAminer,
                                                                                                               sRNAminer_database)
                                print(final_result, file=log_file_content)
                                if returncode == 0:
                                    print(final_result)
                                    returncode, final_result = miRNA_identification_without_align.MIRnamer(dir_path,
                                                                                                           sRNAminer)
                                    print(final_result, file=log_file_content)
                                    if returncode == 0:
                                        print(final_result)
                                        returncode, final_result = miRNA_identification_without_align.for_miRNAinfo(
                                            dir_path)
                                        print(final_result, file=log_file_content)
                                        if returncode == 0:
                                            print(final_result)
                                            returncode, final_result = miRNA_identification_without_align.MIRinfo2GFF(
                                                dir_path, genome_file, sRNAminer)
                                            print(final_result, file=log_file_content)
                        if returncode == 0:
                            print(final_result)
                            gff_file = dir_path + "miRNA.gff3"
                            returncode, final_result = miRNA_expression.feature_region(gff_file, dir_path)
                            print(final_result, file=log_file_content)
                            if returncode == 0:
                                for input_file in new_file_list:
                                    returncode, final_result = miRNA_expression.sRNAQuantify(input_file, dir_path,
                                                                                             sRNAminer)
                                    print(final_result, file=log_file_content)
                                    if returncode != 0:
                                        break
                                    else:
                                        print(final_result)
                            if returncode == 0:
                                returncode, final_result = miRNA_expression.Merge_Exp(dir_path, new_file_list)
                                print(final_result, file=log_file_content)
                        print(final_result)
                    if returncode == 0:
                        if PHAS21_mode:
                            print("Start 21nt-phasiRNAminer workflow....")
                            new_file_list_2 = []
                            for input_file in new_file_list:
                                new_file_list_2.append(dir_path + os.path.basename(input_file) + ".21.PHAS.tab")
                            returncode, final_result = PHAS_identification_one_step.PHASMinerCLI("21",
                                                                                                 dir_path, thread,
                                                                                                 sRNAminer)  # .tab
                            print(final_result, file=log_file_content)

                            if returncode == 0:
                                print(final_result)
                                check_num_PHAS_signal = 0
                                all_phas_list_tmp = open(dir_path + "all.21.PHAS.list", "w+")
                                for i in new_file_list_2:
                                    print(i, file=all_phas_list_tmp)
                                    with open(i, "r") as f:
                                        file_content = f.readlines()
                                    if len(file_content) == 1:
                                        check_num_PHAS_signal += 1
                                all_phas_list_tmp.close()
                                if check_num_PHAS_signal == len(new_file_list_2):
                                    returncode += 1
                                    final_result = "No PHAS locus is found in all Libs."
                                    print(final_result, file=log_file_content)
                                if returncode == 0:
                                    returncode, final_result = PHAS_identification_one_step.PHASMerger(dir_path, "21",
                                                                                                       sRNAminer)
                                    print(final_result, file=log_file_content)
                                if returncode == 0:
                                    print(final_result)
                                    returncode, final_result = PHAS_identification_one_step.filter_phasiRNA_identification(
                                        dir_path, "21", lib_num,
                                        phas_score, pvalue)
                                    print(final_result, file=log_file_content)
                                    if returncode == 0:
                                        print(final_result)
                                        returncode, final_result = PHAS_identification_one_step.PHASMergedListToGFF(
                                            dir_path, genome_file, "21", sRNAminer)
                                        print(final_result, file=log_file_content)

                            if returncode == 0:
                                print(final_result)
                                for input_file in new_file_list:
                                    returncode, final_result = PHAS_Expression.phasiRNAsQuantify(input_file,
                                                                                                 dir_path + "merged.21.PHAS.list.filter.xls",
                                                                                                 "21", dir_path,
                                                                                                 sRNAminer)
                                    print(final_result, file=log_file_content)
                                    if returncode != 0:
                                        break
                                    else:
                                        print(final_result)
                                if returncode == 0:
                                    returncode, final_result = PHAS_Expression.Merge_Exp(new_file_list, "21", dir_path)
                                    print(final_result, file=log_file_content)
                            print(final_result)

                    if returncode == 0:
                        if PHAS24_mode:
                            print("Start 24nt-phasiRNAminer workflow....")
                            new_file_list_3 = []
                            for input_file in new_file_list:
                                new_file_list_3.append(dir_path + os.path.basename(input_file) + ".24.PHAS.tab")
                            returncode, final_result = PHAS_identification_one_step.PHASMinerCLI("24",
                                                                                                 dir_path, thread,
                                                                                                 sRNAminer)  # .tab
                            print(final_result, file=log_file_content)

                            if returncode == 0:
                                print(final_result)
                                check_num_PHAS_signal = 0
                                all_phas_list_tmp = open(dir_path + "all.24.PHAS.list", "w+")
                                for i in new_file_list_3:
                                    print(i, file=all_phas_list_tmp)
                                    with open(i, "r") as f:
                                        file_content = f.readlines()
                                    if len(file_content) == 1:
                                        check_num_PHAS_signal += 1
                                all_phas_list_tmp.close()
                                if check_num_PHAS_signal == len(new_file_list_3):
                                    returncode += 1
                                    final_result = "No PHAS locus is found in all Libs."
                                    print(final_result, file=log_file_content)
                                if returncode == 0:
                                    returncode, final_result = PHAS_identification_one_step.PHASMerger(dir_path, "24",
                                                                                                       sRNAminer)
                                    print(final_result, file=log_file_content)
                                if returncode == 0:
                                    print(final_result)
                                    returncode, final_result = PHAS_identification_one_step.filter_phasiRNA_identification(
                                        dir_path, "24", lib_num,
                                        phas_score, pvalue)
                                    print(final_result, file=log_file_content)
                                    if returncode == 0:
                                        print(final_result)
                                        returncode, final_result = PHAS_identification_one_step.PHASMergedListToGFF(
                                            dir_path, genome_file, "24", sRNAminer)
                                        print(final_result, file=log_file_content)

                            if returncode == 0:
                                print(final_result)
                                for input_file in new_file_list:
                                    returncode, final_result = PHAS_Expression.phasiRNAsQuantify(input_file,
                                                                                                 dir_path + "merged.24.PHAS.list.filter.xls",
                                                                                                 "24", dir_path,
                                                                                                 sRNAminer)
                                    print(final_result, file=log_file_content)
                                    if returncode != 0:
                                        break
                                    else:
                                        print(final_result)
                                if returncode == 0:
                                    returncode, final_result = PHAS_Expression.Merge_Exp(new_file_list, "24", dir_path)
                                    print(final_result, file=log_file_content)
                            print(final_result)

                    if returncode == 0:
                        if hc_siRNA_mode:
                            print("Start hc-siRNAminer workflow....")
                            new_file_list_4 = []
                            for input_file in file_list:
                                input_file = dir_path + os.path.basename(input_file) + ".trimmed.mc.no_rRNA.no_ol.fa"
                                returncode, final_result = hcsiRNA_identification.align(genome_file, input_file, thread,
                                                                                        "0", dir_path)  # .bowtie
                                print(final_result, file=log_file_content)
                                if returncode == 0:
                                    print(final_result)
                            if returncode == 0:
                                returncode, final_result = hcsiRNA_identification.bowtie_format_change(dir_path, thread,
                                                                                                       sRNAminer)  # .aln
                                print(final_result, file=log_file_content)
                            if returncode == 0:
                                print(final_result)
                                returncode, final_result = hcsiRNA_identification.sort_bowtie(dir_path, thread,
                                                                                              sRNAminer)  # .aln
                                print(final_result, file=log_file_content)
                            print(final_result)
                            if returncode == 0:
                                print("Preparing for Bowtie Files(sRNAminer Format) [Finish]")
                                for input_file in file_list:
                                    input_file = dir_path + os.path.basename(
                                        input_file) + ".trimmed.mc.no_rRNA.no_ol.fa.hcsiRNA.bowtie.aln.sorted"
                                    new_file_list_4.append(
                                        dir_path + os.path.basename(input_file) + ".hcsiRNA")
                            if returncode == 0:
                                returncode, final_result = hcsiRNA_identification.hcsiRNAMinerCLI(minRegionGap,
                                                                                                  minRepeatRatio,
                                                                                                  dir_path, thread,
                                                                                                  sRNAminer)  # .hcsiRNA
                                print(final_result, file=log_file_content)

                            if returncode == 0:
                                print(final_result)
                                check_num_hcsiRNA_signal = 0
                                all_hcsiRNA_list_tmp = open(dir_path + "hcsiRNA.list", "w+")
                                for i in new_file_list_4:
                                    print(i, file=all_hcsiRNA_list_tmp)
                                    with open(i, "r") as f:
                                        file_content = f.readlines()
                                    if len(file_content) == 0:
                                        check_num_hcsiRNA_signal += 1
                                all_hcsiRNA_list_tmp.close()
                                if check_num_hcsiRNA_signal == len(new_file_list_4):
                                    returncode += 1
                                    final_result = "No hc-siRNA is found in all Libs."
                                    print(final_result, file=log_file_content)
                                if returncode == 0:
                                    returncode, final_result = hcsiRNA_identification.HcsiRNAmerger_2(dir_path,
                                                                                                      sRNAminer)
                                    print(final_result, file=log_file_content)
                                if returncode == 0:
                                    print(final_result)
                                    input_hc_file = dir_path + "merged.hcsiRNA.info"
                                    returncode, final_result = hcsiRNA_identification.filter_24_PHAS(input_hc_file,
                                                                                                     dir_path + "merged.24.PHAS.list.filter.xls")
                                    print(final_result, file=log_file_content)
                                    if returncode == 0:
                                        print(final_result)
                                        input_for_gff3 = dir_path + "final.hcsiRNA.loci.xls"
                                        returncode, final_result = hcsiRNA_identification.hcsiRNAToGff(input_for_gff3)
                                        print(final_result, file=log_file_content)
                            print(final_result)

                log_file_content.close()
                output_adapter_list_content.close()
                output_readme = open(dir_path + "README_One_Step_sRNAminer.txt", "w+")
                print("*.trimmed.mc.no_rRNA.no_ol.fa\tThe clean data of raw data (after collapsing)",
                      "*.trimmed.mc.no_rRNA.no_ol.fa.bowtie.aln.sorted\tBowtie Results treated by sRNAminer",
                      "*.trimmed.mc.fa.rfam.log\tShow the proportion of rRNA in the raw data",
                      "*.trimmed.mc.no_rRNA.fa.organelle.log\tShow the proportion of organelle in the data (rRNA have been cleaned)",
                      "*.trimmed.mc.no_rRNA.no_ol.fa.bowtie.log\tShow the proportion of reads that map with the genome",
                      "*.trimmed.mc.no_rRNA.no_ol.fa.bowtie.aln.sorted\tThe bowtie results in sRNAminer format",
                      "*.adapter.list\tThe adapter of the raw data",
                      "merged.miRNA.list.anno.rename.xls\tThe list of miRNA identified from the data",
                      "miRNA.gff3\tThe list of miRNA identified from the data (in gff3 Format)",
                      "merged.miRNA.exp.xls	The expression level of miRNA (RP10M [Reads per 10 million])",
                      "merged.21.PHAS.list.xls\tThe list of identified PHAS21 loci",
                      "merged.21.PHAS.list.filter.xls\tThe list of identified PHAS21 loci being filtered (Criteria: Phasing score >= 10, P-value <= 0.001)",
                      "merged.PHAS21.gff3\tThe list of identified PHAS21 loci (in gff3 Format)",
                      "merged.21.PHAS.exp.xls\tThe expression level of PHAS21 loci",
                      "merged.24.PHAS.list.xls\tThe list of identified PHAS24 loci",
                      "merged.24.PHAS.list.filter.xls\tThe list of identified PHAS24 loci being filtered (Criteria: Phasing score >= 10, P-value <= 0.001)",
                      "merged.PHAS24.gff3\tThe list of identified PHAS24 loci (in gff3 Format)",
                      "merged.24.PHAS.exp.xls\tThe expression level of PHAS24 loci",
                      "final.hcsiRNA.loci.xls\tThe list of identified hcsiRNA",
                      "merged.hcsiRNA.gff3\tThe list of identified hcsiRNA (in gff3 Format)",
                      "*.trimmed.mc.no_rRNA.no_ol.fa.hcsiRNA.bowtie.log\tShow the proportion of reads that map with the genome (mapping method with changed to fit for the hcsiRNA identification)",
                      sep="\n", file=output_readme)
                output_readme.close()

    if returncode == 0:
        print("Finish!")