diff mutation_analysis.r @ 119:626a956f3811 draft

Uploaded
author davidvanzessen
date Thu, 11 Aug 2016 10:35:52 -0400
parents ad7ca9c2b748
children 613278c1bde0
line wrap: on
line diff
--- a/mutation_analysis.r	Thu Aug 11 08:00:00 2016 -0400
+++ b/mutation_analysis.r	Thu Aug 11 10:35:52 2016 -0400
@@ -276,17 +276,28 @@
 
 		transition2[is.na(transition2$value),]$value = 0
 		
-		png(filename=paste("transitions_stacked_", name, ".png", sep=""))
-		p = ggplot(transition2, aes(factor(reorder(id, order.x)), y=value, fill=factor(reorder(variable, order.y)))) + geom_bar(position="fill", stat="identity") #stacked bar
-		p = p + xlab("From base") + ylab("To base") + ggtitle("Mutations frequency from base to base") + guides(fill=guide_legend(title=NULL))
-		print(p)
-		dev.off()
-		
-		png(filename=paste("transitions_heatmap_", name, ".png", sep=""))
-		p = ggplot(transition2, aes(factor(reorder(id, order.x)), factor(reorder(variable, order.y)))) + geom_tile(aes(fill = value), colour="white") + scale_fill_gradient(low="white", high="steelblue") #heatmap
-		p = p + xlab("From base") + ylab("To base") + ggtitle("Mutations frequency from base to base")
-		print(p)
-		dev.off()
+		if(!all(transition2$value == 0)){ #having rows of data but a transition table filled with 0 is bad
+
+			print("Plotting stacked transition")
+
+			print(transition2)
+
+			png(filename=paste("transitions_stacked_", name, ".png", sep=""))
+			p = ggplot(transition2, aes(factor(reorder(id, order.x)), y=value, fill=factor(reorder(variable, order.y)))) + geom_bar(position="fill", stat="identity") #stacked bar
+			p = p + xlab("From base") + ylab("To base") + ggtitle("Mutations frequency from base to base") + guides(fill=guide_legend(title=NULL))
+			print(p)
+			dev.off()
+
+			print("Plotting heatmap transition")
+
+			png(filename=paste("transitions_heatmap_", name, ".png", sep=""))
+			p = ggplot(transition2, aes(factor(reorder(id, order.x)), factor(reorder(variable, order.y)))) + geom_tile(aes(fill = value), colour="white") + scale_fill_gradient(low="white", high="steelblue") #heatmap
+			p = p + xlab("From base") + ylab("To base") + ggtitle("Mutations frequency from base to base")
+			print(p)
+			dev.off()
+		} else {
+			print("No data to plot")
+		}
 	}
 
 	#print(paste("writing value file: ", name, "_", fname, "_value.txt" ,sep=""))