<s> """<STR_LIT>""" <EOL> import sys , time , re , operator , sqlite3 , warnings <EOL> from array import * <EOL> import itertools <EOL> import random <EOL> import copy <EOL> from bisect import insort , bisect_left , bisect_right <EOL> import Cistrome . Assoc . R as R <EOL> import Cistrome . Assoc . graphics as graphics <EOL> from Cistrome . Assoc . corelib import * <EOL> class CEASError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message = '<STR_LIT>' , value = None ) : <EOL> self . message = message <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return self . message <EOL> class CEASWarning ( UserWarning ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message = '<STR_LIT>' ) : <EOL> self . message = message <EOL> def __str__ ( self ) : <EOL> return self . message <EOL> MYSQL = True <EOL> try : <EOL> import MySQLdb <EOL> except ImportError : <EOL> MYSQL = False <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> standard_chroms = { '<STR_LIT:I>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:M>' : '<STR_LIT>' , '<STR_LIT:X>' : '<STR_LIT>' } <EOL> if array ( '<STR_LIT:H>' , [ <NUM_LIT:1> ] ) . itemsize == <NUM_LIT:2> : <EOL> BYTE2 = '<STR_LIT:H>' <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if array ( '<STR_LIT:I>' , [ <NUM_LIT:1> ] ) . itemsize == <NUM_LIT:4> : <EOL> BYTE4 = '<STR_LIT:I>' <EOL> elif array ( '<STR_LIT:L>' , [ <NUM_LIT:1> ] ) . itemsize == <NUM_LIT:4> : <EOL> BYTE4 = '<STR_LIT:L>' <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if array ( '<STR_LIT:f>' , [ <NUM_LIT:1> ] ) . itemsize == <NUM_LIT:4> : <EOL> FBYTE4 = '<STR_LIT:f>' <EOL> elif array ( '<STR_LIT:d>' , [ <NUM_LIT:1> ] ) . itemsize == <NUM_LIT:4> : <EOL> FBYTE4 = '<STR_LIT:d>' <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> class Bed ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . bed = None <EOL> self . __name__ = None <EOL> self . __columns = [ '<STR_LIT>' , '<STR_LIT:start>' , '<STR_LIT:end>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . __types = [ '<STR_LIT:str>' , '<STR_LIT:int>' , '<STR_LIT:int>' , '<STR_LIT:str>' , '<STR_LIT:float>' , '<STR_LIT:str>' , '<STR_LIT:int>' , '<STR_LIT:int>' , '<STR_LIT:list>' , '<STR_LIT:int>' , '<STR_LIT:list>' , '<STR_LIT:list>' ] <EOL> self . __format_cols = [ '<STR_LIT:%s>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' , '<STR_LIT>' , '<STR_LIT:%s>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:%s>' , '<STR_LIT>' , '<STR_LIT:%s>' , '<STR_LIT:%s>' ] <EOL> self . __list_i = [ i for i in range ( <NUM_LIT:0> , len ( self . __types ) ) if self . __types [ i ] == '<STR_LIT:list>' ] <EOL> self . __chroms = None <EOL> def read ( self , fn = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> f = open ( fn , '<STR_LIT:r>' ) <EOL> self . __name__ = fn <EOL> standard_chroms = { '<STR_LIT:I>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:M>' : '<STR_LIT>' , '<STR_LIT:X>' : '<STR_LIT>' } <EOL> types = self . __types <EOL> type_coversion = { '<STR_LIT:int>' : '<STR_LIT:l>' , '<STR_LIT:float>' : '<STR_LIT:d>' } <EOL> self . bed = { } <EOL> chrom = '<STR_LIT>' <EOL> for line in f . xreadlines ( ) : <EOL> if re . search ( r'<STR_LIT>' , line ) or line == '<STR_LIT>' or line == '<STR_LIT:\n>' or line [ <NUM_LIT:0> ] == '<STR_LIT:#>' : continue <EOL> l = line . strip ( ) . split ( ) <EOL> try : <EOL> chrom = standard_chroms [ l [ <NUM_LIT:0> ] ] <EOL> except KeyError : <EOL> chrom = l [ <NUM_LIT:0> ] <EOL> if not self . bed . has_key ( chrom ) : <EOL> self . bed [ chrom ] = { } <EOL> for i in range ( <NUM_LIT:1> , len ( l ) ) : <EOL> if types [ i ] == '<STR_LIT:int>' : <EOL> self . bed [ chrom ] [ self . __columns [ i ] ] = array ( '<STR_LIT:l>' , [ ] ) <EOL> elif types [ i ] == '<STR_LIT:float>' : <EOL> self . bed [ chrom ] [ self . __columns [ i ] ] = array ( '<STR_LIT:d>' , [ ] ) <EOL> elif types [ i ] == '<STR_LIT:str>' or types [ i ] == '<STR_LIT:list>' : <EOL> self . bed [ chrom ] [ self . __columns [ i ] ] = [ ] <EOL> for i in range ( <NUM_LIT:1> , len ( l ) ) : <EOL> if types [ i ] == '<STR_LIT:int>' : <EOL> element = int ( l [ i ] ) <EOL> elif types [ i ] == '<STR_LIT:float>' : <EOL> element = float ( l [ i ] ) <EOL> elif types [ i ] == '<STR_LIT:str>' : <EOL> element = l [ i ] <EOL> elif types [ i ] == '<STR_LIT:list>' : <EOL> element = [ int ( e ) for e in l [ i ] . rstrip ( '<STR_LIT:U+002C>' ) . split ( '<STR_LIT:U+002C>' ) ] <EOL> self . bed [ chrom ] [ self . __columns [ i ] ] . append ( element ) <EOL> f . close ( ) <EOL> def write ( self , fn = None ) : <EOL> """<STR_LIT>""" <EOL> f = open ( fn , '<STR_LIT:w>' ) <EOL> f . write ( self . __to_bed ( ) ) <EOL> f . close ( ) <EOL> def sort ( self , key = '<STR_LIT:start>' ) : <EOL> """<STR_LIT>""" <EOL> chroms = self . get_chroms ( ) <EOL> for chrom in chroms : <EOL> try : <EOL> key_column = self . bed [ chrom ] [ key ] <EOL> except KeyError , e : <EOL> raise Exception ( '<STR_LIT>' % ( e ) ) <EOL> try : <EOL> sort_order = self . __argsort ( key_column ) <EOL> except TypeError : <EOL> key_column = list ( key_column ) <EOL> sort_order = self . __argsort ( key_column ) <EOL> try : <EOL> columns = self . bed [ chrom ] . keys ( ) <EOL> except AttributeError : <EOL> raise Exception ( '<STR_LIT>' % ( chrom ) ) <EOL> for column in columns : <EOL> if column == '<STR_LIT>' : continue <EOL> if column == '<STR_LIT:start>' or column == '<STR_LIT:end>' or column == '<STR_LIT>' or column == '<STR_LIT>' or column == '<STR_LIT>' : <EOL> self . bed [ chrom ] [ column ] = array ( '<STR_LIT:l>' , [ self . bed [ chrom ] [ column ] [ so ] for so in sort_order ] ) <EOL> elif column == '<STR_LIT>' : <EOL> self . bed [ chrom ] [ column ] = array ( '<STR_LIT:d>' , [ self . bed [ chrom ] [ column ] [ so ] for so in sort_order ] ) <EOL> else : <EOL> self . bed [ chrom ] [ column ] = [ self . bed [ chrom ] [ column ] [ so ] for so in sort_order ] <EOL> def __argsort ( self , L ) : <EOL> """<STR_LIT>""" <EOL> index_element = [ ( ix , L [ ix ] ) for ix in range ( <NUM_LIT:0> , len ( L ) ) ] <EOL> index_element . sort ( key = operator . itemgetter ( <NUM_LIT:1> ) ) <EOL> return [ ix for ix , el in index_element ] <EOL> def __getitem__ ( self , chrom ) : <EOL> """<STR_LIT>""" <EOL> return self . bed [ chrom ] <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . bed . keys ( ) <EOL> def has_key ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return key in self . bed . keys ( ) <EOL> def get_chroms ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> chroms = self . bed . keys ( ) <EOL> except AttributeError : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> return sort_chroms ( chroms ) <EOL> def __to_bed ( self ) : <EOL> """<STR_LIT>""" <EOL> chroms = self . get_chroms ( ) <EOL> bedout = '<STR_LIT>' <EOL> for chrom in chroms : <EOL> columns = self . bed [ chrom ] . keys ( ) <EOL> len_columns = len ( columns ) <EOL> formated = '<STR_LIT:\t>' . join ( self . __format_cols [ : len_columns + <NUM_LIT:1> ] ) <EOL> for i in xrange ( <NUM_LIT:0> , len ( self . bed [ chrom ] [ self . __columns [ <NUM_LIT:1> ] ] ) ) : <EOL> out = [ chrom ] + [ self . bed [ chrom ] [ column ] [ i ] for column in self . __columns [ <NUM_LIT:1> : len_columns + <NUM_LIT:1> ] ] <EOL> try : <EOL> for j in self . __list_i : out [ j ] = str ( out [ j ] ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] + '<STR_LIT:U+002C>' <EOL> except IndexError : <EOL> pass <EOL> bedout += formated % tuple ( out ) + '<STR_LIT:\n>' <EOL> return bedout <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __to_bed ( ) <EOL> def add_line ( self , chrom , l ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> for i in range ( <NUM_LIT:0> , len ( l ) ) : <EOL> if self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:int>' : <EOL> element = int ( l [ i ] ) <EOL> elif self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:float>' : <EOL> element = float ( l [ i ] ) <EOL> elif self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:str>' : <EOL> element = l [ i ] <EOL> elif self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:list>' : <EOL> element = [ int ( e ) for e in l [ i ] . rstrip ( '<STR_LIT:U+002C>' ) . split ( '<STR_LIT:U+002C>' ) ] <EOL> self . bed [ chrom ] [ self . __columns [ i + <NUM_LIT:1> ] ] . append ( element ) <EOL> except TypeError : <EOL> self . bed = { } <EOL> self . bed [ chrom ] = { } <EOL> for i in range ( <NUM_LIT:0> , len ( l ) ) : <EOL> if self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:int>' : <EOL> self . bed [ chrom ] [ self . __columns [ i + <NUM_LIT:1> ] ] = array ( '<STR_LIT:l>' , [ int ( l [ i ] ) ] ) <EOL> elif self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:float>' : <EOL> self . bed [ chrom ] [ self . __columns [ i + <NUM_LIT:1> ] ] = array ( '<STR_LIT:d>' , [ float ( l [ i ] ) ] ) <EOL> elif self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:str>' or self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:list>' : <EOL> self . bed [ chrom ] [ self . __columns [ i + <NUM_LIT:1> ] ] = [ l [ i ] ] <EOL> elif self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:list>' : <EOL> self . bed [ chrom ] [ self . __columns [ i + <NUM_LIT:1> ] ] = [ [ int ( e ) for e in l [ i ] . rstrip ( '<STR_LIT:U+002C>' ) . split ( '<STR_LIT:U+002C>' ) ] ] <EOL> except KeyError : <EOL> self . bed [ chrom ] = { } <EOL> for i in range ( <NUM_LIT:0> , len ( l ) ) : <EOL> if self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:int>' : <EOL> self . bed [ chrom ] [ self . __columns [ i + <NUM_LIT:1> ] ] = array ( '<STR_LIT:l>' , [ int ( l [ i ] ) ] ) <EOL> elif self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:float>' : <EOL> self . bed [ chrom ] [ self . __columns [ i + <NUM_LIT:1> ] ] = array ( '<STR_LIT:d>' , [ float ( l [ i ] ) ] ) <EOL> elif self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:str>' or self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:list>' : <EOL> self . bed [ chrom ] [ self . __columns [ i + <NUM_LIT:1> ] ] = [ l [ i ] ] <EOL> elif self . __types [ i + <NUM_LIT:1> ] == '<STR_LIT:list>' : <EOL> self . bed [ chrom ] [ self . __columns [ i + <NUM_LIT:1> ] ] = [ [ int ( e ) for e in l [ i ] . rstrip ( '<STR_LIT:U+002C>' ) . split ( '<STR_LIT:U+002C>' ) ] ] <EOL> def filter_by_score ( self , lowercutoff , uppercutoff = None ) : <EOL> """<STR_LIT>""" <EOL> lookup = { } <EOL> for i , x in enumerate ( self . __columns ) : lookup [ x ] = i <EOL> filtered = Bed ( ) <EOL> chroms = self . get_chroms ( ) <EOL> ix = [ ] <EOL> for chrom in chroms : <EOL> for i in xrange ( len ( self . bed [ chrom ] [ '<STR_LIT>' ] ) ) : <EOL> if uppercutoff : <EOL> if lowercutoff <= self . bed [ chrom ] [ '<STR_LIT>' ] [ i ] >= uppercutoff : ix . append ( i ) <EOL> else : <EOL> if lowercutoff <= self . bed [ chrom ] [ '<STR_LIT>' ] [ i ] : ix . append ( i ) <EOL> filtered . bed [ chrom ] = { } <EOL> columns = self . bed [ chrom ] . keys ( ) <EOL> for column in columns : <EOL> if column == '<STR_LIT:start>' or column == '<STR_LIT:end>' or column == '<STR_LIT>' or column == '<STR_LIT>' or column == '<STR_LIT>' : <EOL> filtered . bed [ chrom ] [ column ] = array ( '<STR_LIT:l>' , [ self . bed [ chrom ] [ column ] [ j ] for j in ix ] ) <EOL> elif column == '<STR_LIT>' : <EOL> filtered . bed [ chrom ] [ column ] = array ( '<STR_LIT:d>' , [ self . bed [ chrom ] [ column ] [ j ] for j in ix ] ) <EOL> else : <EOL> filtered . bed [ chrom ] [ column ] = [ self . bed [ chrom ] [ column ] [ j ] for j in ix ] <EOL> return filtered <EOL> def size ( self , chrom = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if chrom : <EOL> try : <EOL> n = len ( self [ chrom ] [ '<STR_LIT:start>' ] ) <EOL> except KeyError : <EOL> n = <NUM_LIT:0> <EOL> else : <EOL> chroms = self . get_chroms ( ) <EOL> n = { } <EOL> a = <NUM_LIT:0> <EOL> for chrom in chroms : <EOL> try : <EOL> n [ chrom ] = len ( self [ chrom ] [ '<STR_LIT:start>' ] ) <EOL> except KeyError : <EOL> n [ chrom ] = <NUM_LIT:0> <EOL> a += n [ chrom ] <EOL> n [ '<STR_LIT>' ] = a <EOL> return n <EOL> class GeneTable ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . table = None <EOL> self . __name__ = name <EOL> self . columns = None <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . table = { } <EOL> self . __name__ = '<STR_LIT>' <EOL> self . columns = ( ) <EOL> def read ( self , Host = None , User = None , Db = None , annotation = None , columns = ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , which = '<STR_LIT>' , where = ( ) ) : <EOL> """<STR_LIT>""" <EOL> if Host and User : <EOL> try : <EOL> dbconnect = MySQLdb . connect ( host = Host , user = User ) <EOL> cursor = dbconnect . cursor ( ) <EOL> cursor . execute ( '<STR_LIT>' + Db ) <EOL> except NameError : <EOL> dbconnect = sqlite3 . connect ( Db ) <EOL> cursor . execute ( ) <EOL> else : <EOL> dbconnect = sqlite3 . connect ( Db ) <EOL> cursor = dbconnect . cursor ( ) <EOL> if which and ( which not in columns ) : columns = columns + ( which , ) <EOL> self . columns = columns <EOL> schema = { } <EOL> for i , x in enumerate ( columns ) : schema [ x ] = i <EOL> if which and where : <EOL> num_where = len ( where ) <EOL> if num_where > <NUM_LIT> : <EOL> table = [ ] <EOL> sqlcmd = "<STR_LIT>" + "<STR_LIT:U+002CU+0020>" . join ( columns ) + "<STR_LIT>" + annotation + "<STR_LIT>" <EOL> for j in range ( <NUM_LIT:0> , num_where / <NUM_LIT> + <NUM_LIT:1> ) : <EOL> sqlcmd = "<STR_LIT>" + "<STR_LIT:U+002CU+0020>" . join ( columns ) + "<STR_LIT>" + annotation + "<STR_LIT>" <EOL> it = iter ( where [ j * <NUM_LIT> : ( j + <NUM_LIT:1> ) * <NUM_LIT> ] ) <EOL> for i in it : <EOL> if type ( i ) . __name__ == '<STR_LIT:str>' : <EOL> sqlcmd += which + "<STR_LIT>" + i + "<STR_LIT:'>" + "<STR_LIT>" <EOL> elif type ( i ) . __name__ == '<STR_LIT:list>' or type ( i ) . __name__ == '<STR_LIT>' or type ( i ) . __name__ == '<STR_LIT>' : <EOL> sqlcmd += which + "<STR_LIT:=>" + str ( i ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] + "<STR_LIT>" <EOL> else : sqlcmd += which + "<STR_LIT:=>" + str ( i ) + "<STR_LIT>" <EOL> sqlcmd = sqlcmd [ : - <NUM_LIT:4> ] <EOL> cursor . execute ( sqlcmd ) <EOL> table += cursor . fetchall ( ) <EOL> else : <EOL> sqlcmd = "<STR_LIT>" + "<STR_LIT:U+002CU+0020>" . join ( columns ) + "<STR_LIT>" + annotation + "<STR_LIT>" <EOL> it = iter ( where ) <EOL> for i in it : <EOL> if type ( i ) . __name__ == '<STR_LIT:str>' : <EOL> sqlcmd += which + "<STR_LIT>" + i + "<STR_LIT:'>" + "<STR_LIT>" <EOL> elif type ( i ) . __name__ == '<STR_LIT:list>' or type ( i ) . __name__ == '<STR_LIT>' or type ( i ) . __name__ == '<STR_LIT>' : <EOL> sqlcmd += which + "<STR_LIT:=>" + str ( i ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] + "<STR_LIT>" <EOL> else : sqlcmd += which + "<STR_LIT:=>" + str ( i ) + "<STR_LIT>" <EOL> sqlcmd = sqlcmd [ : - <NUM_LIT:4> ] <EOL> cursor . execute ( sqlcmd ) <EOL> table = cursor . fetchall ( ) <EOL> else : <EOL> sqlcmd = "<STR_LIT>" + "<STR_LIT:U+002CU+0020>" . join ( columns ) + "<STR_LIT>" + annotation <EOL> cursor . execute ( sqlcmd ) <EOL> table = cursor . fetchall ( ) <EOL> parser_commasep_array = lambda strarray : array ( '<STR_LIT:l>' , [ int ( k ) for k in strarray . tostring ( ) . rstrip ( '<STR_LIT:U+002C>' ) . split ( '<STR_LIT:U+002C>' ) ] ) <EOL> parser_commasep_str = lambda strlist : array ( '<STR_LIT:l>' , [ int ( k ) for k in strlist ] ) <EOL> try : <EOL> i = schema [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> chroms = set ( map ( operator . itemgetter ( i ) , table ) ) <EOL> chroms = list ( chroms ) <EOL> chroms . sort ( ) <EOL> self . table = { } <EOL> for chrom in chroms : self . table [ str ( chrom ) ] = { } <EOL> for tb in table : <EOL> for column in columns : <EOL> if column == '<STR_LIT>' : continue <EOL> value = tb [ schema [ column ] ] <EOL> ctype = type ( tb [ schema [ column ] ] ) . __name__ <EOL> if ctype == '<STR_LIT>' : <EOL> value = parser_commasep_array ( value ) <EOL> elif ctype == '<STR_LIT:str>' or ctype == '<STR_LIT>' : <EOL> try : <EOL> if value [ - <NUM_LIT:1> ] == '<STR_LIT:U+002C>' : <EOL> try : <EOL> temp = value . rstrip ( '<STR_LIT:U+002C>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> value = parser_commasep_str ( temp ) <EOL> except ValueError : <EOL> pass <EOL> else : value = str ( value ) <EOL> except IndexError : <EOL> pass <EOL> try : <EOL> self . table [ tb [ schema [ '<STR_LIT>' ] ] ] [ column ] . append ( value ) <EOL> except KeyError : <EOL> if ctype == '<STR_LIT:int>' or ctype == '<STR_LIT>' : self . table [ tb [ schema [ '<STR_LIT>' ] ] ] [ column ] = array ( '<STR_LIT:l>' , [ value ] ) <EOL> elif ctype == '<STR_LIT:float>' : self . table [ tb [ schema [ '<STR_LIT>' ] ] ] [ column ] = array ( '<STR_LIT:d>' , [ value ] ) <EOL> else : self . table [ tb [ schema [ '<STR_LIT>' ] ] ] [ column ] = [ value ] <EOL> cursor . close ( ) <EOL> dbconnect . close ( ) <EOL> def __getitem__ ( self , chrom ) : <EOL> """<STR_LIT>""" <EOL> return self . table [ chrom ] <EOL> def __setitem__ ( self , chrom , value ) : <EOL> self . table [ chrom ] = value <EOL> def get_genelengths ( self ) : <EOL> """<STR_LIT>""" <EOL> chroms = self . get_chroms ( ) <EOL> lengths = { } <EOL> total_min_gene = <NUM_LIT:0> <EOL> total_max_gene = <NUM_LIT:0> <EOL> total_mean_gene = <NUM_LIT:0> <EOL> total_min_exon = <NUM_LIT:0> <EOL> total_max_exon = <NUM_LIT:0> <EOL> total_mean_exon = <NUM_LIT:0> <EOL> total_min_intron = <NUM_LIT:0> <EOL> total_max_intron = <NUM_LIT:0> <EOL> total_mean_intron = <NUM_LIT:0> <EOL> total_gene_length = <NUM_LIT:0> <EOL> total_exon_length = <NUM_LIT:0> <EOL> total_intron_length = <NUM_LIT:0> <EOL> total_gene_num = <NUM_LIT:0> <EOL> for chrom in chroms : <EOL> gene_length = [ end - start for start , end in zip ( self [ chrom ] [ '<STR_LIT>' ] , self [ chrom ] [ '<STR_LIT>' ] ) ] <EOL> exon_length = [ ] <EOL> intron_length = [ ] <EOL> current_gene_num = len ( self [ chrom ] [ '<STR_LIT>' ] ) <EOL> for i in xrange ( <NUM_LIT:0> , current_gene_num ) : <EOL> exons = zip ( self [ chrom ] [ '<STR_LIT>' ] [ i ] , self [ chrom ] [ '<STR_LIT>' ] [ i ] ) <EOL> try : <EOL> exon_length . append ( sum ( [ end - start for start , end in exons ] ) ) <EOL> intron_length . append ( gene_length [ i ] - exon_length [ i ] ) <EOL> except : <EOL> pass <EOL> current_gene_length = sum ( gene_length ) <EOL> current_exon_length = sum ( exon_length ) <EOL> current_intron_length = current_gene_length - current_exon_length <EOL> current_min_gene = min ( gene_length ) <EOL> current_max_gene = min ( gene_length ) <EOL> current_min_exon = min ( exon_length ) <EOL> current_max_exon = max ( exon_length ) <EOL> current_min_intron = min ( intron_length ) <EOL> current_max_intron = max ( intron_length ) <EOL> current_mean_gene = <NUM_LIT:1.0> * current_gene_length / current_gene_num <EOL> current_mean_exon = <NUM_LIT:1.0> * current_exon_length / current_gene_num <EOL> current_mean_intron = <NUM_LIT:1.0> * current_intron_length / current_gene_num <EOL> lengths [ chrom ] = { '<STR_LIT>' : [ current_min_gene , current_mean_gene , current_max_gene ] , '<STR_LIT>' : [ current_min_exon , current_mean_exon , current_max_exon ] , '<STR_LIT>' : [ current_min_intron , current_mean_intron , current_max_intron ] } <EOL> if total_min_gene == <NUM_LIT:0> : total_min_gene = current_min_gene <EOL> else : total_min_gene = min ( total_min_gene , current_min_gene ) <EOL> if total_min_exon == <NUM_LIT:0> : total_min_exon = current_min_exon <EOL> else : total_min_exon = min ( total_min_exon , current_min_exon ) <EOL> if total_min_intron == <NUM_LIT:0> : total_min_intron = current_min_intron <EOL> else : total_min_intron = min ( total_min_intron , current_min_intron ) <EOL> total_max_gene = max ( total_max_gene , current_max_gene ) <EOL> total_max_exon = max ( total_max_exon , current_max_exon ) <EOL> total_max_intron = max ( total_max_intron , current_max_intron ) <EOL> total_gene_length += current_gene_length <EOL> total_exon_length += current_exon_length <EOL> total_intron_length += current_intron_length <EOL> total_gene_num += current_gene_num <EOL> total_mean_gene = <NUM_LIT:1.0> * total_gene_length / total_gene_num <EOL> total_mean_exon = <NUM_LIT:1.0> * total_exon_length / total_gene_num <EOL> total_mean_intron = <NUM_LIT:1.0> * total_intron_length / total_gene_num <EOL> lengths [ '<STR_LIT>' ] = { '<STR_LIT>' : [ total_min_gene , total_mean_gene , total_max_gene ] , '<STR_LIT>' : [ total_min_exon , total_mean_exon , total_max_exon ] , '<STR_LIT>' : [ total_min_intron , total_mean_intron , total_max_intron ] } <EOL> return lengths <EOL> def remove ( self , chrom ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del self . table [ chrom ] <EOL> except KeyError : <EOL> pass <EOL> def get_gene_lens ( self , chrom = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> chroms = self . get_chroms ( ) <EOL> lengths = [ ] <EOL> if chrom != '<STR_LIT>' and chrom in chroms : <EOL> lengths = map ( lambda start , end : end - start , self [ chrom ] [ '<STR_LIT>' ] , self [ chrom ] [ '<STR_LIT>' ] ) <EOL> return lengths <EOL> elif chrom == '<STR_LIT>' : <EOL> for chrom in chroms : <EOL> lengths += map ( lambda start , end : end - start , self [ chrom ] [ '<STR_LIT>' ] , self [ chrom ] [ '<STR_LIT>' ] ) <EOL> return lengths <EOL> else : <EOL> return lengths <EOL> def turn2dict ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . table : return { } <EOL> chroms = self . get_chroms ( ) <EOL> dict = { } <EOL> for chrom in chroms : <EOL> for i in xrange ( len ( self [ chrom ] [ '<STR_LIT:name>' ] ) ) : <EOL> if not dict . has_key ( self [ chrom ] [ '<STR_LIT:name>' ] [ i ] ) : <EOL> dict [ self [ chrom ] [ '<STR_LIT:name>' ] [ i ] ] = [ chrom ] <EOL> for column in self . columns [ <NUM_LIT:2> : ] : <EOL> dict [ self [ chrom ] [ '<STR_LIT:name>' ] [ i ] ] . append ( self [ chrom ] [ column ] [ i ] ) <EOL> return dict <EOL> def savedb ( self , Db = None , annotation = '<STR_LIT>' , overwrite = False ) : <EOL> """<STR_LIT>""" <EOL> dbconnect = sqlite3 . connect ( Db ) <EOL> cursor = dbconnect . cursor ( ) <EOL> other_columns = [ column for column in self . columns if column != '<STR_LIT>' ] <EOL> columns = [ '<STR_LIT>' ] <EOL> columns . extend ( other_columns ) <EOL> sqlcmd = """<STR_LIT>""" % ( annotation ) + '<STR_LIT:U+002C>' . join ( columns ) + """<STR_LIT:)>""" <EOL> try : <EOL> cursor . execute ( sqlcmd ) <EOL> except sqlite3 . OperationalError , e : <EOL> if overwrite : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> cursor . execute ( """<STR_LIT>""" % ( annotation ) ) <EOL> cursor . execute ( sqlcmd ) <EOL> else : <EOL> raise sqlite3 . OperationalError ( e ) <EOL> num_columns = len ( columns ) <EOL> chroms = self . table . keys ( ) <EOL> chroms . sort ( ) <EOL> for chrom in chroms : <EOL> for i in xrange ( <NUM_LIT:0> , len ( self . table [ chrom ] [ other_columns [ <NUM_LIT:0> ] ] ) ) : <EOL> sqlcmd = """<STR_LIT>""" % ( annotation ) + '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT:?>' for ix in range ( <NUM_LIT:0> , num_columns ) ] ) + """<STR_LIT:)>""" <EOL> export_data = [ chrom ] <EOL> for column in other_columns : <EOL> element = self . table [ chrom ] [ column ] [ i ] <EOL> if type ( element ) . __name__ == '<STR_LIT>' : element = str ( element . tolist ( ) ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] + '<STR_LIT:U+002C>' <EOL> export_data . append ( element ) <EOL> export_data = tuple ( export_data ) <EOL> cursor . execute ( sqlcmd , export_data ) <EOL> dbconnect . commit ( ) <EOL> cursor . close ( ) <EOL> dbconnect . close ( ) <EOL> def droptable ( self , Db = None , table = None ) : <EOL> """<STR_LIT>""" <EOL> dbconnect = sqlite3 . connect ( Db ) <EOL> cursor = dbconnect . cursor ( ) <EOL> sqlcmd = """<STR_LIT>""" % ( table ) <EOL> cursor . execute ( sqlcmd ) <EOL> dbconnect . commit ( ) <EOL> cursor . close ( ) <EOL> dbconnect . close ( ) <EOL> def sort ( self , key = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> chroms = self . table . keys ( ) <EOL> for chrom in chroms : <EOL> try : <EOL> key_column = self . table [ chrom ] [ key ] <EOL> except KeyError , e : <EOL> raise Exception ( '<STR_LIT>' % ( e ) ) <EOL> try : <EOL> sort_order = self . __argsort ( key_column ) <EOL> except TypeError : <EOL> key_column = list ( key_column ) <EOL> sort_order = self . __argsort ( key_column ) <EOL> for column in self . columns : <EOL> if column == '<STR_LIT>' : continue <EOL> if type ( self . table [ chrom ] [ column ] ) . __name__ == '<STR_LIT>' : <EOL> self . table [ chrom ] [ column ] = array ( self . table [ chrom ] [ column ] . typecode , [ self . table [ chrom ] [ column ] [ so ] for so in sort_order ] ) <EOL> else : <EOL> self . table [ chrom ] [ column ] = [ self . table [ chrom ] [ column ] [ so ] for so in sort_order ] <EOL> def __argsort ( self , L ) : <EOL> """<STR_LIT>""" <EOL> index_element = [ ( ix , L [ ix ] ) for ix in range ( <NUM_LIT:0> , len ( L ) ) ] <EOL> index_element . sort ( key = operator . itemgetter ( <NUM_LIT:1> ) ) <EOL> return [ ix for ix , el in index_element ] <EOL> def get_chroms ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> chroms = self . table . keys ( ) <EOL> except AttributeError : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> return sort_chroms ( chroms ) <EOL> def get_gene_num ( self ) : <EOL> """<STR_LIT>""" <EOL> gene_num = { } <EOL> chroms = self . get_chroms ( ) <EOL> for chrom in chroms : <EOL> gene_num [ chrom ] = len ( self [ chrom ] [ '<STR_LIT>' ] ) <EOL> gene_num [ '<STR_LIT>' ] = sum ( [ gene_num [ chrom ] for chrom in chroms ] ) <EOL> return gene_num <EOL> def get_exon_intron_lens ( self , chrom = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in self . columns or '<STR_LIT>' not in self . columns : <EOL> return [ ] , [ ] <EOL> if chrom == '<STR_LIT>' : <EOL> elengths = [ ] <EOL> ilengths = [ ] <EOL> for chrom in self . get_chroms ( ) : <EOL> for es , ee in itertools . izip ( self [ chrom ] [ '<STR_LIT>' ] , self [ chrom ] [ '<STR_LIT>' ] ) : <EOL> elengths . extend ( array_adder ( ee , map ( lambda x : - <NUM_LIT:1> * x , es ) ) [ <NUM_LIT:0> ] ) <EOL> ilengths . extend ( array_adder ( es [ <NUM_LIT:1> : ] , map ( lambda x : - <NUM_LIT:1> * x , ee [ : - <NUM_LIT:1> ] ) ) [ <NUM_LIT:0> ] ) <EOL> else : <EOL> if chrom not in self . get_chroms ( ) : <EOL> return [ ] , [ ] <EOL> elengths = [ ] <EOL> ilengths = [ ] <EOL> for es , ee in itertools . izip ( self [ chrom ] [ '<STR_LIT>' ] , self [ chrom ] [ '<STR_LIT>' ] ) : <EOL> elengths . extend ( array_adder ( ee , map ( lambda x : - <NUM_LIT:1> * x , es ) ) [ <NUM_LIT:0> ] ) <EOL> ilengths . extend ( array_adder ( es [ <NUM_LIT:1> : ] , map ( lambda x : - <NUM_LIT:1> * x , ee [ : - <NUM_LIT:1> ] ) ) [ <NUM_LIT:0> ] ) <EOL> elengths . sort ( ) <EOL> ilengths . sort ( ) <EOL> return elengths , ilengths <EOL> def get_cat_exon_intron_lens ( self , chrom = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in self . columns or '<STR_LIT>' not in self . columns : <EOL> return [ ] , [ ] <EOL> if chrom == '<STR_LIT>' : <EOL> cate = [ ] <EOL> cati = [ ] <EOL> chroms = self . get_chroms ( ) <EOL> for chrom in chroms : <EOL> cate += map ( lambda estarts , eends : sum ( map ( lambda s , e : e - s , estarts , eends ) ) , self [ chrom ] [ '<STR_LIT>' ] , self [ chrom ] [ '<STR_LIT>' ] ) <EOL> cati += map ( lambda estarts , eends : sum ( map ( lambda s , e : s - e , estarts [ <NUM_LIT:1> : ] , eends [ : - <NUM_LIT:1> ] ) ) , self [ chrom ] [ '<STR_LIT>' ] , self [ chrom ] [ '<STR_LIT>' ] ) <EOL> return cate , cati <EOL> else : <EOL> chroms = self . get_chroms ( ) <EOL> if chrom in chroms : <EOL> cate = map ( lambda estarts , eends : sum ( map ( lambda s , e : e - s , estarts , eends ) ) , self [ chrom ] [ '<STR_LIT>' ] , self [ chrom ] [ '<STR_LIT>' ] ) <EOL> cati = map ( lambda estarts , eends : sum ( map ( lambda s , e : s - e , estarts [ <NUM_LIT:1> : ] , eends [ : - <NUM_LIT:1> ] ) ) , self [ chrom ] [ '<STR_LIT>' ] , self [ chrom ] [ '<STR_LIT>' ] ) <EOL> else : <EOL> cate = [ ] <EOL> cati = [ ] <EOL> return cate , cati <EOL> class Wig : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . wig = { } <EOL> self . description = '<STR_LIT>' <EOL> def read ( self , fnwig = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> standard_chroms = { '<STR_LIT:I>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:M>' : '<STR_LIT>' , '<STR_LIT:X>' : '<STR_LIT>' } <EOL> fwig = open ( fnwig , '<STR_LIT:r>' ) <EOL> chrom = '<STR_LIT>' <EOL> wig = { } <EOL> for line in fwig . xreadlines ( ) : <EOL> if re . search ( r'<STR_LIT>' , line ) : <EOL> try : <EOL> description = re . search ( r'<STR_LIT>' , line ) . group ( <NUM_LIT:1> ) <EOL> if not self . description : self . description = description <EOL> except AttributeError : <EOL> pass <EOL> continue <EOL> if re . search ( r'<STR_LIT>' , line ) : <EOL> chrom = re . search ( r'<STR_LIT>' , line ) . group ( <NUM_LIT:1> ) <EOL> try : <EOL> chrom = standard_chroms [ chrom ] <EOL> except KeyError : <EOL> pass <EOL> wig [ chrom ] = [ array ( '<STR_LIT:l>' , [ ] ) , array ( '<STR_LIT:d>' , [ ] ) ] <EOL> continue <EOL> l = line . strip ( ) . split ( ) <EOL> wig [ chrom ] [ <NUM_LIT:0> ] . append ( int ( l [ <NUM_LIT:0> ] ) ) <EOL> wig [ chrom ] [ <NUM_LIT:1> ] . append ( float ( l [ <NUM_LIT:1> ] ) ) <EOL> fwig . close ( ) <EOL> self . wig = wig <EOL> def write ( self , fnwig , span = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> fwig = open ( fnwig , '<STR_LIT:w>' ) <EOL> chroms = self . get_chroms ( ) <EOL> for chrom in chroms : <EOL> fwig . write ( '<STR_LIT>' % ( chrom , span ) ) <EOL> for i in xrange ( <NUM_LIT:0> , len ( self . wig [ chrom ] [ <NUM_LIT:0> ] ) ) : <EOL> fwig . write ( "<STR_LIT>" % ( self . wig [ chrom ] [ <NUM_LIT:0> ] [ i ] , self . wig [ chrom ] [ <NUM_LIT:1> ] [ i ] ) ) <EOL> fwig . close ( ) <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . wig [ key ] <EOL> except TypeError : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> except KeyError : <EOL> chroms = self . wig . keys ( ) <EOL> return_wig = { } <EOL> for chrom in chroms : <EOL> if key == '<STR_LIT>' : <EOL> return_wig [ chrom ] = self . wig [ chrom ] [ <NUM_LIT:0> ] <EOL> elif key == '<STR_LIT:value>' : <EOL> return_wig [ chrom ] = self . wig [ chrom ] [ <NUM_LIT:1> ] <EOL> else : <EOL> raise KeyError , key <EOL> return return_wig <EOL> def __setitem__ ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> if len ( value ) != <NUM_LIT:2> or type ( value [ <NUM_LIT:0> ] ) != array or type ( value [ <NUM_LIT:1> ] ) != array : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . wig [ key ] = value <EOL> def get_chroms ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> chroms = self . wig . keys ( ) <EOL> except AttributeError : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> return sort_chroms ( chroms ) <EOL> def get_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __name__ <EOL> def set_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . __name__ = name <EOL> def add_line ( self , chrom , l ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . wig [ chrom ] [ <NUM_LIT:0> ] . append ( int ( l [ <NUM_LIT:0> ] ) ) <EOL> self . wig [ chrom ] [ <NUM_LIT:1> ] . append ( float ( l [ <NUM_LIT:1> ] ) ) <EOL> except TypeError : <EOL> self . wig = { } <EOL> self . wig [ chrom ] = [ array ( '<STR_LIT:l>' , [ ] ) , array ( '<STR_LIT:d>' , [ ] ) ] <EOL> self . wig [ chrom ] [ <NUM_LIT:0> ] . append ( int ( l [ <NUM_LIT:0> ] ) ) <EOL> self . wig [ chrom ] [ <NUM_LIT:1> ] . append ( float ( l [ <NUM_LIT:1> ] ) ) <EOL> except KeyError : <EOL> self . wig [ chrom ] = [ array ( '<STR_LIT:l>' , [ ] ) , array ( '<STR_LIT:d>' , [ ] ) ] <EOL> self . wig [ chrom ] [ <NUM_LIT:0> ] . append ( int ( l [ <NUM_LIT:0> ] ) ) <EOL> self . wig [ chrom ] [ <NUM_LIT:1> ] . append ( float ( l [ <NUM_LIT:1> ] ) ) <EOL> def length ( self , chrom = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if chrom : <EOL> try : <EOL> return len ( self . wig [ chrom ] [ <NUM_LIT:0> ] ) <EOL> except KeyError : <EOL> return <NUM_LIT:0> <EOL> length = <NUM_LIT:0> <EOL> for chrom in self . get_chroms ( ) : <EOL> length += len ( self . wig [ chrom ] [ <NUM_LIT:0> ] ) <EOL> return length <EOL> def chrom_length ( self , chrom ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if len ( self . wig [ chrom ] [ <NUM_LIT:0> ] ) == <NUM_LIT:0> or len ( self . wig [ chrom ] [ <NUM_LIT:0> ] ) == <NUM_LIT:1> : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return self . wig [ chrom ] [ <NUM_LIT:0> ] [ - <NUM_LIT:1> ] - self . wig [ chrom ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> except KeyError : <EOL> return <NUM_LIT:0> <EOL> def __add__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> selfchroms = self . get_chroms ( ) <EOL> otherchroms = other . get_chroms ( ) <EOL> chroms = list ( set ( selfchroms ) . union ( otherchroms ) ) <EOL> chroms . sort ( ) <EOL> addwig = Wig ( ) <EOL> for chrom in chroms : <EOL> if chrom in selfchroms and chrom not in otherchroms : <EOL> addwig . wig [ chrom ] = self . wig [ chrom ] [ : ] <EOL> continue <EOL> elif chrom not in selfchroms and chrom in otherchroms : <EOL> addwig . wig [ chrom ] = other . wig [ chrom ] [ : ] <EOL> continue <EOL> END1 = False <EOL> END2 = False <EOL> p = <NUM_LIT:0> <EOL> m = <NUM_LIT:0> <EOL> selflen = len ( self . wig [ chrom ] [ <NUM_LIT:0> ] ) <EOL> otherlen = len ( other . wig [ chrom ] [ <NUM_LIT:0> ] ) <EOL> if selflen == <NUM_LIT:0> and otherlen == <NUM_LIT:0> : <EOL> END1 == True <EOL> END2 == True <EOL> addwig . wig [ chrom ] = [ array ( '<STR_LIT:l>' , [ ] ) , array ( '<STR_LIT:d>' , [ ] ) ] <EOL> while END1 != True or END2 != True : <EOL> if END1 != True and END2 != True : <EOL> if self . wig [ chrom ] [ <NUM_LIT:0> ] [ p ] == other . wig [ chrom ] [ <NUM_LIT:0> ] [ m ] : <EOL> addwig . add_line ( chrom , [ self . wig [ chrom ] [ <NUM_LIT:0> ] [ p ] , self . wig [ chrom ] [ <NUM_LIT:1> ] [ p ] + other . wig [ chrom ] [ <NUM_LIT:1> ] [ m ] ] ) <EOL> p += <NUM_LIT:1> <EOL> m += <NUM_LIT:1> <EOL> elif self . wig [ chrom ] [ <NUM_LIT:0> ] [ p ] < other . wig [ chrom ] [ <NUM_LIT:0> ] [ m ] : <EOL> addwig . add_line ( chrom , [ self . wig [ chrom ] [ <NUM_LIT:0> ] [ p ] , self . wig [ chrom ] [ <NUM_LIT:1> ] [ p ] ] ) <EOL> p += <NUM_LIT:1> <EOL> else : <EOL> addwig . add_line ( chrom , [ other . wig [ chrom ] [ <NUM_LIT:0> ] [ m ] , other . wig [ chrom ] [ <NUM_LIT:1> ] [ m ] ] ) <EOL> m += <NUM_LIT:1> <EOL> elif END1 != True and END2 == True : <EOL> addwig . add_line ( chrom , [ self . wig [ chrom ] [ <NUM_LIT:0> ] [ p ] , self . wig [ chrom ] [ <NUM_LIT:1> ] [ p ] ] ) <EOL> p += <NUM_LIT:1> <EOL> elif END1 == True and END2 != True : <EOL> addwig . add_line ( chrom , [ other . wig [ chrom ] [ <NUM_LIT:0> ] [ m ] , other . wig [ chrom ] [ <NUM_LIT:1> ] [ m ] ] ) <EOL> m += <NUM_LIT:1> <EOL> if p == selflen : END1 = True <EOL> if m == otherlen : END2 = True <EOL> return addwig <EOL> def max ( self , chrom = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if chrom : <EOL> try : <EOL> return max ( self [ chrom ] [ <NUM_LIT:1> ] ) <EOL> except ( KeyError , IndexError ) : <EOL> return None <EOL> else : <EOL> mx = None <EOL> for chrom in self . get_chroms ( ) : <EOL> if mx == None : <EOL> mx = max ( self [ chrom ] [ <NUM_LIT:1> ] ) <EOL> else : <EOL> mx = max ( mx , max ( self [ chrom ] [ <NUM_LIT:1> ] ) ) <EOL> return mx <EOL> def min ( self , chrom = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if chrom : <EOL> try : <EOL> return min ( self [ chrom ] [ <NUM_LIT:1> ] ) <EOL> except ( KeyError , IndexError ) : <EOL> return None <EOL> else : <EOL> mn = None <EOL> for chrom in self . get_chroms ( ) : <EOL> if mn == None : <EOL> mn = min ( self [ chrom ] [ <NUM_LIT:1> ] ) <EOL> else : <EOL> mn = min ( mn , min ( self [ chrom ] [ <NUM_LIT:1> ] ) ) <EOL> return mn <EOL> def pseudomedian ( self , chrom = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if chrom : <EOL> meds = [ ] <EOL> for chr in self . get_chroms ( ) : <EOL> meds . append ( median ( self [ chrom ] [ <NUM_LIT:1> ] ) ) <EOL> return median ( meds ) <EOL> else : <EOL> try : <EOL> return median ( self [ chrom ] [ <NUM_LIT:1> ] ) <EOL> except KeyError : <EOL> return None <EOL> def mean ( self , chrom = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if chrom : <EOL> n = self . length ( chrom = chrom ) <EOL> try : <EOL> return <NUM_LIT:1.0> * sum ( self [ chrom ] [ <NUM_LIT:1> ] ) / n <EOL> except ( KeyError , IndexError , ZeroDivisionError ) : <EOL> return None <EOL> else : <EOL> cumsum = <NUM_LIT:0> <EOL> cumcnt = <NUM_LIT:0> <EOL> for chrom in self . get_chroms ( ) : <EOL> try : <EOL> cumsum += sum ( self [ chrom ] [ <NUM_LIT:1> ] ) <EOL> cumcnt += self . length ( chrom = chrom ) <EOL> except ( KeyError , IndexError ) : <EOL> pass <EOL> try : <EOL> return <NUM_LIT:1.0> * cumsum / cumcnt <EOL> except ZeroDivisionError : <EOL> return None <EOL> def histogram ( self , breaks , chrom = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if type ( breaks ) == list : <EOL> histogram = { '<STR_LIT>' : breaks [ : - <NUM_LIT:1> ] } <EOL> if chrom : <EOL> try : <EOL> histogram [ '<STR_LIT:count>' ] = bin ( self [ chrom ] [ <NUM_LIT:1> ] , breaks ) <EOL> return histogram <EOL> except KeyError : <EOL> return None <EOL> else : <EOL> w = [ ] <EOL> for chrom in self . get_chroms ( ) : <EOL> w += self [ chrom ] [ <NUM_LIT:1> ] <EOL> histogram [ '<STR_LIT:count>' ] = bin ( w , breaks ) <EOL> return histogram <EOL> elif type ( breaks ) == int : <EOL> histogram = { } <EOL> if chrom : <EOL> min_this_chr = self . min ( chrom ) <EOL> max_this_chr = self . max ( chrom ) <EOL> bins = linspace ( min_this_chr , max_this_chr , breaks + <NUM_LIT:1> ) <EOL> histogra [ '<STR_LIT>' ] = bins [ : - <NUM_LIT:1> ] <EOL> try : <EOL> histogram [ '<STR_LIT:count>' ] = bin ( self [ chrom ] [ <NUM_LIT:1> ] , bins ) <EOL> return histogram <EOL> except KeyError : <EOL> return None <EOL> else : <EOL> w = [ ] <EOL> for chrom in self . get_chroms ( ) : <EOL> w += self [ chrom ] [ <NUM_LIT:1> ] <EOL> min_all = min ( w ) <EOL> max_all = max ( w ) <EOL> bins = linspace ( min_this_chr , max_this_chr , breaks + <NUM_LIT:1> ) <EOL> histogram [ '<STR_LIT>' ] = bins <EOL> histogram [ '<STR_LIT:count>' ] = bin ( w , bins ) <EOL> return histogram <EOL> else : <EOL> return None <EOL> def get_middle_percent ( self , percent ) : <EOL> """<STR_LIT>""" <EOL> w = [ ] <EOL> for chrom in self . get_chroms ( ) : <EOL> w += self [ chrom ] [ <NUM_LIT:1> ] <EOL> w . sort ( ) <EOL> wlen = len ( w ) <EOL> left = int ( ( ( <NUM_LIT:100> - percent ) / <NUM_LIT> ) * ( wlen - <NUM_LIT:1> ) ) <EOL> return w [ left ] , w [ - <NUM_LIT:1> * left ] <EOL> class XLS : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . __name__ = name <EOL> self . columns = [ ] <EOL> self . types = [ ] <EOL> self . xls = { } <EOL> def read ( self , fn , columns , types ) : <EOL> """<STR_LIT>""" <EOL> self . set_columns ( columns ) <EOL> self . set_types ( types ) <EOL> for line in open ( fn , '<STR_LIT:r>' ) . xreadlines ( ) : <EOL> if line . startswith ( '<STR_LIT:#>' ) or line . startswith ( '<STR_LIT>' ) or line . startswith ( '<STR_LIT>' ) or line == '<STR_LIT>' : continue <EOL> ls = line . strip ( ) . split ( ) <EOL> if not self . xls . has_key ( ls [ <NUM_LIT:0> ] ) : <EOL> self . xls [ ls [ <NUM_LIT:0> ] ] = { } <EOL> for column , type , l in itertools . izip ( columns [ <NUM_LIT:1> : ] , types [ <NUM_LIT:1> : ] , ls [ <NUM_LIT:1> : ] ) : <EOL> self . xls [ ls [ <NUM_LIT:0> ] ] [ column ] = [ self . convert ( l , type ) ] <EOL> else : <EOL> for column , type , l in itertools . izip ( columns [ <NUM_LIT:1> : ] , types [ <NUM_LIT:1> : ] , ls [ <NUM_LIT:1> : ] ) : <EOL> self . xls [ ls [ <NUM_LIT:0> ] ] [ column ] . append ( self . convert ( l , type ) ) <EOL> def write ( self , fn = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if fn : <EOL> f = open ( fn , '<STR_LIT:w>' ) <EOL> else : <EOL> f = open ( self . get_name ( ) + '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> f . write ( self . toxls ( ) ) <EOL> f . close ( ) <EOL> def add ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if type ( line ) == str or type ( line ) == unicode : <EOL> ls = line . strip ( ) . split ( ) <EOL> else : <EOL> ls = line <EOL> if self . xls . has_key ( ls [ <NUM_LIT:0> ] ) : <EOL> for column , tp , l in itertools . izip ( self . columns [ <NUM_LIT:1> : ] , self . types [ <NUM_LIT:1> : ] , ls [ <NUM_LIT:1> : ] ) : <EOL> self . xls [ ls [ <NUM_LIT:0> ] ] [ column ] . append ( self . convert ( l , tp ) ) <EOL> else : <EOL> self . xls [ ls [ <NUM_LIT:0> ] ] = { } <EOL> for column , tp , l in itertools . izip ( self . columns [ <NUM_LIT:1> : ] , self . types [ <NUM_LIT:1> : ] , ls [ <NUM_LIT:1> : ] ) : <EOL> self . xls [ ls [ <NUM_LIT:0> ] ] [ column ] = [ self . convert ( l , tp ) ] <EOL> def get_chroms ( self ) : <EOL> """<STR_LIT>""" <EOL> chroms = self . xls . keys ( ) <EOL> return sort_chroms ( chroms ) <EOL> def get_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __name__ <EOL> def set_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . __name__ = name <EOL> def toxls ( self ) : <EOL> """<STR_LIT>""" <EOL> script = '<STR_LIT>' % self . get_name ( ) <EOL> script += '<STR_LIT:\t>' . join ( self . columns ) + '<STR_LIT:\n>' <EOL> for chrom in self . get_chroms ( ) : <EOL> for i in xrange ( len ( self . xls [ chrom ] [ self . columns [ <NUM_LIT:1> ] ] ) ) : <EOL> line = [ str ( self . xls [ chrom ] [ column ] [ i ] ) for column in self . columns [ <NUM_LIT:1> : ] ] <EOL> script += chrom + '<STR_LIT:\t>' + '<STR_LIT:\t>' . join ( line ) + '<STR_LIT:\n>' <EOL> return script <EOL> def set_columns ( self , columns ) : <EOL> """<STR_LIT>""" <EOL> self . columns = columns <EOL> def set_types ( self , types ) : <EOL> """<STR_LIT>""" <EOL> self . types = types <EOL> def importfromdict ( self , dict ) : <EOL> """<STR_LIT>""" <EOL> self . xls = copy . deepcopy ( dict ) <EOL> self . columns = [ ] <EOL> self . types = [ ] <EOL> def exporttodict ( self ) : <EOL> """<STR_LIT>""" <EOL> return copy . deepcopy ( self . xls ) <EOL> def convert ( self , i , type ) : <EOL> """<STR_LIT>""" <EOL> if type == '<STR_LIT:int>' : <EOL> return int ( i ) <EOL> elif type == '<STR_LIT:float>' : <EOL> return float ( i ) <EOL> else : <EOL> return i <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . toxls ( ) <EOL> def __getitem__ ( self , chrom ) : <EOL> """<STR_LIT>""" <EOL> return self . xls [ chrom ] <EOL> class DataFrame : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . rownames = [ ] <EOL> self . colnames = [ ] <EOL> self . table = { } <EOL> self . __name__ = name <EOL> def set_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . __name__ = name <EOL> def append_column ( self , column , colname = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not self . table : self . rownames = [ '<STR_LIT>' % i for i in range ( len ( column ) ) ] <EOL> if len ( self . rownames ) != len ( column ) : <EOL> raise Exception ( '<STR_LIT>' % ( len ( self . rownames ) , len ( column ) ) ) <EOL> if colname : <EOL> self . table [ colname ] = column <EOL> self . colnames . append ( colname ) <EOL> else : <EOL> ncols = len ( self . table . keys ( ) ) <EOL> self . table [ '<STR_LIT>' % ncols ] = column <EOL> self . colnames . append ( '<STR_LIT>' % ncols ) <EOL> def remove_column ( self , colname ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del self . table [ colname ] <EOL> self . colnames = [ col for col in self . colnames if col != colname ] <EOL> except KeyError : <EOL> pass <EOL> def pop_column ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> lastcolname = self . colnames [ - <NUM_LIT:1> ] <EOL> del self . table [ lastcolname ] <EOL> self . colnames . pop ( ) <EOL> except IndexError : <EOL> pass <EOL> def append_row ( self , row , rowname = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> ncols = len ( self . colnames ) <EOL> lasti = <NUM_LIT:0> <EOL> for i in range ( min ( ncols , len ( row ) ) ) : <EOL> try : <EOL> self . table [ self . colnames [ i ] ] . append ( row [ i ] ) <EOL> except KeyError : <EOL> for colname in self . colnames : <EOL> self . table [ colname ] = [ ] <EOL> self . table [ self . colnames [ i ] ] . append ( row [ i ] ) <EOL> lasti = i <EOL> if lasti < ncols - <NUM_LIT:1> : <EOL> for j in range ( i , ncols ) : <EOL> self . table [ self . colnames [ j ] ] . append ( <NUM_LIT:0> ) <EOL> if rowname : <EOL> self . rownames . append ( rowname ) <EOL> else : <EOL> self . rownames . append ( '<STR_LIT>' % len ( self . rownames ) ) <EOL> def pop_row ( self ) : <EOL> """<STR_LIT>""" <EOL> ncols = len ( self . colnames ) <EOL> for i in range ( ncols ) : <EOL> self . table [ self . colnames [ i ] ] . pop ( ) <EOL> self . rownames . pop ( ) <EOL> def toxls ( self , wrownames = False ) : <EOL> """<STR_LIT>""" <EOL> ncols = len ( self . colnames ) <EOL> nrows = len ( self . rownames ) <EOL> if self . table : <EOL> header = '<STR_LIT:#>' + '<STR_LIT:\t>' . join ( self . colnames ) + '<STR_LIT:\n>' <EOL> rows = '<STR_LIT>' <EOL> for i in xrange ( nrows ) : <EOL> if wrownames : <EOL> rows += self . rownames [ i ] + '<STR_LIT:\t>' + '<STR_LIT:\t>' . join ( [ str ( self . table [ colname ] [ i ] ) for colname in self . colnames ] ) + '<STR_LIT:\n>' <EOL> else : <EOL> rows += '<STR_LIT:\t>' . join ( [ str ( self . table [ colname ] [ i ] ) for colname in self . colnames ] ) + '<STR_LIT:\n>' <EOL> return header + rows <EOL> else : <EOL> return '<STR_LIT>' % ( nrows , ncols ) <EOL> def set_rownames ( self , rownames ) : <EOL> """<STR_LIT>""" <EOL> if not self . table : <EOL> self . rownames = rownames <EOL> else : <EOL> if len ( rownames ) != len ( self . table [ self . colnames [ <NUM_LIT:0> ] ] ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . rownames = rownames <EOL> def get_colnames ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . colnames <EOL> def get_rownames ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . rownames <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . toxls ( ) <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . table [ key ] <EOL> except KeyError , e : <EOL> if type ( key ) == tuple : <EOL> if len ( key ) == <NUM_LIT:2> : <EOL> try : <EOL> rowix = self . rownames . index ( key [ <NUM_LIT:0> ] ) <EOL> return self . table [ key [ <NUM_LIT:1> ] ] [ rowix ] <EOL> except : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> elif len ( key ) == <NUM_LIT:1> : <EOL> try : <EOL> rowix = self . rownames . index ( key [ <NUM_LIT:0> ] ) <EOL> except ValueError : <EOL> raise Exception ( "<STR_LIT>" % key [ <NUM_LIT:0> ] ) <EOL> return [ self . table [ col ] [ rowix ] for col in self . colnames ] <EOL> else : <EOL> raise KeyError , e <EOL> def __setitem__ ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . table [ key ] = value <EOL> except KeyError , e : <EOL> if type ( key ) == tuple : <EOL> if len ( key ) == <NUM_LIT:2> : <EOL> try : <EOL> rowix = self . rownames . index ( key [ <NUM_LIT:0> ] ) <EOL> self . table [ key [ <NUM_LIT:1> ] ] [ rowix ] = value <EOL> except : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> elif len ( key ) == <NUM_LIT:1> : <EOL> try : <EOL> rowix = self . rownames . index ( key [ <NUM_LIT:0> ] ) <EOL> except ValueError : <EOL> raise Exception ( "<STR_LIT>" % key [ <NUM_LIT:0> ] ) <EOL> try : <EOL> for col , val in itertools . izip ( self . colnames , value ) : self . table [ col ] [ rowix ] = val <EOL> except TypeError : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> raise KeyError , e <EOL> def fill_a_row ( self , rowname ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( len ( self . rownames ) , len ( self . colnames ) ) <EOL> def write ( self , header = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> fn = self . __name__ + '<STR_LIT>' <EOL> f = open ( fn , '<STR_LIT:w>' ) <EOL> f . write ( header ) <EOL> f . write ( self . toxls ( ) ) <EOL> f . close ( ) <EOL> def filter_genes ( gene_table , limits = [ <NUM_LIT> , <NUM_LIT> ] ) : <EOL> """<STR_LIT>""" <EOL> chroms = gene_table . get_chroms ( ) <EOL> new_gt = GeneTable ( ) <EOL> new_gt . table = { } <EOL> new_gt . columns = gene_table . columns [ : ] <EOL> for chrom in chroms : <EOL> new_gt [ chrom ] = { } <EOL> columns = gene_table [ chrom ] . keys ( ) <EOL> for col in columns : <EOL> new_gt [ chrom ] [ col ] = [ ] <EOL> for i in xrange ( <NUM_LIT:0> , len ( gene_table [ chrom ] [ '<STR_LIT>' ] ) ) : <EOL> length = gene_table [ chrom ] [ '<STR_LIT>' ] [ i ] - gene_table [ chrom ] [ '<STR_LIT>' ] [ i ] <EOL> if length >= limits [ <NUM_LIT:0> ] and length <= limits [ <NUM_LIT:1> ] : <EOL> for col in columns : <EOL> new_gt [ chrom ] [ col ] . append ( gene_table [ chrom ] [ col ] [ i ] ) <EOL> return new_gt <EOL> def read_gene_subsets ( fns ) : <EOL> """<STR_LIT>""" <EOL> names = [ ] <EOL> for fn in fns : <EOL> name = [ ] <EOL> for line in open ( fn , '<STR_LIT:r>' ) . xreadlines ( ) : <EOL> try : <EOL> name . append ( line . strip ( ) . split ( ) [ <NUM_LIT:0> ] ) <EOL> except IndexError : <EOL> pass <EOL> names . append ( name ) <EOL> return names <EOL> def get_genome_coordinates ( fnwig = None , resolution = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> standard_chroms = { '<STR_LIT:I>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:M>' : '<STR_LIT>' , '<STR_LIT:X>' : '<STR_LIT>' } <EOL> fwig = open ( fnwig , '<STR_LIT:r>' ) <EOL> chrom = '<STR_LIT>' <EOL> coordinates = { } <EOL> for line in fwig . xreadlines ( ) : <EOL> if re . search ( r'<STR_LIT>' , line ) : continue <EOL> if re . search ( r'<STR_LIT>' , line ) : <EOL> chrom = re . search ( r'<STR_LIT>' , line ) . group ( <NUM_LIT:1> ) <EOL> try : <EOL> chrom = standard_chroms [ chrom ] <EOL> except KeyError : <EOL> pass <EOL> coordinates [ chrom ] = array ( '<STR_LIT:l>' , [ ] ) <EOL> continue <EOL> coordinate = ( int ( round ( <NUM_LIT:1.0> * int ( line . strip ( ) . split ( ) [ <NUM_LIT:0> ] ) / resolution ) ) ) * resolution + <NUM_LIT:1> <EOL> if not coordinates [ chrom ] or coordinate != coordinates [ chrom ] [ - <NUM_LIT:1> ] : <EOL> coordinates [ chrom ] . append ( coordinate ) <EOL> fwig . close ( ) <EOL> return coordinates <EOL> def read_genome_coordinates ( Db = None , table = None , resolution = <NUM_LIT:100> ) : <EOL> standard_chroms = { '<STR_LIT:I>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:M>' : '<STR_LIT>' , '<STR_LIT:X>' : '<STR_LIT>' } <EOL> dbconnect = sqlite3 . connect ( Db ) <EOL> cursor = dbconnect . cursor ( ) <EOL> sqlcmd = """<STR_LIT>""" <EOL> cursor . execute ( sqlcmd , ( table , ) ) <EOL> data = cursor . fetchall ( ) <EOL> chroms = set ( map ( str ( operator . itemgetter ( <NUM_LIT:0> ) ) , data ) ) <EOL> chroms = list ( chroms ) <EOL> temp = [ ] <EOL> for chrom in chroms : <EOL> try : <EOL> temp . append ( standard_chroms [ str ( chrom ) ] ) <EOL> except KeyError : <EOL> temp . append ( str ( chrom ) ) <EOL> chroms = temp <EOL> chroms . sort ( ) <EOL> coordinates = { } <EOL> for chrom in chroms : coordinates [ chrom ] = array ( '<STR_LIT:l>' , [ ] ) <EOL> for line in data : <EOL> chrom = line [ <NUM_LIT:0> ] <EOL> coordinate = line [ <NUM_LIT:1> ] <EOL> coordinates [ chrom ] . append ( coordinate ) <EOL> return coordinates <EOL> def parse_wig ( line ) : <EOL> """<STR_LIT>""" <EOL> standard_chroms = { '<STR_LIT:I>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:M>' : '<STR_LIT>' , '<STR_LIT:X>' : '<STR_LIT>' } <EOL> if re . search ( r'<STR_LIT>' , line ) : <EOL> try : <EOL> description = re . search ( r'<STR_LIT>' , line ) . group ( <NUM_LIT:1> ) <EOL> if not self . description : self . description = description <EOL> except AttributeError : <EOL> pass <EOL> elif re . search ( r'<STR_LIT>' , line ) : <EOL> chrom = re . search ( r'<STR_LIT>' , line ) . group ( <NUM_LIT:1> ) <EOL> try : <EOL> chrom = standard_chroms [ chrom ] <EOL> except KeyError : <EOL> pass <EOL> def estimate_wig_interval ( wig ) : <EOL> """<STR_LIT>""" <EOL> chroms = wig . get_chroms ( ) <EOL> if not chroms : return None <EOL> n_random_positions = <NUM_LIT:10> <EOL> intervals = [ ] <EOL> for chrom in chroms : <EOL> len_this_chr = len ( wig [ chrom ] [ <NUM_LIT:0> ] ) <EOL> a = randints ( <NUM_LIT:0> , len_this_chr , <NUM_LIT:2> * n_random_positions ) <EOL> a . sort ( ) <EOL> starts = [ a [ i ] for i in xrange ( len ( a ) ) if a % <NUM_LIT:2> == <NUM_LIT:0> ] <EOL> ends = [ a [ i ] for i in xrange ( len ( a ) ) if a % <NUM_LIT:2> == <NUM_LIT:1> ] <EOL> for start , end in itertools . izip ( starts , ends ) : <EOL> intervals . append ( median ( diff ( wig [ chrom ] [ start : end ] ) ) ) <EOL> return median ( intervals ) <EOL> def select_ChIP_by_score_cutoff ( ChIP , scorecutoff = <NUM_LIT:0> , descend = True ) : <EOL> """<STR_LIT>""" <EOL> chroms = ChIP . get_chroms ( ) <EOL> if not chroms : return None <EOL> YESSCORE = True <EOL> for chrom in chroms : <EOL> YESSCORE = YESSCORE and ChIP [ chrom ] . has_key ( '<STR_LIT>' ) <EOL> if not YESSCORE : return None <EOL> newChIP = Bed ( ) <EOL> for chrom in chroms : <EOL> if descend : <EOL> b = map ( lambda x : x >= scorecutoff , ChIP [ chrom ] [ '<STR_LIT>' ] ) <EOL> else : <EOL> b = map ( lambda x : x <= scorecutoff , ChIP [ chrom ] [ '<STR_LIT>' ] ) <EOL> newChIP . bed = { } <EOL> newChIP . bed [ chrom ] = { } <EOL> for key in ChIP [ chrom ] . keys ( ) : <EOL> if key == '<STR_LIT:start>' or key == '<STR_LIT:end>' : <EOL> newChIP . bed [ chrom ] [ key ] = array ( '<STR_LIT:l>' , array_extractor ( ChIP [ chrom ] [ '<STR_LIT:start>' ] , b ) ) <EOL> elif key == '<STR_LIT>' : <EOL> newChIP . bed [ chrom ] [ key ] = array ( '<STR_LIT:d>' , array_extractor ( ChIP [ chrom ] [ '<STR_LIT>' ] , b ) ) <EOL> else : <EOL> newChIP . bed [ chrom ] [ key ] = array_extractor ( ChIP [ chrom ] [ key ] ) <EOL> return newChIP <EOL> def determine_score_cutoff_by_n_peaks ( ChIP , n_peaks = <NUM_LIT:1000> , descend = True ) : <EOL> """<STR_LIT>""" <EOL> chroms = ChIP . get_chroms ( ) <EOL> if not chroms : return None <EOL> YESSCORE = True <EOL> for chrom in chroms : <EOL> YESSCORE = YESSCORE and ChIP [ chrom ] . has_key ( '<STR_LIT>' ) <EOL> if not YESSCORE : return None <EOL> scores = [ ] <EOL> for chrom in chroms : <EOL> scores += ChIP [ chrom ] [ '<STR_LIT>' ] <EOL> scores . sort ( ) <EOL> if n_peaks >= len ( scores ) : <EOL> try : <EOL> if descend : <EOL> return scores [ - <NUM_LIT:1> ] <EOL> else : <EOL> return scores [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return None <EOL> else : <EOL> if descend : <EOL> return scores [ - <NUM_LIT:1> * n_peaks ] <EOL> else : <EOL> return scores [ n_pekas ] <EOL> def select_ChIP_by_n_peaks ( ChIP , n_peaks = <NUM_LIT:1000> , descend = True ) : <EOL> """<STR_LIT>""" <EOL> chroms = ChIP . get_chroms ( ) <EOL> if not chroms : return None <EOL> YESSCORE = True <EOL> for chrom in chroms : <EOL> YESSCORE = YESSCORE and ChIP [ chrom ] . has_key ( '<STR_LIT>' ) <EOL> if not YESSCORE : return None <EOL> scores = [ ] <EOL> for chrom in chroms : <EOL> scores += ChIP [ chrom ] [ '<STR_LIT>' ] <EOL> scores . sort ( ) <EOL> if n_peaks >= len ( scores ) : <EOL> return ChIP <EOL> if descend : <EOL> cutoff = scores [ - <NUM_LIT:1> * n_peaks ] <EOL> else : <EOL> cutoff = scores [ n_peaks ] <EOL> newChIP = Bed ( ) <EOL> for chrom in chroms : <EOL> if descend : <EOL> b = map ( lambda x : x >= cutoff , ChIP [ chrom ] [ '<STR_LIT>' ] ) <EOL> else : <EOL> b = map ( lambda x : x <= cutoff , ChIP [ chrom ] [ '<STR_LIT>' ] ) <EOL> newChIP . bed = { } <EOL> newChIP . bed [ chrom ] = { } <EOL> for key in ChIP [ chrom ] . keys ( ) : <EOL> if key == '<STR_LIT:start>' or key == '<STR_LIT:end>' : <EOL> newChIP . bed [ chrom ] [ key ] = array ( '<STR_LIT:l>' , array_extractor ( ChIP [ chrom ] [ '<STR_LIT:start>' ] , b ) ) <EOL> elif key == '<STR_LIT>' : <EOL> newChIP . bed [ chrom ] [ key ] = array ( '<STR_LIT:d>' , array_extractor ( ChIP [ chrom ] [ '<STR_LIT>' ] , b ) ) <EOL> else : <EOL> newChIP . bed [ chrom ] [ key ] = array_extractor ( ChIP [ chrom ] [ key ] , b ) <EOL> return newChIP <EOL> def get_percent_from_BED ( bed , percentage , twoside = False ) : <EOL> """<STR_LIT>""" <EOL> chroms = bed . get_chroms ( ) <EOL> if not chroms : return [ ] <EOL> YESSCORE = True <EOL> for chrom in chroms : <EOL> YESSCORE = YESSCORE and bed [ chrom ] . has_key ( '<STR_LIT>' ) <EOL> if not YESSCORE : return [ ] <EOL> percentage = min ( <NUM_LIT:100> , percentage ) <EOL> percentage = max ( <NUM_LIT:0> , percentage ) <EOL> scores = [ ] <EOL> for chrom in chroms : <EOL> scores += bed [ chrom ] [ '<STR_LIT>' ] <EOL> scores . sort ( ) <EOL> l = len ( scores ) <EOL> if twoside : <EOL> left = int ( ( ( <NUM_LIT:100> - percentage ) / <NUM_LIT> ) * ( l - <NUM_LIT:1> ) ) <EOL> right = l - left <EOL> else : <EOL> left = <NUM_LIT:0> <EOL> right = int ( ( percentage / <NUM_LIT> ) * ( l - <NUM_LIT:1> ) ) <EOL> return scores [ left ] , scores [ right ] <EOL> def get_histogram_BED_scores ( bed , breaks , chrom = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> chroms = bed . get_chroms ( ) <EOL> if not chroms : return { } <EOL> YESSCORE = True <EOL> for chrom in chroms : <EOL> YESSCORE = YESSCORE and bed [ chrom ] . has_key ( '<STR_LIT>' ) <EOL> if not YESSCORE : return { } <EOL> if type ( breaks ) == list : <EOL> histogram = { '<STR_LIT>' : breaks [ : - <NUM_LIT:1> ] } <EOL> if chrom : <EOL> try : <EOL> histogram [ '<STR_LIT:count>' ] = bin ( bed [ chrom ] [ '<STR_LIT>' ] , breaks ) <EOL> except KeyError : <EOL> histogram = { } <EOL> else : <EOL> w = [ ] <EOL> for chrom in chroms : <EOL> try : <EOL> w += bed [ chrom ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> histogram [ '<STR_LIT:count>' ] = bin ( w , breaks ) <EOL> except : <EOL> histogram = { } <EOL> return histogram <EOL> elif type ( breaks ) == int : <EOL> histogram = { } <EOL> if chrom : <EOL> try : <EOL> min_this_chr = min ( bed [ chrom ] [ '<STR_LIT>' ] ) <EOL> max_this_chr = max ( bed [ chrom ] [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> return { } <EOL> try : <EOL> bins = linspace ( min_this_chr , max_this_chr , breaks + <NUM_LIT:1> ) <EOL> histogra [ '<STR_LIT>' ] = bins [ : - <NUM_LIT:1> ] <EOL> histogram [ '<STR_LIT:count>' ] = bin ( bed [ chrom ] [ '<STR_LIT>' ] , bins ) <EOL> return histogram <EOL> except : <EOL> return { } <EOL> else : <EOL> w = [ ] <EOL> for chrom in chroms : <EOL> try : <EOL> w += bed [ chrom ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> min_all = min ( w ) <EOL> max_all = max ( w ) <EOL> bins = linspace ( min_this_chr , max_this_chr , breaks + <NUM_LIT:1> ) <EOL> histogram [ '<STR_LIT>' ] = bins <EOL> histogram [ '<STR_LIT:count>' ] = bin ( w , bins ) <EOL> return histogram <EOL> except : <EOL> return { } <EOL> else : <EOL> return { } <EOL> def check_if_yes_score ( bed ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> chroms = bed . get_chroms ( ) <EOL> except AttributeError : <EOL> return False <EOL> if not chroms : <EOL> return False <EOL> YESSCORE = True <EOL> for chrom in chroms : <EOL> YESSCORE = YESSCORE and bed [ chrom ] . has_key ( '<STR_LIT>' ) <EOL> return YESSCORE <EOL> def fill_up_scores_w_val ( bed , val = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> chroms = bed . get_chroms ( ) <EOL> except AttributeError : <EOL> return <EOL> if not chroms : return <EOL> for chrom in chroms : <EOL> if not bed [ chrom ] . has_key ( '<STR_LIT:name>' ) : <EOL> bed [ chrom ] [ '<STR_LIT:name>' ] = [ <NUM_LIT:0> ] * len ( bed [ chrom ] [ '<STR_LIT:start>' ] ) <EOL> bed [ chrom ] [ '<STR_LIT>' ] = array ( '<STR_LIT:d>' , [ <NUM_LIT:1.0> ] * len ( bed [ chrom ] [ '<STR_LIT:start>' ] ) ) <EOL> def downsample_ChIP ( ChIP , interval = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> chroms = ChIP . get_chroms ( ) <EOL> if not chroms : return None <EOL> sampledChIP = Bed ( ) <EOL> sampledChIP . bed = { } <EOL> for chrom in chroms : <EOL> sampledChIP . bed [ chrom ] = { } <EOL> keys = ChIP [ chrom ] . keys ( ) <EOL> for key in keys : <EOL> if key == '<STR_LIT:start>' or key == '<STR_LIT:end>' : <EOL> sampledChIP [ chrom ] [ key ] = array ( '<STR_LIT:l>' , [ ] ) <EOL> elif key == '<STR_LIT>' : <EOL> sampledChIP [ chrom ] [ key ] = array ( '<STR_LIT:d>' , [ ] ) <EOL> else : <EOL> sampledChIP [ chrom ] [ key ] = [ ] <EOL> len_this_chrom = len ( ChIP [ chrom ] [ '<STR_LIT:start>' ] ) <EOL> if len_this_chrom == <NUM_LIT:1> : <EOL> sampledChIP . bed [ chrom ] = copy . deepcopy ( ChIP . bed [ chrom ] ) <EOL> continue <EOL> prev = ChIP [ chrom ] [ '<STR_LIT:start>' ] [ <NUM_LIT:0> ] <EOL> for key in keys : sampledChIP [ chrom ] [ key ] . append ( ChIP [ chrom ] [ key ] [ <NUM_LIT:0> ] ) <EOL> for i , start in itertools . izip ( xrange ( <NUM_LIT:1> , len_this_chrom ) , ChIP [ chrom ] [ '<STR_LIT:start>' ] [ <NUM_LIT:1> : ] ) : <EOL> if start - prev > interval : <EOL> for key in keys : sampledChIP [ chrom ] [ key ] . append ( ChIP [ chrom ] [ key ] [ i ] ) <EOL> prev = start <EOL> return sampledChIP <EOL> def _pval_str ( pvals ) : <EOL> pval_str = [ ] <EOL> for pval in pvals : <EOL> if pval == <NUM_LIT> : pval_str . append ( '<STR_LIT>' % ( pval ) ) <EOL> elif pval < <NUM_LIT> : pval_str . append ( '<STR_LIT>' % ( pval ) ) <EOL> else : pval_str . append ( '<STR_LIT>' % ( pval ) ) <EOL> return pval_str <EOL> def _get_percentage ( fraction ) : <EOL> return [ <NUM_LIT:100> * f for f in fraction ] <EOL> def _percent_str ( percentages ) : <EOL> """<STR_LIT>""" <EOL> pstr = [ ] <EOL> for percent in percentages : <EOL> if percent >= <NUM_LIT:0.1> : <EOL> pstr . append ( '<STR_LIT>' % percent + '<STR_LIT>' ) <EOL> elif percent >= <NUM_LIT> : <EOL> pstr . append ( '<STR_LIT>' % percent + '<STR_LIT>' ) <EOL> else : <EOL> pstr . append ( '<STR_LIT>' % percent + '<STR_LIT>' ) <EOL> return pstr <EOL> def _cat_percent_pval_strs ( percent_str , pval_str , sep = '<STR_LIT:\n>' ) : <EOL> return [ sep . join ( a ) for a in zip ( percent_str , pval_str ) ] <EOL> def _draw_barplot_w_val ( genome , ChIP , pval , names , main = None , xlab = None , ylab = None , horiz = False , upsidedown = False , cex_names = <NUM_LIT:1> , cex_text = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> maxp = max ( max ( genome ) , max ( ChIP ) ) <EOL> marg = maxp / <NUM_LIT> <EOL> if horiz : <EOL> xlim = [ <NUM_LIT:0> , maxp + marg ] <EOL> ylim = None <EOL> if upsidedown : <EOL> genome . reverse ( ) <EOL> ChIP . reverse ( ) <EOL> pval . reverse ( ) <EOL> names . reverse ( ) <EOL> else : <EOL> xlim = None <EOL> ylim = [ <NUM_LIT:0> , maxp + marg ] <EOL> rscript = R . barplot ( [ genome , ChIP ] , names = names , beside = True , horiz = horiz , col = [ '<STR_LIT>' , '<STR_LIT>' ] , main = main , xlab = xlab , ylab = ylab , xlim = xlim , ylim = ylim , cex_names = cex_names ) <EOL> pct_genome = _percent_str ( genome ) <EOL> pct_ChIP = _percent_str ( ChIP ) <EOL> pval_ChIP = _pval_str ( pval ) <EOL> if horiz : <EOL> pct_pval_ChIP = _cat_percent_pval_strs ( pct_ChIP , pval_ChIP , sep = '<STR_LIT:U+0020>' ) <EOL> x1 = genome <EOL> x2 = ChIP <EOL> y1 = '<STR_LIT>' <EOL> y2 = '<STR_LIT>' <EOL> pos = <NUM_LIT:4> <EOL> else : <EOL> pct_pval_ChIP = _cat_percent_pval_strs ( pct_ChIP , pval_ChIP , sep = '<STR_LIT:\n>' ) <EOL> x1 = '<STR_LIT>' <EOL> x2 = '<STR_LIT>' <EOL> y1 = genome <EOL> y2 = ChIP <EOL> pos = <NUM_LIT:3> <EOL> if cex_text == <NUM_LIT:1> : <EOL> rscript += R . text ( x1 , y1 , pct_genome , pos = pos , offset = <NUM_LIT> ) <EOL> rscript += R . text ( x2 , y2 , pct_pval_ChIP , pos = pos , offset = <NUM_LIT> ) <EOL> else : <EOL> rscript += R . text ( x1 , y1 , pct_genome , pos = pos , offset = <NUM_LIT> , cex = cex_text ) <EOL> rscript += R . text ( x2 , y2 , pct_pval_ChIP , pos = pos , offset = <NUM_LIT> , cex = cex_text ) <EOL> return rscript <EOL> def _draw_pie ( x , names , main , pval = None , border = False , cols = None ) : <EOL> """<STR_LIT>""" <EOL> percentages = _percent_str ( [ i * <NUM_LIT> for i in x ] ) <EOL> if not pval : <EOL> labels = [ '<STR_LIT>' % ( n , pct ) for n , pct in zip ( names , percentages ) ] <EOL> else : <EOL> pval_str = _pval_str ( pval ) <EOL> labels = [ '<STR_LIT>' % ( n , pct , pv ) for n , pct , pv in zip ( names , percentages , pval_str ) ] <EOL> if type ( cols ) == list or type ( cols ) == tuple : <EOL> rscript = R . pie ( x , labels = labels , main = main , col = cols [ : len ( x ) ] , clockwise = True , border = border ) <EOL> elif type ( cols ) == str : <EOL> rscript = R . pie ( x , labels = labels , main = main , col = cols , clockwise = True , border = border ) <EOL> else : <EOL> rscript = R . pie ( x , labels = labels , main = main , clockwise = True , border = border ) <EOL> return rscript <EOL> def draw_rel_loc_pie ( genome_p , ChIP_p , pval , gene_div ) : <EOL> label = [ str ( i ) + '<STR_LIT:/>' + str ( gene_div [ <NUM_LIT:0> ] ) for i in range ( <NUM_LIT:1> , gene_div [ <NUM_LIT:0> ] + <NUM_LIT:1> ) ] <EOL> rscript = _draw_pie ( genome_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , label , r'<STR_LIT>' % gene_div [ <NUM_LIT:0> ] ) <EOL> rscript += _draw_pie ( ChIP_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , label , r'<STR_LIT>' , pval = pval [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> label = [ str ( i ) + '<STR_LIT:/>' + str ( gene_div [ <NUM_LIT:1> ] ) for i in range ( <NUM_LIT:1> , gene_div [ <NUM_LIT:1> ] + <NUM_LIT:1> ) ] <EOL> rscript += _draw_pie ( genome_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:1> ] , label , r'<STR_LIT>' % gene_div [ <NUM_LIT:1> ] ) <EOL> rscript += _draw_pie ( ChIP_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:1> ] , label , r'<STR_LIT>' , pval = pval [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) <EOL> return rscript <EOL> def draw_rel_loc_cds_pie ( genome_p , ChIP_p , pval , gene_div ) : <EOL> label = [ str ( i ) + '<STR_LIT:/>' + str ( gene_div [ <NUM_LIT:0> ] ) for i in range ( <NUM_LIT:1> , gene_div [ <NUM_LIT:0> ] + <NUM_LIT:1> ) ] <EOL> rscript = _draw_pie ( genome_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , label , r'<STR_LIT>' % gene_div [ <NUM_LIT:0> ] ) <EOL> rscript += _draw_pie ( ChIP_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , label , r'<STR_LIT>' , pval = pval [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> label = [ str ( i ) + '<STR_LIT:/>' + str ( gene_div [ <NUM_LIT:1> ] ) for i in range ( <NUM_LIT:1> , gene_div [ <NUM_LIT:1> ] + <NUM_LIT:1> ) ] <EOL> rscript += _draw_pie ( genome_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:1> ] , label , r'<STR_LIT>' % gene_div [ <NUM_LIT:1> ] ) <EOL> rscript += _draw_pie ( ChIP_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:1> ] , label , r'<STR_LIT>' , pval = pval [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) <EOL> return rscript <EOL> def draw_pie_distribution_of_elements ( ChIP_EP , prom = <NUM_LIT> , down = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> names = [ '<STR_LIT>' % ( prom / <NUM_LIT:3> ) , '<STR_LIT>' % ( prom / <NUM_LIT:3> , <NUM_LIT:2> * prom / <NUM_LIT:3> ) , '<STR_LIT>' % ( <NUM_LIT:2> * prom / <NUM_LIT:3> , prom ) , '<STR_LIT>' % ( prom / <NUM_LIT:3> ) , '<STR_LIT>' % ( prom / <NUM_LIT:3> , <NUM_LIT:2> * prom / <NUM_LIT:3> ) , '<STR_LIT>' % ( <NUM_LIT:2> * prom / <NUM_LIT:3> , prom ) , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , '<STR_LIT>' ] <EOL> x = ChIP_EP [ '<STR_LIT>' ] [ '<STR_LIT>' ] + ChIP_EP [ '<STR_LIT>' ] [ '<STR_LIT>' ] + ChIP_EP [ '<STR_LIT>' ] [ '<STR_LIT>' ] + [ ChIP_EP [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] <EOL> x_percent = _percent_str ( map ( lambda a : <NUM_LIT> * a , x ) ) <EOL> names_w_percent = map ( lambda x , y : x + '<STR_LIT>' + y , names , x_percent ) <EOL> x = map ( max , x , [ <NUM_LIT> ] * len ( x ) ) <EOL> rscript = '<STR_LIT:\n>' <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += '<STR_LIT:\n>' <EOL> s = <NUM_LIT> <EOL> v = <NUM_LIT> <EOL> init_angle = <NUM_LIT> <EOL> main = '<STR_LIT>' <EOL> colval = '<STR_LIT>' <EOL> mar = mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] <EOL> oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] <EOL> mfrow = [ <NUM_LIT:2> , <NUM_LIT:1> ] <EOL> rscript += R . par ( mar = mar , oma = oma , mfrow = mfrow ) <EOL> rscript += R . rainbow ( len ( x ) , s = s , v = v , rval = colval ) <EOL> rscript += R . pie ( x , labels = "<STR_LIT>" , main = main , col = colval , clockwise = True , border = colval , radius = <NUM_LIT> , init_angle = init_angle , cex = <NUM_LIT> ) <EOL> rscript += R . plot ( [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] , tp = "<STR_LIT:n>" , axes = False , xlab = "<STR_LIT>" , ylab = "<STR_LIT>" , main = "<STR_LIT>" , frame = False ) <EOL> rscript += R . legend ( x = '<STR_LIT>' , legend = names_w_percent , pch = <NUM_LIT:15> , col = colval , bty = "<STR_LIT:n>" ) <EOL> return rscript <EOL> def draw_chrom_barplot ( genome_p , ChIP_p , pval , horiz = True , upsidedown = True ) : <EOL> chroms = set ( [ chrom for chrom in genome_p . get_chroms ( ) if chrom != '<STR_LIT>' ] ) . intersection ( set ( [ chrom for chrom in ChIP_p . get_chroms ( ) if chrom != '<STR_LIT>' ] ) ) <EOL> chroms = sort_chroms ( list ( chroms ) ) <EOL> n_chroms = len ( chroms ) <EOL> if not chroms : return '<STR_LIT>' <EOL> genome_chrom_p = [ ] <EOL> ChIP_chrom_p = [ ] <EOL> pval_chrom = [ ] <EOL> for chrom in chroms : <EOL> genome_chrom_p . append ( genome_p [ chrom ] [ '<STR_LIT>' ] ) <EOL> ChIP_chrom_p . append ( ChIP_p [ chrom ] [ '<STR_LIT>' ] ) <EOL> pval_chrom . append ( pval [ chrom ] [ '<STR_LIT>' ] ) <EOL> gp = _get_percentage ( genome_chrom_p ) <EOL> cp = _get_percentage ( ChIP_chrom_p ) <EOL> chroms = [ chrom . rsplit ( '<STR_LIT>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] for chrom in chroms ] <EOL> cex_text = lininterpol ( [ <NUM_LIT:6> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , n_chroms ) <EOL> cex_text = max ( min ( <NUM_LIT> , cex_text ) , <NUM_LIT> ) <EOL> cex_names = lininterpol ( [ <NUM_LIT:6> , <NUM_LIT:1> ] , [ <NUM_LIT> , <NUM_LIT> ] , n_chroms ) <EOL> cex_names = max ( min ( <NUM_LIT:1> , cex_names ) , <NUM_LIT> ) <EOL> rscript = _draw_barplot_w_val ( gp , cp , pval_chrom , chroms , '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , horiz = horiz , upsidedown = upsidedown , cex_names = cex_names , cex_text = cex_text ) <EOL> rscript += R . legend ( x = '<STR_LIT:right>' , legend = [ '<STR_LIT>' , '<STR_LIT>' ] , pch = <NUM_LIT:15> , col = [ '<STR_LIT>' , '<STR_LIT>' ] , bty = '<STR_LIT:n>' ) <EOL> return rscript <EOL> def draw_promoter_barplot ( genome_p , ChIP_p , pval , prom ) : <EOL> names = [ '<STR_LIT>' % ( prom / <NUM_LIT:3> ) , '<STR_LIT>' % ( <NUM_LIT:2> * prom / <NUM_LIT:3> ) , '<STR_LIT>' % prom ] <EOL> gp = _get_percentage ( genome_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> cp = _get_percentage ( ChIP_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> rscript = _draw_barplot_w_val ( gp , cp , pval [ '<STR_LIT>' ] [ '<STR_LIT>' ] , names , '<STR_LIT>' , ylab = '<STR_LIT>' ) <EOL> rscript += R . legend ( x = '<STR_LIT>' , legend = [ '<STR_LIT>' , '<STR_LIT>' ] , pch = <NUM_LIT:15> , col = [ '<STR_LIT>' , '<STR_LIT>' ] , bty = '<STR_LIT:n>' ) <EOL> return rscript <EOL> def draw_bipromoter_barplot ( genome_p , ChIP_p , pval , biprom ) : <EOL> names = [ '<STR_LIT>' % ( biprom / <NUM_LIT:2> ) , '<STR_LIT>' % biprom ] <EOL> gp = _get_percentage ( genome_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> cp = _get_percentage ( ChIP_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> rscript = _draw_barplot_w_val ( gp , cp , pval [ '<STR_LIT>' ] [ '<STR_LIT>' ] , names , '<STR_LIT>' , ylab = '<STR_LIT>' ) <EOL> rscript += R . legend ( x = '<STR_LIT>' , legend = [ '<STR_LIT>' , '<STR_LIT>' ] , pch = <NUM_LIT:15> , col = [ '<STR_LIT>' , '<STR_LIT>' ] , bty = '<STR_LIT:n>' ) <EOL> return rscript <EOL> def draw_downstream_barplot ( genome_p , ChIP_p , pval , down ) : <EOL> names = [ '<STR_LIT>' % ( down / <NUM_LIT:3> ) , '<STR_LIT>' % ( <NUM_LIT:2> * down / <NUM_LIT:3> ) , '<STR_LIT>' % down ] <EOL> gp = _get_percentage ( genome_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> cp = _get_percentage ( ChIP_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> rscript = _draw_barplot_w_val ( gp , cp , pval [ '<STR_LIT>' ] [ '<STR_LIT>' ] , names , '<STR_LIT>' , ylab = '<STR_LIT>' ) <EOL> rscript += R . legend ( x = '<STR_LIT>' , legend = [ '<STR_LIT>' , '<STR_LIT>' ] , pch = <NUM_LIT:15> , col = [ '<STR_LIT>' , '<STR_LIT>' ] , bty = '<STR_LIT:n>' ) <EOL> return rscript <EOL> def draw_gene_barplot ( genome_p , ChIP_p , pval ) : <EOL> names = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> gp = _get_percentage ( genome_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> cp = _get_percentage ( ChIP_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> rscript = _draw_barplot_w_val ( gp , cp , pval [ '<STR_LIT>' ] [ '<STR_LIT>' ] , names , '<STR_LIT>' , ylab = '<STR_LIT>' ) <EOL> rscript += R . legend ( x = '<STR_LIT>' , legend = [ '<STR_LIT>' , '<STR_LIT>' ] , pch = <NUM_LIT:15> , col = [ '<STR_LIT>' , '<STR_LIT>' ] , bty = '<STR_LIT:n>' ) <EOL> return rscript <EOL> def draw_roi_barplot ( genome_p , ChIP_p , pval ) : <EOL> names = [ '<STR_LIT>' ] <EOL> gp = _get_percentage ( [ genome_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] ) <EOL> cp = _get_percentage ( [ ChIP_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] ) <EOL> rscript = _draw_barplot_w_val ( gp , cp , [ pval [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] , names , '<STR_LIT>' , ylab = '<STR_LIT>' ) <EOL> rscript += R . legend ( x = '<STR_LIT>' , legend = [ '<STR_LIT>' , '<STR_LIT>' ] , pch = <NUM_LIT:15> , col = [ '<STR_LIT>' , '<STR_LIT>' ] , bty = '<STR_LIT:n>' ) <EOL> return rscript <EOL> def draw_colorbar ( lim , n_cols = <NUM_LIT> , cmap = '<STR_LIT>' , vertical = False , hist = None , linecol = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if vertical : <EOL> left = <NUM_LIT:0> <EOL> right = <NUM_LIT:1> <EOL> mid = sum ( lim ) / <NUM_LIT> <EOL> rscript = R . plot ( [ left , right ] , lim , ylim = lim , xlim = [ left , right ] , tp = "<STR_LIT:n>" , frame = False , xaxt = "<STR_LIT:n>" , xlab = "<STR_LIT>" , ylab = "<STR_LIT>" , main = "<STR_LIT>" ) <EOL> rscript += R . polygon ( [ left , right , right , left ] , [ lim [ <NUM_LIT:0> ] , lim [ <NUM_LIT:0> ] , lim [ <NUM_LIT:1> ] , lim [ <NUM_LIT:1> ] ] ) <EOL> vals = linspace ( lim [ <NUM_LIT:0> ] , lim [ <NUM_LIT:1> ] , max ( <NUM_LIT:2> , n_cols ) ) <EOL> rscript += R . heatmap_bar ( vals , vals , lim , lim , bot = left , top = right , vertical = vertical , cmap = cmap ) <EOL> else : <EOL> top = <NUM_LIT:1> <EOL> bot = <NUM_LIT:0> <EOL> mid = sum ( lim ) / <NUM_LIT> <EOL> rscript = R . plot ( lim , [ bot , top ] , xlim = lim , ylim = [ bot , top ] , tp = "<STR_LIT:n>" , frame = False , yaxt = "<STR_LIT:n>" , xlab = "<STR_LIT>" , ylab = "<STR_LIT>" , main = '<STR_LIT>' ) <EOL> rscript += R . polygon ( [ lim [ <NUM_LIT:0> ] , lim [ <NUM_LIT:1> ] , lim [ <NUM_LIT:1> ] , lim [ <NUM_LIT:0> ] ] , [ bot , bot , top , top ] ) <EOL> vals = linspace ( lim [ <NUM_LIT:0> ] , lim [ <NUM_LIT:1> ] , max ( <NUM_LIT:2> , n_cols ) ) <EOL> rscript += R . heatmap_bar ( vals , vals , lim , lim , bot = bot , top = top , cmap = cmap ) <EOL> if hist : <EOL> N = <NUM_LIT> <EOL> max_count = max ( hist [ '<STR_LIT:count>' ] ) <EOL> probability = [ N * h / max_count for h in hist [ '<STR_LIT:count>' ] ] <EOL> within_lim = arglim ( hist [ '<STR_LIT>' ] , lim ) <EOL> bins2draw = array_extractor ( hist [ '<STR_LIT>' ] , within_lim ) <EOL> prob2draw = array_extractor ( probability , within_lim ) <EOL> rscript += R . lines ( bins2draw , prob2draw , col = [ linecol ] , xlim = lim , ylim = [ bot , top ] ) <EOL> return rscript <EOL> def draw_histogram_bar ( histogram , lim , main = '<STR_LIT>' , xlab = '<STR_LIT>' , linecol = '<STR_LIT>' , bgcol = '<STR_LIT>' , cex = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> top = <NUM_LIT:1> <EOL> bot = <NUM_LIT:0> <EOL> mid = sum ( lim ) / <NUM_LIT> <EOL> if cex == <NUM_LIT:1> : <EOL> rscript = R . plot ( lim , [ bot , top ] , xlim = lim , ylim = [ bot , top ] , tp = "<STR_LIT:n>" , frame = False , yaxt = "<STR_LIT:n>" , xlab = xlab , ylab = "<STR_LIT>" , main = main ) <EOL> else : <EOL> rscript = R . plot ( lim , [ bot , top ] , xlim = lim , ylim = [ bot , top ] , tp = "<STR_LIT:n>" , frame = False , yaxt = "<STR_LIT:n>" , xlab = xlab , ylab = "<STR_LIT>" , main = main , cex = cex ) <EOL> rscript += R . polygon ( [ lim [ <NUM_LIT:0> ] , lim [ <NUM_LIT:1> ] , lim [ <NUM_LIT:1> ] , lim [ <NUM_LIT:0> ] ] , [ bot , bot , top , top ] , col = [ bgcol ] ) <EOL> N = <NUM_LIT> <EOL> max_count = max ( histogram [ '<STR_LIT:count>' ] ) <EOL> probability = [ N * h / max_count for h in histogram [ '<STR_LIT:count>' ] ] <EOL> within_lim = arglim ( histogram [ '<STR_LIT>' ] , lim ) <EOL> bins2draw = array_extractor ( histogram [ '<STR_LIT>' ] , within_lim ) <EOL> prob2draw = array_extractor ( probability , within_lim ) <EOL> rscript += R . lines ( bins2draw , prob2draw , col = [ linecol ] , xlim = lim , ylim = [ bot , top ] , lwd = <NUM_LIT:2> ) <EOL> return rscript <EOL> def draw_nullbar ( ) : <EOL> """<STR_LIT>""" <EOL> lim = [ - <NUM_LIT:3> , <NUM_LIT:3> ] <EOL> top = <NUM_LIT:1> <EOL> bot = <NUM_LIT:0> <EOL> rscript = R . plot ( lim , [ bot , top ] , xlim = lim , ylim = [ bot , top ] , tp = "<STR_LIT:n>" , axes = False , xlab = "<STR_LIT>" , ylab = "<STR_LIT>" , main = '<STR_LIT>' ) <EOL> return rscript <EOL> def draw_chrom_frame ( chrominfo , chrom , maxchrom , centromere ) : <EOL> """<STR_LIT>""" <EOL> bot = - <NUM_LIT:1> <EOL> top = <NUM_LIT:1> <EOL> mid = ( bot + top ) / <NUM_LIT:2> <EOL> rscript = R . plot ( chrominfo , [ bot , top ] , xlim = maxchrom , ylim = [ bot , top ] , tp = "<STR_LIT:n>" , frame = False , yaxt = "<STR_LIT:n>" , xlab = "<STR_LIT>" , ylab = "<STR_LIT>" , main = chrom ) <EOL> if centromere : <EOL> p_start = centromere [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> p_end = centromere [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> q_start = centromere [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> q_end = centromere [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> rscript += R . polygon ( [ chrominfo [ <NUM_LIT:0> ] , p_start , p_end , p_start , chrominfo [ <NUM_LIT:0> ] , chrominfo [ <NUM_LIT:0> ] ] , [ bot , bot , mid , top , top , top ] ) <EOL> rscript += R . polygon ( [ q_start , q_end , chrominfo [ <NUM_LIT:1> ] , chrominfo [ <NUM_LIT:1> ] , q_end , q_start ] , [ mid , bot , bot , top , top , mid ] ) <EOL> rscript += R . polygon ( [ p_start , p_end , p_start ] , [ bot , mid , top ] , col = "<STR_LIT>" ) <EOL> rscript += R . polygon ( [ q_start , q_end , q_end ] , [ mid , bot , top ] , col = "<STR_LIT>" ) <EOL> else : <EOL> rscript += R . polygon ( [ chrominfo [ <NUM_LIT:0> ] , chrominfo [ <NUM_LIT:1> ] , chrominfo [ <NUM_LIT:1> ] , chrominfo [ <NUM_LIT:0> ] ] , [ bot , bot , top , top ] ) <EOL> return rscript <EOL> def draw_a_chrom_heatmap ( wig , chrom , ylim , chrominfo , maxchrom , centromere = None , bot = - <NUM_LIT:1> , top = <NUM_LIT:1> , line = True , linecol = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> rscript = '<STR_LIT>' <EOL> if centromere : <EOL> wig_bef_cent = [ [ ] , [ ] ] <EOL> wig_aft_cent = [ [ ] , [ ] ] <EOL> i = <NUM_LIT:0> <EOL> between = [ ] <EOL> for c , v in intertools . izip ( wig [ <NUM_LIT:0> ] , wig [ <NUM_LIT:1> ] ) : <EOL> if c < centromeres [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] : <EOL> wig_bef_cent [ <NUM_LIT:0> ] . append ( c ) <EOL> wig_bef_cent [ <NUM_LIT:1> ] . append ( v ) <EOL> elif c > centromeres [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] : <EOL> wig_aft_cent [ <NUM_LIT:0> ] . append ( c ) <EOL> wig_aft_cent [ <NUM_LIT:1> ] . append ( v ) <EOL> else : <EOL> between = [ c , v ] <EOL> wig_bef_cent [ <NUM_LIT:0> ] . append ( centromeres [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> if between : <EOL> wig_aft_cent [ <NUM_LIT:0> ] . insert ( <NUM_LIT:0> , centromeres [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> wig_aft_cent [ <NUM_LIT:1> ] . insert ( <NUM_LIT:0> , between [ <NUM_LIT:1> ] ) <EOL> rscript += R . heatmap_bar ( wig_bef_cent [ <NUM_LIT:0> ] , wig_bef_cent [ <NUM_LIT:1> ] , [ chrominfo [ <NUM_LIT:0> ] , centromere [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ] , ylim , bot = bot , top = top ) <EOL> rscript += R . heatmap_bar ( wig_aft_cent [ <NUM_LIT:0> ] , wig_aft_cent [ <NUM_LIT:1> ] , [ centromere [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] , chrominfo [ <NUM_LIT:1> ] ] , ylim , bot = bot , top = top ) <EOL> else : <EOL> rscript += R . heatmap_bar ( wig [ <NUM_LIT:0> ] , wig [ <NUM_LIT:1> ] , chrominfo , ylim , bot = bot , top = top ) <EOL> if line : <EOL> med = ( top + bot ) / <NUM_LIT:2> <EOL> hdelta = ( top - bot ) / <NUM_LIT:2> <EOL> scaling = <NUM_LIT> <EOL> scaled_wig = map ( lambda x : hdelta * scaling * ( ( max ( min ( ylim [ <NUM_LIT:1> ] , x ) , ylim [ <NUM_LIT:0> ] ) - ylim [ <NUM_LIT:1> ] - ylim [ <NUM_LIT:0> ] ) / ( ylim [ <NUM_LIT:1> ] - ylim [ <NUM_LIT:0> ] ) ) + med , wig [ <NUM_LIT:1> ] ) <EOL> rscript += R . lines ( wig [ <NUM_LIT:0> ] , scaled_wig , col = [ linecol ] ) <EOL> return rscript <EOL> def draw_single_profile ( breaks , prfl , col = [ ] , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = [ ] , v = None ) : <EOL> if not col : col = [ '<STR_LIT>' ] <EOL> if ylim : rscript = R . plot ( breaks , prfl , col = col , main = main , xlab = xlab , ylab = ylab , ylim = ylim , lwd = <NUM_LIT:2> ) <EOL> else : rscript = R . plot ( breaks , prfl , col = col , main = main , xlab = xlab , ylab = ylab , lwd = <NUM_LIT:2> ) <EOL> if v != None : rscript += R . abline ( v = v , lty = <NUM_LIT:2> , col = [ '<STR_LIT>' ] ) <EOL> return rscript <EOL> def draw_multiple_profiles ( breaks , prfls , cols = [ ] , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = [ ] , v = None , legends = [ ] ) : <EOL> n_prfls = len ( prfls ) <EOL> if not ylim : ylim = [ min ( min_col_by_col ( prfls ) ) , max ( max_col_by_col ( prfls ) ) ] <EOL> if cols : <EOL> rscript += R . plot ( breaks , prfls [ <NUM_LIT:0> ] , col = cols [ <NUM_LIT:0> ] , main = main , xlab = xlab , ylab = ylab , ylim = ylim , lwd = <NUM_LIT:2> ) <EOL> for i in range ( <NUM_LIT:1> , n_prfls ) : <EOL> rscript += R . lines ( breaks , prfls [ i ] , col = [ cols [ i ] ] , lwd = <NUM_LIT:2> ) <EOL> rscript += R . legend ( x = '<STR_LIT>' , legend = legends , pch = <NUM_LIT:15> , col = col [ : len ( prfls ) ] , bty = '<STR_LIT:o>' ) <EOL> else : <EOL> rval = '<STR_LIT>' <EOL> rscript = R . rainbow ( n_prfls - <NUM_LIT:1> , rval = '<STR_LIT:%s>' % rval ) <EOL> rscript += '<STR_LIT>' % ( rval , rval ) <EOL> rscript += R . plot ( breaks , prfls [ <NUM_LIT:0> ] , col = '<STR_LIT>' % rval , main = main , xlab = xlab , ylab = ylab , ylim = ylim , lwd = <NUM_LIT:2> ) <EOL> for i in range ( <NUM_LIT:1> , n_prfls ) : <EOL> rscript += R . lines ( breaks , prfls [ i ] , col = '<STR_LIT>' % ( rval , i + <NUM_LIT:1> ) , lwd = <NUM_LIT:2> ) <EOL> if not legends : <EOL> legends = [ '<STR_LIT>' % i for i in range ( <NUM_LIT:1> , len ( prfls ) + <NUM_LIT:1> ) ] <EOL> rscript += R . legend ( x = '<STR_LIT>' , legend = legends , pch = <NUM_LIT:15> , col = '<STR_LIT:%s>' % ( rval ) , bty = '<STR_LIT:o>' ) <EOL> if v != None : rscript += R . abline ( v = v , lty = <NUM_LIT:2> , col = [ '<STR_LIT>' ] ) <EOL> return rscript <EOL> def draw_profile_plot ( breaks , avg_upstream , avg_downstream , metagene_breaks , avg_metagene , metacatexon_breaks , avg_metacatexon , metacatintron_breaks , avg_metacatintron , metagene_breaks_lim = [ - <NUM_LIT:1000> , <NUM_LIT:1000> ] ) : <EOL> R . comment ( '<STR_LIT>' ) <EOL> R . comment ( '<STR_LIT>' ) <EOL> R . comment ( '<STR_LIT>' ) <EOL> rscript = R . par ( mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] , oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> mat = [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> ] ] <EOL> rscript += R . layout ( mat , widths = [ <NUM_LIT:1> , <NUM_LIT:1> ] , heights = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> step = breaks [ <NUM_LIT:1> ] - breaks [ <NUM_LIT:0> ] <EOL> start_up , end_up = where2 ( max ( metagene_breaks_lim [ <NUM_LIT:0> ] , breaks [ <NUM_LIT:0> ] ) , <NUM_LIT:0> , breaks , step ) <EOL> start_down , end_down = where2 ( step , min ( metagene_breaks_lim [ <NUM_LIT:1> ] + <NUM_LIT:1> , breaks [ - <NUM_LIT:1> ] + <NUM_LIT:1> ) , breaks , step ) <EOL> comb_breaks = breaks [ start_up : end_up ] + metagene_breaks [ <NUM_LIT:1> : - <NUM_LIT:1> ] + map ( lambda b : b + metagene_breaks [ - <NUM_LIT:1> ] , breaks [ start_down : end_down ] ) <EOL> comb_metagene = avg_upstream [ start_up : end_up ] + avg_metagene [ <NUM_LIT:1> : - <NUM_LIT:1> ] + avg_downstream [ start_down : end_down ] <EOL> rscript += draw_single_profile ( breaks , avg_upstream , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , v = <NUM_LIT:0> ) <EOL> rscript += draw_single_profile ( breaks , avg_downstream , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , v = <NUM_LIT:0> ) <EOL> rscript += draw_single_profile ( comb_breaks , comb_metagene , main = '<STR_LIT>' , xlab = '<STR_LIT>' % int ( metagene_breaks [ - <NUM_LIT:1> ] ) , ylab = '<STR_LIT>' ) <EOL> rscript += R . abline ( v = metagene_breaks [ <NUM_LIT:0> ] , lty = <NUM_LIT:2> , col = [ '<STR_LIT>' ] ) <EOL> rscript += R . abline ( v = metagene_breaks [ - <NUM_LIT:1> ] , lty = <NUM_LIT:2> , col = [ '<STR_LIT>' ] ) <EOL> ylim = [ min ( min_w_nan ( avg_metacatexon ) , min_w_nan ( avg_metacatintron ) ) , max ( max_w_nan ( avg_metacatexon ) , max_w_nan ( avg_metacatintron ) ) ] <EOL> rscript += draw_single_profile ( metacatexon_breaks , avg_metacatexon , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = ylim ) <EOL> rscript += draw_single_profile ( metacatintron_breaks , avg_metacatintron , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = ylim ) <EOL> return rscript <EOL> def draw_exon_intron_profile_plot ( metaexon_breaks , avg_metaexon , metaintron_breaks , avg_metaintron , elowers , euppers , ilowers , iuppers ) : <EOL> n_rows = <NUM_LIT:3> <EOL> rscript = '<STR_LIT>' <EOL> temp_metaexon = filter ( lambda x : x , avg_metaexon ) <EOL> temp_metaintron = filter ( lambda x : x , avg_metaintron ) <EOL> ylim = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> if temp_metaexon : <EOL> mins_avg_metaexon = map ( min_w_nan , temp_metaexon ) <EOL> maxs_avg_metaexon = map ( max_w_nan , temp_metaexon ) <EOL> else : <EOL> mins_avg_metaexon = [ ] <EOL> maxs_avg_metaexon = [ ] <EOL> if temp_metaintron : <EOL> mins_avg_metaintron = map ( min_w_nan , temp_metaintron ) <EOL> maxs_avg_metaintron = map ( max_w_nan , temp_metaintron ) <EOL> else : <EOL> mins_avg_metaintron = [ ] <EOL> maxs_avg_metaintron = [ ] <EOL> try : <EOL> ylim [ <NUM_LIT:0> ] = min ( mins_avg_metaexon + mins_avg_metaintron ) <EOL> except ValueError : <EOL> ylim [ <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> try : <EOL> ylim [ <NUM_LIT:1> ] = max ( maxs_avg_metaexon + maxs_avg_metaintron ) <EOL> except ValueError : <EOL> ylim [ <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> ylim = [ ylim [ <NUM_LIT:0> ] , ylim [ <NUM_LIT:1> ] + <NUM_LIT> * ylim [ <NUM_LIT:1> ] ] <EOL> row = <NUM_LIT:0> <EOL> for metaexon_break , metaintron_break , ame , ami , elower , eupper , ilower , iupper in map ( None , metaexon_breaks , metaintron_breaks , avg_metaexon , avg_metaintron , elowers , euppers , ilowers , iuppers ) : <EOL> if row % n_rows == <NUM_LIT:0> : rscript += R . par ( mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] , oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] , mfrow = [ n_rows , <NUM_LIT:2> ] ) <EOL> if ame : <EOL> rscript += draw_single_profile ( metaexon_break , ame , main = '<STR_LIT>' % ( elower , eupper ) , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = ylim ) <EOL> else : <EOL> rscript += draw_nullbar ( ) <EOL> if ami : <EOL> rscript += draw_single_profile ( metaintron_break , ami , main = '<STR_LIT>' % ( ilower , iupper ) , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = ylim ) <EOL> else : <EOL> rscript += draw_nullbar ( ) <EOL> row += <NUM_LIT:1> <EOL> return rscript <EOL> def draw_profile_plots ( breaks , avg_upstreams , avg_downstreams , metagene_breaks , avg_metagenes , metaexon_breaks , avg_metaexons , metaintron_breaks , avg_metaintrons , metagene_breaks_lim = [ - <NUM_LIT:1000> , <NUM_LIT:1000> ] , legends = [ ] ) : <EOL> R . comment ( '<STR_LIT>' ) <EOL> R . comment ( '<STR_LIT>' ) <EOL> R . comment ( '<STR_LIT>' ) <EOL> rscript = R . par ( mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] , oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> mat = [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> ] ] <EOL> rscript += R . layout ( mat , widths = [ <NUM_LIT:1> , <NUM_LIT:1> ] , heights = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> n_profiles = len ( avg_upstreams ) <EOL> if legends : <EOL> legends += [ '<STR_LIT>' ] <EOL> else : <EOL> legends = [ '<STR_LIT>' % i for i in range ( <NUM_LIT:1> , n_profiles ) ] + [ '<STR_LIT>' ] <EOL> start_up , end_up = where ( max ( metagene_breaks_lim [ <NUM_LIT:0> ] , breaks [ <NUM_LIT:0> ] ) , <NUM_LIT:0> , breaks ) <EOL> start_down , end_down = where ( <NUM_LIT:1> , min ( metagene_breaks_lim [ <NUM_LIT:1> ] + <NUM_LIT:1> , breaks [ - <NUM_LIT:1> ] + <NUM_LIT:1> ) , breaks ) <EOL> comb_breaks = breaks [ start_up : end_up ] + metagene_breaks + [ b + metagene_breaks [ - <NUM_LIT:1> ] for b in breaks [ start_down : end_down ] ] <EOL> comb_metagenes = [ up [ start_up : end_up ] + mg + down [ start_down : end_down ] for up , mg , down in zip ( avg_upstreams , avg_metagenes , avg_downstreams ) ] <EOL> ylim = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> no_empty_upstreams = filter_out_empty_rows ( avg_upstreams ) <EOL> if no_empty_upstreams : <EOL> ylim = [ min ( min_row_by_row ( no_empty_upstreams , True ) ) - abs ( min ( min_row_by_row ( no_empty_upstreams , True ) ) ) * <NUM_LIT:0.1> , max ( max_row_by_row ( no_empty_upstreams , True ) ) + abs ( max ( max_row_by_row ( no_empty_upstreams , True ) ) ) * <NUM_LIT:0.1> ] <EOL> rscript += draw_multiple_profiles ( breaks , avg_upstreams , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , v = <NUM_LIT:0> , legends = legends , cols = [ ] , ylim = ylim ) <EOL> ylim = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> no_empty_downstreams = filter_out_empty_rows ( avg_downstreams ) <EOL> if no_empty_downstreams : <EOL> ylim = [ min ( min_row_by_row ( no_empty_downstreams , True ) ) - abs ( min ( min_row_by_row ( no_empty_downstreams , True ) ) ) * <NUM_LIT:0.1> , max ( max_row_by_row ( no_empty_downstreams , True ) ) + abs ( max ( max_row_by_row ( no_empty_downstreams , True ) ) ) * <NUM_LIT:0.1> ] <EOL> rscript += draw_multiple_profiles ( breaks , avg_downstreams , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , v = <NUM_LIT:0> , legends = legends , cols = [ ] , ylim = ylim ) <EOL> ylim = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> no_empty_metagenes = filter_out_empty_rows ( comb_metagenes ) <EOL> if no_empty_metagenes : <EOL> ylim = [ min ( min_row_by_row ( no_empty_metagenes , True ) ) - abs ( min ( min_row_by_row ( no_empty_metagenes , True ) ) ) * <NUM_LIT:0.1> , max ( max_row_by_row ( no_empty_metagenes , True ) ) + abs ( max ( max_row_by_row ( no_empty_metagenes , True ) ) ) * <NUM_LIT:0.1> ] <EOL> rscript += draw_multiple_profiles ( comb_breaks , comb_metagenes , main = '<STR_LIT>' , xlab = '<STR_LIT>' % int ( metagene_breaks [ - <NUM_LIT:1> ] ) , ylab = '<STR_LIT>' , legends = legends , cols = [ ] , ylim = ylim ) <EOL> rscript += R . abline ( v = metagene_breaks [ <NUM_LIT:0> ] , lty = <NUM_LIT:2> , col = [ '<STR_LIT>' ] ) <EOL> rscript += R . abline ( v = metagene_breaks [ - <NUM_LIT:1> ] , lty = <NUM_LIT:2> , col = [ '<STR_LIT>' ] ) <EOL> ylim = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> no_empty_metaexons = filter_out_empty_rows ( avg_metaexons ) <EOL> no_empty_metaintrons = filter_out_empty_rows ( avg_metaintrons ) <EOL> if no_empty_metaexons and no_empty_metaintrons : <EOL> ylim = [ min ( min ( min_row_by_row ( no_empty_metaexons , True ) ) , min ( min_row_by_row ( no_empty_metaintrons , True ) ) ) , max ( max ( max_row_by_row ( no_empty_metaexons , True ) ) , max ( max_row_by_row ( no_empty_metaintrons , True ) ) ) + abs ( max ( max ( max_row_by_row ( no_empty_metaexons , True ) ) , max ( max_row_by_row ( no_empty_metaintrons , True ) ) ) ) * <NUM_LIT> ] <EOL> elif not no_empty_metaexons and no_empty_metaintrons : <EOL> ylim = [ min ( min_row_by_row ( no_empty_metaintrons , True ) ) - abs ( min ( min_row_by_row ( no_empty_metaintrons , True ) ) ) * <NUM_LIT:0.1> , max ( max_row_by_row ( no_empty_metaintrons , True ) ) + abs ( max ( max_row_by_row ( no_empty_metaintrons , True ) ) ) * <NUM_LIT:0.1> ] <EOL> elif no_empty_metaexons and not no_empty_metaintrons : <EOL> ylim = [ min ( min_row_by_row ( no_empty_metaexons , True ) ) - abs ( min ( min_row_by_row ( no_empty_metaexons , True ) ) ) * <NUM_LIT:0.1> , max ( max_row_by_row ( no_empty_metaexons , True ) ) + abs ( max ( max_row_by_row ( no_empty_metaexons , True ) ) ) * <NUM_LIT:0.1> ] <EOL> rscript += draw_multiple_profiles ( metaexon_breaks , avg_metaexons , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = ylim , cols = [ ] , legends = legends ) <EOL> rscript += draw_multiple_profiles ( metaintron_breaks , avg_metaintrons , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = ylim , cols = [ ] , legends = legends ) <EOL> return rscript <EOL> def draw_exon_intron_profile_plots ( metaexon_breaks , avg_metaexons , metaintron_breaks , avg_metaintrons , elowers , euppers , ilowers , iuppers , legends = [ ] ) : <EOL> n_rows = <NUM_LIT:3> <EOL> rscript = '<STR_LIT>' <EOL> ylim = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> temp_metaexons = map ( lambda x : filter ( lambda y : y , x ) , avg_metaexons ) <EOL> temp_metaexons = filter ( lambda x : x , temp_metaexons ) <EOL> if temp_metaexons : <EOL> mins_avg_metaexons = map ( min , map ( min_row_by_row , temp_metaexons , [ True ] * len ( temp_metaexons ) ) ) <EOL> maxs_avg_metaexons = map ( max , map ( max_row_by_row , temp_metaexons , [ True ] * len ( temp_metaexons ) ) ) <EOL> else : <EOL> mins_avg_metaexons = [ ] <EOL> maxs_avg_metaexons = [ ] <EOL> temp_metaintrons = map ( lambda x : filter ( lambda y : y , x ) , avg_metaintrons ) <EOL> temp_metaintrons = filter ( lambda x : x , temp_metaintrons ) <EOL> if temp_metaintrons : <EOL> mins_avg_metaintrons = map ( min , map ( min_row_by_row , temp_metaintrons , [ True ] * len ( temp_metaintrons ) ) ) <EOL> maxs_avg_metaintrons = map ( max , map ( max_row_by_row , temp_metaintrons , [ True ] * len ( temp_metaintrons ) ) ) <EOL> else : <EOL> mins_avg_metaintrons = [ ] <EOL> maxs_avg_metaintrons = [ ] <EOL> try : <EOL> ylim [ <NUM_LIT:0> ] = min ( mins_avg_metaexons + mins_avg_metaintrons ) <EOL> except ValueError : <EOL> ylim [ <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> try : <EOL> ylim [ <NUM_LIT:1> ] = max ( maxs_avg_metaexons + maxs_avg_metaintrons ) <EOL> except ValueError : <EOL> ylim [ <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> ylim = [ ylim [ <NUM_LIT:0> ] , ylim [ <NUM_LIT:1> ] + <NUM_LIT> * ylim [ <NUM_LIT:1> ] ] <EOL> rows = <NUM_LIT:0> <EOL> for metaexon_break , metaintron_break , ame , ami , elower , eupper , ilower , iupper in map ( None , metaexon_breaks , metaintron_breaks , avg_metaexons , avg_metaintrons , elowers , euppers , ilowers , iuppers ) : <EOL> if rows % n_rows == <NUM_LIT:0> : rscript += R . par ( mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] , oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] , mfrow = [ n_rows , <NUM_LIT:2> ] ) <EOL> if ame : <EOL> rscript += draw_multiple_profiles ( metaexon_break , ame , main = '<STR_LIT>' % ( elower , eupper ) , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = ylim , cols = [ ] , legends = legends ) <EOL> else : <EOL> rscript += draw_nullbar ( ) <EOL> if ami : <EOL> rscript += draw_multiple_profiles ( metaintron_break , ami , main = '<STR_LIT>' % ( ilower , iupper ) , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = ylim , cols = [ ] , legends = legends ) <EOL> else : <EOL> rscript += draw_nullbar ( ) <EOL> rows += <NUM_LIT:1> <EOL> return rscript <EOL> def draw_metaexon_metaintron_profile_plot ( breaks , avg_upstream , avg_downstream , metaexon_breaks , avg_metaexon , metaintron_breaks , avg_metaintron ) : <EOL> rscript = R . par ( mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] , oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> mat = [ [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ] <EOL> rscript += R . layout ( mat , widths = [ <NUM_LIT:1> , <NUM_LIT:1> ] , heights = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> rscript += draw_single_profile ( breaks , avg_upstream , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , v = <NUM_LIT:0> ) <EOL> rscript += draw_single_profile ( breaks , avg_downstream , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , v = <NUM_LIT:0> ) <EOL> ylim = [ min ( min ( avg_metaexon ) , min ( avg_metaintron ) ) , max ( max ( avg_metaexon ) , max ( avg_metaintron ) ) ] <EOL> rscript += draw_single_profile ( metaexon_breaks , avg_metaexon , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = ylim , v = <NUM_LIT:0> ) <EOL> rscript += draw_single_profile ( metaintron_breaks , avg_metaintron , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = ylim , v = <NUM_LIT:0> ) <EOL> return rscript <EOL> def draw_metaexon_metaintron_profile_plots ( breaks , avg_upstreams , avg_downstreams , metaexon_breaks , avg_metaexons , metaintron_breaks , avg_metagenes , legends = [ ] ) : <EOL> rscript = R . par ( mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] , oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> mat = [ [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ] <EOL> rscript += R . layout ( mat , widths = [ <NUM_LIT:1> , <NUM_LIT:1> ] , heights = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> rscript += draw_multiple_profiles ( breaks , avg_upstreams , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , v = <NUM_LIT:0> , cols = [ ] , legends = legends ) <EOL> rscript += draw_multiple_profiles ( breaks , avg_upstreams , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , v = <NUM_LIT:0> , cols = [ ] , legends = legends ) <EOL> ylim = [ min ( min ( min_col_by_col ( avg_metaexon ) ) , min ( min_col_by_col ( avg_metaintron ) ) ) , max ( max ( max_col_by_col ( avg_metaexon ) ) , max ( max_col_by_col ( avg_metaintron ) ) ) ] <EOL> rscript += draw_multiple_profiles ( breaks , avg_upstreams , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = ylim , v = <NUM_LIT:0> , cols = [ ] , legends = legends ) <EOL> rscript += draw_multiple_profiles ( breaks , avg_upstreams , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , ylim = ylim , v = <NUM_LIT:0> , cols = [ ] , legends = legends ) <EOL> return rscript <EOL> def draw_single_profile_heatmap ( profl , col = '<STR_LIT>' , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' ) : <EOL> rscript = '<STR_LIT>' <EOL> rscript += heatmap_2 ( x = profl , Rowv = False , Colv = False , col = col , main = '<STR_LIT>' , xlab = '<STR_LIT>' , ylab = '<STR_LIT>' , key = True , density_info = '<STR_LIT>' , keysize = <NUM_LIT> , labRow = '<STR_LIT>' ) <EOL> return rscript <EOL> def draw_CEAS ( genome_p , ChIP_p , pval , bg_res = <NUM_LIT:100> , chip_res = <NUM_LIT> , prom = <NUM_LIT> , biprom = <NUM_LIT> , down = <NUM_LIT> , gene_div = ( <NUM_LIT:3> , <NUM_LIT:5> ) ) : <EOL> timestamp = time . strftime ( "<STR_LIT>" , time . localtime ( ) ) <EOL> rscript = '<STR_LIT:\n>' <EOL> rscript += R . comment ( '<STR_LIT:%s>' % timestamp ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += '<STR_LIT:\n>' <EOL> rscript += '<STR_LIT:\n>' <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += '<STR_LIT:\n>' <EOL> rscript += R . par ( mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] , oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> rscript += draw_chrom_barplot ( genome_p , ChIP_p , pval ) <EOL> rscript += '<STR_LIT:\n>' <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += '<STR_LIT:\n>' <EOL> roi = True <EOL> if genome_p [ '<STR_LIT>' ] [ '<STR_LIT>' ] == <NUM_LIT:0.0> : roi = False <EOL> if roi : rscript += R . par ( mfrow = [ <NUM_LIT:5> , <NUM_LIT:1> ] , mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] , oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> else : rscript += R . par ( mfrow = [ <NUM_LIT:4> , <NUM_LIT:1> ] , mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] , oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> rscript += draw_promoter_barplot ( genome_p , ChIP_p , pval , prom ) <EOL> rscript += draw_bipromoter_barplot ( genome_p , ChIP_p , pval , biprom ) <EOL> rscript += draw_downstream_barplot ( genome_p , ChIP_p , pval , down ) <EOL> rscript += draw_gene_barplot ( genome_p , ChIP_p , pval ) <EOL> if roi : rscript += draw_roi_barplot ( genome_p , ChIP_p , pval ) <EOL> return rscript <EOL> def draw_wig_heatmap_overview ( swig , ylim = None , chrominfo = None , colormap = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> rscript = '<STR_LIT:\n>' <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += '<STR_LIT:\n>' <EOL> chroms = swig . get_chroms ( ) <EOL> n_chroms = len ( chroms ) <EOL> if n_chroms == <NUM_LIT:0> : return rscript <EOL> hdelta = ( <NUM_LIT> - <NUM_LIT> ) / ( <NUM_LIT> - <NUM_LIT:6> ) * ( n_chroms - <NUM_LIT:6> ) + <NUM_LIT> <EOL> delta = <NUM_LIT:100> <EOL> widths = [ <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> heights = [ <NUM_LIT:1> , <NUM_LIT> ] <EOL> mat = [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] ] <EOL> mins = [ ] <EOL> maxs = [ ] <EOL> if chrominfo : <EOL> mins = [ chrominfo [ chrom ] [ '<STR_LIT:start>' ] [ <NUM_LIT:0> ] for chrom in chrominfo . get_chroms ( ) ] <EOL> maxs = [ chrominfo [ chrom ] [ '<STR_LIT:end>' ] [ - <NUM_LIT:1> ] for chrom in chrominfo . get_chroms ( ) ] <EOL> else : <EOL> mins = [ swig [ chrom ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] for chrom in swig . get_chroms ( ) ] <EOL> maxs = [ swig [ chrom ] [ <NUM_LIT:0> ] [ - <NUM_LIT:1> ] for chrom in swig . get_chroms ( ) ] <EOL> mins . sort ( ) <EOL> maxs . sort ( ) <EOL> maxchrom = [ mins [ <NUM_LIT:0> ] , maxs [ - <NUM_LIT:1> ] ] <EOL> if not ylim : <EOL> percentage = <NUM_LIT> <EOL> left , right = swig . get_middle_percent ( percentage ) <EOL> ylim = [ - <NUM_LIT:1> * max ( abs ( left ) , abs ( right ) ) , max ( abs ( left ) , abs ( right ) ) ] <EOL> maxmax = max ( ylim ) <EOL> n_bins = <NUM_LIT> <EOL> histogram = swig . histogram ( breaks = linspace ( ylim [ <NUM_LIT:0> ] , ylim [ <NUM_LIT:1> ] , n_bins + <NUM_LIT:1> ) ) <EOL> cmd = '<STR_LIT>' % colormap <EOL> exec ( cmd ) <EOL> cmap = '<STR_LIT>' <EOL> rscript += R . vector ( cm , varname = cmap ) <EOL> rscript += R . par ( mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] , oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> rscript += R . layout ( mat = mat , widths = widths , heights = heights ) <EOL> rscript += draw_colorbar ( ylim , vertical = False , hist = histogram , linecol = '<STR_LIT>' ) <EOL> rscript += R . plot ( maxchrom , [ <NUM_LIT:1> - hdelta , n_chroms + hdelta ] , tp = "<STR_LIT:n>" , col = "<STR_LIT>" , ylab = "<STR_LIT>" , xlim = maxchrom , ylim = [ <NUM_LIT:1> - hdelta , n_chroms + hdelta ] , frame = False , xaxt = "<STR_LIT:s>" , yaxt = "<STR_LIT:n>" , main = "<STR_LIT>" , xlab = "<STR_LIT>" ) <EOL> counter = n_chroms <EOL> for chrom in chroms : <EOL> bot = counter - hdelta <EOL> top = counter + hdelta <EOL> rscript += draw_a_chrom_heatmap ( swig [ chrom ] , chrom , ylim , [ swig [ chrom ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , swig [ chrom ] [ <NUM_LIT:0> ] [ - <NUM_LIT:1> ] + delta ] , maxchrom , centromere = None , bot = bot , top = top , line = True , linecol = '<STR_LIT>' ) <EOL> rscript += R . mtext ( chrom . rsplit ( '<STR_LIT>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] , side = <NUM_LIT:2> , at = ( top + bot ) / <NUM_LIT:2> ) <EOL> counter -= <NUM_LIT:1> <EOL> return rscript <EOL> def draw_ChIP_over_genome_var_colors ( ChIP , wig_size , n_peaks = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> width = <NUM_LIT> <EOL> height = <NUM_LIT> <EOL> pixel = <NUM_LIT:1.0> / <NUM_LIT> <EOL> mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] <EOL> oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] <EOL> rough_plot_width = <NUM_LIT> <EOL> widths = [ <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> heights = [ <NUM_LIT:1> , <NUM_LIT> ] <EOL> mat = [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] ] <EOL> percentage = <NUM_LIT> <EOL> n_colors = <NUM_LIT:16> <EOL> if wig_size != None : <EOL> chroms = wig_size . keys ( ) <EOL> chroms = sort_chroms ( chroms ) <EOL> else : <EOL> chroms = ChIP . get_chroms ( ) <EOL> n_chroms = len ( chroms ) <EOL> if n_chroms == <NUM_LIT:0> : return rscript <EOL> hdelta = ( <NUM_LIT> - <NUM_LIT> ) / ( <NUM_LIT> - <NUM_LIT:6> ) * ( n_chroms - <NUM_LIT:6> ) + <NUM_LIT> <EOL> if wig_size != None : <EOL> minx = min ( [ wig_size [ chrom ] [ <NUM_LIT:0> ] for chrom in chroms ] ) <EOL> maxx = max ( [ wig_size [ chrom ] [ <NUM_LIT:1> ] for chrom in chroms ] ) <EOL> else : <EOL> minx = min ( [ ChIP [ chrom ] [ '<STR_LIT:start>' ] [ <NUM_LIT:0> ] for chrom in chroms ] ) <EOL> maxx = max ( [ ChIP [ chrom ] [ '<STR_LIT:end>' ] [ - <NUM_LIT:1> ] for chrom in chroms ] ) <EOL> maxchrom = [ minx , maxx ] <EOL> reducedChIP = select_ChIP_by_n_peaks ( ChIP , n_peaks = n_peaks , descend = True ) <EOL> interval = ( maxchrom [ <NUM_LIT:1> ] - maxchrom [ <NUM_LIT:0> ] ) / ( rough_plot_width * int ( <NUM_LIT:1> / pixel ) ) <EOL> reducedChIP = downsample_ChIP ( reducedChIP , interval = interval ) <EOL> left , right = get_percent_from_BED ( reducedChIP , percentage , twoside = False ) <EOL> ylim = [ left , right ] <EOL> maxmax = max ( [ abs ( ylim [ <NUM_LIT:0> ] ) , ylim [ <NUM_LIT:1> ] ] ) <EOL> n_bins = <NUM_LIT:16> <EOL> if ylim [ <NUM_LIT:0> ] < <NUM_LIT:0> : <EOL> ylim = [ - <NUM_LIT:1> * maxmax , maxmax ] <EOL> breaks = linspace ( - <NUM_LIT:1> * maxmax , maxmax , n_bins + <NUM_LIT:1> ) <EOL> else : <EOL> ylim = [ <NUM_LIT:0> , ylim [ <NUM_LIT:1> ] ] <EOL> breaks = linspace ( <NUM_LIT:0> , ylim [ <NUM_LIT:1> ] + <NUM_LIT> , n_bins + <NUM_LIT:1> ) <EOL> histogram = get_histogram_BED_scores ( reducedChIP , breaks ) <EOL> rscript = '<STR_LIT:\n>' <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += '<STR_LIT:\n>' <EOL> rscript += R . par ( mar = mar , oma = oma ) <EOL> rscript += R . layout ( mat = mat , widths = widths , heights = heights ) <EOL> if ylim [ <NUM_LIT:0> ] < <NUM_LIT:0> : <EOL> colormap = "<STR_LIT>" <EOL> else : <EOL> colormap = "<STR_LIT>" <EOL> cmd = '<STR_LIT>' % colormap <EOL> exec ( cmd ) <EOL> rscript += R . colorRampPalette ( cm , rval = colormap ) <EOL> cmap = '<STR_LIT>' <EOL> rscript += '<STR_LIT>' % ( cmap , colormap , str ( n_colors ) ) <EOL> rscript += draw_colorbar ( ylim , vertical = False , cmap = cmap , hist = histogram , linecol = '<STR_LIT>' ) <EOL> rscript += R . plot ( maxchrom , [ <NUM_LIT:1> - hdelta , n_chroms + hdelta ] , tp = "<STR_LIT:n>" , col = "<STR_LIT>" , ylab = "<STR_LIT>" , xlim = maxchrom , ylim = [ <NUM_LIT:1> - hdelta , n_chroms + hdelta ] , frame = False , xaxt = "<STR_LIT:s>" , yaxt = "<STR_LIT:n>" , main = "<STR_LIT>" , xlab = "<STR_LIT>" ) <EOL> count = n_chroms <EOL> for chrom in chroms : <EOL> bot = count - hdelta <EOL> top = count + hdelta <EOL> try : <EOL> rscript += R . rectangles_with_heights_and_colors ( reducedChIP [ chrom ] [ '<STR_LIT:start>' ] , reducedChIP [ chrom ] [ '<STR_LIT:end>' ] , reducedChIP [ chrom ] [ '<STR_LIT>' ] , ylim , bot = bot , top = top , xaxt = "<STR_LIT:n>" , cmap = cmap ) <EOL> rscript += R . mtext ( chrom . rsplit ( '<STR_LIT>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] , side = <NUM_LIT:2> , at = ( top + bot ) / <NUM_LIT:2> ) <EOL> except KeyError : <EOL> rscript += R . rectangles_with_heights_and_colors ( None , None , None , [ minx , maxx ] , ylim , bot = bot , top = top , xaxt = "<STR_LIT:n>" ) <EOL> rscript += R . mtext ( chrom . rsplit ( '<STR_LIT>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] , side = <NUM_LIT:2> , at = ( top + bot ) / <NUM_LIT:2> ) <EOL> count -= <NUM_LIT:1> <EOL> return rscript <EOL> def draw_ChIP_over_genome_mono_col ( ChIP , wig_size , n_peaks = <NUM_LIT> , barcol = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> width = <NUM_LIT> <EOL> height = <NUM_LIT> <EOL> pixel = <NUM_LIT:1.0> / <NUM_LIT> <EOL> mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] <EOL> oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] <EOL> rough_plot_width = <NUM_LIT:7> <EOL> widths = [ <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> heights = [ <NUM_LIT:1> , <NUM_LIT:5> ] <EOL> mat = [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] ] <EOL> percentage = <NUM_LIT> <EOL> if wig_size != None : <EOL> chroms = wig_size . keys ( ) <EOL> chroms = sort_chroms ( chroms ) <EOL> else : <EOL> chroms = ChIP . get_chroms ( ) <EOL> n_chroms = len ( chroms ) <EOL> if n_chroms == <NUM_LIT:0> : return rscript <EOL> hdelta = ( <NUM_LIT> - <NUM_LIT> ) / ( <NUM_LIT> - <NUM_LIT:6> ) * ( n_chroms - <NUM_LIT:6> ) + <NUM_LIT> <EOL> if wig_size != None : <EOL> minx = min ( [ wig_size [ chrom ] [ <NUM_LIT:0> ] for chrom in chroms ] ) <EOL> maxx = max ( [ wig_size [ chrom ] [ <NUM_LIT:1> ] for chrom in chroms ] ) <EOL> else : <EOL> minx = min ( [ ChIP [ chrom ] [ '<STR_LIT:start>' ] [ <NUM_LIT:0> ] for chrom in chroms ] ) <EOL> maxx = max ( [ ChIP [ chrom ] [ '<STR_LIT:end>' ] [ - <NUM_LIT:1> ] for chrom in chroms ] ) <EOL> maxchrom = [ minx , maxx ] <EOL> reducedChIP = select_ChIP_by_n_peaks ( ChIP , n_peaks = n_peaks , descend = True ) <EOL> interval = ( maxchrom [ <NUM_LIT:1> ] - maxchrom [ <NUM_LIT:0> ] ) / ( rough_plot_width * int ( <NUM_LIT:1> / pixel ) ) <EOL> reducedChIP = downsample_ChIP ( reducedChIP , interval = interval ) <EOL> left , right = get_percent_from_BED ( reducedChIP , percentage , twoside = False ) <EOL> ylim = [ left , right ] <EOL> maxmax = max ( [ abs ( ylim [ <NUM_LIT:0> ] ) , ylim [ <NUM_LIT:1> ] ] ) <EOL> n_bins = <NUM_LIT:16> <EOL> if ylim [ <NUM_LIT:0> ] < <NUM_LIT:0> : <EOL> ylim = [ - <NUM_LIT:1> * maxmax , maxmax ] <EOL> breaks = linspace ( - <NUM_LIT:1> * maxmax - <NUM_LIT> , maxmax + <NUM_LIT> , n_bins + <NUM_LIT:1> ) <EOL> else : <EOL> ylim = [ <NUM_LIT:0> , ylim [ <NUM_LIT:1> ] ] <EOL> breaks = linspace ( <NUM_LIT:0> , ylim [ <NUM_LIT:1> ] + <NUM_LIT> , n_bins + <NUM_LIT:1> ) <EOL> histogram = get_histogram_BED_scores ( reducedChIP , breaks ) <EOL> rscript = '<STR_LIT:\n>' <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += '<STR_LIT:\n>' <EOL> rscript += R . par ( mar = mar , oma = oma ) <EOL> rscript += R . layout ( mat = mat , widths = widths , heights = heights ) <EOL> rscript += draw_histogram_bar ( histogram , ylim , main = "<STR_LIT>" , linecol = '<STR_LIT>' , bgcol = '<STR_LIT>' , cex = <NUM_LIT> ) <EOL> rscript += R . plot ( maxchrom , [ <NUM_LIT:1> - hdelta , n_chroms + hdelta ] , tp = "<STR_LIT:n>" , col = "<STR_LIT>" , ylab = "<STR_LIT>" , xlim = maxchrom , ylim = [ <NUM_LIT:1> - hdelta , n_chroms + hdelta ] , frame = False , xaxt = "<STR_LIT:s>" , yaxt = "<STR_LIT:n>" , main = "<STR_LIT>" , xlab = "<STR_LIT>" ) <EOL> count = n_chroms <EOL> for chrom in chroms : <EOL> bot = count - hdelta <EOL> top = count + hdelta <EOL> try : <EOL> rscript += R . rectangles_with_heights ( reducedChIP [ chrom ] [ '<STR_LIT:start>' ] , reducedChIP [ chrom ] [ '<STR_LIT:end>' ] , reducedChIP [ chrom ] [ '<STR_LIT>' ] , ylim , bot = bot , top = top , xaxt = "<STR_LIT:n>" , col = [ barcol ] ) <EOL> rscript += R . mtext ( chrom . rsplit ( '<STR_LIT>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] , side = <NUM_LIT:2> , at = ( top + bot ) / <NUM_LIT:2> ) <EOL> except KeyError : <EOL> rscript += R . rectangles_with_heights ( None , None , None , ylim , bot = bot , top = top , xaxt = "<STR_LIT:n>" ) <EOL> rscript += R . mtext ( chrom . rsplit ( '<STR_LIT>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] , side = <NUM_LIT:2> , at = ( top + bot ) / <NUM_LIT:2> ) <EOL> count -= <NUM_LIT:1> <EOL> return rscript <EOL> def draw_wig_overview ( wig , plots_per_pg = <NUM_LIT:4> ) : <EOL> """<STR_LIT>""" <EOL> rscript = R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> rscript += R . comment ( '<STR_LIT>' ) <EOL> chroms = wig . get_chroms ( ) <EOL> if len ( chroms ) == <NUM_LIT:0> : return rscript <EOL> chromcnt = <NUM_LIT:1> <EOL> for chrom in chroms : <EOL> if chromcnt % plots_per_pg == <NUM_LIT:1> : rscript += R . par ( mar = [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT> ] , oma = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> rscript += R . barplot ( wig [ chrom ] [ <NUM_LIT:1> ] , names = wig [ chrom ] [ <NUM_LIT:0> ] , col = [ "<STR_LIT>" ] , border = False , space = <NUM_LIT:0> , main = chrom , ylab = "<STR_LIT>" , xlab = "<STR_LIT>" ) <EOL> chromcnt += <NUM_LIT:1> <EOL> return rscript </s>
