.packageName <- "ChIPpeakAnno"
#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/BED2RangedData.R"
BED2RangedData <- function(data.BED,header=FALSE)
{
	if (missing(data.BED) || (class(data.BED) != "data.frame") || dim(data.BED)[2] <3)
	{
		stop("No valid data.BED passed in, which is a data frame as BED format file with at least 3 fields in the order of: chromosome, start and end. Optional fields are name, score and strand etc. Please refer to http://genome.ucsc.edu/FAQ/FAQformat#format1 for details.")
	}
	if (header == TRUE)
	{	
		myPeak = data.BED[-1,]
	}
	else
	{
		myPeak = data.BED
	}	
	if (dim(myPeak)[2] >=4)
	{
		names = as.character(myPeak[,4])
		if (length(unique(names))==1)
		{
			  names = formatC(1:dim(myPeak)[1], width=nchar(dim(myPeak)[1]), flag='0')
		}
	}
	else
	{
		 names = formatC(1:dim(myPeak)[1], width=nchar(dim(myPeak)[1]), flag='0')
	}
	if (dim(myPeak)[2] >= 5)
	{		
		score = as.numeric(as.character(myPeak[,5]))
	}
	else
	{
		score = rep(1, dim(myPeak)[1])
	}
	if (dim(myPeak)[2] >= 6)
	{		
		strand = as.character(myPeak[,6])
		strand[strand== "+"] = 1
		strand[strand=="-"] = -1
	}
	else
	{
		strand = rep(1, dim(myPeak)[1])
	}
	RangedData(IRanges(start=as.numeric(as.character(myPeak[,2])), end=as.numeric(as.character(myPeak[,3])), names = names), space = sub("chr", "", as.character(myPeak[,1])), strand = strand, score=score )
}
#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/GFF2RangedData.R"
GFF2RangedData <- function(data.GFF,header=FALSE)
{
	if (missing(data.GFF) || (class(data.GFF) != "data.frame") || dim(data.GFF)[2] != 9)
	{
		stop("No valid data.GFF passed in, which is a data frame as GFF format with 9 required fields. Please refer to http://genome.ucsc.edu/FAQ/FAQformat#format3 for details.")
	}
	if (header == TRUE)
	{	
		myPeak = data.GFF[-1,]
	}
	else
	{
		myPeak = data.GFF
	}	
	strand = as.character(myPeak[,7])
	strand[strand== "+"] = 1
	strand[strand=="-"] = -1
	names = formatC(1:dim(myPeak)[1], width=nchar(dim(myPeak)[1]), flag='0')
	
	RangedData(IRanges(start=as.numeric(as.character(myPeak[,4])), end=as.numeric(as.character(myPeak[,5])), names = names), space = sub("chr", "", as.character(myPeak[,1])), strand = strand, score=as.numeric(as.character(myPeak[,6])))
}
#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/addAncestors.R"
addAncestors <- function(go.ids, ontology=c("bp","cc", "mf"))
{
		ontology = match.arg(ontology)
		if (missing(go.ids))
		{
			stop("missing required parameter go.ids!")
		}
		if (class(go.ids) != "matrix" | dim(go.ids)[2] <4)
		{
			stop("go.ids need to be a matrix with at least 4 columns, the first column is go id and the second column is the entrez ID!")
		}
		if (ontology =="bp")
		{
			xx <- as.list(GOBPANCESTOR)
		}
		else if (ontology =="cc")
		{
			xx <- as.list(GOCCANCESTOR)
		}
		else if (ontology =="mf")
		{
			xx <- as.list(GOMFANCESTOR)
		}
		xx <- xx[!is.na(xx)]
		
	if (length(xx) <=0)
	{
		stop("No GO Ancestors! Should not have happened!")
	}
	go.ids.withAncestor = intersect(as.character(go.ids[,1]), names(xx))
	if (length(go.ids.withAncestor) >0)
	{
	Ancestors =	do.call(rbind, lapply(go.ids.withAncestor,function(x1)
		{
			r= xx[names(xx)==x1]
			if (length(r) > 0 )
			{
				cbind(rep(x1, length(r[[1]])), r[[1]])
			}
		}))
	Ancestors = Ancestors[Ancestors[,2] !="all",]
	colnames(Ancestors) = c("child", "ancestor")
	children = cbind(go.ids[,1],go.ids[,4])
	colnames(children) = c("child", "entrezID")
	go.all = merge(children, Ancestors, by="child")
	temp = cbind(c(as.character(go.all$child),as.character(go.all$ancestor)), c(as.character(go.all$entrezID), as.character(go.all$entrezID)))
	temp = unique(temp)
	if (length(temp) <3)
	{
		as.character(go.ids[,1])
	}
	else
	{
		temp[,1]
	}
  }
  else
  {
	as.character(go.ids[,1])
  }
}
#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/annotatePeakInBatch.R"
annotatePeakInBatch <-
function(myPeakList, mart,featureType=c("TSS","miRNA", "Exon"), AnnotationData,output=c("nearestStart", "overlapping","both"),multiple=c(FALSE,TRUE), maxgap=0)
{
		featureType = match.arg(featureType)
		if (missing(output))
		{
			output = "nearestStart"
		}
		if ((output == "overlapping" || output == "both") && missing(multiple))
		{
			stop("Missing requried logical argument multiple. It is requried when output is overlapping or both!") 
		}
		
		if (missing(myPeakList))
		{
			stop("Missing required argument myPeakList!")
		}
		if (class(myPeakList) != "RangedData")
		{
			stop("No valid myPeakList passed in. It needs to be RangedData object")
		}
		if (missing(AnnotationData))
		{
			message("No AnnotationData as RangedData is passed in, so now querying biomart database for AnnotationData ....")
			if (missing(mart) || class(mart) !="Mart")
			{
				stop("Error in querying biomart database. No valid mart object is passed in! Suggest call getAnnotation before calling annotatePeakInBatch")
			}
			AnnotationData<- getAnnotation(mart, feature=featureType)
			message("Done querying biomart database, start annotating ....Better way would be calling getAnnotation before calling annotatePeakInBatch")
		}
		if (class(AnnotationData) != "RangedData")
		{
			stop("AnnotationData needs to be RangedData object")
		}
		
		TSS.ordered <-AnnotationData
		rm(AnnotationData)
		if (!length(rownames(TSS.ordered)))
		{
			rownames(TSS.ordered) = formatC(1:dim(TSS.ordered)[1], width=nchar(dim(TSS.ordered)[1]), flag='0')
		}
		if (length(TSS.ordered$strand) == length(start(TSS.ordered))) 
{ 
	r2 = cbind(rownames(TSS.ordered), start(TSS.ordered), end(TSS.ordered), as.character(TSS.ordered$strand))
}
else
{
	TSS.ordered$strand = rep("+",length(start(TSS.ordered)))
	r2 = cbind(rownames(TSS.ordered), start(TSS.ordered), end(TSS.ordered), rep("+",length(start(TSS.ordered))))
}

		colnames(r2) = c("feature_id", "start_position", "end_position", "strand")
		allChr.Anno = unique(space(TSS.ordered))
		
		#allChr.Anno = sub("chr", '', allChr.Anno)
		
		numberOfChromosome = length(unique(space(myPeakList)))
	
		if (!length(rownames(myPeakList)))
		{
			rownames(myPeakList) = formatC(1:dim(myPeakList)[1], width=nchar(dim(myPeakList)[1]), flag='0')
		}
		
		allChr = unique(as.character(space(myPeakList)))
		allChr = sub(' +', '',allChr)
		
		if(length(grep("chr", allChr, fixed=TRUE))>0 && length(grep("chr", allChr.Anno, fixed=TRUE))==0)
		{
			allChr = sub("chr", '', allChr)
			myPeakList = RangedData(IRanges(start=start(myPeakList), 
							end= end(myPeakList),
							names = rownames(myPeakList)),
							space = sub("chr", "", space(myPeakList))
							)
		}
		
		if(length(grep("chr", allChr, fixed=TRUE))==0 && length(grep("chr", allChr.Anno, fixed=TRUE))>0)
		{
			allChr = paste("chr", allChr, sep="")
			myPeakList = RangedData(IRanges(start=start(myPeakList), 
							end= end(myPeakList),
							names = rownames(myPeakList)),
							space = paste("chr", space(myPeakList), sep="")
							)
		}
		if (output == "nearestStart" || output == "both" || output == "n" || output == "b")
		{
			z1 = cbind(as.character(rownames(myPeakList)), as.character(space(myPeakList)),start(myPeakList), end(myPeakList))
			colnames(z1) = c("name", "chr", "peakStart", "peakEnd")
			z1[,2] = sub(' +', '',z1[,2])
		
			plusAnno = TSS.ordered[TSS.ordered$strand==1 | TSS.ordered$strand =="+",]
			minusAnno = TSS.ordered[TSS.ordered$strand== -1 | TSS.ordered$strand =="-",]
			r1 = do.call(rbind, lapply(seq_len(numberOfChromosome), function(i) {
				chr = allChr[i]
				if(chr %in% allChr.Anno)
				{
           		featureStart = c(start(plusAnno[chr]), end(minusAnno[chr]))				
				peakStart = start(myPeakList[chr])
				peakEnd = end(myPeakList[chr])
				name = rownames(myPeakList[chr])
				peakRanges = IRanges(start=peakStart, end=peakEnd, names=name)
				featureID  = c(rownames(plusAnno[chr]), rownames(minusAnno[chr]))
				featureRanges = IRanges(start=featureStart, end=featureStart, names=featureID)
				nearestFeature = featureRanges[nearest(peakRanges,featureRanges)]
         		data.frame(name = name,
                    chr = rep(chr,length(peakStart)),
                    peakStart = peakStart,
                    peakEnd = peakEnd,
                    feature_id =  names(nearestFeature))
				}
			}))
		
		if (length(r1)>0)
		{
			r3 = merge(r1,r2, by="feature_id")
			r = merge(r3, z1, all.y=TRUE)
			r.n = r
			r11 = r[!is.na(r$strand) & (r$strand==1 | r$strand =="+"),]
			r22 = r[!is.na(r$strand) & (r$strand==-1 | r$strand =="-"),]
			r33 = r[is.na(r$strand),]
			r33$insideFeature = replicate(length(r33$name), NA)
			r33$distancetoFeature = replicate(length(r33$name), NA)
		
		if (dim(r11)[1] >0)
		{
		distancetoFeature =  as.numeric(as.character(r11$peakStart)) - as.numeric(as.character(r11$start_position))
		length =  as.numeric(as.character(r11$end_position)) -  as.numeric(as.character(r11$start_position))
		#insideFeature = (distancetoFeature>=0 & distancetoFeature <= length)
		insideFeature = do.call(c, lapply(seq(from=1,to=dim(r11)[1],by=1), function(i) 	{
		if (as.numeric(as.character(r11$peakStart[i]))>= as.numeric(as.character(r11$start_position[i])) &&
			as.numeric(as.character(r11$peakStart[i]))<= as.numeric(as.character(r11$end_position[i])))
		{
			if (as.numeric(as.character(r11$peakEnd[i])) >= as.numeric(as.character(r11$start_position[i])) &&
			as.numeric(as.character(r11$peakEnd[i])) <= as.numeric(as.character(r11$end_position[i])))
			{
				"inside"
			}
			else
			{
				"overlapEnd"
			}
		}
		else if (as.numeric(as.character(r11$peakEnd[i])) >= as.numeric(as.character(r11$start_position[i])) &&
			as.numeric(as.character(r11$peakEnd[i])) <= as.numeric(as.character(r11$end_position[i])))
		{
				"overlapStart"
		}
		else if (as.numeric(as.character(r11$peakEnd[i])) >= as.numeric(as.character(r11$end_position[i])) &&
			as.numeric(as.character(r11$peakStart[i])) <= as.numeric(as.character(r11$start_position[i])))
		{
				 "includeFeature"
		}
		else if (as.numeric(as.character(r11$peakEnd[i])) < as.numeric(as.character(r11$start_position[i])))
		{
				"upstream"
		}
		else if (as.numeric(as.character(r11$peakStart[i])) > as.numeric(as.character(r11$end_position[i])))
		{
				"downstream"
		}
		}))
		r11$insideFeature = insideFeature		
		r11$distancetoFeature = distancetoFeature
		r.n= r11
		}
		if (dim(r22)[1] >0)
		{		
		distancetoFeature =  as.numeric(as.character(r22$end_position)) - as.numeric(as.character(r22$peakStart))
		length =  as.numeric(as.character(r22$end_position)) -  as.numeric(as.character(r22$start_position))
		#insideFeature = (distancetoFeature>=0 & distancetoFeature <= length)	
		insideFeature = do.call(c, lapply(seq(from=1,to=dim(r22)[1],by=1), function(i) 	{		
			if (as.numeric(as.character(r22$peakStart[i]))>= as.numeric(as.character(r22$start_position[i])) &&
			as.numeric(as.character(r22$peakStart[i]))<= as.numeric(as.character(r22$end_position[i])))
			{
			if (as.numeric(as.character(r22$peakEnd[i])) >= as.numeric(as.character(r22$start_position[i])) &&
			as.numeric(as.character(r22$peakEnd[i])) <= as.numeric(as.character(r22$end_position[i])))
			{
				"inside"
			}
			else
			{
				"overlapStart"
			}
			}
		else if (as.numeric(as.character(r22$peakEnd[i])) >= as.numeric(as.character(r22$start_position[i])) &&
			as.numeric(as.character(r22$peakEnd[i])) <= as.numeric(as.character(r22$end_position[i])))
		{
				"overlapEnd"
		}
		else if (as.numeric(as.character(r22$peakEnd[i])) >= as.numeric(as.character(r22$end_position[i])) &&
			as.numeric(as.character(r22$peakStart[i])) <= as.numeric(as.character(r22$start_position[i])))
		{
				 "includeFeature"
		}
		else if (as.numeric(as.character(r22$peakEnd[i])) < as.numeric(as.character(r22$start_position[i])))
		{
				"downstream"
		}
		else if (as.numeric(as.character(r22$peakStart[i])) > as.numeric(as.character(r22$end_position[i])))
		{
				"upstream"
		}
		}))					
							
		r22$insideFeature = insideFeature
		r22$distancetoFeature = distancetoFeature
		if (dim(r11)[1] >0)
		{
			r.n = rbind(r.n,r22)
		}
		else
		{
			r.n = r22
		}
		}## if dim(r22)[1] >0
		if (dim(r33)[1] >0)
		{
			if (dim(r11)[1] > 0 || dim(r22)[1] >0)
			{
				r.n = rbind(r.n, r33)
			}
			else
			{
				r.n = r33
			}
		}
		r.n$fromOverlappingOrNearest = rep("NearestStart", dim(r.n)[1])
		shortestDistance = apply(cbind(abs(as.numeric(as.character(r.n$start_position))-as.numeric(as.character(r.n$peakEnd))), 
					abs(as.numeric(as.character(r.n$start_position))-as.numeric(as.character(r.n$peakStart))), 
					abs(as.numeric(as.character(r.n$end_position))-as.numeric(as.character(r.n$peakEnd))),
					abs(as.numeric(as.character(r.n$end_position))-as.numeric(as.character(r.n$peakStart))))
		,1,min)	
		r.n$shortestDistance = shortestDistance
	
		} ## if length(r1) >0
		} ## if output == "nearestStart"
		if (output == "overlapping" || output == "both" || output == "o" || output == "b")
		{
			r.o = findOverlappingPeaks(myPeakList,TSS.ordered ,maxgap=maxgap,multiple=multiple, NameOfPeaks1="peak", NameOfPeaks2="feature")$OverlappingPeaks
			if (dim(r.o)[1] >0)
			{
			r.o$fromOverlappingOrNearest = rep("Overlapping", dim(r.o)[1])
			distancetoFeature =  do.call(c, lapply(seq_len(dim(r.o)[1]), function(i)
			{
				if (as.character(r.o$strand[i])=="1" | as.character(r.o$strand[i]) =="+")
				{
					as.numeric(as.character(r.o$peak_start[i])) - as.numeric(as.character(r.o$feature_start[i]))
				}
				else
				{
					as.numeric(as.character(r.o$feature_end[i])) - as.numeric(as.character(r.o$peak_start[i]))
				}
			}))
			r.o$distancetoFeature = distancetoFeature
			}
		}
		if (output == "nearestStart" || output == "n" || ((output =="both" || output=="b")  && dim(r.o)[1] ==0))
		{
			if(length(r1)>0)
			{
				r.output = RangedData(IRanges(start=as.numeric(as.character(r.n$peakStart)), 
						end=as.numeric(as.character(r.n$peakEnd)),
						names=paste(as.character(r.n$name),as.character(r.n$feature_id))),
          		peak = as.character(r.n$name),
				strand = as.character(r.n$strand), 
				feature = as.character(r.n$feature_id),
				start_position= as.numeric(as.character(r.n$start_position)),
				end_position=as.numeric(as.character(r.n$end_position)),
				insideFeature=as.character(r.n$insideFeature), 
				distancetoFeature=as.numeric(as.character(r.n$distancetoFeature)),
				shortestDistance = as.numeric(as.character(r.n$shortestDistance)),
				fromOverlappingOrNearest = as.character(r.n$fromOverlappingOrNearest),
				space = as.character(r.n$chr)
				)
				#r.output[order(rownames(r.output)),]
			}
			else
			{
				r.output = myPeakList
			}
		}
		else if ((output =="overlapping" || output =="o") || ((output =="both" || output=="b") && length(r1) ==0))
		{
			if (dim(r.o)[1] >0)
			{
				r.output=RangedData(IRanges(start=as.numeric(as.character(r.o$peak_start)), end=as.numeric(as.character(r.o$peak_end)),
								names=paste(as.character(r.o$peak),as.character(r.o$feature))), 
								peak = as.character(r.o$peak),
								strand = as.character(r.o$strand),
								feature = as.character(r.o$feature),
								start_position= as.numeric(as.character(r.o$feature_start)),
								end_position= as.numeric(as.character(r.o$feature_end)),
								insideFeature= as.character(r.o$overlapFeature),
								distancetoFeature=as.numeric(as.character(r.o$distancetoFeature)),
								shortestDistance = as.numeric(as.character(r.o$shortestDistance)),
								fromOverlappingOrNearest = as.character(r.o$fromOverlappingOrNearest),
								space = as.character(r.o$chr)
								)
			}
			else
			{
				r.output = myPeakList
			}
		}		
		else if (output == "both" || output=="b")
		{
			debug =0
			if (debug == 0)
			{
				r.o = cbind(as.character(r.o$peak), as.character(r.o$chr),as.numeric(as.character(r.o$peak_start)), 
						as.numeric(as.character(r.o$peak_end)), as.character(r.o$feature),
						as.numeric(as.character(r.o$feature_start)), as.numeric(as.character(r.o$feature_end)),
						as.character(r.o$strand),  as.character(r.o$overlapFeature),
						as.numeric(as.character(r.o$distancetoFeature)),as.character(r.o$fromOverlappingOrNearest),as.numeric(as.character(r.o$shortestDistance))
						)
				colnames(r.o) = c("name","chr", "peakStart", "peakEnd", "feature_id", "start_position", "end_position",
								"strand", "insideFeature", "distancetoFeature", "fromOverlappingOrNearest", "shortestDistance")
				temp = setdiff(paste(r.o[,1], r.o[,5]), paste(r.n[,1], r.n[,5]))
				if (length(temp) >0)
				{
					r.o.only = r.o[paste(r.o[,1], r.o[,5]) %in% temp,]
					r.o.only = matrix(r.o.only, ncol=12)
					colnames(r.o.only) = c("name","chr", "peakStart", "peakEnd", "feature_id", "start_position", "end_position",
								"strand", "insideFeature", "distancetoFeature", "fromOverlappingOrNearest", "shortestDistance")
					r.both = rbind(r.n, r.o.only)
					r.output = RangedData(IRanges(start=as.numeric(as.character(r.both$peakStart)), 
						end=as.numeric(as.character(r.both$peakEnd)),
						names=paste(as.character(r.both$name),as.character(r.both$feature_id))), 
					peak = as.character(r.both$name),
					strand = as.character(r.both$strand), 
					feature = as.character(r.both$feature_id),
					start_position= as.numeric(as.character(r.both$start_position)),
					end_position=as.numeric(as.character(r.both$end_position)),
					insideFeature=as.character(r.both$insideFeature), 
					distancetoFeature=as.numeric(as.character(r.both$distancetoFeature)),
					shortestDistance = as.numeric(as.character(r.both$shortestDistance)),
					fromOverlappingOrNearest = as.character(r.both$fromOverlappingOrNearest),
					space = as.character(r.both$chr))
				}
				else
				{
					r.output = RangedData(IRanges(start=as.numeric(as.character(r.n$peakStart)), 
						end=as.numeric(as.character(r.n$peakEnd)),
						names=paste(as.character(r.n$name),as.character(r.n$feature_id))),
					peak = as.character(r.n$name),
					strand = as.character(r.n$strand), 
					feature = as.character(r.n$feature_id),
					start_position= as.numeric(as.character(r.n$start_position)),
					end_position=as.numeric(as.character(r.n$end_position)),
					insideFeature=as.character(r.n$insideFeature), 
					distancetoFeature=as.numeric(as.character(r.n$distancetoFeature)),
					shortestDistance = as.numeric(as.character(r.n$shortestDistance)),
					fromOverlappingOrNearest = as.character(r.n$fromOverlappingOrNearest),
					space = as.character(r.n$chr)
				)
				}
			}
		}
			r.output
		}
#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/convert2EntrezID.R"
convert2EntrezID <- function(IDs, orgAnn, ID_type="ensembl_gene_id")
{
	GOgenome = sub(".db","",orgAnn)
	if (ID_type == "ensembl_gene_id")
	{
		orgAnn <- get(paste(GOgenome,"ENSEMBL2EG", sep=""))
	}
	else if (ID_type == "gene_symbol")
	{
		orgAnn <- get(paste(GOgenome,"SYMBOL2EG", sep=""))
	}
	else if (ID_type == "refseq_id")
	{
		orgAnn <- get(paste(GOgenome,"REFSEQ2EG", sep=""))
	}
	else
	{
		stop("Currently only the following type of IDs are supported: ensembl_gene_id, refseq_id and gene_symbol!")
	}

	if(class(orgAnn) != "AnnDbBimap")
	{
		stop("orgAnn is not a valid annotation dataset! For example, orgs.Hs.eg.db package for human
			 and the org.Mm.eg.db package for mouse.")
	}
	xx <- as.list(orgAnn)
	IDs = unique(IDs[!is.na(IDs)])
	x = do.call(rbind, lapply(IDs,function(x1)
		{
			r= xx[names(xx)==x1]
			if (length(r) >0)
			{
				r[[1]][1]
			}
		}))
	unique(x)
}
#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/findOverlappingPeaks.R"
findOverlappingPeaks<-function (Peaks1, Peaks2, maxgap = 100, multiple = c(TRUE, FALSE), 
    NameOfPeaks1 = "TF1", NameOfPeaks2 = "TF2") 
{
    if (missing(Peaks1)) {
        stop("Missing Peaks1 which is required!")
    }
    if (class(Peaks1) != "RangedData") {
        stop("No valid Peaks1 passed in. It needs to be RangedData object")
    }
    if (missing(Peaks2)) {
        stop("No Peaks2 as RangedData is passed in")
    }
    if (class(Peaks2) != "RangedData") {
        stop("Peaks2 needs to be RangedData object")
    }
    if (!length(rownames(Peaks1))) {
        rownames(Peaks1) = formatC(1:dim(Peaks1)[1], width = nchar(dim(Peaks1)[1]), 
            flag = "0")
    }
    if (!length(rownames(Peaks2))) {
        rownames(Peaks2) = formatC(1:dim(Peaks2)[1], width = nchar(dim(Peaks2)[1]), 
            flag = "0")
    }
    if(length(Peaks2$strand) ==0)
    {
    		Peaks2$strand = rep("+", length(start(Peaks2)))
    	}
 else
    {
        Peaks2$strand[as.character(Peaks2$strand) == "1"] = "+";
        Peaks2$strand[as.character(Peaks2$strand) == "-1"] = "-";
   }

    if(length(Peaks1$strand) ==0)
    {
    		Peaks1$strand = rep("+", length(start(Peaks1)))
    	}
	 else
    {
        Peaks1$strand[as.character(Peaks1$strand) == "1"] = "+";
        Peaks1$strand[as.character(Peaks1$strand) == "-1"] = "-";       
    }

    r2 = cbind(rownames(Peaks2), start(Peaks2), end(Peaks2), 
        Peaks2$strand)
    colnames(r2) = c(NameOfPeaks2, paste(NameOfPeaks2, "start", 
        sep = "_"), paste(NameOfPeaks2, "end", sep = "_"), "strand")
    allChr.Anno = unique(space(Peaks2))
    numberOfChromosome = length(unique(space(Peaks1)))
    allChr = unique(as.character(space(Peaks1)))
    allChr = sub(" +", "", allChr)
    if (length(grep("chr", allChr, fixed = TRUE)) > 0 && length(grep("chr", 
        allChr.Anno, fixed = TRUE)) == 0) {
        allChr = sub("chr", "", allChr)
        Peaks1 = RangedData(IRanges(start = start(Peaks1), end = end(Peaks1), 
            names = rownames(Peaks1)), space = sub("chr", "", 
            space(Peaks1)), strand = Peaks1$strand)
    }
    if (length(grep("chr", allChr, fixed = TRUE)) == 0 && length(grep("chr", 
        allChr.Anno, fixed = TRUE)) > 0) {
        allChr = paste("chr", allChr, sep = "")
        Peaks1 = RangedData(IRanges(start = start(Peaks1), end = end(Peaks1), 
            names = rownames(Peaks1)), space = paste("chr", space(Peaks1), 
            sep = ""), strand = Peaks1$strand)
    }
    z1 = cbind(as.character(rownames(Peaks1)), as.character(space(Peaks1)), 
        start(Peaks1), end(Peaks1), Peaks1$strand)
    if (length(Peaks1$strand) == 0) {
        z1 = cbind(z1, rep("1", dim(z1)[1]))
    }
    colnames(z1) = c(NameOfPeaks1, "chr", paste(NameOfPeaks1, 
        "start", sep = "_"), paste(NameOfPeaks1, "end", sep = "_"), 
        "strand1")
    z1[, 2] = sub(" +", "", z1[, 2])
    r1 = do.call(rbind, lapply(seq_len(numberOfChromosome), function(i) {
        chr = allChr[i]
        if (chr %in% allChr.Anno) {
            Ranges1 = sort(IRanges(start = start(Peaks1[chr]), 
                end = end(Peaks1[chr]), names = rownames(Peaks1[chr])))
            Ranges2 = sort(IRanges(start = start(Peaks2[chr]), 
                end = end(Peaks2[chr]), names = rownames(Peaks2[chr])))
            tree = IntervalTree(Ranges2)
            matches = findOverlaps(tree, query = Ranges1, maxgap = maxgap, 
                multiple = multiple)
            if (multiple) {
                matchmatrix = matchMatrix(matches)
                qname = names(Ranges1)[matchmatrix[, 1]]
                tname = names(Ranges2)[matchmatrix[, 2]]
                data.frame(Peaks1 = qname, chr = rep(chr, dim(matchmatrix)[1]), 
                  Peaks2 = tname)
            }
            else {
                qname = names(Ranges1)
                tname = names(Ranges2)[matches]
                data.frame(Peaks1 = qname, chr = rep(chr, length(qname)), 
                  Peaks2 = tname)
            }
        }
    }))
    colnames(r1) = c(NameOfPeaks1, "chr", NameOfPeaks2)
    r3 = merge(r1, r2, by = NameOfPeaks2)
    r = merge(r3, z1)
    if (dim(r)[1] < 1) {
        Peaks1withOverlaps = 0
        Peaks2withOverlaps = 0
        MergedPeaks = 0
    }
    else {
        shortestDistance = apply(cbind(abs(as.numeric(as.character(r[[paste(NameOfPeaks2, 
            "start", sep = "_")]])) - as.numeric(as.character(r[[paste(NameOfPeaks1, 
            "end", sep = "_")]]))), abs(as.numeric(as.character(r[[paste(NameOfPeaks2, 
            "start", sep = "_")]])) - as.numeric(as.character(r[[paste(NameOfPeaks1, 
            "start", sep = "_")]]))), abs(as.numeric(as.character(r[[paste(NameOfPeaks2, 
            "end", sep = "_")]])) - as.numeric(as.character(r[[paste(NameOfPeaks1, 
            "end", sep = "_")]]))), abs(as.numeric(as.character(r[[paste(NameOfPeaks2, 
            "end", sep = "_")]])) - as.numeric(as.character(r[[paste(NameOfPeaks1, 
            "start", sep = "_")]])))), 1, min)
        overlapFeature = do.call(c, lapply(seq(from = 1, to = dim(r)[1], 
            by = 1), function(i) {
            if (as.numeric(as.character(r[[paste(NameOfPeaks1, 
                "start", sep = "_")]][i])) >= as.numeric(as.character(r[[paste(NameOfPeaks2, 
                "start", sep = "_")]][i])) && as.numeric(as.character(r[[paste(NameOfPeaks1, 
                "start", sep = "_")]][i])) <= as.numeric(as.character(r[[paste(NameOfPeaks2, 
                "end", sep = "_")]][i]))) {
                if (as.numeric(as.character(r[[paste(NameOfPeaks1, 
                  "end", sep = "_")]][i])) >= as.numeric(as.character(r[[paste(NameOfPeaks2, 
                  "start", sep = "_")]][i])) && as.numeric(as.character(r[[paste(NameOfPeaks1, 
                  "end", sep = "_")]][i])) <= as.numeric(as.character(r[[paste(NameOfPeaks2, 
                  "end", sep = "_")]][i]))) {
                  "inside"
                }
                else if (as.character(r$strand[i]) == "1" || as.character(r$strand[i]) == 
                  "+") {
                  "overlapEnd"
                }
                else {
                  "overlapStart"
                }
            }
            else if (as.numeric(as.character(r[[paste(NameOfPeaks1, 
                "end", sep = "_")]][i])) >= as.numeric(as.character(r[[paste(NameOfPeaks2, 
                "start", sep = "_")]][i])) && as.numeric(as.character(r[[paste(NameOfPeaks1, 
                "end", sep = "_")]][i])) <= as.numeric(as.character(r[[paste(NameOfPeaks2, 
                "end", sep = "_")]][i]))) {
                if (as.character(r$strand[i]) == "1" | as.character(r$strand[i]) == 
                  "+") {
                  "overlapStart"
                }
                else {
                  "overlapEnd"
                }
            }
            else if (as.numeric(as.character(r[[paste(NameOfPeaks1, 
                "end", sep = "_")]][i])) >= as.numeric(as.character(r[[paste(NameOfPeaks2, 
                "end", sep = "_")]][i])) && as.numeric(as.character(r[[paste(NameOfPeaks1, 
                "start", sep = "_")]][i])) <= as.numeric(as.character(r[[paste(NameOfPeaks2, 
                "start", sep = "_")]][i]))) {
                "includeFeature"
            }
            else if (as.numeric(as.character(r[[paste(NameOfPeaks1, 
                "end", sep = "_")]][i])) < as.numeric(as.character(r[[paste(NameOfPeaks2, 
                "start", sep = "_")]][i]))) {
                if (as.character(r$strand[i]) == "1" || as.character(r$strand[i]) == 
                  "+") {
                  "upstream"
                }
                else {
                  "downstream"
                }
            }
            else if (as.numeric(as.character(r[[paste(NameOfPeaks1, 
                "start", sep = "_")]][i])) > as.numeric(as.character(r[[paste(NameOfPeaks2, 
                "end", sep = "_")]][i]))) {
                if (as.character(r$strand[i]) == "1" || as.character(r$strand[i]) == 
                  "+") {
                  "downstream"
                }
                else {
                  "upstream"
                }
            }
        }))
        r$overlapFeature = overlapFeature
        r$shortestDistance = shortestDistance
        minstart = apply(cbind(as.numeric(as.character(r[[paste(NameOfPeaks1, 
            "start", sep = "_")]])), as.numeric(as.character(r[[paste(NameOfPeaks2, 
            "start", sep = "_")]]))), MARGIN = 1, min)
        maxend = apply(cbind(as.numeric(as.character(r[[paste(NameOfPeaks1, 
            "end", sep = "_")]])), as.numeric(as.character(r[[paste(NameOfPeaks2, 
            "end", sep = "_")]]))), MARGIN = 1, max)
        MergedPeaks = RangedData(IRanges(start = minstart, end = maxend, 
            names = paste(NameOfPeaks1, as.character(r[[NameOfPeaks1]]), 
                NameOfPeaks2, as.character(r[[NameOfPeaks2]]), 
                sep = "-")), space = as.character(r$chr))
        r1 = unique(cbind(as.character(r[[NameOfPeaks1]]), as.character(r$chr), 
            as.numeric(as.character(r[[paste(NameOfPeaks1, "start", 
                sep = "_")]])), as.numeric(as.character(r[[paste(NameOfPeaks1, 
                "end", sep = "_")]])), as.character(r$strand1)))
        r2 = unique(cbind(as.character(r[[NameOfPeaks2]]), as.character(r$chr), 
            as.numeric(as.character(r[[paste(NameOfPeaks2, "start", 
                sep = "_")]])), as.numeric(as.character(r[[paste(NameOfPeaks2, 
                "end", sep = "_")]])), as.character(r$strand)))
        Peaks1withOverlaps = RangedData(IRanges(start = as.numeric(as.character(r1[, 
            3])), end = as.numeric(as.character(r1[, 4])), names = as.character(r1[, 
            1])), space = as.character(r1[, 2]), strand = as.character(r1[, 
            5]))
        Peaks2withOverlaps = RangedData(IRanges(start = as.numeric(as.character(r2[, 
            3])), end = as.numeric(as.character(r2[, 4])), names = as.character(r2[, 
            1])), space = as.character(r2[, 2]), strand = as.character(r2[, 
            5]))
    }
    list(OverlappingPeaks = r[order(r[[NameOfPeaks1]]), ], MergedPeaks = MergedPeaks, 
        Peaks1withOverlaps = Peaks1withOverlaps, Peaks2withOverlaps = Peaks2withOverlaps)
}
#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/findVennCounts.R"
findVennCounts <- function(Peaks, NameOfPeaks, maxgap=0,  totalTest)
{
	if (missing(totalTest))
	{
		stop("Missing totalTest which is required! The parameter totalTest is the total number of possible Peaks in the testing space.")
	}
	if (missing(NameOfPeaks) ||  mode(NameOfPeaks) != "character")
	{
		stop("Missing required character vector NameOfPeaks")
	}
	if (missing(Peaks))
	{
		stop("Missing Peaks which is a list of peaks in RangedData!")
	}
	n1 = length(Peaks)
	n2 = length(NameOfPeaks)
	if (n1 <2)
	{
		stop("The number of element in NameOfPeaks is less than 1, need at least 2 peak ranges!")
	}
	if (n1 > n2)
	{
		stop("The number of element in NameOfPeaks is less than the number of elements in Peaks, need to be equal!")
	}
	if (n1 < n2)
	{
		stop("The number of element in NameOfPeaks is larger than the number of elements in Peaks, need to be equal!")
	}
	lapply(seq_len(n1), function(i)
	{
		if (class(Peaks[[i]]) != "RangedData")
		{
			err.msg = paste("Element", i, "in Peaks is not a valid RangedData object", sep=" ")
			stop(err.msg)
		}
	})
	lapply(seq_len(n1), function(i)
	{	
		if (!length(rownames(Peaks[i])))
		{
			rownames(Peaks[[i]]) = formatC(1:dim(Peaks[[i]])[1], width=nchar(dim(Peaks[[i]])[1]), flag='0')
		}		
	})
	if (n1 == 2)
	{
		a1 = cbind(c(0,0,1,1),c(0,1,0,1))
		colnames(a1) = NameOfPeaks
		a2 = vennCounts(a1)
		overlappingPeaks = findOverlappingPeaks(Peaks[[1]],Peaks[[2]],NameOfPeaks1 = NameOfPeaks[1], NameOfPeaks2=NameOfPeaks[2], maxgap=maxgap, multiple=F)$OverlappingPeaks
		p1.and.p2 = length(unique(overlappingPeaks[[NameOfPeaks[1]]]))
		p1 = length(rownames(Peaks[[1]]))
		p2 = length(rownames(Peaks[[2]]))
		p1only = p1 - p1.and.p2
		#p1only = length(setdiff(as.character(rownames(Peaks[[1]])), as.character(overlappingPeaks[[NameOfPeaks[1]]])))
		#p2only = length(setdiff(as.character(rownames(Peaks[[2]])), as.character(overlappingPeaks[[NameOfPeaks[2]]])))
		p2only = p2 - p1.and.p2
		p2inp1 = length(unique(overlappingPeaks[[NameOfPeaks[2]]]))
		neither = totalTest - p1only - p2 
		Counts =c(neither,p2only,p1only,p1.and.p2)
		a2[,3] = Counts
		p.value = phyper(p1.and.p2 -1, p2, totalTest-p2, p1, lower.tail = FALSE,log.p = FALSE)
		list(p.value=p.value, vennCounts = a2)
	}
	else
	{
		stop("Larger than 2 list is not implemented yet!") 
	}
}
#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/getAllPeakSequence.R"
getAllPeakSequence <-
function(myPeakList, upstream=200, downstream=200, genome, AnnotationData)
{
		if (missing(genome))
		{
			stop("genome is required parameter, please pass in either a BSgenome object or a Mart object!")
		}
		if (class(genome)== "BSgenome")
		{
			Start = start(myPeakList) - as.numeric(upstream)
			End = end(myPeakList) + as.numeric(downstream)
			strand = myPeakList$strand
			if (length(strand) ==0)
			{
				strand = rep("+", length(Start))
			}
			chr = as.character(space(myPeakList))
			if (!length(i<-grep("chr",chr[1])) &  length(i<- grep("chr",names(seqlengths(genome))[1])))
			{
				chr = paste("chr", chr, sep="")
			}
					
			for (i in 1:length(Start))		
			{
				thisChr =chr[i]
				thisEnd = min(End[i], seqlengths(genome)[thisChr][[1]])
				thisStart = max(1, Start[i])
				thisStrand = as.character(strand[i])
				if (thisStrand == "1")
				{
					thisStrand = "+"
				}
				else if (thisStrand == "-1")
				{
					thisStrand = "-"
				}
				if (i ==1)
				{
					seq = getSeq(genome, thisChr, start=thisStart, end=thisEnd, strand=thisStrand, width=NA, as.character=TRUE)
				}
				else
				{
					seq = c(seq, getSeq(genome, thisChr, start=thisStart, end=thisEnd, strand=thisStrand, width=NA, as.character=TRUE))
				}
			}
			RangedData(IRanges(start=start(myPeakList), end = end(myPeakList), names=rownames(myPeakList)), space=chr, 
				upstream=rep(upstream,length(start(myPeakList))), downstream=rep(downstream,length(start(myPeakList))), sequence=seq,strand=strand)
		}
		else if (class(genome) =="Mart")
		{
			if (missing(AnnotationData))
			{
				message("No AnnotationData is passed in, so now querying biomart database for AnnotationData!")
				AnnotationData <- getAnnotation(genome)
				message("Done querying biomart database for AnnotationData, better way would be to call getAnnotation first, start querying for sequence ....")
			}
			if (class(AnnotationData)  != "RangedData")
			{
				message("No AnnotationData as RangedData is passed in, so now querying biomart database for AnnotationData!")
				AnnotationData = getAnnotation(genome)
				message("Done querying biomart database for AnnotationData, better way would be to call getAnnotation first, start querying for sequence ....")
			}
			downstream.bk = downstream
			plusAnno = AnnotationData[AnnotationData$strand==1 | as.character(AnnotationData$strand)=="+",]
			temp = annotatePeakInBatch(myPeakList, AnnotationData=plusAnno)
			TSSlength =temp$end_position - temp$start_position
			downstream = end(temp) - start(temp) + downstream
			temp$downstream=  downstream
			temp$TSSlength = TSSlength
	
			myList3 = as.data.frame(temp)
			rm(temp)
			
		if (dim(myList3)[1] != 0)
		{
			l3 =  cbind(as.character(myList3$feature), as.numeric(as.character(myList3$distancetoFeature)), as.numeric(rep(upstream, dim(myList3)[1])), as.numeric(as.character(myList3$downstream)), as.numeric(as.character(myList3$start_position)), as.numeric(as.character(myList3$end_position)))
			r3 = apply(l3, 1, getGeneSeq,genome)
		}
		else
		{
			r3 = 0
		}
		
		if (is.list(r3))
		{
			r = as.data.frame(do.call("rbind",r3))
		}
		else
		{
			 stop("No sequence found error!")
		}
		colnames(r)= c("feature", "distancetoFeature", "upstream", "downstream", "seq")
		r4 = merge(r, myList3)
		RangedData(IRanges(start=r4$start, end = r4$end, names =as.character(r4$name)), space=as.character(r4$space), upstream=rep(upstream, dim(r4)[1]), downstream= rep(downstream.bk, dim(r4)[1]), sequence=unlist(r4$seq))
	}
	else
	{
		stop("genome needs to be either a BSgenome object or Mart object!")
	}
}
#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/getAnnotation.R"
getAnnotation <-
function(mart, featureType=c("TSS","miRNA", "Exon", "5utr", "3utr", "ExonPlusUtr"))
{
		featureType = match.arg(featureType)
		if (missing(mart) || class(mart) !="Mart")
		{
			stop("No valid mart object is passed in!")
		}
		if (featureType == "TSS")
		{
			TSS = getBM(c("ensembl_gene_id","chromosome_name", "start_position", "end_position", "strand", "description"), mart = mart)
		}
		else if (featureType == "miRNA")
		{
			TSS = getBM(c("ensembl_gene_id","chromosome_name", "start_position", "end_position",  "strand", "description", "gene_biotype"), filter=c("biotype"),values="miRNA", mart = mart)
		}
		else if (featureType == "Exon")
		{
			TSS = getBM(c("ensembl_exon_id","chromosome_name", "exon_chrom_start", "exon_chrom_end", "strand", "description"), mart = mart)
		}
		else if (featureType == "5utr")
		{
			TSS = getBM(c("ensembl_transcript_id", "chromosome_name", "5_utr_start", "5_utr_end", "strand", "description"), mart = mart)
			TSS = TSS[!is.na(TSS[,3]),]
		}
		else if (featureType == "3utr")
                {
                        TSS = getBM(c("ensembl_transcript_id", "chromosome_name", "3_utr_start", "3_utr_end", "strand", "description"), mart = mart)
			
			TSS = TSS[!is.na(TSS[,3]),]
		}
	#	TSS = getBM(c("ensembl_gene_id","go_biological_process_id", "go_cellular_component_id", "go_molecular_function_id"), mart = mart)
		else if (featureType == "ExonPlusUtr")
		{
			TSS = getBM(c('ensembl_exon_id','chromosome_name', 'exon_chrom_start','exon_chrom_end', 
				'strand', 'ensembl_gene_id', '5_utr_start', '5_utr_end','3_utr_start','3_utr_end', 'description'), mart=mart)
		}
		TSS = unique(TSS)
		TSS = TSS[order(TSS[,3]),]		
		duplicatedID = TSS[duplicated(TSS[,1]),1]
		TSS = TSS[!duplicated(TSS[,1]),]
		if (length(duplicatedID) >0)
		{
			warning("Following duplicated IDs found, only one of entries of the duplicated id will be returned!")
			warning(duplicatedID)
		}
		if (featureType == "ExonPlusUtr")
		{
			RangedData(IRanges(start=as.numeric(as.character(TSS[,3])),
					end = as.numeric(as.character(TSS[,4])),
					names= as.character(TSS[,1])),
					ensembl_gene_id = as.character(TSS[,6]),
					utr5start =  as.numeric(as.character(TSS[,7])),
					utr5end =  as.numeric(as.character(TSS[,8])),
					utr3start =  as.numeric(as.character(TSS[,9])),
					utr3end =  as.numeric(as.character(TSS[,10])),
          				strand = TSS[,5],
					description=as.character(TSS[,11]),
					space = as.character(TSS[,2]))
		}
		else
		{
			RangedData(IRanges(start=as.numeric(TSS[,3]), end=as.numeric(TSS[,4]), names= as.character(TSS[,1])),
          strand = TSS[,5],   description=as.character(TSS[,6]), space = as.character(TSS[,2]))
		}
}
#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/getEnrichedGO.R"
getEnrichedGO <-
function(annotatedPeak, orgAnn, feature_id_type="ensembl_gene_id", maxP=0.01, multiAdj=FALSE, minGOterm=10, multiAdjMethod="")
{	
	if (missing(annotatedPeak))
	{
		stop("Missing required argument annotatedPeak!")	
	}
	if (missing(orgAnn))
	{
		message("No valid organism specific GO gene mapping package as argument orgAnn is passed in!")
		stop("Please refer http://www.bioconductor.org/packages/release/data/annotation/ for available org.xx.eg.db packages")
	}
	GOgenome = sub(".db","",orgAnn)
	if (nchar(GOgenome) <1)
	{
		message("No valid organism specific GO gene mapping package as parameter orgAnn is passed in!")
		stop("Please refer http://www.bioconductor.org/packages/release/data/annotation/ for available org.xx.eg.db packages")
	}
	if (class(annotatedPeak) == "RangedData")
	{
		feature_ids = unique(annotatedPeak$feature)
	}
	else if (class(annotatedPeak)  ==  "character")
	{
		feature_ids = unique(annotatedPeak)
	}
	else
	{
		stop("annotatedPeak needs to be RangedData type with feature variable holding the feature id or a character vector holding the IDs of the features used to annotate the peaks!")
	}
	if (feature_id_type == "entrez_id")
	{
		entrezIDs <- feature_ids
	}
	else
	{
		entrezIDs <- convert2EntrezID(feature_ids, orgAnn, feature_id_type)
	}
		
	goAnn <- get(paste(GOgenome,"GO", sep=""))
	mapped_genes <- mappedkeys(goAnn)
	xx <- as.list(goAnn[mapped_genes])
	#all.GO= matrix(unlist(unlist(xx)),ncol=3,byrow=TRUE)
	
	all.GO <- do.call(rbind, lapply(mapped_genes,function(x1)
	{
		temp = unlist(xx[names(xx) ==x1])
		if (length(temp) >0)
		{
			temp1 =matrix(temp,ncol=3,byrow=TRUE)
			cbind(temp1,rep(x1,dim(temp1)[1]))
		}
	}))

	this.GO <- do.call(rbind, lapply(entrezIDs,function(x1)
	{
		temp = unlist(xx[names(xx) ==x1])
		if (length(temp) >0)
		{
			temp1 =matrix(temp,ncol=3,byrow=TRUE)
			cbind(temp1,rep(x1,dim(temp1)[1]))
		}
	}))

	bp.go.this  = addAncestors(this.GO[this.GO[,3]=="BP",],"bp")
	cc.go.this = addAncestors(this.GO[this.GO[,3]=="CC",], "cc")
	mf.go.this = addAncestors(this.GO[this.GO[,3]=="MF",],"mf")

	bp.go.all  = addAncestors(all.GO[all.GO[,3]=="BP",], "bp")
	cc.go.all = addAncestors(all.GO[all.GO[,3]=="CC",], "cc")
	mf.go.all = addAncestors(all.GO[all.GO[,3]=="MF",], "mf")
		
	total.mf = length(mf.go.all)
	total.cc = length(cc.go.all)
	total.bp = length(bp.go.all)
	this.mf = length(mf.go.this)
	this.cc = length(cc.go.this)
	this.bp = length(bp.go.this)
	
	this.bp.count = getUniqueGOidCount(as.character(bp.go.this[bp.go.this!=""]))
	this.mf.count = getUniqueGOidCount(as.character(mf.go.this[mf.go.this!=""]))
	this.cc.count = getUniqueGOidCount(as.character(cc.go.this[cc.go.this!=""]))
	
	all.bp.count = getUniqueGOidCount(as.character(bp.go.all[bp.go.all!=""]))
	all.mf.count = getUniqueGOidCount(as.character(mf.go.all[mf.go.all!=""]))
	all.cc.count = getUniqueGOidCount(as.character(cc.go.all[cc.go.all!=""]))
	
	bp.selected = hyperGtest(all.bp.count,this.bp.count, total.bp, this.bp)
	mf.selected = hyperGtest(all.mf.count,this.mf.count, total.mf, this.mf)
	cc.selected = hyperGtest(all.cc.count,this.cc.count, total.cc, this.cc)
	
	bp.selected = data.frame(bp.selected)
	mf.selected = data.frame(mf.selected)
	cc.selected = data.frame(cc.selected)
	
	colnames(bp.selected) = c("go.id", "count.InDataset", "count.InGenome", "pvalue", "totaltermInDataset", "totaltermInGenome")
	colnames(mf.selected) = c("go.id", "count.InDataset", "count.InGenome", "pvalue", "totaltermInDataset", "totaltermInGenome")
	colnames(cc.selected) = c("go.id", "count.InDataset", "count.InGenome", "pvalue","totaltermInDataset", "totaltermInGenome")
	
	if (multiAdj == FALSE | multiAdjMethod == "")
	{
		bp.s = bp.selected[as.numeric(as.character(bp.selected[,4]))<maxP & as.numeric(as.character(bp.selected[,3]))>=minGOterm,]
		mf.s = mf.selected[as.numeric(as.character(mf.selected[,4]))<maxP & as.numeric(as.character(mf.selected[,3]))>=minGOterm,]
		cc.s = cc.selected[as.numeric(as.character(cc.selected[,4]))<maxP & as.numeric(as.character(cc.selected[,3]))>=minGOterm,]
	}
	else
	{
		procs = c(multiAdjMethod)
		res <- mt.rawp2adjp(as.numeric(as.character(bp.selected[,4])), procs)
        adjp = unique(res$adjp)
		colnames(adjp)[1] = colnames(bp.selected)[4]
		colnames(adjp)[2] = paste(multiAdjMethod, "adjusted.p.value", sep=".")
		bp.selected[,4] = as.numeric(as.character(bp.selected[,4]))
		bp1 = merge(bp.selected, adjp, all.x=TRUE)
		
		res <- mt.rawp2adjp(as.numeric(as.character(mf.selected[,4])), procs)
        adjp = unique(res$adjp)
		colnames(adjp)[1] = colnames(mf.selected)[4]
		colnames(adjp)[2] = paste(multiAdjMethod, "adjusted.p.value", sep=".")
		mf.selected[,4] = as.numeric(as.character(mf.selected[,4]))
		mf1 = merge(mf.selected, adjp, all.x=TRUE)

		res <- mt.rawp2adjp(as.numeric(as.character(cc.selected[,4])), procs)
        adjp = unique(res$adjp)
		colnames(adjp)[1] = colnames(cc.selected)[4]
		colnames(adjp)[2] = paste(multiAdjMethod, "adjusted.p.value", sep=".")
		cc.selected[,4] = as.numeric(as.character(cc.selected[,4]))
		cc1 = merge(cc.selected, adjp, all.x=TRUE)
		
		bp.s = bp1[as.numeric(as.character(bp1[,dim(bp1)[2]]))<maxP &  !is.na(bp1[,dim(bp1)[2]]) & as.numeric(as.character(bp1[,4]))>=minGOterm,]
		mf.s = mf1[as.numeric(as.character(mf1[,dim(mf1)[2]]))<maxP & !is.na(mf1[,dim(mf1)[2]]) & as.numeric(as.character(mf1[,4]))>=minGOterm,]
		cc.s = cc1[as.numeric(as.character(cc1[,dim(cc1)[2]]))<maxP & !is.na(cc1[,dim(cc1)[2]]) & as.numeric(as.character(cc1[,4]))>=minGOterm,]
	}
	
    xx <- as.list(GOTERM)
	goterm.bp = do.call(rbind, lapply(as.character(bp.s$go.id),function(x1)
		{
			r= xx[names(xx)==x1]
			if (length(r) >0)
			{
				c(GOID(r[[1]]),Term(r[[1]]),Definition(r[[1]]), Ontology(r[[1]]))
			}
		}))
	if (length(goterm.bp) <1)
	{
		goterm.bp =matrix(ncol=4)
	}
	colnames(goterm.bp) = c("go.id", "go.term", "Definition", "Ontology")
	
	goterm.mf = do.call(rbind, lapply(as.character(mf.s$go.id),function(x1)
		{
			r= xx[names(xx)==x1]
			if (length(r) >0)
			{
				c(GOID(r[[1]]),Term(r[[1]]),Definition(r[[1]]), Ontology(r[[1]]))
			}
		}))
	if (length(goterm.mf) <1)
	{
		goterm.mf =matrix(ncol=4)
	}
	colnames(goterm.mf) = c("go.id", "go.term", "Definition", "Ontology")
	
	goterm.cc = do.call(rbind, lapply(as.character(cc.s$go.id),function(x1)
		{
			r= xx[names(xx)==x1]
			if (length(r) >0)
			{
				c(GOID(r[[1]]),Term(r[[1]]),Definition(r[[1]]), Ontology(r[[1]]))
			}
		}))
	if (length(goterm.cc) <1)
	{
		goterm.cc =matrix(ncol=4)
	}
	colnames(goterm.cc) = c("go.id", "go.term", "Definition", "Ontology")
	
	bp.selected = merge(goterm.bp, bp.s,by="go.id")
	mf.selected = merge(goterm.mf, mf.s,by="go.id")
	cc.selected = merge(goterm.cc,cc.s,by="go.id")
	list(bp=bp.selected, mf=mf.selected, cc=cc.selected)
}
#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/getGeneSeq.R"
getGeneSeq <-
function(LocationParameters, mart)
{
	### ensembl_gene_id, distanceToNearestTSS, upstream, downstream, TSSStart, TSSEnd, mart
	if (missing(LocationParameters) || length(LocationParameters) <6)
	{
		stop("No valid LocationParameters passed in. It should contain six fields as c(distanceToNearestTSS, upstream, downstream, GeneStart, GeneEnd)!")
	}
	if (missing(mart) || class(mart) !="Mart")
	{
		stop("No valid mart object is passed in!")
	}
	distanceToNearestTSS = as.numeric(LocationParameters[2])
	downstream = as.numeric(LocationParameters[4])
	upstream = as.numeric(LocationParameters[3])
	TSSStart = as.numeric(LocationParameters[5])
	TSSEnd = as.numeric(LocationParameters[6])
	
	TSSLen = TSSEnd - TSSStart
	seqLen = downstream + upstream
	
	seq = getSequence(id=as.character(LocationParameters[1]), type="ensembl_gene_id",seqType="gene_exon_intron",mart=mart)
	if (nchar(seq[1]) < TSSLen)
	{
		upstream = upstream + TSSLen - nchar(seq[1])
	}
	if (distanceToNearestTSS <= upstream & TSSLen - distanceToNearestTSS > downstream)
	{
		upstreamOffset = upstream - distanceToNearestTSS 
		seq = getSequence(id=as.character(LocationParameters[1]), type="ensembl_gene_id",seqType="gene_exon_intron",upstream=upstreamOffset,mart=mart)
		start = 1
		end = seqLen + 1
		seq1 = substr(seq[1], start, end)
	}
	else if (TSSLen - distanceToNearestTSS < downstream & distanceToNearestTSS > upstream)
	{
		downstreamOffset = downstream - TSSLen + distanceToNearestTSS
		seq = getSequence(id=as.character(LocationParameters[1]), type="ensembl_gene_id",seqType="gene_exon_intron",downstream=downstreamOffset,mart=mart)
		start = distanceToNearestTSS - upstream
		end = distanceToNearestTSS + downstream + 1
		seq1 = substr(seq[1], start, end)
	}	
	else if ( distanceToNearestTSS > upstream & TSSLen - distanceToNearestTSS >= downstream)
	{
		seq = getSequence(id=as.character(LocationParameters[1]), type="ensembl_gene_id",seqType="gene_exon_intron",mart=mart)
		start = distanceToNearestTSS - upstream
		end = distanceToNearestTSS + downstream + 1
		seq1 = substr(seq[1], start, end)
	}
	else if (TSSLen - distanceToNearestTSS <= downstream & distanceToNearestTSS <= upstream)
	{
		downstreamOffset = downstream - TSSLen + distanceToNearestTSS
		upstreamOffset = upstream - distanceToNearestTSS
		seq1 = getSequence(id=as.character(LocationParameters[1]), type="ensembl_gene_id",seqType="gene_exon_intron",upstream=upstreamOffset,mart=mart)
		seq2 = getSequence(id=as.character(LocationParameters[1]), type="ensembl_gene_id",seqType="gene_exon_intron",downstream=downstreamOffset,mart=mart)
		seq3 = substr(seq1[1], 1, upstreamOffset)
		seq = paste(seq3, seq2[1], sep="")
		start = 1
		end = seqLen + 1
 		seq1 = substr(seq, start, end)
	}
	
	if (is.na(seq1))
	{
		seq1 ="No Sequence Found"
	}
	temp = c(LocationParameters[1:4], seq1)
	dim(temp) =c(1,5)

	colnames(temp) = c("feature_id", "distancetoFeature", "upstream", "downstream", "seq")
	list(feature_id=temp[1],distancetoFeature=temp[2],upstream=temp[3],downstream=temp[4],seq=temp[5])
}
#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/getUniqueGOidCount.R"
getUniqueGOidCount <-
function(goList)
{
	x = goList[order(goList)]
	duplicated.go<-duplicated(x)
	unique.go <- unique(x)
	go.count<- numeric()

	count <- 1
	j <- 1

	for (i in 2:length(duplicated.go))
	{
		
		if (duplicated.go[i] == FALSE)
		{
			go.count[j] <- count # previous GO
			j <- j + 1
			count <- 1
		}
		else
		{
			count <- count + 1
		}
	}
	go.count[j] <- count #last GO
	list(GOterm=unique.go, GOcount=go.count)
}

#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/hyperGtest.R"
hyperGtest <-
function(alltermcount,thistermcount, totaltermInGenome, totaltermInPeakList)
{
	pvalue = as.numeric()
	thistermtotal = as.numeric()
	for (i in 1:length(thistermcount$GOterm))
	{
		m = as.numeric(alltermcount$GOcount[alltermcount$GOterm==thistermcount$GOterm[i]])
		q = as.numeric(thistermcount$GOcount[i])
		n = as.numeric(totaltermInGenome)
		k = as.numeric(totaltermInPeakList)
		####### k - number of total GO terms in the peak list
		####### n - number of GO terms total
		####### q - number of this GO terms in the peak list
		####### m - number of this GO terms total
		pvalue[i] = phyper(q-1, m, n-m, k, lower.tail = FALSE, log.p = FALSE)
		thistermtotal[i] = m
	}
	list(thisterm=thistermcount$GOterm, thistermcount=thistermcount$GOcount,thistermtotal=thistermtotal, pvalue=pvalue, totaltermInPeakList=k, totaltermInGenome=n)
}

#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/makeVennDiagram.R"
makeVennDiagram <-
function(Peaks, NameOfPeaks, maxgap=0,  totalTest, cex=1.5, counts.col="red")
{
	if (missing(totalTest))
	{
		stop("Missing totalTest which is required! The parameter totalTest is the total number of possible Peaks in the testing space.")
	}
	if (missing(Peaks))
	{
		stop("Missing Peaks which is a list of peaks in RangedData!")
	}
	if (missing(NameOfPeaks) ||  mode(NameOfPeaks) != "character")
	{
		stop("Missing required character vector NameOfPeaks")
	}
	n1 = length(Peaks)
	n2 = length(NameOfPeaks)
	if (n1 <2)
	{
		stop("The number of element in NameOfPeaks is less than 1, need at least 2 peak ranges!")
	}
	if (n1 > n2)
	{
		stop("The number of element in NameOfPeaks is less than the number of elements in Peaks, need to be equal!")
	}
	if (n1 < n2)
	{
		stop("The number of element in NameOfPeaks is larger than the number of elements in Peaks, need to be equal!")
	}
	lapply(seq_len(n1), function(i)
	{
		if (class(Peaks[[i]]) != "RangedData")
		{
			err.msg = paste("Element", i, "in Peaks is not a valid RangedData object", sep=" ")
			stop(err.msg)
		}
	})
	lapply(seq_len(n1), function(i)
	{	
		if (!length(rownames(Peaks[i])))
		{
			rownames(Peaks[[i]]) = formatC(1:dim(Peaks[[i]])[1], width=nchar(dim(Peaks[[i]])[1]), flag='0')
		}		
	})
	if (n1 == 2)
	{
		x = findVennCounts(Peaks=Peaks,NameOfPeaks=NameOfPeaks,maxgap=maxgap,totalTest=totalTest)
		a2 = x$vennCounts
		p.value = x$p.value
		vennDiagram(a2,names = NameOfPeaks, cex=cex, counts.col = counts.col)
		list(p.value=p.value,vennCounts=a2)
	}
	else if (n1 == 3)
	{
		a1 = cbind(c(0, 0, 0,0,1, 1,1,1), c(0, 0,1,1, 0,0,1, 1),c(0,1,0,1,0,1,0,1))
		colnames(a1) = NameOfPeaks
		a2 = vennCounts(a1)
		x1 = findVennCounts(list(Peaks[[1]],Peaks[[2]]), NameOfPeaks=NameOfPeaks[1:2], maxgap=maxgap,
						totalTest=totalTest)
		p.value.1vs2 = x1$p.value
		counts1 = x1$vennCounts
		p1.and.p2 = counts1[4,3]
		
		x2 = findVennCounts(list(Peaks[[1]],Peaks[[3]]), NameOfPeaks=c(NameOfPeaks[1], NameOfPeaks[3]), maxgap=maxgap,
						totalTest=totalTest)
		p.value.1vs3 = x2$p.value
		counts2 = x2$vennCounts	
		p1.and.p3 = counts2[4,3]
		
		x3 = findVennCounts(list(Peaks[[2]],Peaks[[3]]), NameOfPeaks=NameOfPeaks[2:3], maxgap=maxgap,
						totalTest=totalTest)
		p.value.2vs3 = x3$p.value
		counts3 = x3$vennCounts
		p2.and.p3 = counts3[4,3]
		
		overlappingPeaks123 = findOverlappingPeaks(
				findOverlappingPeaks(Peaks[[1]],Peaks[[2]],NameOfPeaks1 = NameOfPeaks[1], NameOfPeaks2=NameOfPeaks[2], maxgap=maxgap, multiple=F)$Peaks1withOverlap,
				Peaks[[3]], NameOfPeaks1 = NameOfPeaks[1], NameOfPeaks2=NameOfPeaks[3],maxgap=maxgap, multiple=F)$OverlappingPeaks
		p1.and.p2.and.p3 = length(unique(overlappingPeaks123[[NameOfPeaks[1]]]))
		
		p1 = length(rownames(Peaks[[1]]))
		p2 = length(rownames(Peaks[[2]]))
		p3 = length(rownames(Peaks[[3]]))
		p1only = p1 - p1.and.p2 - p1.and.p3 + p1.and.p2.and.p3
		p2only = p2 - p1.and.p2 - p2.and.p3 + p1.and.p2.and.p3
		p3only = p3 - p2.and.p3 - p1.and.p3 + p1.and.p2.and.p3
		
		neither123 = totalTest -p1only - p2only - p3only - p1.and.p2 - p1.and.p3 - p2.and.p3 + 2 * p1.and.p2.and.p3
		
		Counts =c(neither123,p3only,p2only,p2.and.p3, p1only, p1.and.p3, p1.and.p2,p1.and.p2.and.p3)
		a2[,4] = Counts		
		vennDiagram(a2, names = NameOfPeaks)
		p.value.overall3 = phyper(p1.and.p2.and.p3 -1, p3, totalTest-p3, p1.and.p2, lower.tail = FALSE,log.p = FALSE)	
		p.value.overall1 = phyper(p1.and.p2.and.p3 -1, p1, totalTest-p1, p2.and.p3, lower.tail = FALSE,log.p = FALSE)
  		p.value.overall2 = phyper(p1.and.p2.and.p3 -1, p2, totalTest-p2, p1.and.p3, lower.tail = FALSE,log.p = FALSE)
		p.value.overall = max(p.value.overall1, p.value.overall2, p.value.overall3)
		list(p.value.1vs2 = p.value.1vs2, p.value.1vs3 = p.value.1vs3, p.value.2vs3 = p.value.2vs3, vennCounts=a2)
	}
	else
	{
		stop("Larger than 3 lists are not implemented yet")
	}
}

#source("~/dev/svndev/ChIPpeakAnno/R/makeVennDiagram.R")
#peaks1 = RangedData(IRanges(start = c(967654, 2010897, 2496704), end = c(967754, 2010997, 2496804), names = c("Site1", "Site2", "Site3")), space = c("1", "2", "3"), strand=as.integer(1))
#peaks2 = RangedData(IRanges(start = c(967659, 2010898,  2496700, 3075866, 3123260), end = c(967869, 2011108, 2496920, 3076166, 3123470), names = c("t1", "t2", "t3", "t4", "t5")), space = c("1", "2", "3", "1", "2"), strand = c(1, 1, -1,-1,1))
#makeVennDiagram(list(peaks1,peaks2,peaks2),NameOfPeaks,totalTest=100)
#makeVennDiagram(list(peaks1,peaks1,peaks1),NameOfPeaks,totalTest=100)
#makeVennDiagram(list(peaks1,peaks2,peaks1),NameOfPeaks,totalTest=100)
#line 1 "E:/biocbld/bbs-2.6-bioc/tmpdir/RtmpvJ8oOu/R.INSTALL2ec648f8/ChIPpeakAnno/R/write2FASTA.R"
write2FASTA <-
function(mySeq, file="", width=80)
{
	descriptions=rownames(mySeq)
	sequences=mySeq$sequence
	numF = length(descriptions)
	ff = lapply(seq_len(numF),function(i)
	{
		desc <- descriptions[i]
        seq <- sequences[i]
        list(desc = desc, seq = seq)
    })
	writeFASTA(ff, file=file, width=width)
}
