-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.xml
162 lines (143 loc) · 6.76 KB
/
macros.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?xml version="1.0"?>
<macros>
<xml name="requirements">
<requirements>
<requirement type="package" version="@VERSION@">r-sigQC</requirement>
<requirement type="package" version="2.3">r-gridextra</requirement>
<yield />
</requirements>
</xml>
<token name="@VERSION@">0.1.14</token>
<token name="@R_INIT@"><![CDATA[
## Setup R error handling to go to stderr
options(show.error.messages=F, error=function(){cat(geterrmessage(), file=stderr()); q("no",1,F)})
## Unify locale settings
loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
]]></token>
<token name='@R_Function_Fix@'><![CDATA[
eval_compactness_loc <- function(gene_sigs_list,names_sigs, mRNA_expr_matrix, names_datasets, out_dir = '~',file=NULL,showResults = FALSE,radar_plot_values,logged=T,origin=NULL){
max_title_length <- -999
for(i in 1:length(names_sigs)){
for(j in 1:length(names_datasets)){
if(nchar(paste0(names_datasets[[j]] ,' ',names_sigs[[i]])) > max_title_length){
max_title_length <- nchar(paste0(names_datasets[[j]] ,' ',names_sigs[[i]]))
}
}
}
graphics::par(cex.main=min(0.8,(3*6/max_title_length)),cex.lab = 0.6,oma=c(2,0,0,0),mar=c(0,0,0,0))
grDevices::pdf(paste0('out_dir/sig_autocor_hmaps.pdf'),width=6,height=6)
hmaps <- lapply(1:(length(names_sigs) *length(names_datasets)),function(i) {
dataset_ind <- i %% length(names_datasets)
if (dataset_ind == 0 ){
dataset_ind <- length(names_datasets)
}
sig_ind <- ceiling(i/length(names_datasets))
gene_sig <- gene_sigs_list[[names_sigs[sig_ind]]]
if(is.matrix(gene_sig)){gene_sig = as.vector(gene_sig);}
data.matrix = mRNA_expr_matrix[[names_datasets[dataset_ind]]]
inter <- intersect(gene_sig, row.names(data.matrix))
autocors <- stats::cor(t(stats::na.omit(data.matrix[inter,])),method='spearman')
if(!dir.exists(file.path(out_dir,'autocorrelation_matrices'))){
dir.create(file.path(out_dir,'autocorrelation_matrices'))
}
utils::write.table(autocors,file=file.path(out_dir,'autocorrelation_matrices', paste0('autocorrelation_matrix_',names_sigs[sig_ind],'_',names_datasets[dataset_ind],'.txt')),quote=F,sep='\t')
tryCatch({
gplots::heatmap.2( stats::na.omit(autocors),
col = gplots::colorpanel(100,"blue","white","red"),
trace = "none",
na.color="grey",
labRow=rownames(autocors),
labCol=colnames(autocors),
main = paste0("\n\nAutocorrelation\n", names_datasets[[dataset_ind]] ,' ',names_sigs[[sig_ind]],'\n'),
dendrogram = "col",
symbreaks = T,
Rowv = T,Colv=T ,key.xlab='Rho',key.ylab=NA, key.title=NA,cexRow=max(min(0.5,(4*4/length(rownames(autocors)))),0.06),cexCol=max(min(0.5,(4*4/length(rownames(autocors)))),0.06),margins=c(1+(max(nchar(rownames(autocors)))/2),1+ (max(nchar(rownames(autocors)))/2)))
},
error=function(err){
graphics::plot.new()
graphics::title(paste0('\n\nToo many NA values in \n',names_datasets[dataset_ind],' ',names_sigs[sig_ind]))
})
})
grDevices::dev.off()
if(grDevices::dev.cur()!=1){
g <- grDevices::dev.off()
}
graphics::par(mar=c(0,0,0,0),cex=0.6)
graphics::plot.new()
legend_names <- c()
legend_cols <- c()
legend_lty <- c()
for(k in 1:length(names_sigs)){
for (i in 1:length(names_datasets) ){
legend_names <- c(legend_names,paste0(names_datasets[i],' ',names_sigs[k]))
legend_cols <- c(legend_cols,i)
legend_lty <- c(legend_lty,k)
}
}
l <- graphics::legend(0, 0,legend_names,col=legend_cols,lty=legend_lty,lwd=rep(1,times=(length(names_datasets) * length(names_sigs))),pt.cex=1,cex=min(0.5,(4*10/max_title_length)), plot=FALSE)
tmp <- l[['rect']]
tmp <- tmp[['w']]
w <- graphics::grconvertX(tmp, to='ndc')- graphics::grconvertX(0, to='ndc')
w <- graphics::grconvertX(w,from="ndc",to="inches") + graphics::grconvertX(10,from="device",to="inches")
if (showResults){
grDevices::dev.new()
}else{
grDevices::pdf(file.path(out_dir,'sig_autocor_dens.pdf'),width=5,height=5)
}
graphics::par(cex.main=0.8,cex.lab = 0.6,mar=c(3,3,4,1),mfrow=c(1,1),xpd=TRUE,omi=(c(0,0,0,w)))
max_dens <- -9999
max_x_coord <- -9999
for(k in 1:length(names_sigs)){
gene_sig <- gene_sigs_list[[names_sigs[k]]]
if(is.matrix(gene_sig)){gene_sig = as.vector(gene_sig);}
for (i in 1:length(names_datasets) ){
data.matrix = mRNA_expr_matrix[[names_datasets[i]]]
inter <- intersect(gene_sig, row.names(data.matrix))
autocors <- stats::cor(t(stats::na.omit(data.matrix[inter,])),method='spearman')
if(dim(autocors)[1] > 1){
cur_max <- max(stats::density(unlist(stats::na.omit(autocors)))[['y']])
if (max_dens < cur_max){
max_dens <- cur_max
}
cur_max <- max(stats::density(unlist(stats::na.omit(autocors)))[['x']])
if (max_x_coord < cur_max){
max_x_coord <- cur_max
}
}
}
}
plots_count <- 0
for(k in 1:length(names_sigs)){
gene_sig <- gene_sigs_list[[names_sigs[k]]]
if(is.matrix(gene_sig)){gene_sig = as.vector(gene_sig);}
for (i in 1:length(names_datasets) ){
data.matrix = mRNA_expr_matrix[[names_datasets[i]]]
inter <- intersect(gene_sig, row.names(data.matrix))
autocors <- stats::cor(t(stats::na.omit(data.matrix[inter,])),method='spearman')
if(dim(autocors)[1] > 1){
if (plots_count==0){
graphics::plot(stats::density(unlist(stats::na.omit(autocors))),ylim=c(0,ceiling(max_dens)),xlim=c(-1,1),col=i,main=NA,lwd=2,lty=k)
plots_count <- 1
}else{
graphics::lines(stats::density(unlist(stats::na.omit(autocors))),ylim=c(0,ceiling(max_dens)),xlim=c(-1,1),col=i,main=NA,lwd=2,lty=k)
}
## radar_plot_values[[names_sigs[k]]][[names_datasets[i]]]['autocor_median'] <- stats::median(autocors,na.rm=T)
}else{
## radar_plot_values[[names_sigs[k]]][[names_datasets[i]]]['autocor_median'] <- stats::median(stats::na.omit(autocors))
}
}
}
graphics::mtext(side = 2, line = 2, 'Density',cex=0.8)
graphics::mtext(side = 1, line = 2, 'Rho',cex=0.8)
graphics::mtext(side = 3, line = 2,'Autocorrelation Density')
op <- graphics::par(cex=0.6)
graphics::legend(graphics::par('usr')[2]+0.05, graphics::par('usr')[4],xpd=NA,legend_names,col=legend_cols,lty=legend_lty,lwd=rep(1,times=(length(names_datasets) * length(names_sigs))),pt.cex=1,cex=min(0.5,(4*10/max_title_length)))
if(showResults){
grDevices::dev.copy(grDevices::pdf,file.path(out_dir,'sig_autocor_dens.pdf'),width=5,height=5)
}
if(grDevices::dev.cur()!=1){
g <- grDevices::dev.off()
}
}
]]></token>
</macros>