# TODO: Add comment
# 
# Author: E.Korsching Oct 15, 2014
###############################################################################


join.dataframe <- function(x,y, filter.space=F, info=F, multihit=F){
	# join two data.frames or matrices
	#  which own one (or some) id columns which define the relation between both objects
	# x,y: give one or more columns for each data.frame
	# filter.space: beforehand remove all blanks from the string
	# info: T: 1:n relations are resulting in a warning (see console)
	# multihit: T: keep all relations, F: only the first hit will be returned
	# return value - a list:
	#   x  and  y
	#  two data.frames which contain the perspectives  x->y and y->x  useful to merge the objects
	#   x.max  and  y.max
	#  the length of the two objects
	# related to : column.align.insert()  and R  merge()
	
	xld <- is.data.frame(x)
	xlv <- is.vector(x)
	yld <- is.data.frame(y)
	ylv <- is.vector(y)
	if((xld==F&xlv==F)|(yld==F&ylv==F)){ stop("x/y needs to be vector or data.frame - stop") }
	
	if(xld){
		nr.x <- nrow(x)
		nc.x <- ncol(x)
		cat("\n rows in x ",nr.x," cols in x ",nc.x,"\n")
	}
	if(xlv){
		nr.x <- length(x)
		nc.x <- 1
		cat("\n length of x ",nr.x,"\n")
	}
	if(yld){
		nr.y <- nrow(y)
		nc.y <- ncol(y)
		cat("\n rows in y ",nr.y," cols in y ",nc.y,"\n")
	}
	if(ylv){
		nr.y <- length(y)
		nc.y <- 1
		cat("\n length of y ",nr.y,"\n")
	}
	
	if(nc.x>1){
		x <- apply(x,1,paste,collapse="")
	}else{
		x <- as.character(x)
	}
	if(filter.space){ x <- gsub(" +", "", x) }
	
	if(nc.y>1){
		y <- apply(y,1,paste,collapse="")
	}else{
		y <- as.character(y)
	}
	if(filter.space){ y <- gsub(" +", "", y) }
#	return(list(x1,y1))
	
	# analyse x
	erg.x <- data.frame(matrix(0,1,3))
	names(erg.x) <- c("Xpos","inYpos","contentX")
	n1 <- 0	# count ambiguous relation(s)
	n2 <- 0	# count hits
	for(i in 1:nr.x){
		tmp <- x[i]		# get search str
		pos.found <- grep(pattern=paste("^",tmp,"$",sep=""), x=y, ignore.case=F, perl=F, value=F, fixed=F, invert=F)
		pos.found.len <- length(pos.found)
		if(pos.found.len>1){
			if(info){ cat("\n X: ambiguous relation x->y ;  posX: ",i," posY found [",pos.found.len,"] : ",pos.found,"\n   pattern: ",tmp) }
			n1 <- n1 +1
		}
		if(pos.found.len>0){
			if(multihit){
				for(j in 1:pos.found.len){		# get all hits
					erg.x <- rbind(erg.x,c(i,pos.found[j],tmp))
					n2 <- n2 +1
				}
			}else{		# only the first hit
				erg.x <- rbind(erg.x,c(i,pos.found[1],tmp))
				n2 <- n2 +1
			}
		}
	}
	
	# remove first template row
	erg.x <- erg.x[-1 , ]
	# customize
	erg.x <- transform(erg.x, Xpos=as.numeric(Xpos), inYpos=as.numeric(inYpos))
	
	# analyse y
	erg.y <- data.frame(matrix(0,1,3))
	names(erg.y) <- c("Ypos","inXpos","contentY")
	n3 <- 0	# counter ambiguous relation
	n4 <- 0	# counter hits
	for(i in 1:nr.y){
		tmp <- y[i]		# get search str
		pos.found <- grep(pattern=paste("^",tmp,"$",sep=""), x=x, ignore.case=F, perl=F, value=F, fixed=F, invert=F)
		pos.found.len <- length(pos.found)
		if(pos.found.len>1){
			if(info){ cat("\n Y: ambiguous relation y->x ;  posY: ",i," posX found [",pos.found.len,"] : ",pos.found,"\n   pattern: ",tmp) }
			n3 <- n3 +1
		}
		if(pos.found.len>0){
			if(multihit){
				for(j in 1:pos.found.len){		# get all hits
					erg.y <- rbind(erg.y,c(i,pos.found[j],tmp))
					n4 <- n4 +1
				}
			}else{		# only the first hit
				erg.y <- rbind(erg.y,c(i,pos.found[1],tmp))
				n4 <- n4 +1
			}
		}
	}
	
	# remove first template row
	erg.y <- erg.y[-1 , ]
	# customize
	erg.y <- transform(erg.y, Ypos=as.numeric(Ypos), inXpos=as.numeric(inXpos))
	
	# clean up
	cat("\n\n x: number of ambiguous relations: ",n1," number of hits: ",n2,"\n")
	cat("\n y: number of ambiguous relations: ",n3," number of hits: ",n4,"\n")
	return(list(x=erg.x, y=erg.y, x.len=nr.x, y.len=nr.y))
}


# aa <- join.dataframe(x=h.br.09.2014.a[,1:3],y=h.br.09.2014.clin.a[,1:3], filter.space=T, info=F, multihit=F)
# aa <- join.dataframe(x=h.br.09.2014.a[,1:3],y=h.br.09.2014.clin.a[,1:3], filter.space=F)
# ab <- join.dataframe(x=h.br.09.2014.a[,1],y=h.br.09.2014.clin.a[,1], filter.space=T)
# ab <- join.dataframe(x=h.br.09.2014.a[,1],y=h.br.09.2014.clin.a[,1], filter.space=F)
# ab[[1]]

# checkset
#h.br.join <- join.dataframe(x=h.br.09.2014.a[,1],y=h.br.09.2014.clin[,1], filter.space=T)
# x: number of ambiguous relations:  41  number of hits:  318 
# y: number of ambiguous relations:  0  number of hits:  359 
#
#nrow(h.br.join[[1]])	# 318
#nrow(h.br.join[[2]])	# 359
#
#aa <- cbind(h.br.09.2014.a[h.br.join[[1]][,1], 1:3],
#		h.br.09.2014.clin[h.br.join[[1]][,2], 1:3] )
#ab <- cbind(h.br.09.2014.a[h.br.join[[2]][,2], 1:3],
#		h.br.09.2014.clin[h.br.join[[2]][,1], 1:3] )
#
#aa <- aa[order(aa[,1]),]
#ab <- ab[order(ab[,1]),]
#ab <- ab[!duplicated(ab[,1]),]		# 318


