def test_general_mapping_stats(): 'General mapping statistics' sam = NamedTemporaryFile(suffix='.sam') sam.write(SAM) sam.flush() bam_fhand = NamedTemporaryFile() sam2bam(sam.name, bam_fhand.name) out_fhand = StringIO() bam_general_stats(bam_fhand, out_fhand) result = out_fhand.getvalue() assert 'illumina\t3\t100.0' in result assert 'Secondary alignments: 1' in result assert 'Reads with one X0 best alignment: 1' in result assert 'Total number of reads: 7' in result out_fhand = StringIO() unmapped_fhand = NamedTemporaryFile() unmapped_fhand.write('1\n2\n3\n') unmapped_fhand.flush() bam_general_stats(bam_fhand, out_fhand, unmapped_fhand) result = out_fhand.getvalue() assert 'illumina\t3\t100.0' in result assert 'Secondary alignments: 1' in result assert 'Reads with one X0 best alignment: 1' in result assert 'Total number of reads: 10' in result
def run(self): '''It runs the analysis.''' self._log({'analysis_started':True}) settings = self._project_settings self._create_output_dirs()['result'] project_name = settings['General_settings']['project_name'] sample_size = settings['Sam_stats']['sampling_size'] project_path = settings['General_settings']['project_path'] inputs = self._get_input_fpaths() bam_path = inputs['bam'] bam_fpath = bam_path.last_version bam_fhand = open(bam_fpath) out_dir = os.path.abspath(self._get_output_dirs()['result']) summary_fname = os.path.join(out_dir, BACKBONE_BASENAMES['statistics_file']) summary_fhand = open(summary_fname, 'w') # non mapped_reads_fhand unmapped_fpath = os.path.join(project_path, BACKBONE_DIRECTORIES['mappings'][0], BACKBONE_BASENAMES['unmapped_list']) if os.path.exists(unmapped_fpath): unmapped_fhand = GzipFile(unmapped_fpath) else: unmapped_fhand = None #The general statistics bam_general_stats(bam_fhand, summary_fhand, unmapped_fhand) for kind in ('coverage', 'mapq'): basename = os.path.join(out_dir, "%s" % (project_name)) bam_fhand.seek(0) bam_distribs(bam_fhand, kind, basename=basename, sample_size=sample_size, summary_fhand=summary_fhand, plot_file_format=PLOT_FILE_FORMAT) bam_fhand.close() if unmapped_fhand is not None: unmapped_fhand.close()