@@ -333,10 +333,15 @@ def cut_scaffold_if_too_long(self, scffld: Scaffold) -> list[Scaffold]:
333333 cut_at = whole // div
334334 gap_i = self .index_of_nearest_gap_to_ideal_cut_site (to_cut , cut_at )
335335 rows = to_cut .rows
336+
336337 # First part is everything up to, but not including, the gap
337- cut_parts .append (Scaffold (to_cut .name , rows [:gap_i ]))
338+ cut = to_cut .clone_empty ()
339+ cut .rows = rows [:gap_i ]
340+ cut_parts .append (cut )
341+
338342 # Second part is everything after the gap
339- to_cut = Scaffold (to_cut .name , rows [gap_i + 1 :])
343+ to_cut = to_cut .clone_empty ()
344+ to_cut .rows = rows [gap_i + 1 :]
340345 cut_parts .append (to_cut )
341346
342347 # Add suffix "_1", "_2" etc... to cut scaffolds
@@ -393,6 +398,7 @@ def index_of_nearest_gap_to_ideal_cut_site(self, to_cut: Scaffold, cut_at: int):
393398 break
394399
395400 if gap_i_before is None and gap_i_after is None :
401+ ### Make a cut here ###
396402 msg = (
397403 f"Failed to find gap before or after { cut_at :_d} in '{ to_cut .name } '"
398404 )
@@ -403,47 +409,35 @@ def index_of_nearest_gap_to_ideal_cut_site(self, to_cut: Scaffold, cut_at: int):
403409 elif gap_i_after is None :
404410 ovr_i = gap_i_before
405411 else :
406- length_before = 0
407- length_after = 0
408- for i , this_row in enumerate (rows ):
409- if i < gap_i_before :
410- length_before += this_row .length
411-
412- if i < gap_i_after :
413- length_after += this_row .length
414- else :
415- break
412+ length_before = (
413+ idx_asm .start_end_of_row (to_cut .name , gap_i_before )[0 ] - 1
414+ )
415+ length_after = idx_asm .start_end_of_row (to_cut .name , gap_i_after )[0 ] - 1
416416
417417 # Choose the gap before or after, whichever is nearest to the
418418 # ideal cut point.
419419 ovr_i = (
420420 gap_i_before
421- if abs (cut_at - length_before ) < abs (cut_at - length_after )
421+ if abs (cut_at - length_before ) < abs (length_after - cut_at )
422422 else gap_i_after
423423 )
424424
425425 return ovr_i
426426
427427 def scaffolds_fused_by_name (self ) -> list [Scaffold ]:
428428 gap = self .default_gap
429- hap_name_scaffold : dict [tuple [str , str ], Scaffold ] = {}
429+ hap_name_scaffold : dict [tuple [str | None , str ], Scaffold ] = {}
430430 for scffld in self .scaffolds :
431431 if not scffld .rows :
432432 # discard_overhanging_fragments() may have removed the only
433433 # row from an OverlapResult
434434 continue
435435
436- build_scffld = hap_name_scaffold .setdefault (
437- (scffld .haplotype , scffld .name ),
438- Scaffold (
439- scffld .name ,
440- tag = scffld .tag ,
441- haplotype = scffld .haplotype ,
442- rank = scffld .rank ,
443- original_name = scffld .original_name ,
444- original_tags = scffld .original_tags ,
445- ),
446- )
436+ idx = scffld .haplotype , scffld .name
437+ build_scffld = hap_name_scaffold .get (idx )
438+ if not build_scffld :
439+ hap_name_scaffold [idx ] = build_scffld = scffld .clone_empty ()
440+
447441 if isinstance (scffld , OverlapResult ):
448442 build_scffld .append_scaffold (scffld .to_scaffold (), gap )
449443 else :
0 commit comments