@@ -99,7 +99,7 @@ def get_qpcr_probes(kmers, ambiguous_consensus, alignment_cleaned, num_processes
9999 kmers = list (kmers )
100100
101101 # Split kmers into batches
102- batch_size = int (len (kmers ) / num_processes )
102+ batch_size = max ( 1 , int (len (kmers ) / num_processes ) )
103103 batches = [kmers [i :i + batch_size ] for i in range (0 , len (kmers ), batch_size )]
104104
105105 # Prepare arguments for each dimer
@@ -282,7 +282,7 @@ def find_qcr_schemes(qpcr_probes, left_primer_candidates, right_primer_candidate
282282 amplicon_nr = - 1
283283
284284 # Prepare arguments for parallel processing - pass full primer lists
285- batch_size = int (len (qpcr_probes ) / num_processes )
285+ batch_size = max ( 1 , int (len (qpcr_probes ) / num_processes ) )
286286 callable_f = functools .partial (
287287 find_single_qpcr_scheme ,
288288 left_primer_candidates , right_primer_candidates , qpcr_probes , majority_consensus , ambiguous_consensus
@@ -315,7 +315,7 @@ def find_qcr_schemes(qpcr_probes, left_primer_candidates, right_primer_candidate
315315 return qpcr_scheme_candidates
316316
317317
318- def process_single_amplicon_deltaG (amplicon , majority_consensus ):
318+ def process_single_amplicon_deltaG (majority_consensus , amplicon ):
319319 """
320320 Process a single amplicon to test its deltaG and apply filtering.
321321 This function will be called concurrently by multiple threads.
@@ -341,32 +341,33 @@ def test_amplicon_deltaG_parallel(qpcr_schemes_candidates, majority_consensus, n
341341 """
342342 final_amplicons = []
343343
344- # Create a pool of processes to handle the concurrent processing
344+ # Create a list of the first n amplicon tuples for processing
345+ # The list is sorted first on whether offset targets were predicted for the amplicon,
346+ # then by penalty. This ensures that amplicons with offset targets are always considered last
347+ amplicons = list (sorted (qpcr_schemes_candidates , key = lambda x : (x .get ("offset_targets" , False ), x ["penalty" ])))
348+ # process amplicons concurrently
349+ batch_size = max (1 , int (n_to_test / n_processes ))
350+ callable_f = functools .partial (
351+ process_single_amplicon_deltaG ,
352+ majority_consensus
353+ )
345354 with multiprocessing .Pool (processes = n_processes ) as pool :
346- # Create a list of the first n amplicon tuples for processing
347- # The list is sorted first on whether offset targets were predicted for the amplicon,
348- # then by penalty. This ensures that amplicons with offset targets are always considered last
349- amplicons = itertools .islice (
350- sorted (qpcr_schemes_candidates , key = lambda x : (x .get ("offset_targets" , False ), x ["penalty" ])),
351- n_to_test
352- )
353- # process amplicons concurrently
354- results = pool .starmap (process_single_amplicon_deltaG , [(amp , majority_consensus ) for amp in amplicons ])
355- # Process the results
356- retained_ranges = []
357- for amp in results :
358- # check if the amplicon overlaps with an amplicon that was previously
359- # found and had a high enough deltaG
360- if amp ["deltaG" ] <= deltaG_cutoff :
361- continue
362- amp_range = range (amp ["LEFT" ][1 ], amp ["RIGHT" ][2 ])
363- overlaps_retained = False
364- for r in retained_ranges :
365- if amp_range .start < r .stop and r .start < amp_range .stop :
366- overlaps_retained = True
367- break
368- if not overlaps_retained :
369- final_amplicons .append (amp )
370- retained_ranges .append (amp_range )
355+ results = pool .map (callable_f , amplicons , chunksize = batch_size )
356+ # Process the results
357+ retained_ranges = []
358+ for amp in results :
359+ # check if the amplicon overlaps with an amplicon that was previously
360+ # found and had a high enough deltaG
361+ if amp ["deltaG" ] <= deltaG_cutoff :
362+ continue
363+ amp_range = range (amp ["LEFT" ][1 ], amp ["RIGHT" ][2 ])
364+ overlaps_retained = False
365+ for r in retained_ranges :
366+ if amp_range .start < r .stop and r .start < amp_range .stop :
367+ overlaps_retained = True
368+ break
369+ if not overlaps_retained :
370+ final_amplicons .append (amp )
371+ retained_ranges .append (amp_range )
371372
372373 return final_amplicons
0 commit comments