#NEWLINE# def deseqNormalize( infile , time_points , reps , conditions = None ) : #NEWLINE# #INDENT# '\n    Library size normalisation and variance stabilizing transformation of\n    timeseries RNA-seq data\n\n    :param infile: count table from NGS-seq experiment\n    :type infile: str\n    :param time_points: time point labels\n    :type time_points: str list\n    :param reps: replicates labels\n    :type reps: str list\n    :param conditions: if  multiple experimental conditions\n    are to be normalised at the same time\n    :type conditions: str list\n    ' #NEWLINE# pandas2ri . activate ( ) #NEWLINE# reps = reps #NEWLINE# R ( 'suppressMessages(library("DESeq"))' ) #NEWLINE# E . info ( 'converting to pandas dataframe object' ) #NEWLINE# if ( infile . split ( '.' ) [ ( - 1 ) ] == 'gz' ) : #NEWLINE# #INDENT# comp = 'gzip' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# comp = None #NEWLINE# #UNINDENT# data_frame = pd . read_table ( infile , index_col = 0 , header = 0 , sep = '\t' , compression = comp ) #NEWLINE# pandas2ri . activate ( ) #NEWLINE# rdf = pandas2ri . py2ri ( data_frame ) #NEWLINE# if ( not conditions ) : #NEWLINE# #INDENT# time_rep_comb = [ x for x in itertools . product ( time_points , reps ) ] #NEWLINE# time_cond = ro . StrVector ( [ x [ 0 ] for x in time_rep_comb ] ) #NEWLINE# rep_cond = ro . StrVector ( [ x [ 1 ] for x in time_rep_comb ] ) #NEWLINE# R . assign ( 'countsTable' , rdf ) #NEWLINE# R ( ( 'design <- data.frame(row.names=colnames(countsTable),times=%s, replicates=%s)' % ( time_cond . r_repr ( ) , rep_cond . r_repr ( ) ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if conditions : #NEWLINE# #INDENT# design_dict = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for x in data_frame . columns . values : #NEWLINE# #INDENT# sample_dict = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# sample_dict [ 'condition' ] = str ( x ) . split ( '.' ) [ 0 ] #NEWLINE# sample_dict [ 'times' ] = int ( str ( x ) . split ( '.' ) [ 1 ] ) #NEWLINE# sample_dict [ 'replicates' ] = str ( x ) . split ( '.' ) [ 2 ] #NEWLINE# design_dict [ x ] = sample_dict #NEWLINE# design_frame = pd . DataFrame ( design_dict ) #NEWLINE# design_frame = design_frame . T #NEWLINE# #UNINDENT# des_cond = design_frame [ 'condition' ] . values . tolist ( ) #NEWLINE# des_time = design_frame [ 'times' ] . values . tolist ( ) #NEWLINE# des_reps = design_frame [ 'replicates' ] . values . tolist ( ) #NEWLINE# cond_cond = ro . StrVector ( [ x for x in des_cond ] ) #NEWLINE# time_cond = ro . StrVector ( [ x for x in des_time ] ) #NEWLINE# rep_cond = ro . StrVector ( [ x for x in des_reps ] ) #NEWLINE# R . assign ( 'countsTable' , rdf ) #NEWLINE# R . assign ( 'design' , design_frame ) #NEWLINE# #UNINDENT# #UNINDENT# E . info ( 'calculating size factors and dispersion' ) #NEWLINE# R ( 'notZero <- (rowMeans(countsTable) > 1)' ) #NEWLINE# R ( 'cds <- newCountDataSet(countsTable[notZero, ], design)' ) #NEWLINE# R ( 'cds_size <- estimateSizeFactors(cds)' ) #NEWLINE# R ( 'cds_disp <- estimateDispersions(cds_size, method="blind")' ) #NEWLINE# E . info ( 'applying variance stabilizing transformation' ) #NEWLINE# R ( 'vst <- varianceStabilizingTransformation(cds_disp)' ) #NEWLINE# R ( ( 'replicates <- c(%s)' % rep_cond . r_repr ( ) ) ) #NEWLINE# R ( ( 'times <- c(%s)' % time_cond . r_repr ( ) ) ) #NEWLINE# if conditions : #NEWLINE# #INDENT# R ( ( 'conditions <- c(%s)' % cond_cond . r_repr ( ) ) ) #NEWLINE# R ( 'trans_vst = data.frame(t(exprs(vst)), times, replicates, conditions)' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# R ( 'trans_vst = data.frame(t(exprs(vst)), times, replicates)' ) #NEWLINE# #UNINDENT# data_file = pandas2ri . ri2py ( R [ 'trans_vst' ] ) #NEWLINE# return data_file