<s> import copy <EOL> from textwrap import dedent <EOL> import numpy as np <EOL> import pandas <EOL> from matplotlib import pyplot as plt <EOL> from mpl_toolkits . axes_grid1 import make_axes_locatable <EOL> import matplotlib . patches as patches <EOL> import matplotlib <EOL> import plotutils <EOL> from matplotlib . transforms import blended_transform_factory <EOL> from matplotlib . collections import EventCollection <EOL> import gffutils <EOL> import pybedtools <EOL> from pybedtools import featurefuncs <EOL> _base_doc = """<STR_LIT>""" <EOL> class ResultsTable ( object ) : <EOL> __doc__ = _base_doc % dedent ( <EOL> """<STR_LIT>""" ) <EOL> def __init__ ( self , data , db = None , import_kwargs = None ) : <EOL> if isinstance ( data , basestring ) : <EOL> import_kwargs = import_kwargs or { } <EOL> data = pandas . read_table ( data , ** import_kwargs ) <EOL> if not isinstance ( data , pandas . DataFrame ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . data = data <EOL> self . _kwargs = dict ( db = db , import_kwargs = import_kwargs ) <EOL> self . attach_db ( db ) <EOL> self . _cached_features = None <EOL> def __getattr__ ( self , attr ) : <EOL> if attr in self . __dict__ : <EOL> return getattr ( self , attr ) <EOL> return getattr ( self . data , attr ) <EOL> def __getitem__ ( self , attr ) : <EOL> if isinstance ( attr , basestring ) : <EOL> return self . data . __getitem__ ( attr ) <EOL> else : <EOL> return self . __class__ ( self . data . __getitem__ ( attr ) , ** self . _kwargs ) <EOL> def update ( self , dataframe ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( dataframe , ** self . _kwargs ) <EOL> def copy ( self ) : <EOL> data = self . data . copy ( deep = True ) <EOL> return self . __class__ ( data , db = self . db , import_kwargs = self . _kwargs ) <EOL> def __repr__ ( self ) : <EOL> s = [ ] <EOL> s . append ( "<STR_LIT>" <EOL> % self . __class__ . __name__ ) <EOL> s . append ( '<STR_LIT>' ) <EOL> s . extend ( '<STR_LIT:\t>' + i for i in repr ( self . data ) . splitlines ( False ) ) <EOL> s . append ( '<STR_LIT:>>' ) <EOL> return '<STR_LIT:\n>' . join ( s ) <EOL> def attach_db ( self , db ) : <EOL> """<STR_LIT>""" <EOL> if db is not None : <EOL> if isinstance ( db , basestring ) : <EOL> db = gffutils . FeatureDB ( db ) <EOL> if not isinstance ( db , gffutils . FeatureDB ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> self . _kwargs [ '<STR_LIT>' ] = db <EOL> self . db = db <EOL> def features ( self , ignore_unknown = False ) : <EOL> """<STR_LIT>""" <EOL> if not self . db : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> for i in self . data . index : <EOL> try : <EOL> yield gffutils . helpers . asinterval ( self . db [ i ] ) <EOL> except gffutils . FeatureNotFoundError : <EOL> if ignore_unknown : <EOL> continue <EOL> else : <EOL> raise gffutils . FeatureNotFoundError ( '<STR_LIT>' % i ) <EOL> def reindex_to ( self , x , attribute = "<STR_LIT:Name>" ) : <EOL> """<STR_LIT>""" <EOL> names = [ i [ attribute ] for i in x ] <EOL> new = self . copy ( ) <EOL> new . data = new . data . reindex ( names ) <EOL> return new <EOL> def five_prime ( self , upstream = <NUM_LIT:1> , downstream = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return pybedtools . BedTool ( self . features ( ) ) . each ( featurefuncs . five_prime , upstream , downstream ) . saveas ( ) <EOL> def three_prime ( self , upstream = <NUM_LIT:0> , downstream = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return pybedtools . BedTool ( self . features ( ) ) . each ( featurefuncs . three_prime , upstream , downstream ) . saveas ( ) <EOL> TSS = five_prime <EOL> TTS = three_prime <EOL> def align_with ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( self . data . reindex_like ( other ) , ** self . _kwargs ) <EOL> def genes_in_common ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return self . index & other . index <EOL> def __and__ ( self , other ) : <EOL> return self . index & other . index <EOL> def __or__ ( self , other ) : <EOL> return self . index | other . index <EOL> def __sub__ ( self , other ) : <EOL> return self . index - other . index <EOL> def __len__ ( self ) : <EOL> return len ( self . data ) <EOL> def scatter ( self , x , y , xfunc = None , yfunc = None , xscale = None , yscale = None , <EOL> xlab = None , ylab = None , genes_to_highlight = None , <EOL> label_genes = False , marginal_histograms = False , <EOL> general_kwargs = dict ( color = "<STR_LIT:k>" , alpha = <NUM_LIT> , picker = True ) , <EOL> general_hist_kwargs = None , offset_kwargs = { } , label_kwargs = None , <EOL> ax = None , one_to_one = None , callback = None , xlab_prefix = None , <EOL> ylab_prefix = None , sizefunc = None , hist_size = <NUM_LIT> , hist_pad = <NUM_LIT:0.0> , <EOL> nan_offset = <NUM_LIT> , pos_offset = <NUM_LIT> , linelength = <NUM_LIT> , <EOL> neg_offset = <NUM_LIT> , figure_kwargs = None ) : <EOL> """<STR_LIT>""" <EOL> _x = self . data [ x ] <EOL> _y = self . data [ y ] <EOL> def identity ( x ) : <EOL> return x . copy ( ) <EOL> if xlab_prefix is None : <EOL> xlab_prefix = "<STR_LIT>" <EOL> if ylab_prefix is None : <EOL> ylab_prefix = "<STR_LIT>" <EOL> if xlab is None : <EOL> xlab = x <EOL> if xfunc is not None : <EOL> xlab = xlab_prefix + "<STR_LIT>" % ( xfunc . __name__ , str ( x ) ) <EOL> else : <EOL> xlab = xlab_prefix + "<STR_LIT:%s>" % ( str ( x ) ) <EOL> if ylab is None : <EOL> ylab = y <EOL> if yfunc is not None : <EOL> ylab = ylab_prefix + "<STR_LIT>" % ( yfunc . __name__ , str ( y ) ) <EOL> else : <EOL> ylab = ylab_prefix + "<STR_LIT:%s>" % ( str ( y ) ) <EOL> if xfunc is None : <EOL> xfunc = identity <EOL> if yfunc is None : <EOL> yfunc = identity <EOL> if general_kwargs is None : <EOL> general_kwargs = { } <EOL> if general_hist_kwargs is None : <EOL> general_hist_kwargs = { } <EOL> if genes_to_highlight is None : <EOL> genes_to_highlight = [ ] <EOL> if ax is None : <EOL> if figure_kwargs is None : <EOL> figure_kwargs = { } <EOL> fig = plt . figure ( ** figure_kwargs ) <EOL> ax = fig . add_subplot ( <NUM_LIT> ) <EOL> if label_kwargs is None : <EOL> label_kwargs = dict ( <EOL> horizontalalignment = '<STR_LIT:right>' , <EOL> verticalalignment = '<STR_LIT>' , <EOL> style = '<STR_LIT>' , <EOL> bbox = dict ( facecolor = '<STR_LIT:w>' , edgecolor = '<STR_LIT:None>' , alpha = <NUM_LIT:0.5> ) <EOL> ) <EOL> xi = xfunc ( _x ) <EOL> yi = yfunc ( _y ) <EOL> x_is_pos_inf = np . isinf ( xi ) & ( xi > <NUM_LIT:0> ) <EOL> x_is_neg_inf = np . isinf ( xi ) & ( xi < <NUM_LIT:0> ) <EOL> x_is_nan = np . isnan ( xi ) <EOL> y_is_pos_inf = np . isinf ( yi ) & ( yi > <NUM_LIT:0> ) <EOL> y_is_neg_inf = np . isinf ( yi ) & ( yi < <NUM_LIT:0> ) <EOL> y_is_nan = np . isnan ( yi ) <EOL> x_valid = ~ ( x_is_pos_inf | x_is_neg_inf | x_is_nan ) <EOL> y_valid = ~ ( y_is_pos_inf | y_is_neg_inf | y_is_nan ) <EOL> gmin = max ( xi [ x_valid ] . min ( ) , yi [ y_valid ] . min ( ) ) <EOL> gmax = min ( xi [ x_valid ] . max ( ) , yi [ y_valid ] . max ( ) ) <EOL> _genes_to_highlight = [ ] <EOL> for block in genes_to_highlight : <EOL> ind = block [ <NUM_LIT:0> ] <EOL> if ind . dtype != '<STR_LIT:bool>' : <EOL> new_ind = ( np . zeros_like ( xi ) == <NUM_LIT:0> ) <EOL> new_ind [ ind ] = True <EOL> _genes_to_highlight . append ( <EOL> tuple ( [ new_ind ] + list ( block [ <NUM_LIT:1> : ] ) ) <EOL> ) <EOL> else : <EOL> if hasattr ( ind , '<STR_LIT>' ) : <EOL> ind = ind . values <EOL> _genes_to_highlight . append ( <EOL> tuple ( [ ind ] + list ( block [ <NUM_LIT:1> : ] ) ) <EOL> ) <EOL> allind = np . zeros_like ( xi ) == <NUM_LIT:0> <EOL> for block in _genes_to_highlight : <EOL> ind = block [ <NUM_LIT:0> ] <EOL> allind [ ind ] = False <EOL> general_hist_kwargs = plotutils . _updatecopy ( <EOL> orig = general_hist_kwargs , update_with = general_kwargs , <EOL> keys = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> _genes_to_highlight . insert ( <EOL> <NUM_LIT:0> , <EOL> ( allind , general_kwargs , general_hist_kwargs ) <EOL> ) <EOL> self . marginal = plotutils . MarginalHistScatter ( <EOL> ax , hist_size = hist_size , pad = hist_pad ) <EOL> rug_x_kwargs = dict ( <EOL> linelength = linelength , <EOL> transform = blended_transform_factory ( ax . transData , ax . transAxes ) , <EOL> ) <EOL> rug_y_kwargs = dict ( <EOL> linelength = linelength , <EOL> transform = blended_transform_factory ( ax . transAxes , ax . transData ) , <EOL> orientation = '<STR_LIT>' , <EOL> ) <EOL> color_converter = matplotlib . colors . ColorConverter ( ) . to_rgb <EOL> if one_to_one : <EOL> ax . plot ( [ gmin , gmax ] , <EOL> [ gmin , gmax ] , <EOL> ** one_to_one ) <EOL> self . _seen = np . ones_like ( xi ) == <NUM_LIT:0> <EOL> for block in _genes_to_highlight : <EOL> ind = block [ <NUM_LIT:0> ] <EOL> kwargs = block [ <NUM_LIT:1> ] <EOL> if len ( block ) == <NUM_LIT:3> : <EOL> hist_kwargs = block [ <NUM_LIT:2> ] <EOL> else : <EOL> hist_kwargs = { } <EOL> names = kwargs . pop ( '<STR_LIT>' , None ) <EOL> _marginal_histograms = ( <EOL> kwargs . pop ( '<STR_LIT>' , False ) or <EOL> marginal_histograms ) <EOL> updated_kwargs = plotutils . _updatecopy ( <EOL> orig = kwargs , update_with = general_kwargs ) <EOL> updated_hist_kwargs = plotutils . _updatecopy ( <EOL> orig = hist_kwargs , update_with = general_hist_kwargs ) <EOL> updated_hist_kwargs = plotutils . _updatecopy ( <EOL> orig = updated_hist_kwargs , update_with = kwargs , <EOL> keys = [ '<STR_LIT>' , '<STR_LIT>' ] , override = True ) <EOL> xhist_kwargs = updated_kwargs . pop ( '<STR_LIT>' , None ) <EOL> yhist_kwargs = updated_kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . marginal . append ( <EOL> xi [ ind & x_valid & y_valid ] , <EOL> yi [ ind & x_valid & y_valid ] , <EOL> scatter_kwargs = dict ( ** updated_kwargs ) , <EOL> hist_kwargs = updated_hist_kwargs , <EOL> xhist_kwargs = xhist_kwargs , <EOL> yhist_kwargs = yhist_kwargs , <EOL> marginal_histograms = _marginal_histograms , <EOL> ) <EOL> coll = self . marginal . scatter_ax . collections [ - <NUM_LIT:1> ] <EOL> coll . df = self . data <EOL> coll . ind = ind & x_valid & y_valid <EOL> color = color_converter ( updated_kwargs [ '<STR_LIT>' ] ) <EOL> rug_x_kwargs [ '<STR_LIT>' ] = color <EOL> rug_y_kwargs [ '<STR_LIT>' ] = color <EOL> items = [ <EOL> ( xi , ind & x_valid & y_is_pos_inf , pos_offset , rug_x_kwargs ) , <EOL> ( xi , ind & x_valid & y_is_nan , nan_offset , rug_x_kwargs ) , <EOL> ( xi , ind & x_valid & y_is_neg_inf , neg_offset , rug_x_kwargs ) , <EOL> ( yi , ind & y_valid & x_is_pos_inf , pos_offset , rug_y_kwargs ) , <EOL> ( yi , ind & y_valid & x_is_nan , nan_offset , rug_y_kwargs ) , <EOL> ( yi , ind & y_valid & x_is_neg_inf , neg_offset , rug_y_kwargs ) , <EOL> ] <EOL> for values , index , offset , kwargs in items : <EOL> coll = EventCollection ( <EOL> values [ index ] , lineoffset = offset , ** kwargs ) <EOL> coll . df = self . data <EOL> coll . ind = index <EOL> ax . add_collection ( coll ) <EOL> if names : <EOL> transOffset = matplotlib . transforms . offset_copy ( <EOL> ax . transData , fig = ax . figure , ** offset_kwargs ) <EOL> for xii , yii , name in zip ( xi [ ind ] , yi [ ind ] , names ) : <EOL> ax . text ( xii , <EOL> yii , <EOL> name , <EOL> transform = transOffset , <EOL> ** label_kwargs ) <EOL> if callback is None : <EOL> callback = self . _default_callback <EOL> def wrapped_callback ( event ) : <EOL> for _id in self . _id_callback ( event ) : <EOL> callback ( _id ) <EOL> ax . figure . canvas . mpl_connect ( '<STR_LIT>' , wrapped_callback ) <EOL> ax . set_xlabel ( xlab ) <EOL> ax . set_ylabel ( ylab ) <EOL> ax . axis ( '<STR_LIT>' ) <EOL> return ax <EOL> def radviz ( self , column_names , transforms = dict ( ) , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> x = self . data [ column_names ] . copy ( ) <EOL> for k , v in transforms . items ( ) : <EOL> x [ k ] = v ( x [ k ] ) <EOL> def normalize ( series ) : <EOL> mn = min ( series ) <EOL> mx = max ( series ) <EOL> return ( series - mn ) / ( mx - mn ) <EOL> df = x . apply ( normalize ) <EOL> to_plot = [ ] <EOL> n = len ( column_names ) <EOL> s = np . array ( [ ( np . cos ( t ) , np . sin ( t ) ) <EOL> for t in [ <NUM_LIT> * np . pi * ( i / float ( n ) ) <EOL> for i in range ( n ) ] ] ) <EOL> for i in range ( len ( x ) ) : <EOL> row = df . irow ( i ) . values <EOL> row_ = np . repeat ( np . expand_dims ( row , axis = <NUM_LIT:1> ) , <NUM_LIT:2> , axis = <NUM_LIT:1> ) <EOL> to_plot . append ( ( s * row_ ) . sum ( axis = <NUM_LIT:0> ) / row . sum ( ) ) <EOL> x_ , y_ = zip ( * to_plot ) <EOL> self . data [ '<STR_LIT>' ] = x_ <EOL> self . data [ '<STR_LIT>' ] = y_ <EOL> ax = self . scatter ( '<STR_LIT>' , '<STR_LIT>' , ** kwargs ) <EOL> ax . add_patch ( patches . Circle ( ( <NUM_LIT:0.0> , <NUM_LIT:0.0> ) , radius = <NUM_LIT:1.0> , facecolor = '<STR_LIT:none>' ) ) <EOL> for xy , name in zip ( s , column_names ) : <EOL> ax . add_patch ( patches . Circle ( xy , radius = <NUM_LIT> , facecolor = '<STR_LIT>' ) ) <EOL> if xy [ <NUM_LIT:0> ] < <NUM_LIT:0.0> and xy [ <NUM_LIT:1> ] < <NUM_LIT:0.0> : <EOL> ax . text ( xy [ <NUM_LIT:0> ] - <NUM_LIT> , xy [ <NUM_LIT:1> ] - <NUM_LIT> , name , <EOL> ha = '<STR_LIT:right>' , va = '<STR_LIT>' , size = '<STR_LIT>' ) <EOL> elif xy [ <NUM_LIT:0> ] < <NUM_LIT:0.0> and xy [ <NUM_LIT:1> ] >= <NUM_LIT:0.0> : <EOL> ax . text ( xy [ <NUM_LIT:0> ] - <NUM_LIT> , xy [ <NUM_LIT:1> ] + <NUM_LIT> , name , <EOL> ha = '<STR_LIT:right>' , va = '<STR_LIT>' , size = '<STR_LIT>' ) <EOL> elif xy [ <NUM_LIT:0> ] >= <NUM_LIT:0.0> and xy [ <NUM_LIT:1> ] < <NUM_LIT:0.0> : <EOL> ax . text ( xy [ <NUM_LIT:0> ] + <NUM_LIT> , xy [ <NUM_LIT:1> ] - <NUM_LIT> , name , <EOL> ha = '<STR_LIT:left>' , va = '<STR_LIT>' , size = '<STR_LIT>' ) <EOL> elif xy [ <NUM_LIT:0> ] >= <NUM_LIT:0.0> and xy [ <NUM_LIT:1> ] >= <NUM_LIT:0.0> : <EOL> ax . text ( xy [ <NUM_LIT:0> ] + <NUM_LIT> , xy [ <NUM_LIT:1> ] + <NUM_LIT> , name , <EOL> ha = '<STR_LIT:left>' , va = '<STR_LIT>' , size = '<STR_LIT>' ) <EOL> ax . axis ( '<STR_LIT>' ) <EOL> return ax <EOL> def _id_callback ( self , event ) : <EOL> subset_df = event . artist . df . ix [ event . artist . ind ] <EOL> for i in event . ind : <EOL> _id = subset_df . index [ i ] <EOL> yield _id <EOL> def _default_callback ( self , i ) : <EOL> print self . data . ix [ i ] <EOL> def strip_unknown_features ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . db : <EOL> return self <EOL> ind = [ ] <EOL> for i , gene_id in enumerate ( self . data . index ) : <EOL> try : <EOL> self . db [ gene_id ] <EOL> ind . append ( i ) <EOL> except gffutils . FeatureNotFoundError : <EOL> pass <EOL> ind = np . array ( ind ) <EOL> return self . __class__ ( self . data . ix [ ind ] , ** self . _kwargs ) <EOL> def genes_with_peak ( self , peaks , transform_func = None , split = False , <EOL> intersect_kwargs = None , id_attribute = '<STR_LIT>' , * args , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def _transform_func ( x ) : <EOL> """<STR_LIT>""" <EOL> result = transform_func ( x ) <EOL> if isinstance ( result , pybedtools . Interval ) : <EOL> result = [ result ] <EOL> for i in result : <EOL> if i : <EOL> yield result <EOL> intersect_kwargs = intersect_kwargs or { } <EOL> if not self . _cached_features : <EOL> self . _cached_features = pybedtools . BedTool ( self . features ( ) ) . saveas ( ) <EOL> if transform_func : <EOL> if split : <EOL> features = self . _cached_features . split ( _transform_func , * args , ** kwargs ) <EOL> else : <EOL> features = self . _cached_features . each ( transform_func , * args , ** kwargs ) <EOL> else : <EOL> features = self . _cached_features <EOL> hits = list ( set ( [ i [ id_attribute ] for i in features . intersect ( <EOL> peaks , ** intersect_kwargs ) ] ) ) <EOL> return self . data . index . isin ( hits ) <EOL> class DifferentialExpressionResults ( ResultsTable ) : <EOL> __doc__ = _base_doc % dedent ( """<STR_LIT>""" ) <EOL> pval_column = '<STR_LIT>' <EOL> lfc_column = '<STR_LIT>' <EOL> mean_column = '<STR_LIT>' <EOL> def __init__ ( self , data , db = None , header_check = True , ** kwargs ) : <EOL> import_kwargs = kwargs . pop ( '<STR_LIT>' , { } ) <EOL> if header_check and isinstance ( data , basestring ) : <EOL> comment_char = import_kwargs . get ( '<STR_LIT>' , '<STR_LIT:#>' ) <EOL> for i , line in enumerate ( open ( data ) ) : <EOL> if line [ <NUM_LIT:0> ] != comment_char : <EOL> break <EOL> import_kwargs [ '<STR_LIT>' ] = i <EOL> import_kwargs [ '<STR_LIT>' ] = [ '<STR_LIT>' ] <EOL> import_kwargs [ '<STR_LIT>' ] = import_kwargs . pop ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> super ( DifferentialExpressionResults , self ) . __init__ ( <EOL> data = data , db = db , import_kwargs = import_kwargs , ** kwargs ) <EOL> def changed ( self , thresh = <NUM_LIT> , idx = True ) : <EOL> """<STR_LIT>""" <EOL> ind = self . data [ self . pval_column ] <= thresh <EOL> if idx : <EOL> return ind <EOL> return self [ ind ] <EOL> def unchanged ( self , thresh = <NUM_LIT> , idx = True ) : <EOL> """<STR_LIT>""" <EOL> ind = ( <EOL> ( self . data [ self . pval_column ] > thresh ) <EOL> | np . isnan ( self . data [ self . pval_column ] ) <EOL> ) <EOL> if idx : <EOL> return ind <EOL> return self [ ind ] <EOL> def enriched ( self , thresh = <NUM_LIT> , idx = True ) : <EOL> """<STR_LIT>""" <EOL> return self . upregulated ( thresh = thresh , idx = idx ) <EOL> def upregulated ( self , thresh = <NUM_LIT> , idx = True ) : <EOL> """<STR_LIT>""" <EOL> ind = ( <EOL> ( self . data [ self . pval_column ] <= thresh ) <EOL> & ( self . data [ self . lfc_column ] > <NUM_LIT:0> ) <EOL> ) <EOL> if idx : <EOL> return ind <EOL> return self [ ind ] <EOL> def downregulated ( self , thresh = <NUM_LIT> , idx = True ) : <EOL> """<STR_LIT>""" <EOL> ind = ( <EOL> ( self . data [ self . pval_column ] <= thresh ) <EOL> & ( self . data [ self . lfc_column ] < <NUM_LIT:0> ) <EOL> ) <EOL> if idx : <EOL> return ind <EOL> return self [ ind ] <EOL> def disenriched ( self , thresh = <NUM_LIT> , idx = True ) : <EOL> """<STR_LIT>""" <EOL> return self . downregulated ( thresh = thresh , idx = idx ) <EOL> def ma_plot ( self , thresh , up_kwargs = None , dn_kwargs = None , <EOL> zero_line = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> genes_to_highlight = kwargs . pop ( '<STR_LIT>' , [ ] ) <EOL> genes_to_highlight . append ( <EOL> ( self . upregulated ( thresh ) , <EOL> up_kwargs or dict ( color = '<STR_LIT:r>' ) ) ) <EOL> genes_to_highlight . append ( <EOL> ( self . downregulated ( thresh ) , <EOL> dn_kwargs or dict ( color = '<STR_LIT:b>' ) ) ) <EOL> if zero_line is None : <EOL> zero_line = { } <EOL> x = self . mean_column <EOL> y = self . lfc_column <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = np . log <EOL> ax = self . scatter ( <EOL> x = x , <EOL> y = y , <EOL> genes_to_highlight = genes_to_highlight , <EOL> ** kwargs ) <EOL> if zero_line : <EOL> ax . axhline ( <NUM_LIT:0> , ** zero_line ) <EOL> return ax <EOL> threshdoc = """<STR_LIT>""" <EOL> enriched . __doc__ = enriched . __doc__ . format ( threshdoc = threshdoc ) <EOL> disenriched . __doc__ = disenriched . __doc__ . format ( threshdoc = threshdoc ) <EOL> upregulated . __doc__ = upregulated . __doc__ . format ( threshdoc = threshdoc ) <EOL> downregulated . __doc__ = downregulated . __doc__ . format ( threshdoc = threshdoc ) <EOL> class EdgeRResults ( DifferentialExpressionResults ) : <EOL> __doc__ = _base_doc % dedent ( <EOL> """<STR_LIT>""" ) <EOL> pval_column = '<STR_LIT>' <EOL> lfc_column = '<STR_LIT>' <EOL> mean_column = '<STR_LIT>' <EOL> class DESeqResults ( DifferentialExpressionResults ) : <EOL> __doc__ = _base_doc % dedent ( <EOL> """<STR_LIT>""" ) <EOL> def colormapped_bedfile ( self , genome , cmap = None ) : <EOL> """<STR_LIT>""" <EOL> if self . db is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> db = gffutils . FeatureDB ( self . db ) <EOL> def scored_feature_generator ( d ) : <EOL> for i in range ( len ( d ) ) : <EOL> try : <EOL> feature = db [ d . ix [ i ] ] <EOL> except gffutils . FeatureNotFoundError : <EOL> raise gffutils . FeatureNotFoundError ( d . ix [ i ] ) <EOL> score = - <NUM_LIT:10> * np . log10 ( d . padj [ i ] ) <EOL> lfc = d . log2FoldChange [ i ] <EOL> if np . isnan ( lfc ) : <EOL> score = <NUM_LIT:0> <EOL> if lfc < <NUM_LIT:0> : <EOL> score *= - <NUM_LIT:1> <EOL> feature . score = str ( score ) <EOL> feature = extend_fields ( <EOL> gff2bed ( gffutils . helpers . asinterval ( feature ) ) , <NUM_LIT:9> ) <EOL> fields = feature . fields [ : ] <EOL> fields [ <NUM_LIT:6> ] = fields [ <NUM_LIT:1> ] <EOL> fields [ <NUM_LIT:7> ] = fields [ <NUM_LIT:2> ] <EOL> fields . append ( str ( d . padj [ i ] ) ) <EOL> fields . append ( str ( d . pval [ i ] ) ) <EOL> fields . append ( '<STR_LIT>' % d . log2FoldChange [ i ] ) <EOL> fields . append ( '<STR_LIT>' % d . baseMeanB [ i ] ) <EOL> fields . append ( '<STR_LIT>' % d . baseMeanB [ i ] ) <EOL> yield pybedtools . create_interval_from_list ( fields ) <EOL> x = pybedtools . BedTool ( scored_feature_generator ( self ) ) . saveas ( ) <EOL> norm = x . colormap_normalize ( ) <EOL> if cmap is None : <EOL> cmap = cm . RdBu_r <EOL> cmap = colormap_adjust . cmap_center_point_adjust ( <EOL> cmap , [ norm . vmin , norm . vmax ] , <NUM_LIT:0> ) <EOL> def score_zeroer ( f ) : <EOL> f . score = '<STR_LIT:0>' <EOL> return f <EOL> return x . each ( add_color , cmap = cmap , norm = norm ) . sort ( ) . each ( score_zeroer ) . truncate_to_chrom ( genome ) . saveas ( ) <EOL> def autosql_file ( self ) : <EOL> """<STR_LIT>""" <EOL> fn = pybedtools . BedTool . _tmp ( ) <EOL> AUTOSQL = dedent ( <EOL> """<STR_LIT>""" ) <EOL> fout = open ( fn , '<STR_LIT:w>' ) <EOL> fout . write ( AUTOSQL ) <EOL> fout . close ( ) <EOL> return fn <EOL> class DESeq2Results ( DESeqResults ) : <EOL> __doc__ = _base_doc % dedent ( <EOL> """<STR_LIT>""" ) <EOL> pval_column = '<STR_LIT>' <EOL> lfc_column = '<STR_LIT>' <EOL> mean_column = '<STR_LIT>' <EOL> class LazyDict ( object ) : <EOL> def __init__ ( self , fn_dict , dbfn , index_file , extra = None , cls = DESeqResults , <EOL> modifier = None ) : <EOL> """<STR_LIT>""" <EOL> self . fn_dict = fn_dict <EOL> self . _dict = { } <EOL> self . dbfn = dbfn <EOL> self . index = [ i . strip ( ) for i in open ( index_file ) ] <EOL> if extra is not None : <EOL> self . extra = extra . ix [ self . index ] <EOL> else : <EOL> self . extra = extra <EOL> self . modifier = modifier <EOL> self . _cls = cls <EOL> def __getitem__ ( self , key ) : <EOL> if key not in self . _dict : <EOL> fn = self . fn_dict [ key ] <EOL> obj = self . _cls ( fn , db = self . dbfn ) <EOL> obj . data = obj . data . ix [ self . index ] <EOL> if self . extra is not None : <EOL> obj . data = pandas . merge ( obj . data , self . extra , left_index = True , <EOL> right_index = True ) <EOL> if self . modifier : <EOL> obj = self . modifier ( obj ) <EOL> self . _dict [ key ] = obj <EOL> return self . _dict [ key ] <EOL> def __repr__ ( self ) : <EOL> s = "<STR_LIT>" % ( self . __class__ . __name__ , self . fn_dict . keys ( ) ) <EOL> s += "<STR_LIT>" <EOL> s += repr ( self . _dict ) <EOL> return s <EOL> def keys ( self ) : <EOL> return self . fn_dict . keys ( ) <EOL> def values ( self ) : <EOL> return [ self . _dict [ key ] for key in self . keys ( ) ] <EOL> def items ( self ) : <EOL> return list ( ( key , self . _dict [ key ] ) for key in self . keys ( ) ) </s>
