def run_mafft(mafftdir, outputDir, fastaFileList, outputFileNameList,
               startNum, endNum, algorithm):
     # Set up
     import platform
     for i in range(startNum, endNum):
         # Run MAFFT
         if platform.system() == 'Windows':
             mafft_cline = MafftCommandline(os.path.join(
                 mafftdir, 'mafft.bat'),
                                            input=fastaFileList[i])
         else:
             mafft_cline = MafftCommandline(os.path.join(mafftdir, 'mafft'),
                                            input=fastaFileList[i])
         if algorithm != None:
             if algorithm.lower() == 'genafpair':
                 mafft_cline.genafpair = True
             elif algorithm.lower() == 'localpair':
                 mafft_cline.localpair = True
             elif algorithm.lower() == 'globalpair':
                 mafft_cline.globalpair = True
         stdout, stderr = mafft_cline()
         if stdout == '':
             raise Exception('MAFFT error text below' + str(stderr))
         # Process MAFFT output
         stdout = stdout.split('\n')
         while stdout[-1] == '\n' or stdout[-1] == '' or stdout[
                 -1] == 'Terminate batch job (Y/N)?\n':  # Remove junk, sometimes MAFFT will have the 'Terminate ...' line
             del stdout[-1]
         stdout = '\n'.join(stdout)
         # Create output alignment files
         with open(outputFileNameList[i], 'w') as fileOut:
             fileOut.write(stdout)
os.mkdir("Alignments")

directory = './Extracted_Sequences'

i = 1
for filename in os.listdir(directory):
    #Check that we are using only fasta files and setting mafft parameters
    if filename.endswith(".fasta"):
        print(filename, "(", i, "/", Blast_bed['query'].nunique(), ")")
        in_file = os.path.join(directory, filename)
        mafft_cline = MafftCommandline(input=in_file)
        mafft_cline.thread = int(NTHREADS)
        mafft_cline.adjustdirection = True
        if MAFFT_OPT == 'einsi':
            mafft_cline.genafpair = True
            mafft_cline.maxiterate = 1000
        stdout, stderr = mafft_cline()
        i += 1

        with open("./Alignments/%s.mafft" % filename, "w") as output:
            output.write(stdout)
            align = AlignIO.read("./Alignments/%s.mafft" % filename, "fasta")

print("########DONE")

############################################################################################################
#----------------------------------------------CLEAN-------------------------------------------------------#
############################################################################################################

print("Cleaning alignments...")