comparison heatmap_viz.R @ 0:00960579bcd3 draft default tip

planemo upload commit 004439cca3c2fd3b5132eff246d846e5050bfd4f-dirty
author proteore
date Tue, 28 Aug 2018 10:37:03 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:00960579bcd3
1 #!/usr/bin/Rscript
2
3 suppressMessages(library('plotly'))
4 suppressMessages(library('heatmaply'))
5
6 #packageVersion('plotly')
7
8 get_args <- function(){
9
10 ## Collect arguments
11 args <- commandArgs(TRUE)
12
13 ## Default setting when no arguments passed
14 if(length(args) < 1) {
15 args <- c("--help")
16 }
17
18 ## Help section
19 if("--help" %in% args) {
20 cat("Pathview R script
21 Arguments:
22 --help Print this test
23 --input path of the input file (must contains a colum of uniprot and/or geneID accession number)
24 --output Output file
25 --type type of output file, could be html, pdf, jpg or png
26 --cols Columns to use for heatmap, exemple : '3:8' to use columns from the third to the 8th
27 --row_names Column which contains row names
28 --header True or False
29 --col_text_angle Angle of columns label ; from -90 to 90 degres
30
31 Example:
32 ./heatmap_viz.R --input='dat.nucl.norm.imputed.tsv' --output='heatmap.html' --cols='3:8' --row_names='2' --header=TRUE --col_text_angle=0 \n\n")
33
34 q(save="no")
35 }
36
37 #save(args,file="/home/dchristiany/proteore_project/ProteoRE/tools/pathview/args.Rda")
38 #load("/home/dchristiany/proteore_project/ProteoRE/tools/pathview/args.Rda")
39 parseArgs <- function(x) strsplit(sub("^--", "", x), "=")
40 argsDF <- as.data.frame(do.call("rbind", parseArgs(args)))
41 args <- as.list(as.character(argsDF$V2))
42 names(args) <- argsDF$V1
43
44 return(args)
45 }
46
47 read_file <- function(path,header){
48 file <- try(read.table(path,header=header, sep="\t",stringsAsFactors = FALSE, quote=""),silent=TRUE)
49 if (inherits(file,"try-error")){
50 stop("File not found !")
51 }else{
52 return(file)
53 }
54 }
55
56 str2bool <- function(x){
57 if (any(is.element(c("t","true"),tolower(x)))){
58 return (TRUE)
59 }else if (any(is.element(c("f","false"),tolower(x)))){
60 return (FALSE)
61 }else{
62 return(NULL)
63 }
64 }
65
66 args <- get_args()
67 header=str2bool(args$header)
68 output <- rapply(strsplit(args$output,"\\."),c) #remove extension
69 output <- paste(output[1:length(output)-1],collapse=".")
70 output <- paste(output,args$type,sep=".")
71 first_col=as.numeric(substr(args$cols,1,1))
72 last_col=as.numeric(substr(args$cols,3,3))
73
74 ###save and load args in rda file for testing
75 #save(args,file="/home/dchristiany/proteore_project/ProteoRE/tools/heatmap_viz/args.Rda")
76 #load("/home/dchristiany/proteore_project/ProteoRE/tools/heatmap_viz/args.Rda")
77
78
79 uto <- read_file(args$input,header = header)
80 uto_light <- uto[,first_col:last_col]
81 rownames(uto_light) <- uto[,as.numeric(args$row_names)]
82 colnames(uto_light) <- sapply(colnames(uto_light),function(x) gsub("iBAQ_","",x),USE.NAMES = FALSE)
83
84 if (header) {
85 heatmaply(uto_light, file=output, margins=c(100,50,NA,0), plot_method="plotly", labRow = rownames(uto_light), labCol = names(uto_light),
86 grid_gap = 0,cexCol = 1, column_text_angle = as.numeric(args$col_text_angle), width = 1000, height=1000, colors = c('blue','green','yellow','red'))
87 }else{
88 names(uto_light) <-c(first_col:last_col)
89 heatmaply(uto_light, file=output, margins=c(100,50,NA,0), plot_method="plotly", labRow = rownames(uto_light),
90 grid_gap = 0,cexCol = 1, column_text_angle = as.numeric(args$col_text_angle), width = 1000, height=1000, colors = c('blue','green','yellow','red'))
91 }
92
93
94 #write.table(uto_light, file = "uto_light.tsv",sep="\t",row.names = FALSE)
95
96 ####heatmaply
97
98 simulateExprData <- function(n, n0, p, rho0, rho1){
99 # n: total number of subjects
100 # n0: number of subjects with exposure 0
101 # n1: number of subjects with exposure 1
102 # p: number of genes
103 # rho0: rho between Z_i and Z_j for subjects with exposure 0
104 # rho1: rho between Z_i and Z_j for subjects with exposure 1
105
106 # Simulate gene expression values according to exposure 0 or 1,
107 # according to a centered multivariate normal distribution with
108 # covariance between Z_i and Z_j being rho^|i-j|
109 n1 <- n - n0
110 times <- 1:p
111 H <- abs(outer(times, times, "-"))
112 V0 <- rho0^H
113 V1 <- rho1^H
114
115 # rows are people, columns are genes
116 genes0 <- MASS::mvrnorm(n = n0, mu = rep(0,p), Sigma = V0)
117 genes1 <- MASS::mvrnorm(n = n1, mu = rep(0,p), Sigma = V1)
118 genes <- rbind(genes0,genes1)
119 return(genes)
120 }
121
122 #genes <- simulateExprData(n = 50, n0 = 25, p = 100, rho0 = 0.01, rho1 = 0.95)
123
124 #heatmaply(genes, k_row = 2, k_col = 2)
125
126 #heatmaply(cor(genes), k_row = 2, k_col = 2)
127
128
129
130
131
132
133