-
Notifications
You must be signed in to change notification settings - Fork 0
/
readPara.r
153 lines (128 loc) · 2.74 KB
/
readPara.r
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
readParaSQLite<-function(paraDB)
{
# create a SQLite instance and create one connection.
m <- dbDriver("SQLite")
con <- dbConnect(m, dbname = paraDB)
# query
rs <- dbGetQuery(con, "select * from options where pname='File'")
# clean up
dbDisconnect(con)
params<-NULL
for(i in 1:nrow(rs))
{
paraName<-rs$pname[i]
paraValue<-rs$pvalue[i]
expr<-paste("params$DataFiles$",paraName,i,"=paraValue",sep="")
eval(parse(text=expr))
}
con <- dbConnect(m, dbname = paraDB)
# query
rs <- dbGetQuery(con, "select * from options where pname<>'File'")
# clean up
dbDisconnect(con)
for(i in 1:nrow(rs))
{
paraName<-rs$pname[i]
paraValue<-rs$pvalue[i]
expr<-paste("params$",paraName,"=paraValue",sep="")
eval(parse(text=expr))
}
return(params)
}
getDataFileFullPath<-function(params)
{
DataFilelist=params$DataFiles
for(i in 1:length(DataFilelist))
DataFilelist[[i]]<-paste(params$DataDir,DataFilelist[[i]],sep="")
DataFilelist
}
readParaFromCmd<-function(args)
{
library(RSQLite)
# dbFile<-"~/testing/KQC2/KQC.db"
dbFile<-args[1] # the first arg is dbFile name (includes path)
params<-readParaSQLite(dbFile)
params$DataFiles<-getDataFileFullPath(params)
params$clustType="MPI"
params$dbFile<-dbFile
params
}
split.matrix<-function(x,f) {
#print('processing matrix')
v=lapply(
1:dim(x)[[2]]
, function(i) {
base:::split.default(x[,i],f)#the difference is here
}
)
w=lapply(
seq(along=v[[1]])
, function(i) {
result=do.call(
cbind
, lapply(v,
function(vj) {
vj[[i]]
}
)
)
colnames(result)=colnames(x)
return(result)
}
)
names(w)=names(v[[1]])
return(w)
}
parseFileName<-function(filePath,isPath=T)
{
if(isPath)
{
fileName<-strsplit(filePath,"/")[[1]]#parse the path
fileName<-fileName[length(fileName)]#get last element as filename
}else
{
fileName<-filePath
}
sepFilename<-strsplit(fileName,".",fixed=TRUE)[[1]]
extName<-sepFilename[length(sepFilename)]
fileName<-substr(fileName,0,nchar(fileName)-nchar(extName)-1)#remove extention name
return(fileName)
}
dotProduct<- function(x, y)
{
if(length(x)<=10||length(y)<=10)
{
dotprod<-0
}else
{
dotprod<-(x%*%y)/((x%*%x)^0.5*(y%*%y)^0.5)
dotprod<-as.numeric(dotprod[1])
}
angle<-acos(dotprod)*180/pi
if(is.na(angle))
angle<-90
angle
#dotprod
}
f<- function(x, y)
{
(1-cor(x,y))*1000
}
distCal<-function (x)
{
ncy <- ncx <- ncol(x)
if (ncx == 0)
stop("'x' is empty")
r <- matrix(0, nrow = ncx, ncol = ncy)
for (i in seq_len(ncx)) {
for (j in seq_len(i)) {
x2 <- x[, i]
y2 <- x[, j]
r[i, j] <- ifelse(i==j,0,dotProduct(x2, y2))
}
}
r <- r + t(r) - diag(diag(r))
rownames(r) <- colnames(x)
colnames(r) <- colnames(x)
r
}