annotate PLIDflow/scripts/clusterfinder_Auto.R @ 4:b9e7ec4e3cde draft

Uploaded
author bitlab
date Mon, 27 Jan 2020 07:12:19 -0500
parents 6fcfa4756040
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
1 # #clusterfinder_Auto.R makes a file containig the coordenates of the identified cluster/-s by Silhouette criterion
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
2 #
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
3 #!/usr/bin/env Rscript
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
4 args = commandArgs(trailingOnly=TRUE)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
5
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
6 if(length(args) < 1){
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
7 stop("USE: Rscript clusterfinder_Auto.R <session_id>")
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
8 }
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
9
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
10 #Arguments definition
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
11
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
12 session_id <- args[1]
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
13
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
14 print(session_id)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
15
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
16 setwd(session_id)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
17
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
18 options(error=traceback)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
19 options(warn=-1)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
20
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
21 #Load required library
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
22 library(cluster)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
23
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
24 #Load required libraries
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
25 library(clValid)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
26
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
27 #Loading required package: cluster
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
28 library(fpc)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
29
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
30 #Install NbClust package
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
31 #install.packages("NbClust",dependencies = TRUE)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
32
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
33 #Loading required package: NbClust
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
34 library(NbClust)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
35
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
36
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
37 #Read the dataset
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
38 data_centers <- read.table("fillouts1file.txt", header=TRUE, sep=";", na.strings="")
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
39 #data_centers <- read.table("fillouts1file.txt", header=TRUE, sep=";", na.strings="")
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
40
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
41
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
42 #Run the XXX for selection of the number of clusters
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
43
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
44 clustersdata <- NbClust(data_centers, diss=NULL, distance = "euclidean", min.nc=2, max.nc=15, method = "kmeans", index = "all", alphaBeale = 0.1)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
45
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
46 #capture.output(clustersdata$Best.nc[1,], file = "bestnumberclusters.txt")
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
47
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
48 #capture.output(NbClust(data_centers, diss=NULL, distance = "euclidean", min.nc=2, max.nc=15, method = "kmeans", index = "all", alphaBeale = 0.1), file = "proofclusters.txt")
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
49
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
50 #capture.output(NbClust(data_centers, diss=NULL, distance = "euclidean", min.nc=2, max.nc=15, method = "kmeans", index = "all", alphaBeale = 0.1), file = "numclusters.txt")
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
51
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
52 #best_num_cluster <- scan("proofclusters.txt", what = character(), quiet = TRUE)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
53
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
54 k.max <- as.integer(rownames(table(clustersdata$Best.nc[1,]))[which.max(apply(table(clustersdata$Best.nc[1,]),MARGIN=1,max))])
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
55
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
56 km.res <- kmeans(as.matrix(data_centers), centers = k.max, nstart = 25)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
57
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
58 best_position <- as.integer(rownames(table(clustersdata$Best.nc[1,]))[which.max(apply(table(clustersdata$Best.nc[1,]),MARGIN=1,max))])
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
59
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
60 # Make clustercoordenates table
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
61 #X Coordenate
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
62 start_x <- best_position + 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
63 end_x <- start_x + (best_position - 1)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
64
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
65 cluster_pos_x <- 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
66 x_pos <- c()
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
67 for (c in start_x:end_x){
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
68 x_pos[cluster_pos_x] <- km.res$centers[c]
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
69 cluster_pos_x <- cluster_pos_x + 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
70 }
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
71 #print(x_pos)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
72
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
73 #Y Coordenate
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
74 start_y <- end_x + 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
75 end_y <- start_y + (best_position - 1)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
76
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
77 cluster_pos_y <- 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
78 y_pos <- c()
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
79 for(cc in start_y:end_y){
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
80 y_pos[cluster_pos_y] <- km.res$centers[cc]
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
81 cluster_pos_y <- cluster_pos_y + 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
82 }
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
83 #print(y_pos)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
84
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
85 #Z Coordenate
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
86 start_z <- end_y + 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
87 end_z <- start_z + (best_position - 1)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
88
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
89 cluster_pos_z <- 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
90 z_pos <- c()
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
91 for(ccc in start_z:end_z){
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
92 z_pos[cluster_pos_z] <- km.res$centers[ccc]
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
93 cluster_pos_z <- cluster_pos_z + 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
94 }
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
95 #print(z_pos)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
96
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
97 #Create a file with clusters coordenates. Cluster coordenates are vectors x_pos, y_pos, z_pos
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
98 num_filas <- length(x_pos)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
99 clusters_tabla <- matrix(1, nrow = num_filas, ncol = 4) #columns are column 1 number of ccluster, column 2 x-coordenates, colum 3 y-coordenates, column 4 z-coordenates
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
100
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
101 ##Add number of cluster located in column 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
102 for(i in 1:num_filas){
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
103 clusters_tabla[i,1] <- i
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
104 }
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
105
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
106 ##Add cluster x,y,z-coordenates in clusters_tabla
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
107 #x-coordenates
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
108 v_pos_x <- 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
109 for(f in 1:num_filas){
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
110 clusters_tabla[f,2] <- x_pos[v_pos_x]
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
111 v_pos_x <- v_pos_x + 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
112 }
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
113
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
114 #y-coordenates
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
115 v_pos_y <- 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
116 for(ff in 1:num_filas){
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
117 clusters_tabla[ff,3] <- y_pos[v_pos_y]
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
118 v_pos_y <- v_pos_y + 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
119 }
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
120
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
121 #z-coordenates
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
122 v_pos_z <- 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
123 for(fff in 1:num_filas){
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
124 clusters_tabla[fff,4] <- z_pos[v_pos_z]
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
125 v_pos_z <- v_pos_z + 1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
126 }
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
127
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
128 #Write the head for the file which contains the number of clusters annd their coordenates
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
129 cabecera_clusterscoordenates <- paste("cluster", sep = " ")
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
130 cabecera <- c("x","y","z")
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
131
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
132 for(i in 1:3){
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
133 cabecera_clusterscoordenates <- paste(cabecera_clusterscoordenates, cabecera[i], sep=" ")
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
134 }
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
135 #write(cabecera_clusterscoordenates, file="/home/galaxy/galaxy/tools/proteindocking/scripts/clusterscoordenates.txt", append= TRUE)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
136 write(cabecera_clusterscoordenates, file="clusterscoordenates.txt", append= TRUE)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
137
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
138 #Write rows containing number of cluster and coordenates
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
139 for(i in 1:nrow(clusters_tabla)){
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
140 fila_completa <- paste(clusters_tabla[i,1], sep = " ")
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
141 for(j in 2:ncol(clusters_tabla)){
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
142 fila_completa <- paste(fila_completa, clusters_tabla[i,j], sep=" ")
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
143 }
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
144 write(fila_completa, file="clusterscoordenates.txt", append= TRUE)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
145 #write(fila_completa, file="/home/galaxy/galaxy/tools/proteindocking/scripts/clusterscoordenates.txt", append= TRUE)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
146 }
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
147
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
148
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
149 #Table to see Binding Sited finded in Galaxy screen
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
150 #BS_screen <- scan("/home/galaxy/galaxy/tools/proteindocking/scripts/clusterscoordenates.txt", what = character(), quiet = TRUE)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
151 BS_screen <- scan("clusterscoordenates.txt", what = character(), quiet = TRUE)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
152
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
153 BS_table <- matrix(1, nrow = nrow(clusters_tabla), ncol = ncol(clusters_tabla))
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
154
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
155 bs_count <- 5
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
156 for(i in 1:nrow(clusters_tabla)){
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
157 for(j in 1:ncol(clusters_tabla)){
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
158 BS_table[i,j] <- BS_screen[bs_count]
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
159 bs_count <- bs_count +1
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
160 }
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
161 }
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
162
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
163 colnames(BS_table) <- c("Binding Site", "X", "Y", "Z")
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
164 print(BS_table)
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
165
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
166
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
167
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
168
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
169
6fcfa4756040 Uploaded
bitlab
parents:
diff changeset
170