diff --git a/midas/run/snps.py b/midas/run/snps.py index d439945..9d355d8 100644 --- a/midas/run/snps.py +++ b/midas/run/snps.py @@ -160,9 +160,8 @@ def keep_read(aln): else: aln_stats['mapped_reads'] += 1 return True - + def species_pileup(args, species_id, contigs): - # Set global variables for read filtering global global_args # need global for keep_read function global_args = args @@ -170,10 +169,10 @@ def species_pileup(args, species_id, contigs): # summary stats global aln_stats aln_stats = {'genome_length':0, - 'total_depth':0, - 'covered_bases':0, - 'aligned_reads':0, - 'mapped_reads':0} + 'total_depth':0, + 'covered_bases':0, + 'aligned_reads':0, + 'mapped_reads':0} # open outfiles out_path = '%s/snps/output/%s.snps.gz' % (args['outdir'], species_id) @@ -220,6 +219,8 @@ def pysam_pileup(args, species, contigs): start = time() print("\nCounting alleles") args['log'].write("\nCounting alleles\n") + args['log'].close() + args.pop('log', None) # run pileups per species in parallel argument_list = [] diff --git a/midas/utility.py b/midas/utility.py index ed8d628..03ddfee 100644 --- a/midas/utility.py +++ b/midas/utility.py @@ -5,6 +5,7 @@ # Freely distributed under the GNU General Public License (GPLv3) import io, os, stat, sys, resource, gzip, platform, bz2, Bio.SeqIO +from distutils.spawn import find_executable __version__ = '1.3.0' @@ -106,23 +107,23 @@ def init_worker(): pool.join() sys.exit("\nKeyboardInterrupt") +def find_exe(exe, path): + """ Finding executable """ + if find_executable(exe) is None: + exe = os.path.join(path, exe) + if not os.path.isfile(exe): + sys.exit("\nError: File not found: %s\n" % exe) + if find_executable(exe) is None: + sys.exit("\nError: Executable not found: %s\n" % exe) + return exe + def add_executables(args): """ Identify relative file and directory paths """ src_dir = os.path.dirname(os.path.abspath(__file__)) main_dir = os.path.dirname(src_dir) - args['stream_seqs'] = '/'.join([src_dir, 'run', 'stream_seqs.py']) - args['hs-blastn'] = '/'.join([main_dir, 'bin', platform.system(), 'hs-blastn']) - args['bowtie2-build'] = '/'.join([main_dir, 'bin', platform.system(), 'bowtie2-build']) - args['bowtie2'] = '/'.join([main_dir, 'bin', platform.system(), 'bowtie2']) - args['samtools'] = '/'.join([main_dir, 'bin', platform.system(), 'samtools']) - - for arg in ['hs-blastn', 'stream_seqs', 'bowtie2-build', 'bowtie2', 'samtools']: - if not os.path.isfile(args[arg]): - sys.exit("\nError: File not found: %s\n" % args[arg]) - - for arg in ['hs-blastn', 'bowtie2-build', 'bowtie2', 'samtools']: - if not os.access(args[arg], os.X_OK): - sys.exit("\nError: File not executable: %s\n" % args[arg]) + args['stream_seqs'] = find_exe('stream_seqs.py', os.path.join(src_dir, 'run')) + for exe in ['hs-blastn', 'bowtie2-build', 'bowtie2', 'samtools']: + args[exe] = find_exe(exe, os.path.join(main_dir, 'bin', platform.system())) import subprocess as sp