-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-plot-matrix.r
More file actions
executable file
·93 lines (81 loc) · 3.03 KB
/
Copy pathscript-plot-matrix.r
File metadata and controls
executable file
·93 lines (81 loc) · 3.03 KB
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
#-------------------------------------------------------------------------
# This script plots 0-1 matrix
# Usage:
# Rscript script-plot-matrix.r <0_1_matrix_file>
# If you use this script, please cite corresponding article.
# Chernomor et al. "Identifying equally scoring species-trees in phylogenomics with incomplete data using Gentrius"
#-------------------------------------------------------------------------
# The script provides two plots:
# 1. the order of rows and columns of input matrix are fixed (file ext: <*reorder_FALSE.pdf>)
# 2. the rows and columns are reordered according to their row/column sums (file ext: <*reorder_TRUE.pdf>)
#
# Colors:
# - rows with row sum > 1: 1 - black, 0 - grey
# - rows with row sum = 1: 1 - red, 0 - pale pink
#
#-------------------------------------------------------------------------
reorder_columns<-function(m){
n=nrow(m)
k=ncol(m)
cov=array(-1,k)
for(j in 1:k){
cov[j]=sum(m[,j])
}
new_order=c()
while(length(which(cov[]==-1))!=k){
max=max(cov)
ids=which(cov[]==max)
new_order=c(new_order,ids)
for(i in ids){
cov[i]=-1
}
}
new_m=matrix(-1,nrow=n,ncol=k)
for(j in 1:length(new_order)){
new_m[,j]=m[,new_order[j]]
}
return(new_m)
}
plot_one_matrix<-function(m,tag_order,n,k){
if(tag_order==TRUE){
a=reorder_columns(m)
b=reorder_columns(t(a))
m=t(b)
}
num=2
colors=colorRampPalette(c("lightgrey","black"))
for(i in 1:n){
if(sum(m[i,])==1){
id=which(m[i,]==1)
m[i,]=rep(3,k)
m[i,id]=2
num=4
colors=colorRampPalette(c("lightgrey","black","firebrick","#cfa9a9"))
}
}
image(t(m[n:1,]), col=colors(num),axes = FALSE,xlab="",ylab="")
}
#============================================================================================
#============================================================================================
# ARGUMENTS
#============================================================================================
#============================================================================================
args = commandArgs(trailingOnly=TRUE)
file_in=args[1] # matrix file
cell=1
tag_order_all=c("FALSE","TRUE")
for(tag_order in tag_order_all){
if(file.exists(file_in)){
m=read.table(file_in,skip=1,row.names=1)
m=as.matrix(m)
n=nrow(m)
k=ncol(m)
eps=ifelse(n/k>50,5,1)
pdf(paste(file_in,"-plot_matrix-reorder_",tag_order,".pdf",sep=""),width=k*eps*cell,height=n*cell)
par(mar=c(0,0,0,0),oma=c(0,0,0,0))
plot_one_matrix(m,tag_order,n,k)
dev.off()
}else{
print(paste("File not found:",file_in))
}
}