def asFile( self, dataframe, row_headers, col_headers, title ): '''save the table as HTML file. Multiple files of the same Renderer/Tracker combination are distinguished by the title. ''' self.debug("%s: saving %i x %i table as file'"% (id(self), len(row_headers), len(col_headers))) lines = [] lines.append("`%i x %i table <#$html %s$#>`__" %\ (len(row_headers), len(col_headers), title) ) r = ResultBlock( "\n".join(lines) + "\n", title = title) out = StringIO.StringIO() dataframe.to_csv( out ) lines = out.getvalue().split("\n") # create an html table data = ["<table>"] data.append( "<tr><th></th><th>%s</th></tr>" % "</th><th>".join( map(str,lines[0].split(","))) ) data.extend( ["<tr><td>%s</td></tr>" % \ ("</td><td>".join(x.split(","))) for x in lines[1:]] ) data.append( "</table>\n" ) # substitute links data = [ re.sub("`(.*?(?:\".+\"|\'.+\')?.*?)\s<(.*?(?:\".+\"|\'.+\')?.*?)>`_", r'<a href="\2">\1</a>', x) \ for x in data ] r.html = "\n".join( data ) return r
def asFile( self, matrix, row_headers, col_headers, title ): '''save the table as HTML file. Multiple files of the same Renderer/Tracker combination are distinguished by the title. ''' self.debug("%s: saving %i x %i table as file'"% (id(self), len(row_headers), len(col_headers))) lines = [] lines.append("`%i x %i table <#$html %s$#>`__" %\ (len(row_headers), len(col_headers), title) ) r = ResultBlock( "\n".join(lines), title = title) # create an html table data = ["<table>"] data.append( "<tr><th></th><th>%s</th></tr>" % "</th><th>".join( map(str,col_headers)) ) for h, row in zip( row_headers, matrix): data.append( "<tr><th>%s</th><td>%s</td></tr>" % (h, "</td><td>".join(map(str,row)) )) data.append( "</table>\n" ) r.html = "\n".join( data ) return ResultBlocks( r )
def __call__(self, track, slice = None ): edir = EXPORTDIR method = "utr_extension" blocks = ResultBlocks() filepath = "%(edir)s/%(method)s/%(track)s.readextension_%(region)s_%(direction)s.%(slice)s.png" block = \ ''' .. figure:: %(filename)s :height: 300 ''' # append spaces for file extension block = "\n".join( [ x + " " * 200 for x in block.split("\n") ] ) for region, direction in itertools.product( ("downstream", "upstream"), ("sense", "antisense", "anysense" )): filename = filepath % locals() if os.path.exists( filename ): blocks.append( ResultBlock( text = block % locals(), title = "%(track)s %(region)s %(direction)s" % locals() ) ) # else: # blocks.append( ResultBlock( "", # title = "%(track)s %(region)s %(direction)s" % locals() ) ) return odict( (("rst", "\n".join(Utils.layoutBlocks( blocks, layout="columns-3" ))),))
def __call__(self, track, slice=None): # note there are spaces behind the %(image)s directive to accomodate # for path substitution block = ''' .. figure:: %(image)s :height: 300 ''' blocks = ResultBlocks() tracks = sorted([x.asFile() for x in TRACKS]) for track in tracks: files = glob.glob( os.path.join(EXPORTDIR, "fastqc", "%s*_fastqc" % track)) for x, fn in enumerate(sorted(files)): y = x + 1 image = os.path.abspath( os.path.join(fn, "Images", "%s.png" % slice)) if not os.path.exists(image): continue blocks.append( ResultBlock(text=block % locals(), title=os.path.basename(fn))) return odict( (("rst", "\n".join(Utils.layoutBlocks(blocks, layout="columns-2"))), ))
def render(self, dataframe, path ): R.library( 'ggplot2' ) rframe = pandas.rpy.common.convert_to_r_dataframe(dataframe) # sometimes the row/column mapping did not work # rframe.colnames = dataframe.columns unAsIs = R('''function (x) { if(typeof(x) %in% c("integer","double")) { class(x) <- "numeric" return (x)} else if (typeof(x) == "character") { class(x) <- "character" return (x) } else { return(x) } }''') rframe = R["as.data.frame"](R.lapply(rframe,unAsIs)) R.assign( "rframe", rframe ) # start plot R('''gp = ggplot( rframe )''') # add aesthetics and geometries try: pp = R('''gp + %s ''' % self.statement ) except ValueError as msg: raise ValueError( "could not interprete R statement: gp + %s; msg=%s" % (self.statement, msg )) figname = re.sub( '/', '_', path2str(path) ) r = ResultBlock( '#$ggplot %s$#' % figname, title = path2str(path) ) r.rggplot = pp r.figname = figname return ResultBlocks( r )
def asSpreadSheet( self, dataframe, row_headers, col_headers, title ): '''save the table as an xls file. Multiple files of the same Renderer/Tracker combination are distinguished by the title. ''' self.debug("%s: saving %i x %i table as spread-sheet'"% (id(self), len(row_headers), len(col_headers))) quick = len(dataframe) > 10000 if quick: # quick writing, only append method works wb = openpyxl.Workbook( optimized_write = True) def addWorksheet( wb, dataframe, title ): ws = wb.create_sheet() ws.append( [""] + list(col_headers) ) for x,row in enumerate( dataframe.iterrows() ): ws.append( [path2str(row_headers[x])] + list(row) ) # patch: maximum title length seems to be 31 ws.title = title[:30] else: # do it cell-by-cell, this might be slow wb = openpyxl.Workbook( optimized_write = False) def addWorksheet( wb, dataframe, title ): ws = wb.create_sheet() # regex to detect rst hypelinks regex_link = re.compile( '`(.*) <(.*)>`_') for column, column_name in enumerate( dataframe.columns ): c = ws.cell( row=0, column=column) c.value = column_name dataseries = dataframe[column_name] if dataseries.dtype == object: for row, value in enumerate( dataseries ): c = ws.cell( row=row+1, column=column) value = str(value) if value.startswith('`'): c.value, c.hyperlink = regex_link.match( value ).groups() else: c.value = value else: for row, value in enumerate( dataseries ): c = ws.cell( row=row+1, column=column) c.value = value # patch: maximum title length seems to be 31 ws.title = title[:30] is_hierarchical = isinstance( dataframe.index, pandas.core.index.MultiIndex ) split = is_hierarchical and len(dataframe.index.levels) > 1 if split: # create separate worksheets for nested indices nlevels = len(dataframe.index.levels) paths = map( tuple, DataTree.unique( [ x[:nlevels-1] for x in dataframe.index.unique() ] )) ws = wb.worksheets[0] ws.title = 'Summary' ws.append( [dataframe.index.labels[:nlevels-1]] + ["Worksheet", "Rows" ] ) for row, path in enumerate(paths): # select data frame as cross-section work = dataframe.xs(path, axis=0 ) title = path2str( path )[:30] ws.append( list(path) + [title, len(work)] ) c = ws.cell( row = row+1, column = nlevels ) c.hyperlink = "#%s" % title addWorksheet( wb, work, title = title ) else: writeWorksheet( wb, dataframe, title = title ) # write result block lines = [] lines.append("`%i x %i table <#$xls %s$#>`__" %\ (len(row_headers), len(col_headers), title) ) lines.append( "" ) r = ResultBlock( "\n".join(lines), title = title) r.xls = wb self.debug("%s: saved %i x %i table as spread-sheet'"% (id(self), len(row_headers), len(col_headers))) return r