def tab_file(self, tab_file=None): """Output a tab-delimited version of the spreadsheet data Creates and returns a string representation of the statistics in a tab-delimited format. If the tab_file argument is supplied then also writes this to the file specified. Arguments: tab_file: (optional) name of file to write tab-delimited data to Returns: String representing the statistics data. """ # Fetch the xls xls = self.xls() # Remove the formatting for the read numbers xls.worksheet["mapping"].get_style("B5").number_format = None # Generate the tab file text end_cell = cell(column_integer_to_index(self.n_samples), xls.worksheet["mapping"].last_row) txt = xls.worksheet["mapping"].render_as_text(eval_formulae=True, apply_format=True, start="A3", end=end_cell) if tab_file is not None: print "Writing statistics to tab-delimited file %s" % tab_file open(tab_file, "w").write(txt) return txt
def xls(self, xls_out=None): """Output an XLS spreadsheet with the sample data Create and return a simple_xls.XLSWorkBook object representing the statistics for all the samples. If the 'xls_out' argument is supplied then an XLS spreadsheet file will also be written to the name it contains. Arguments: xls_out: (optional) specify the name of an XLS file to write the spreadsheet to. Will overwrite an existing file with the same name. Returns XLSWorkBook object. """ # Set up reusable spreadsheet styles reads_style = XLSStyle( bgcolor="ivory", border="medium", number_format=NumberFormats.THOUSAND_SEPARATOR, centre=True ) pcent_style = XLSStyle(bgcolor="ivory", border="medium", number_format=NumberFormats.PERCENTAGE, centre=True) headr_style = XLSStyle(color="red", bgcolor="ivory", border="medium") table_style = XLSStyle(bgcolor="ivory", border="medium", centre=True) # Create spreadsheet wb = XLSWorkBook() mapping = wb.add_work_sheet("mapping") mapping.insert_column( "A", data=[ "Sample", "", "total reads", "didn't align", "total mapped reads", " % of all reads", "uniquely mapped", " % of all reads", " % of mapped reads", ], from_row=3, ) mapping["A1"] = "MAPPING STATS" mapping.set_style(XLSStyle(bold=True), "A1", "A11") # Build spreadsheet for sample in self.samples: sample_name = sample.name # Add input file names to sample ids if there were multiple input files if len(self.files) > 1 and sample.filen is not None: sample_name += " (" + sample.filen + ")" # Insert data into the spreadsheet col = mapping.append_column( data=[ sample_name, "", sample.total_reads, sample.didnt_align, "=#5-#6", "=#7/#5", sample.uniquely_mapped, "=#9/#5", "=#9/#7", ], from_row=3, ) mapping.set_style(headr_style, cell(col, 3)) mapping.set_style(table_style, cell(col, 4)) mapping.set_style(reads_style, cell(col, 5), cell(col, 7)) mapping.set_style(pcent_style, cell(col, 8)) mapping.set_style(reads_style, cell(col, 9)) mapping.set_style(pcent_style, cell(col, 10), cell(col, 11)) # Header mapping["C1"] = "Mapped with Bowtie" mapping.set_style(XLSStyle(centre=True), "C1") # Finished if xls_out is not None: print "Writing statistics to XLS file %s" % xls_out wb.save_as_xls(xls_out) return wb