diff small_rna_map.r @ 3:2e0dc6032a98 draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/small_rna_map commit 93f212712d9846c7aaa389de60babb332d38363e
author artbio
date Tue, 18 Jul 2017 13:34:36 -0400
parents 7feee0446c5c
children d65045e976e6
line wrap: on
line diff
--- a/small_rna_map.r	Wed Jul 12 13:40:36 2017 -0400
+++ b/small_rna_map.r	Tue Jul 18 13:34:36 2017 -0400
@@ -16,6 +16,23 @@
 theme_set(theme_bw()) #a theme with a white background
 Table = read.delim(args$output_tab, header=T, row.names=NULL)
 Table <- within(Table, Nbr_reads[Polarity=="R"] <- (Nbr_reads[Polarity=="R"]*-1))
+Chr_limits <- unique(data.frame(Dataset=Table$Dataset, Chromosome=Table$Chromosome,
+                                Chrom_length=Table$Chrom_length))
+Chr_limits_inf <- data.frame(Coordinate=Chr_limits$Chrom_length*0,
+                             Nbr_reads=Chr_limits$Chrom_length*0,
+                             Polarity=rep("F", length(Chr_limits$Dataset)),
+                             Max=Chr_limits$Chrom_length*0,
+                             Mean=Chr_limits$Chrom_length*0,
+                             Median=Chr_limits$Chrom_length*0)
+Chr_limits_inf <- cbind(Chr_limits, Chr_limits_inf)
+Chr_limits_sup <- data.frame(Coordinate=Chr_limits$Chrom_length+1,
+                             Nbr_reads=Chr_limits$Chrom_length*0,
+                             Polarity=rep("F", length(Chr_limits$Dataset)),
+                             Max=Chr_limits$Chrom_length*0,
+                             Mean=Chr_limits$Chrom_length*0,
+                             Median=Chr_limits$Chrom_length*0)
+Chr_limits_sup <- cbind(Chr_limits, Chr_limits_sup)
+Table <- rbind(Table, Chr_limits_inf, Chr_limits_sup)
  
 #To assign colors to categorical variables in ggplot2 that have stable mapping
 myColors <- brewer.pal(3,"Set1")
@@ -23,12 +40,14 @@
 colScale <- scale_colour_manual(name = "Polarity",values = myColors)
  
 #Make initial figures
+
 p <- ggplot(Table, aes(x=Coordinate, y=Nbr_reads, colour=Polarity)) +
   colScale+
-  geom_segment(aes(y = 0, x = Coordinate, yend = Nbr_reads, xend = Coordinate, color=Polarity), alpha=1) +
-  geom_segment(aes(y = Nbr_reads, x = 0, yend=Nbr_reads, xend=Chrom_length), alpha=0)+
+  geom_segment(aes(y = 0, x = Coordinate, yend = Nbr_reads, xend = Coordinate, color=Polarity)) +
+#  geom_segment(aes(y = Nbr_reads, x = 0, yend=Nbr_reads, xend=Chrom_length), alpha=0)+
   facet_wrap(Dataset~Chromosome, scales="free", nrow=1, labeller = label_wrap_gen(multi_line = FALSE))+
-  scale_y_continuous(breaks = function(x) round(pretty(seq(-(max(x) + 1), (max(x) + 1)))))+#to display only integer values on y axis
+#  scale_x_continuous(limits = c(rep(0, length(Table$Chromosome)), Chr_lengths$Chrom_length)) +
+  scale_y_continuous(breaks = function(x) round(pretty(seq(-(max(x) + 1), (max(x) + 1)))))+ # to display only integer values on y axis
   geom_hline(yintercept=0, size=0.3)+
   theme(strip.text = element_text(size = 6, lineheight = 0.1), #specify strip size
         panel.grid.major = element_line(colour = "#ffffff"),#conceal major grid lines
@@ -49,7 +68,7 @@
   scale_colour_manual(name="", values=cols)+ 
   expand_limits(y = seq(0,max(Table$Median),by=5)) +
   facet_wrap(Dataset~Chromosome, scales="free", nrow=1, labeller = label_wrap_gen(multi_line = FALSE))+
-  geom_segment(aes(y = Nbr_reads, x = 0, yend=Nbr_reads, xend=Chrom_length), alpha=0)+
+#  geom_segment(aes(y = Nbr_reads, x = 0, yend=Nbr_reads, xend=Chrom_length), alpha=0)+
   scale_y_continuous(limits = c(0,max(Table$Median)), position = "left")+
   theme(strip.background = element_blank(),
         strip.text.x = element_blank(),
@@ -64,10 +83,9 @@
 # Transforme ggplot graphs on list of graphs
 plot.list1 <- by(data     = Table,
                 INDICES  = c(Table$Chromosome),
-                simplify = TRUE,
-                FUN      = function(x) {
-                  p %+% x 
-                })
+                #simplify = TRUE,
+                FUN      = function(x) {p %+% x }
+                )
  
 plot.list2 <- by(data     = Table,
                 INDICES  = c(Table$Chromosome),
@@ -75,9 +93,10 @@
                 FUN      = function(x) {
                   p2 %+% x 
                 })
- 
+
 # Plotting in multiple pages with different rows
-multi.plot<-do.call(marrangeGrob,list(grobs=rbind(plot.list1,plot.list2),ncol=1,nrow=8,top=NULL, 
+
+grobs=rbind(plot.list1,plot.list2)
+multi.plot<-do.call(marrangeGrob,list(grobs,ncol=1,nrow=8,top=NULL, 
             bottom="Coordinates(nt)", left="Number of reads / Median & Mean", right= mylegend))
-ggsave(args$output_pdf, device="pdf", plot=multi.plot, height=11.69, width=8.2)
-
+ggsave(args$output_pdf, device="pdf", plot=multi.plot, height=11.69, width=8.2)
\ No newline at end of file