Skip to content

Commit

Permalink
#77 Creation of /sources/ works with --quick plus some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
josiahseaman committed Jan 11, 2019
1 parent eb96746 commit 8018a74
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 38 deletions.
5 changes: 3 additions & 2 deletions DDV/AnnotationAlignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
filter_repeats_by_chromosome_and_family
from DDV.Span import alignment_chopping_index, AlignedSpans, Span
from DDV.TransposonLayout import TransposonLayout
from DDV.DDVUtils import make_output_dir_with_suffix
from DDV.DDVUtils import make_output_directory


def create_aligned_annotation_fragments(alignment, repeat_entries):
Expand Down Expand Up @@ -73,7 +73,8 @@ def align_annotation(annotation_filename, ref_fasta, query_fasta, chain_file):
continue # There's no usable entries left to use

# modify chain.alignment to only contain annotated stretches
chain.output_folder = make_output_dir_with_suffix(chain.output_prefix, ending) # create a folder specifically for this repeat
chain.output_folder = chain.output_prefix + ending
make_output_directory(chain.output_folder) # create a folder specifically for this repeat
chain.query_seq_gapped = editable_str('') # these need to be cleared so they don't accumulate the previous family
chain.ref_seq_gapped = editable_str('')
trimmed_alignment = create_aligned_annotation_fragments(chain.alignment, repeat_entries)
Expand Down
5 changes: 3 additions & 2 deletions DDV/ChainParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from DNASkittleUtils.DDVUtils import first_word, ReverseComplement, BlankIterator, editable_str
from DDV.DefaultOrderedDict import DefaultOrderedDict
from DDV.ChainFiles import chain_file_to_list, match
from DDV.DDVUtils import make_output_dir_with_suffix, keydefaultdict, read_contigs_to_dict, copy_to_sources
from DDV.DDVUtils import make_output_directory, keydefaultdict, read_contigs_to_dict, copy_to_sources
from DDV.Span import AlignedSpans, Span, alignment_chopping_index
from DDV import gap_char
from DDV.TileLayout import hex_to_rgb
Expand Down Expand Up @@ -510,7 +510,8 @@ def setup_for_reference_chromosome(self, ref_chr, ending=''):
'__separate_translocations' * self.separate_translocations + \
'__translocations' * self.show_translocations_only + \
'__aligned_only' * self.aligned_only
self.output_folder = make_output_dir_with_suffix(self.output_prefix, ending)
self.output_folder = self.output_prefix + ending
make_output_directory(self.output_folder)
ref_name = first_word(os.path.basename(self.ref_source))
q_name = first_word(os.path.basename(self.query_source))
names = {'ref': ref_chr + '_%s.fa' % ref_name,
Expand Down
9 changes: 4 additions & 5 deletions DDV/DDVUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,15 @@ def create_deepzoom_stack(input_image, output_dzi):
creator.create(input_image, output_dzi)


def make_output_dir_with_suffix(base_path, suffix=''):
def make_output_directory(base_path, no_webpage=False):
from os import errno
output_dir = base_path + suffix
print("Creating Chromosome Output Directory...", os.path.basename(output_dir))
try:
os.makedirs(os.path.join(output_dir, 'sources'))
os.makedirs(os.path.join(base_path, '' if no_webpage else 'sources'))
print("Creating Chromosome Output Directory...", os.path.basename(base_path))
except OSError as e: # exist_ok=True
if e.errno != errno.EEXIST:
raise
return output_dir


def copy_to_sources(output_folder, data_file):
if data_file is None:
Expand Down
6 changes: 3 additions & 3 deletions DDV/MultipleAlignmentLayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from DDV.TileLayout import hex_to_rgb, TileLayout
from natsort import natsorted

from DDVUtils import make_output_dir_with_suffix
from DDVUtils import make_output_directory
from Layouts import level_layout_factory


Expand Down Expand Up @@ -92,7 +92,7 @@ def __init__(self, sort_contigs=False, **kwargs):

def process_all_alignments(self, input_fasta_folder, output_folder, output_file_name):
start_time = datetime.now()
make_output_dir_with_suffix(output_folder)
make_output_directory(output_folder)
self.preview_all_files(input_fasta_folder)
self.calculate_mixed_layout()
print("Tallied all contigs :", datetime.now() - start_time)
Expand All @@ -113,7 +113,7 @@ def process_all_alignments(self, input_fasta_folder, output_folder, output_file_
input_path = os.path.join(input_fasta_folder, single_MSA)
self.output_fasta(output_folder, input_path, False, None, False, append_fasta_sources=False)
print("\nDrew Nucleotides:", datetime.now() - start_time)
self.output_image(output_folder, output_file_name)
self.output_image(output_folder, output_file_name, False)
print("Output Image in:", datetime.now() - start_time)


Expand Down
2 changes: 1 addition & 1 deletion DDV/ParallelGenomeLayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def process_file(self, output_folder, output_file_name, fasta_files=list(),
traceback.print_exc()
# self.draw_the_viz_title(fasta_files) # Needs padding in origins to work
# self.generate_html(output_folder, output_file_name) # done in fluentdna.py
self.output_image(output_folder, output_file_name)
self.output_image(output_folder, output_file_name, no_webpage)
print("Output Image in:", datetime.now() - start_time)

def changes_per_genome(self):
Expand Down
4 changes: 2 additions & 2 deletions DDV/Sequenaut.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from DDV.TileLayout import TileLayout
from DDV.fluentdna import create_tile_layout_viz_from_fasta
from DDV.DDVUtils import make_output_dir_with_suffix, base_directories, interpolate
from DDV.DDVUtils import make_output_directory, base_directories, interpolate


def hasDepth(listLike):
Expand Down Expand Up @@ -172,7 +172,7 @@ def process_file(self, input_file_path, output_folder, output_file_name):
def run_sequenaut(args):
SERVER_HOME, base_path = base_directories(args.output_name)
# TODO: allow batch of tiling layout by chromosome
output_dir = make_output_dir_with_suffix(base_path, '')
output_dir = make_output_directory(base_path)
renderer = Sequenaut(layout=args.layout, oligomer_size=args.oligomer_size, peak=args.peak, baseline=args.baseline, log_scale=not args.linear_scale)
create_tile_layout_viz_from_fasta(args, args.fasta, args.output_name, renderer)
sys.exit(0)
Expand Down
38 changes: 19 additions & 19 deletions DDV/TileLayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from DDV import gap_char
from DDV.DDVUtils import multi_line_height, pretty_contig_name, viridis_palette, \
make_output_dir_with_suffix, filter_by_contigs, copy_to_sources
make_output_directory, filter_by_contigs, copy_to_sources
from DDV.Layouts import LayoutFrame, LayoutLevel, level_layout_factory, parse_custom_layout

small_title_bp = 10000
Expand Down Expand Up @@ -183,7 +183,7 @@ def enable_fat_headers(self):

def process_file(self, input_file_path, output_folder, output_file_name,
no_webpage=False, extract_contigs=None):
make_output_dir_with_suffix(output_folder, '')
make_output_directory(output_folder, no_webpage)
start_time = datetime.now()
self.final_output_location = output_folder
self.image_length = self.read_contigs_and_calc_padding(input_file_path, extract_contigs)
Expand All @@ -210,7 +210,7 @@ def process_file(self, input_file_path, output_folder, output_file_name,
print('Encountered exception while drawing titles:', '\n')
traceback.print_exc()

self.output_image(output_folder, output_file_name)
self.output_image(output_folder, output_file_name, no_webpage)
print("Output Image in:", datetime.now() - start_time)
self.output_fasta(output_folder, input_file_path, no_webpage,
extract_contigs, self.sort_contigs)
Expand Down Expand Up @@ -246,25 +246,23 @@ def draw_nucleotides(self, verbose=True):
print('')


def output_fasta(self, output_folder, fasta, no_webpage, extract_contigs, sort_contigs,
append_fasta_sources=True):
def output_fasta(self, output_folder, fasta, no_webpage, extract_contigs, sort_contigs, append_fasta_sources=True):
bare_file = os.path.basename(fasta)
fasta_destination = os.path.join(output_folder, 'sources', bare_file)
if not no_webpage: # these support the webpage
if append_fasta_sources:
self.fasta_sources.append(bare_file)

#also make single file
if not no_webpage:
write_contigs_to_chunks_dir(output_folder, bare_file, self.contigs)
self.remember_contig_spacing()
#also make single file
if extract_contigs or sort_contigs:
length_sum = sum([len(c.seq) for c in self.contigs])
fasta_destination = '%s__%ibp.fa' % (os.path.splitext(fasta_destination)[0], length_sum)
write_contigs_to_file(fasta_destination, self.contigs) # shortened fasta
else:
if not no_webpage:
fasta_destination = os.path.join(output_folder, 'sources', bare_file)
if extract_contigs or sort_contigs: # customized_fasta
length_sum = sum([len(c.seq) for c in self.contigs])
fasta_destination = '%s__%ibp.fa' % (os.path.splitext(fasta_destination)[0], length_sum)
write_contigs_to_file(fasta_destination, self.contigs) # shortened fasta
else:
copy_to_sources(output_folder, fasta)
if append_fasta_sources:
self.fasta_sources.append(bare_file)
print("Sequence saved in:", fasta_destination)
return fasta_destination
print("Sequence saved in:", fasta_destination)

def calc_all_padding(self):
total_progress = 0 # pointer in image
Expand Down Expand Up @@ -428,12 +426,14 @@ def get_font(self, font_name, font_size):
font = ImageFont.truetype(font_name, font_size)
return font

def output_image(self, output_folder, output_file_name):
def output_image(self, output_folder, output_file_name, no_webpage):
try:
del self.pixels
del self.draw
except BaseException:
pass # this is just memory optimization
if not no_webpage: # sources directory only exists for non-quick
output_folder = os.path.join(output_folder, 'sources',)
self.final_output_location = os.path.join(output_folder, output_file_name + ".png")
print("-- Writing:", self.final_output_location, "--")
self.image.save(self.final_output_location, 'PNG')
Expand Down
4 changes: 2 additions & 2 deletions DDV/TransposonLayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def create_image_from_preprocessed_alignment(self, input_file_path, consensus_wi
self.initialize_image_by_sequence_dimensions(consensus_width, num_lines) # sets self.layout
self.read_contigs_and_calc_padding(input_file_path)
super(TransposonLayout, self).draw_nucleotides() # uses self.contigs and self.layout to draw
self.output_image(output_folder, output_file_name)
self.output_image(output_folder, output_file_name, False)


def process_all_repeats(self, ref_fasta, output_folder, output_file_name, repeat_annotation_filename, chromosomes=None):
Expand All @@ -61,7 +61,7 @@ def process_all_repeats(self, ref_fasta, output_folder, output_file_name, repeat
except Exception as e:
print('Encountered exception while drawing nucleotides:', '\n')
traceback.print_exc()
self.output_image(output_folder, output_file_name)
self.output_image(output_folder, output_file_name, False)
print("Output Image in:", datetime.now() - start_time)
copy_to_sources(output_folder, ref_fasta)
copy_to_sources(output_folder, repeat_annotation_filename)
Expand Down
5 changes: 3 additions & 2 deletions DDV/fluentdna.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import argparse

from DNASkittleUtils.CommandLineUtils import just_the_name
from DDV.DDVUtils import create_deepzoom_stack, make_output_dir_with_suffix, base_directories, \
from DDV.DDVUtils import create_deepzoom_stack, make_output_directory, base_directories, \
hold_console_for_windows, beep, copy_to_sources
from DDV.ParallelGenomeLayout import ParallelLayout
from DDV.AnnotatedTrackLayout import AnnotatedTrackLayout
Expand Down Expand Up @@ -549,7 +549,8 @@ def main():
args.output_dir = os.path.dirname(
os.path.abspath(args.fasta)) # just place the image next to the fasta
elif not args.chain_file:
args.output_dir = make_output_dir_with_suffix(base_path, '')
args.output_dir = base_path
make_output_directory(base_path)

ddv(args)

Expand Down

0 comments on commit 8018a74

Please sign in to comment.