Skip to content

Commit

Permalink
1.0.11: Added print_if, verbose handling. pp() no longer prints
Browse files Browse the repository at this point in the history
  • Loading branch information
josiahseaman committed Nov 15, 2018
1 parent 936f3cc commit 05e66d1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
10 changes: 8 additions & 2 deletions DNASkittleUtils/CommandLineUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 14 additions & 10 deletions DNASkittleUtils/Contigs.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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 '>'
Expand Down
1 change: 0 additions & 1 deletion DNASkittleUtils/DDVUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def pp(variable):
rep = '{:%}'.format(variable)
elif variable > 2.0:
rep = '{:.3}'.format(variable)
print(rep)
return rep


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]',
Expand Down

0 comments on commit 05e66d1

Please sign in to comment.