diff scripts/modules/run_rematch.py @ 3:0cbed1c0a762 draft default tip

planemo upload commit 15239f1674081ab51ab8dd75a9a40cf1bfaa93e8
author cstrittmatter
date Tue, 28 Jan 2020 10:42:31 -0500
parents 965517909457
children
line wrap: on
line diff
--- a/scripts/modules/run_rematch.py	Wed Jan 22 09:10:12 2020 -0500
+++ b/scripts/modules/run_rematch.py	Tue Jan 28 10:42:31 2020 -0500
@@ -1,8 +1,16 @@
 import functools
 import os
 import sys
+import multiprocessing
 
-import utils
+try:
+    import modules.utils as utils
+except ImportError:
+    from pathotyping.modules import utils as utils
+
+
+# {'noMatter': '/home/ubuntu/NGStools/patho_typing/mpmachado_stuff.out_test/rematch/sample.noMatter.fasta', 'correct': '/home/ubuntu/NGStools/patho_typing/mpmachado_stuff.out_test/rematch/sample.correct.fasta', 'alignment': '/home/ubuntu/NGStools/patho_typing/mpmachado_stuff.out_test/rematch/sample.alignment.fasta'}
+
 
 def remove_alignment(alignment_file):
     directory = os.path.dirname(alignment_file)
@@ -24,7 +32,7 @@
 def clean_rematch_folder(consensus_files, bam_file, reference_file, outdir, doNotRemoveConsensus, debug_mode_true):
     if not debug_mode_true:
         if not doNotRemoveConsensus:
-            for consensus_type, file_path in consensus_files.items():
+            for consensus_type, file_path in list(consensus_files.items()):
                 if os.path.isfile(file_path):
                     os.remove(file_path)
         if bam_file is not None:
@@ -39,16 +47,12 @@
 
     sequences, headers = utils.get_sequence_information(reference_file, length_extra_seq)
 
-    threads_2_use = rematch.determine_threads_2_use(len(sequences), threads)
-
-    import multiprocessing
-
     pool = multiprocessing.Pool(processes=threads)
     for sequence_counter in sequences:
         sequence_dir = os.path.join(sequence_data_outdir, str(sequence_counter), '')
         utils.removeDirectory(sequence_dir)
         os.makedirs(sequence_dir)
-        pool.apply_async(rematch.analyse_sequence_data, args=(bam_file, sequences[sequence_counter], sequence_dir, sequence_counter, reference_file, length_extra_seq, minimum_depth_presence, minimum_depth_call, minimum_depth_frequency_dominant_allele, threads_2_use,))
+        pool.apply_async(rematch.analyse_sequence_data, args=(bam_file, sequences[sequence_counter], sequence_dir, sequence_counter, reference_file, length_extra_seq, minimum_depth_presence, minimum_depth_call, minimum_depth_frequency_dominant_allele,))
     pool.close()
     pool.join()
 
@@ -57,12 +61,13 @@
     return run_successfully, sample_data, consensus_files, consensus_sequences
 
 
-def write_report(outdir, sample_data, minimum_gene_coverage, minimum_gene_identity):
-    print 'Writing report file'
+def determine_general_statistics(sample_data, minimum_gene_coverage, minimum_gene_identity):
+    print('Writing report file')
     number_absent_genes = 0
     number_genes_multiple_alleles = 0
     mean_sample_coverage = 0
-    with open(os.path.join(outdir, 'rematchModule_report.txt'), 'wt') as writer:
+
+    with open('output_dir/rematch/rematchModule_report.txt', 'wt') as writer:
         writer.write('\t'.join(['#gene', 'percentage_gene_coverage', 'gene_mean_read_coverage', 'percentage_gene_low_coverage', 'number_positions_multiple_alleles', 'percentage_gene_identity']) + '\n')
         for i in range(1, len(sample_data) + 1):
             writer.write('\t'.join([sample_data[i]['header'], str(round(sample_data[i]['gene_coverage'], 2)), str(round(sample_data[i]['gene_mean_read_coverage'], 2)), str(round(sample_data[i]['gene_low_coverage'], 2)), str(sample_data[i]['gene_number_positions_multiple_alleles']), str(round(sample_data[i]['gene_identity'], 2))]) + '\n')
@@ -81,11 +86,12 @@
 
         writer.write('\n'.join(['#general', '>number_absent_genes', str(number_absent_genes), '>number_genes_multiple_alleles', str(number_genes_multiple_alleles), '>mean_sample_coverage', str(round(mean_sample_coverage, 2))]) + '\n')
 
-        print '\n'.join([str('number_absent_genes: ' + str(number_absent_genes)), str('number_genes_multiple_alleles: ' + str(number_genes_multiple_alleles)), str('mean_sample_coverage: ' + str(round(mean_sample_coverage, 2)))]) + '\n'
+        print('\n'.join([str('number_absent_genes: ' + str(number_absent_genes)),
+                     str('number_genes_multiple_alleles: ' + str(number_genes_multiple_alleles)),
+                     str('mean_sample_coverage: ' + str(round(mean_sample_coverage, 2)))]) + '\n')
 
     return number_absent_genes, number_genes_multiple_alleles, mean_sample_coverage
 
-
 module_timer = functools.partial(utils.timer, name='Module ReMatCh')
 
 
@@ -95,14 +101,16 @@
     utils.removeDirectory(module_dir)
     os.makedirs(module_dir)
 
-    sys.path.append(os.path.join(os.path.dirname(rematch), 'modules', ''))
+    sys.path.append(os.path.join(os.path.dirname(rematch), 'modules'))
     import rematch_module as rematch
 
-    print 'Analysing alignment data'
+    print('Analysing alignment data')
     run_successfully, sample_data, consensus_files, consensus_sequences = sequence_data('sample', reference_file, bam_file, module_dir, threads, length_extra_seq, minimum_depth_presence, minimum_depth_call, minimum_depth_frequency_dominant_allele, debug_mode_true, rematch)
 
     if run_successfully:
-        number_absent_genes, number_genes_multiple_alleles, mean_sample_coverage = write_report(outdir, sample_data, minimum_gene_coverage, minimum_gene_identity)
+        number_absent_genes, number_genes_multiple_alleles, mean_sample_coverage = \
+            determine_general_statistics(sample_data=sample_data, minimum_gene_coverage=minimum_gene_coverage,
+                                         minimum_gene_identity=minimum_gene_identity)
 
     if not debug_mode_true:
         utils.removeDirectory(module_dir)