|
12 | 12 | import perChrom |
13 | 13 | import sqlite3 |
14 | 14 | import perChrom |
15 | | - |
| 15 | +try: |
| 16 | + import tqdm |
| 17 | +except: |
| 18 | + print('Cannot import tqdm. Will not use tqdm.') |
| 19 | + tqdm = False |
16 | 20 | # # Global variables |
17 | 21 | # con = None |
18 | 22 | # df_mutations = None |
@@ -211,6 +215,13 @@ def save_mutation_and_proteins(df_transcript3, outprefix): |
211 | 215 | if pd.notnull(r['new_AA']) and r['new_AA'] != r['AA_seq']: |
212 | 216 | fout.write('>{}\tchanged\n{}\n'.format(r['protein_id_fasta'], r['new_AA'])) |
213 | 217 | fout.close() |
| 218 | +# Define the wrapper function needed for imap |
| 219 | +def translate_wrapper(args): |
| 220 | + """Unpacks arguments for translateCDSplusWithMut2""" |
| 221 | + row_series, mutations_df = args |
| 222 | + # Assuming perChrom.translateCDSplusWithMut2 exists and works like this |
| 223 | + return perChrom.translateCDSplusWithMut2(row_series, mutations_df) |
| 224 | + |
214 | 225 |
|
215 | 226 | class PerChrom_sqlite(object): |
216 | 227 | """ |
@@ -292,6 +303,8 @@ def __init__( |
292 | 303 | self.df_mutations['chr'] = chromosome |
293 | 304 | elif chromosome.startswith('chr') and chromosome[3:] in chromosome_with_genomicLocs: |
294 | 305 | self.df_mutations['chr'] = chromosome[3:] |
| 306 | + elif 'chr' + chromosome in chromosome_with_genomicLocs: |
| 307 | + self.df_mutations['chr'] = 'chr' + chromosome |
295 | 308 | else: |
296 | 309 | print(f'warning! chromosome {chromosome} not in file_sqlite') |
297 | 310 |
|
@@ -351,7 +364,16 @@ def run_perChrom(self, save_results = True): |
351 | 364 |
|
352 | 365 | if cpu_counts > 1: |
353 | 366 | pool = Pool(cpu_counts) |
354 | | - results = pool.starmap(perChrom.translateCDSplusWithMut2, [[r, df_mutations] for _,r in df_transcript3.iterrows()]) |
| 367 | + starmap_args = [[r, df_mutations] for _,r in df_transcript3.iterrows()] |
| 368 | + |
| 369 | + chunk_size = min(200, total_tasks // cpu_counts // 4) |
| 370 | + total_tasks = df_transcript3.shape[0] |
| 371 | + # results = pool.starmap(perChrom.translateCDSplusWithMut2, starmap_args, chunksize=100) |
| 372 | + imap_results = pool.imap(translate_wrapper, starmap_args, chunksize=chunk_size) |
| 373 | + if tqdm: |
| 374 | + results = list(tqdm.tqdm(imap_results, total=total_tasks, desc=f"Translating {chromosome}")) |
| 375 | + else: |
| 376 | + results = list(imap_results) |
355 | 377 | pool.close() |
356 | 378 | pool.join() |
357 | 379 | else: |
|
0 commit comments