`initializeCategories` <-
function(CategoryID2GeneID, AllGeneIDs = NULL, minGenesInCategory=10, maxGenesInCategory=1000, inBkg = TRUE, verbose = TRUE){
	
	if(is.null(AllGeneIDs)){
		AllGeneIDs <- unique(unlist(CategoryID2GeneID))
	}
	if(verbose) cat("Total Number of categories \n",length(CategoryID2GeneID),"\n")
	
	#Getting frequencies of genes on the microarray in each category
	GenesInCategories<-unlist(lapply(CategoryID2GeneID,function(x) length(intersect(x,AllGeneIDs))))
	nonEmptyCategories<-GenesInCategories > 0
	GenesInCategories<-GenesInCategories[nonEmptyCategories]
	allGenesInCategories<-intersect(unique(unlist(CategoryID2GeneID[nonEmptyCategories])),AllGeneIDs)
	nGenesInCategories<-length(allGenesInCategories)
	if(verbose) cat("Number of genes on microarray represented in functional categories\n",nGenesInCategories,"\n")
	CategoriesWithGenes<-names(GenesInCategories)
	CategoriesWithGenes<-CategoryID2GeneID[CategoriesWithGenes]

	if(verbose) cat("Number of categories with genes from the list\n",length(GenesInCategories),"\n")

	minMaxCategories<-names(GenesInCategories[GenesInCategories>=minGenesInCategory & GenesInCategories<=maxGenesInCategory])
	nMinMaxCategories<-length(minMaxCategories)
	if(verbose) cat("Number of categories with more than ",minGenesInCategory," genes\n",nMinMaxCategories,"\n")
	#Getting all genes in such categories
	allGenes <- CategoriesWithGenes[minMaxCategories]
	CategoriesWithGenesMinMax<-allGenes
	allGenes<-intersect(unique(unlist(CategoriesWithGenesMinMax)), allGenesInCategories)
	CategoriesWithGenes <- CategoriesWithGenesMinMax

	if(inBkg) AllGeneIDs <- allGenes
	if(inBkg) nAllGenes<-length(allGenes)
	else nAllGenes<-length(AllGeneIDs)
	
	if(inBkg & verbose) cat("Only genes in occuring in considered categories are used in the analysis\n")
	if(verbose) cat("Number of genes in the background list of categories\n",nAllGenes,"\n")

	CategoriesWithGenes <- lapply(CategoriesWithGenes, function(l) intersect(AllGeneIDs, l))

	list(GenesInCategories=GenesInCategories[names(CategoriesWithGenes)],CategoriesWithGenes=CategoriesWithGenes,
		allGenesInCategories=allGenesInCategories,nGenesInCategories=nGenesInCategories, AllGeneIDs=AllGeneIDs)
}

