[Patchwork-commits] r94 - in pkg: patchwork/R patchworkCG/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Feb 15 17:31:23 CET 2012


Author: mayrhofer
Date: 2012-02-15 17:31:22 +0100 (Wed, 15 Feb 2012)
New Revision: 94

Modified:
   pkg/patchwork/R/patchwork.CNA.r
   pkg/patchwork/R/patchwork.applyref.r
   pkg/patchwork/R/patchwork.createreference.r
   pkg/patchwork/R/patchwork.findCNs.r
   pkg/patchworkCG/R/patchwork.CGCNV.r
Log:
changes to reference file structure

Modified: pkg/patchwork/R/patchwork.CNA.r
===================================================================
--- pkg/patchwork/R/patchwork.CNA.r	2012-02-15 15:29:39 UTC (rev 93)
+++ pkg/patchwork/R/patchwork.CNA.r	2012-02-15 16:31:22 UTC (rev 94)
@@ -1,4 +1,4 @@
-patchwork.CNA <- function(BamFile,Pileup,reference=NULL,normal.bam=NULL,normal.pileup=NULL,Alpha=0.0001,SD=1)
+patchwork.CNA <- function(BamFile,Pileup,reference=NULL,normal.pileup=NULL,Alpha=0.0001,SD=1)
 	{
 	#library(patchwork)
 	#patchwork.CNA("cancer.bam","pile.up",reference="illumina")
@@ -10,6 +10,9 @@
 	#load(paste(packagepath,"/data/ideogram.RData",sep=""))
 	#load(paste(packagepath,"/data/normaldata.RData",sep=""))
 	
+	#Check whether reference is a bam file.
+	normal.bam <- ifelse(length(grep('.bam',reference))>1, reference, NULL)
+
 	#Attempt to read file pile.alleles, incase its already been created.
 	alf <- normalalf <- NULL
 	try( load("pile.alleles.Rdata"), silent=TRUE )
@@ -38,75 +41,52 @@
 	
 	#If data object does not exist, IE it was not loaded in the previous line
 	#perform the function on the chromosomes to create the object.
-	if(is.null(data))
-		{
+	if(is.null(data)) {
 		cat("Initiating Read Chroms \n")
 		data = patchwork.readChroms(BamFile,chroms)
 		cat("Read Chroms Complete \n")
 		save(data,file='data.Rdata')
-		}
+	}
 	
 	#Perform GC normalization if the amount of columns indicate that gc normalization
 	#has not already been performed.
-	if(length(data) < 7)
-		{
+	if(length(data) < 7) {
 		cat("Initiating Gc Normalization \n")
 		data = patchwork.GCNorm(data)
 		cat("GC Normalization Complete \n")
 		save(data,file='data.Rdata')
-		}
+	}
 	#after this, no further normalization was done on reference sequences.
 
 	#If matched normal bam file was defined, load and normalize it the same way.
-	if (!is.null(normal.bam)) 
-		{
+	if (!is.null(normal.bam)) {
 		normaldata=NULL
 		try ( load('normaldata.Rdata'), silent=TRUE )
-		if(is.null(normaldata))
-			{
-			filename=strsplit(normal.bam,'/')[[1]]; filename=filename[length(filename)]
-			path=strsplit(normal.bam,paste('/',filename,sep=''))[[1]][1]
-			olddir=getwd(); setwd(path); path=getwd(); setwd(olddir)
-			if (path != getwd()) {
-				temp=data; data=NULL
-				try( load(paste(path,'data.Rdata',sep='/')), silent=T )
-				if (is.null(data)) {
-				data=temp
-				} else {
-					normaldata=data
-					data=temp
-				}
-			}
-		}
-		if(is.null(normaldata))
-			{
+		if(is.null(normaldata)) {
 			cat("Initiating Read Chroms (matched normal) \n")
 			normaldata = patchwork.readChroms(BamFile,chroms)
 			cat("Read Chroms Complete (matched normal) \n")
-			save(normaldata,file='normaldata.Rdata')
-			}
 		}
-		if(length(normaldata) < 7)
-			{
+	
+		if(length(normaldata) < 7) {
 			cat("Initiating Gc Normalization (matched normal) \n")
 			normaldata = patchwork.GCNorm(normaldata)
 			cat("GC Normalization Complete \n")
 			save(normaldata,file='normaldata.Rdata')
 		}
-
-
-
+		if (length(normaldata)>3) {
+			normaldata <- data.frame(chr=normaldata$chr,pos=normaldata$pos,reference=normaldata$norm)
+		}
+		save(normaldata,file='normaldata.Rdata')
+	} else load(reference)
 		
-	#Apply normal reference to data.
-	if(length(data) < 9)
-		{
+	#Apply reference to data.
+	if(length(data) < 9) {
 		cat("Initiating Application of Reference \n")
-		if (is.null(normal.bam)) {
-			data <- patchwork.applyref(data,reference=reference)
-		} else data <- patchwork.applyref(data,normaldata=normaldata)
+		data <- patchwork.applyref(data,reference=normaldata)
 		cat("Application of Reference Complete \n")
 		save(data,file='data.Rdata')
-		}
+	}
 	
 	#Apply smoothing to the data.
 	kbsegs = NULL

Modified: pkg/patchwork/R/patchwork.applyref.r
===================================================================
--- pkg/patchwork/R/patchwork.applyref.r	2012-02-15 15:29:39 UTC (rev 93)
+++ pkg/patchwork/R/patchwork.applyref.r	2012-02-15 16:31:22 UTC (rev 94)
@@ -1,15 +1,7 @@
-patchwork.applyref <- function(data,reference=NULL,normaldata=NULL)
+patchwork.applyref <- function(data,reference=NULL)
 	{
 
-	if (!is.null(reference)) load(reference)
-	
-	if (!is.null(normaldata)) {
-		normaldata=normaldata[,c(1,2,7)]
-		colnames(normaldata)[3]='norm_mean'
-	}
-
 	data <- merge(data[,1:7],normaldata,all=F,by=1:2)
-	#data$ratio <- data$norm / data$norm_mean
 	colnames(data)[8] = 'reference'
 	data=data[order(data$pos),]
 	data=data[order(data$chr),]

Modified: pkg/patchwork/R/patchwork.createreference.r
===================================================================
--- pkg/patchwork/R/patchwork.createreference.r	2012-02-15 15:29:39 UTC (rev 93)
+++ pkg/patchwork/R/patchwork.createreference.r	2012-02-15 16:31:22 UTC (rev 94)
@@ -40,7 +40,7 @@
 		normaldata = data.frame(chr=data_$chr,pos=data_$pos,
 							#counts_mean=apply(data_[,counts_],
 							#1,mean,na.rm=TRUE),
-							norm_mean=apply(data_[,norm_],
+							reference=apply(data_[,norm_],
 							1,mean,na.rm=TRUE))
 		}
 	else
@@ -50,10 +50,10 @@
 		cat("Note: Creating from single bamfile. Cannot obtain mean ")
 		cat("counts or norm however naming columns counts_mean and ")
 		cat("norm_mean for compatability \n")
-		normaldata = data.frame(chr=data$chr,pos=data$pos,gc=data$gc,
-							gck=data$gck,gcm=data$gcm,
-							counts_mean=data$counts,
-							norm_mean=data$norm)
+		normaldata = data.frame(chr=data$chr,pos=data$pos,#gc=data$gc,
+							#gck=data$gck,gcm=data$gcm,
+							#counts_mean=data$counts,
+							reference=data$norm)
 		}
 	
 	#conform chromosome naming to suit

Modified: pkg/patchwork/R/patchwork.findCNs.r
===================================================================
--- pkg/patchwork/R/patchwork.findCNs.r	2012-02-15 15:29:39 UTC (rev 93)
+++ pkg/patchwork/R/patchwork.findCNs.r	2012-02-15 16:31:22 UTC (rev 94)
@@ -1,4 +1,4 @@
-patchwork.findCNs = function(name = "findCNs_",cn2,delta,het,hom,maxCn=8,ceiling=1)
+patchwork.findCNs = function(name = "findCNs_",cn2,delta,het,hom,maxCn=8,ceiling=1,forcedelta=F)
 	{
 	
 	#packagepath = system.file(package="patchwork")
@@ -30,6 +30,7 @@
     temp <- regions[tix$cn1,]
     med <- weightedMedian(temp$median,temp$np)
     int$cn1 <- ifelse(!is.null(med),med,expectedAt)
+    if (forcedelta) int$cn1 <- int$cn2-delta
 
     ## likely cn0 regions sit below cn1 - delta:
     d <- int$cn2-int$cn1
@@ -38,7 +39,9 @@
     temp <- regions[tix$cn0,]
     med <- weightedMedian(temp$median,temp$np)
     int$cn0 <- ifelse(!is.null(med),med,expectedAt)
+    if (forcedelta) int$cn0 <- int$cn1-delta
 
+
     ## likely cn3 regions sit at about cn2+delta
     d <- delta
     expectedAt <- int$cn2+d
@@ -46,7 +49,9 @@
     temp <- regions[tix$cn3,]
     med <- weightedMedian(temp$median,temp$np)
     int$cn3 <- ifelse(!is.null(med),med,expectedAt)
+    if (forcedelta) int$cn3 <- int$cn2+delta
 
+
     ## cn4 follows at ...
     d <- delta
     expectedAt <- int$cn3+d
@@ -54,6 +59,7 @@
     temp <- regions[tix$cn4,]
     med <- weightedMedian(temp$median,temp$np)
     int$cn4 <- ifelse(!is.null(med),med,expectedAt)
+    if (forcedelta) int$cn4 <- int$cn3+delta
 
     ## generalized for higher cns
     for (cn in 5:maxCn)
@@ -67,6 +73,7 @@
     	temp <- regions[tix[thisCn][[1]],]
     	med <- weightedMedian(temp$median,temp$np)
     	int[thisCn] <- ifelse(!is.null(med),med,expectedAt)
+        if (forcedelta) int[[thisCn]] <- int[[prevCn]]+delta
     	}
 
 	#  smooth the cn relationship ?

Modified: pkg/patchworkCG/R/patchwork.CGCNV.r
===================================================================
--- pkg/patchworkCG/R/patchwork.CGCNV.r	2012-02-15 15:29:39 UTC (rev 93)
+++ pkg/patchworkCG/R/patchwork.CGCNV.r	2012-02-15 16:31:22 UTC (rev 94)
@@ -1,4 +1,4 @@
-patchwork.CGCNV = function(cn2,delta,het,hom,maxCn=8,ceiling=1,name="CGCNV_",CGfile=NULL)
+patchwork.CGCNV = function(cn2,delta,het,hom,maxCn=8,ceiling=1,name="CGCNV_",CGfile=NULL,forcedelta=F)
 	{
 	data(ideogram,package="patchworkCG")
 	
@@ -35,6 +35,7 @@
     temp <- regions[tix$cn1,]
     med <- weightedMedian(temp$median,temp$np)
     int$cn1 <- ifelse(!is.null(med),med,expectedAt)
+    if (forcedelta) int$cn1 <- int$cn2-delta
 
     ## likely cn0 regions sit below cn1 - delta:
     d <- int$cn2-int$cn1
@@ -43,6 +44,7 @@
     temp <- regions[tix$cn0,]
     med <- weightedMedian(temp$median,temp$np)
     int$cn0 <- ifelse(!is.null(med),med,expectedAt)
+    if (forcedelta) int$cn0 <- int$cn1-delta
 
     ## likely cn3 regions sit at about cn2+delta
     d <- delta
@@ -51,6 +53,7 @@
     temp <- regions[tix$cn3,]
     med <- weightedMedian(temp$median,temp$np)
     int$cn3 <- ifelse(!is.null(med),med,expectedAt)
+    if (forcedelta) int$cn3 <- int$cn2+delta
 
     ## cn4 follows at ...
     d <- delta
@@ -59,6 +62,7 @@
     temp <- regions[tix$cn4,]
     med <- weightedMedian(temp$median,temp$np)
     int$cn4 <- ifelse(!is.null(med),med,expectedAt)
+    if (forcedelta) int$cn4 <- int$cn3+delta
 
     ## generalized for higher cns
     for (cn in 5:maxCn)
@@ -72,6 +76,8 @@
     	temp <- regions[tix[thisCn][[1]],]
     	med <- weightedMedian(temp$median,temp$np)
     	int[thisCn] <- ifelse(!is.null(med),med,expectedAt)
+        if (forcedelta) int[[thisCn]] <- int[[prevCn]]+delta
+   
     	}
 
 	#  smooth the cn relationship ?



More information about the Patchwork-commits mailing list