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)
#!/usr/bin/env python # coding: utf-8 # In[2]: from Bio.Align.Applications import MafftCommandline mafft_exe = "/usr/bin/mafft" in_file = "/home/corinn/Documents/sequences/ITS/pops/fasta/full_renamed_minus_aw_d_og_renamed.fasta" mafft_cline = MafftCommandline(mafft_exe, input=in_file) mafft_cline.adjustdirection = True mafft_cline.globalpair = True print(mafft_cline) stdout, stderr = mafft_cline() output_loc = "/home/corinn/Documents/sequences/ITS/pops/fasta/" output_prefix = in_file.split("/")[-1].split(".")[0] output = output_loc + output_prefix + "mafft.fasta" with open(output, 'w') as handle: handle.write(stdout) print("DONE") # In[ ]: