From 05e66d15e0727b24d10b8540920847b2256d814a Mon Sep 17 00:00:00 2001 From: Josiah Seaman Date: Thu, 15 Nov 2018 14:22:33 +0000 Subject: [PATCH] 1.0.11: Added print_if, verbose handling. pp() no longer prints --- DNASkittleUtils/CommandLineUtils.py | 10 ++++++++-- DNASkittleUtils/Contigs.py | 24 ++++++++++++++---------- DNASkittleUtils/DDVUtils.py | 1 - setup.py | 2 +- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/DNASkittleUtils/CommandLineUtils.py b/DNASkittleUtils/CommandLineUtils.py index 416c667..5f84095 100644 --- a/DNASkittleUtils/CommandLineUtils.py +++ b/DNASkittleUtils/CommandLineUtils.py @@ -10,6 +10,12 @@ log_file_name = "WHAT_I_DID_" + str(datetime.date.today()) + '.log' +def print_if(*args, **kwargs): + verbose = kwargs.get('verbose', True) + if verbose: + print(*args) + + def log_command(args): command = ' '.join(args) if isinstance(args, list) else args print(command) @@ -75,9 +81,9 @@ def delete_file_contents(file_path, scratch_only=False): print("ERROR: Not blanking file because it's not in a scratch folder", file_path, file=sys.stderr) -def make_output_dir_with_suffix(base_path, suffix): +def make_output_dir_with_suffix(base_path, suffix, verbose=True): output_dir = base_path + suffix - print("Creating Directory...", os.path.basename(output_dir)) + print_if("Creating Directory...", os.path.basename(output_dir), verbose=verbose) from os import errno try: os.makedirs(output_dir) diff --git a/DNASkittleUtils/Contigs.py b/DNASkittleUtils/Contigs.py index 913d2af..39ceaf2 100644 --- a/DNASkittleUtils/Contigs.py +++ b/DNASkittleUtils/Contigs.py @@ -1,7 +1,9 @@ from __future__ import print_function, division, absolute_import, with_statement from array import array -from DNASkittleUtils.CommandLineUtils import just_the_name +import sys + +from DNASkittleUtils.CommandLineUtils import just_the_name, print_if from DNASkittleUtils.DDVUtils import chunks @@ -58,7 +60,7 @@ def _write_fasta_lines(filestream, seq): index = 0 while index < len(contigs): if len(contigs) > index + 1 and contigs[index].startswith('>') and contigs[index+1].startswith('>'): - print("Warning: Orphaned header:", contigs[index]) + print("Warning: Orphaned header:", contigs[index], file=sys.stderr) if contigs[index].startswith('>'): header, contents = contigs[index], contigs[index + 1] index += 2 @@ -83,21 +85,23 @@ def write_complete_fasta(file_path, seq_content_array, header=None): _write_fasta_lines(filestream, ''.join(seq_content_array)) -def write_contigs_to_file(out_filename, contigs): +def write_contigs_to_file(out_filename, contigs, verbose=True): with open(out_filename, 'w') as outfile: for contig in contigs: __do_write(outfile, header='>' + contig.name, seq=contig.seq) - if hasattr(contigs, '__len__'): # in case of iterator e.g. itertools.chain() - print("Done writing ", len(contigs), "contigs and {:,}bp".format(sum([len(x.seq) for x in contigs]))) - else: - print("Done writing {:,}bp".format(sum([len(x.seq) for x in contigs]))) + if verbose: + if hasattr(contigs, '__len__'): # in case of iterator e.g. itertools.chain() + print("Done writing ", len(contigs), + "contigs and {:,}bp".format(sum([len(x.seq) for x in contigs]))) + else: + print("Done writing {:,}bp".format(sum([len(x.seq) for x in contigs]))) -def pluck_contig(chromosome_name, genome_source): +def pluck_contig(chromosome_name, genome_source, verbose=True): """Scan through a genome fasta file looking for a matching contig name. When it find it, find_contig collects the sequence and returns it as a string with no cruft.""" chromosome_name = '>' + chromosome_name - print("Searching for", chromosome_name) + print_if("Searching for", chromosome_name, verbose=verbose) seq_collection = [] printing = False with open(genome_source, 'r') as genome: @@ -107,7 +111,7 @@ def pluck_contig(chromosome_name, genome_source): line = line.rstrip() if line.upper() == chromosome_name.upper(): printing = True - print("Found", line) + print_if("Found", line, verbose=verbose) elif printing: break # we've collected all sequence and reached the beginning of the next contig elif printing: # This MUST come after the check for a '>' diff --git a/DNASkittleUtils/DDVUtils.py b/DNASkittleUtils/DDVUtils.py index a41c8ae..2348e2e 100644 --- a/DNASkittleUtils/DDVUtils.py +++ b/DNASkittleUtils/DDVUtils.py @@ -51,7 +51,6 @@ def pp(variable): rep = '{:%}'.format(variable) elif variable > 2.0: rep = '{:.3}'.format(variable) - print(rep) return rep diff --git a/setup.py b/setup.py index c561c91..5322bac 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='DNASkittleUtils', - version='1.0.10', + version='1.0.11', description='Bioinformatics functions that have been useful in multiple projects. Manipulating FASTA files, executing pipelines, etc.', author='Josiah Seaman', author_email='josiah.seaman@gmail.com',