annotate phyloseq_filter.R @ 0:910739de7b80 draft

planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
author simon-gladman
date Wed, 05 Sep 2018 22:21:43 -0400
parents
children 9fbb104e16d9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
1 library('getopt')
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
2 library('ape')
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
3 library('ggplot2')
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
4 suppressPackageStartupMessages(library('phyloseq'))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
5 library(biomformat)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
6 library(plyr)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
7 Sys.setenv("DISPLAY"=":1")
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
8 library(biomformat)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
9 suppressPackageStartupMessages(library(metagenomeSeq))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
10 suppressPackageStartupMessages(library("doParallel"))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
11 ncores = ceiling(detectCores() * 0.8)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
12 registerDoParallel(cores=ncores)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
13
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
14 options(warn=-1)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
15
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
16 theme_set(theme_bw())
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
17 ## ggplot
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
18 # http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
19 # https://gist.github.com/Mikeyj/5429538
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
20 # http://microbiome-tutorials.readthedocs.io/en/latest/_static/Composition.html
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
21 # https://rstudio-pubs-static.s3.amazonaws.com/268156_d3ea37937f4f4469839ab6fa2c483842.html#otus_that_differ_by (stacked bar plot)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
22
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
23 ## color
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
24 ## http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
25
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
26 #http://saml.rilspace.com/creating-a-galaxy-tool-for-r-scripts-that-output-images-and-pdfs
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
27 #http://joey711.github.io/phyloseq-demo/phyloseq-demo.html
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
28 option_specification = matrix(c(
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
29 'otu_table','o',2,'character',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
30 'tax_table','t',2,'character',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
31 'meta_table','m',2,'character',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
32 'biom','b',2,'character',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
33 'filter','f',2,'numeric',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
34 'kingdom','k',2,'character',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
35 'cutoff','c',2,'numeric',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
36 'keep','p',2,'numeric',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
37 'outbiom','h',2,'character',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
38 'outdir','d',2,'character',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
39 'htmlfile','w',2,'character'
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
40 ),byrow=TRUE,ncol=4);
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
41
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
42
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
43 options <- getopt(option_specification);
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
44 options(bitmapType="cairo")
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
45
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
46 if (!is.null(options$outdir)) {
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
47 # Create the directory
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
48 dir.create(options$outdir,FALSE)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
49 }
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
50
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
51
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
52
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
53 cutoff_value<-options$cutoff
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
54 ### select a kingdom for phyloseq plot (e.g., "phylum")
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
55 #kingdom_str<-colnames(tax_table)[options$kingdom]
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
56 kingdom_str<-options$kingdom
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
57 keep<-options$keep
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
58 filter<-options$filter
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
59
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
60 ### prepare the directory and file name
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
61 pdffile <- gsub("[ ]+", "", paste(options$outdir,"/pdffile.pdf"))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
62 pngfile_before_filtering <- gsub("[ ]+", "", paste(options$outdir,"/barplot_before_filtering.png"))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
63 pngfile_after_filtering <- gsub("[ ]+", "", paste(options$outdir,"/barplot_after_filtering.png"))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
64 pngfile_pre_phyla_filtering <- gsub("[ ]+", "", paste(options$outdir,"/barplot_before_phyla_filtering.png"))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
65 pngfile_post_phyla_filtering<- gsub("[ ]+", "", paste(options$outdir,"/barplot_after_phyla_filtering.png"))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
66 htmlfile <- gsub("[ ]+", "", paste(options$htmlfile))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
67
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
68 ### This function accepts different two different type of BIOM file format
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
69 readBIOM<-function(inBiom){
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
70 tryCatch({
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
71 phyloseq_obj<-import_biom(inBiom,parallel=TRUE)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
72 return(phyloseq_obj)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
73 },
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
74 error=function(e){
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
75 biom_obj<-read_biom(inBiom)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
76
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
77 otu_matrix = as(biom_data(biom_obj), "matrix")
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
78 OTU_TABLE = otu_table(otu_matrix, taxa_are_rows=TRUE)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
79
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
80 taxonomy_matrix = as.matrix(observation_metadata(biom_obj), rownames.force=TRUE)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
81 TAXONOMY_TABLE = tax_table(taxonomy_matrix)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
82
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
83 metadata.temp<-sample_metadata(biom_obj)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
84 METADATA_TABLE<-plyr::ldply(metadata.temp, rbind)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
85 rownames(METADATA_TABLE)<-as.character(METADATA_TABLE$.id)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
86
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
87 phyloseq_obj = phyloseq(OTU_TABLE, TAXONOMY_TABLE,sample_data(METADATA_TABLE))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
88 return(phyloseq_obj)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
89 }
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
90 )
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
91 }
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
92
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
93
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
94 create_PDF<-function(pdf_file,OTU_DATAFRAME_BEFORE_FILTERING,OTU_DATAFRAME_AFTER_FILTERING,physeq_pre_phyla_filtering,physeq_post_phyla_filtering,kingdom_str,htmlfile,pngfile_before_filtering,pngfile_after_filtering,pngfile_pre_phyla_filtering,pngfile_post_phyla_filtering){
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
95 pdf(pdf_file);
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
96 barplot_before_filtering<-ggplot(OTU_DATAFRAME_BEFORE_FILTERING,aes(rownames(OTU_DATAFRAME_BEFORE_FILTERING))) +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
97 geom_bar(aes(weight=Abundance),fill="tomato3") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
98 labs(title="Sample Depth Bar Chart",subtitle="Sample Vs Abundance (Before Filtering)",caption="source: Input Biom") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
99 xlab("Sample") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
100 ylab("Abundance") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
101 theme(axis.text.x=element_text(angle=65,vjust=0.6))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
102 print(barplot_before_filtering)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
103
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
104 barplot_after_filtering<-ggplot(OTU_DATAFRAME_AFTER_FILTERING,aes(rownames(OTU_DATAFRAME_AFTER_FILTERING))) +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
105 geom_bar(aes(weight=Abundance),fill="blue") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
106 labs(title="Sample Depth Bar Chart",subtitle="Sample Vs Abundance (After Filtering)", caption="source: Input Biom") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
107 xlab("Sample") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
108 ylab("Abundance") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
109 theme(axis.text.x=element_text(angle=65,vjust=0.6))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
110 print(barplot_after_filtering)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
111
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
112 barplot_pre_phyla_filtering<-plot_bar(physeq_pre_phyla_filtering, x=colnames(sample_data(physeq_pre_phyla_filtering))[1], fill=kingdom_str) +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
113 geom_bar(stat="identity", position="stack") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
114 labs(title=paste("Sample Depth Bar Chart",kingdom_str,sep=":"),subtitle="Sample Vs Abundance (Pre phyla Filtering)",caption="source: Input Biom") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
115 xlab("Sample") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
116 ylab("Abundance") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
117 theme(axis.text.x=element_text(angle=90,vjust=0.6)) +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
118 scale_fill_hue()
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
119 print(barplot_pre_phyla_filtering)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
120
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
121 barplot_post_phyla_filtering<-plot_bar(physeq_post_phyla_filtering, x=colnames(sample_data(physeq_post_phyla_filtering))[1], fill=kingdom_str) +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
122 geom_bar(stat="identity", position="stack") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
123 labs(title=paste("Sample Depth Bar Chart",kingdom_str,sep=":"),subtitle="Sample Vs Abundance (Post phyla Filtering)",caption="source: Input Biom") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
124 xlab("Sample") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
125 ylab("Abundance") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
126 theme(axis.text.x=element_text(angle=90,vjust=0.6)) +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
127 scale_fill_hue()
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
128 print(barplot_post_phyla_filtering)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
129 garbage<-dev.off();
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
130
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
131 #png('barplot_before_filtering.png')
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
132 bitmap(pngfile_before_filtering,"png16m")
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
133 barplot_before_filtering_png<-ggplot(OTU_DATAFRAME_BEFORE_FILTERING,aes(rownames(OTU_DATAFRAME_BEFORE_FILTERING))) +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
134 geom_bar(aes(weight=Abundance),fill="tomato3") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
135 labs(title="Sample Depth Bar Chart",subtitle="Sample Vs Abundance (Before Filtering)",caption="source: Input Biom") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
136 xlab("Sample") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
137 ylab("Abundance") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
138 theme(axis.text.x=element_text(angle=65,vjust=0.6))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
139 print(barplot_before_filtering_png)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
140 garbage<-dev.off()
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
141
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
142 #png('barplot_after_filtering.png')
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
143 bitmap(pngfile_after_filtering,"png16m")
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
144 barplot_after_filtering_png<-ggplot(OTU_DATAFRAME_AFTER_FILTERING,aes(rownames(OTU_DATAFRAME_AFTER_FILTERING))) +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
145 geom_bar(aes(weight=Abundance),fill="blue") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
146 labs(title="Sample Depth Bar Chart",subtitle="Sample Vs Abundance (After Filtering)", caption="source: Input Biom") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
147 xlab("Sample") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
148 ylab("Abundance") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
149 theme(axis.text.x=element_text(angle=65,vjust=0.6))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
150 print(barplot_after_filtering_png)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
151 garbage<-dev.off()
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
152
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
153 #png('barplot_pre_phyla_filtering.png')
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
154 bitmap(pngfile_pre_phyla_filtering,"png16m")
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
155 print(sample_data(physeq_pre_phyla_filtering))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
156 barplot_pre_phyla_filtering<-plot_bar(physeq_pre_phyla_filtering, x=colnames(sample_data(physeq_pre_phyla_filtering))[1], fill=kingdom_str) +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
157 geom_bar(stat="identity", position="stack") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
158 labs(title=paste("Sample Depth Bar Chart",kingdom_str,sep=":"),subtitle="Sample Vs Abundance (Pre Phyla Filtering)",caption="source: Input Biom") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
159 xlab("Sample") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
160 ylab("Abundance") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
161 theme(axis.text.x=element_text(angle=90,vjust=0.6)) +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
162 scale_fill_hue()
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
163 print(barplot_pre_phyla_filtering)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
164 garbage<-dev.off()
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
165
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
166 #png('barplot_post_phyla_filtering.png')
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
167 bitmap(pngfile_post_phyla_filtering,"png16m")
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
168 barplot_post_phyla_filtering<-plot_bar(physeq_post_phyla_filtering, x=colnames(sample_data(physeq_post_phyla_filtering))[1], fill=kingdom_str) +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
169 geom_bar(stat="identity", position="stack") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
170 labs(title=paste("Sample Depth Bar Chart",kingdom_str,sep=":"),subtitle="Sample Vs Abundance (Post Phyla Filtering)",caption="source: Input Biom") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
171 xlab("Sample") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
172 ylab("Abundance") +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
173 theme(axis.text.x=element_text(angle=90,vjust=0.6)) +
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
174 scale_fill_hue()
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
175 print(barplot_post_phyla_filtering)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
176 garbage<-dev.off()
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
177
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
178 create_HTML(htmlfile)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
179 }
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
180
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
181 create_HTML<-function(htmlfile){
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
182 htmlfile_handle <- file(htmlfile)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
183 html_output = c('<html><body>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
184 '<table align="center>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
185 '<tr>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
186 '<td valign="middle" style="vertical-align:middle;">',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
187 '<a href="pdffile.pdf"><img src="barplot_before_filtering.png"/></a>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
188 '</td>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
189 '</tr>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
190 '<tr>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
191 '<td valign="middle" style="vertical-align:middle;">',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
192 '<a href="pdffile.pdf"><img src="barplot_after_filtering.png"/></a>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
193 '</td>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
194 '</tr>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
195 '<tr>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
196 '<td valign="middle" style="vertical-align:middle;">',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
197 '<a href="pdffile.pdf"><img src="barplot_before_phyla_filtering.png"/></a>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
198 '</td>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
199 '</tr>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
200 '<tr>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
201 '<td valign="middle" style="vertical-align:middle;">',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
202 '<a href="pdffile.pdf"><img src="barplot_after_phyla_filtering.png"/></a>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
203 '</td>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
204 '</tr>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
205 '</table>',
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
206 '</html></body>');
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
207 writeLines(html_output, htmlfile_handle);
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
208 close(htmlfile_handle);
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
209 }
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
210
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
211 convert_phyloseq_otutable_to_dataframe<-function(physeq_obj){
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
212 temp.df<-data.frame(otu_table(physeq_obj))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
213 temp.df.counts<-as.data.frame(colSums(temp.df))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
214 colnames(temp.df.counts)<-"Abundance"
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
215 print(temp.df.counts)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
216 return(temp.df.counts)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
217 }
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
218
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
219 if(!is.null(options$biom)){
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
220
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
221 #physeq<-import_biom(options$biom)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
222 physeq<-readBIOM(options$biom)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
223
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
224 if(length(rank_names(physeq)) == 8){
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
225 tax_table(physeq) <- tax_table(physeq)[,-1]
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
226 colnames(tax_table(physeq)) <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
227 } else {
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
228 colnames(tax_table(physeq)) <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
229 }
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
230
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
231 ### select column name from sample table for nmds plot
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
232 ## which(colnames(sample_data(biom)) == "vegetation_type_id")
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
233 #category_type<-colnames(sample_data(physeq))[options$subset]
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
234 #category_type <- options$subset
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
235
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
236 ### obtain the unique value in the selected column from sample table
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
237 #category_option<-unique(sample_data(physeq))[,options$subset]
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
238
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
239 }else{
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
240
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
241 ### read the data into correct data type to create phyloseq object
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
242 otu_table<-as.matrix(read.table(options$otu_table,header=T,sep="\t"))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
243 tax_table<-as.matrix(read.table(options$tax_table,header=T,sep="\t"))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
244 sample_table<-read.table(options$meta_table,header=T,sep="\t",stringsAsFactors=F)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
245
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
246
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
247 ### select column name from sample table for nmds plot
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
248 #category_type<-colnames(sample_table)[options$category]
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
249
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
250 ### obtain the unique value in the selected column from sample table
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
251 #category_option<-unique(sample_table[,options$category])
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
252
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
253
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
254 ### create a sample object for phyloseq
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
255 sample_object<-sample_data(sample_table)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
256
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
257 ### create otu object for phyloseq
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
258 OTU<-otu_table(otu_table, taxa_are_rows = TRUE)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
259
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
260 ### create tax object for phyloseq
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
261 TAX<-tax_table(tax_table)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
262
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
263 ### create a phyloseq object
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
264 physeq = phyloseq(OTU,TAX,sample_object)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
265 }
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
266 ### make the first column to be the sample ID in the phyloseq object
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
267
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
268 firstColumn = sample_data(physeq)[,1]
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
269 row_names = rownames(sample_data(physeq))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
270 check = all( firstColumn == row_names)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
271 if(!check){
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
272 sample_data(physeq) <- cbind(SampleID= rownames(sample_data(physeq)),sample_data(physeq))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
273 }
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
274
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
275
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
276 ### extract otu table from phyloseq object
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
277 before_filtering_dataframe_sampleCounts<-convert_phyloseq_otutable_to_dataframe(physeq)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
278
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
279 ### filtering OTUs based on cutoff value (e.g., 5)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
280 #physeq_temp =genefilter_sample(physeq, filterfun_sample(function(x) x > cutoff_value), A=0.1*nsamples(physeq))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
281 physeq_temp =genefilter_sample(physeq, filterfun_sample(function(x) x > cutoff_value), A=filter*nsamples(physeq))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
282
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
283 ### phyloseq object after filtered
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
284 physeq_filter = prune_taxa(physeq_temp, physeq)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
285
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
286
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
287
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
288 ## Transform to even sampling depth
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
289 #physeq_filter = transform_sample_counts(physeq_filter, function(x) 1E6 * x/sum(x))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
290
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
291 #after_filtering.dataframe<-data.frame(otu_table(physeq_filter))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
292 #after_filtering_dataframe_sampleCounts<-as.data.frame(colSums(after_filtering.dataframe))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
293 #colnames(after_filtering_dataframe_sampleCounts)<-"Abundance"
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
294 after_filtering_dataframe_sampleCounts<-convert_phyloseq_otutable_to_dataframe(physeq_filter)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
295
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
296 # create_PDF(pdffile,before_filtering_dataframe_sampleCounts,after_filtering_dataframe_sampleCounts,htmlfile,pngfile_before_filtering,pngfile_after_filtering)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
297
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
298 # kingdom_str <- as.numeric(kingdom_str)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
299 ## Keep only the most abundant five phyla
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
300
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
301 ### Phyla - Pre transformation (Transform to even sampling depth)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
302
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
303 #physeq_filter_pre_transform = tapply(taxa_sums(physeq_filter), tax_table(physeq_filter)[, kingdom_str], sum,na.rm=TRUE)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
304
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
305 phylum.sum_pre_transform= tapply(taxa_sums(physeq_filter), tax_table(physeq_filter)[, kingdom_str], sum,na.rm=TRUE)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
306
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
307 topphyla_pre_transform = names(sort(phylum.sum_pre_transform, TRUE))[1:keep]
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
308
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
309 physeq_filter_pre_transform = prune_taxa((tax_table(physeq_filter)[, kingdom_str] %in% topphyla_pre_transform), physeq_filter)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
310
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
311 ### Phyla - Post Transformation (Transform to even sampling depth)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
312 physeq_filter_post_transform = transform_sample_counts(physeq_filter, function(x) 1E6 * x/sum(x))
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
313
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
314 phylum.sum_post_transform = tapply(taxa_sums(physeq_filter_post_transform), tax_table(physeq_filter_post_transform)[, kingdom_str], sum,na.rm=TRUE)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
315 ### number of most abundance phyla to keep
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
316 topphyla_post_transform = names(sort(phylum.sum_post_transform, TRUE))[1:keep]
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
317
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
318 physeq_filter_post_transform = prune_taxa((tax_table(physeq_filter_post_transform)[, kingdom_str] %in% topphyla_post_transform), physeq_filter_post_transform)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
319
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
320
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
321 create_PDF(pdffile,before_filtering_dataframe_sampleCounts,after_filtering_dataframe_sampleCounts,physeq_filter_pre_transform,physeq_filter_post_transform,kingdom_str,htmlfile,pngfile_before_filtering,pngfile_after_filtering,pngfile_pre_phyla_filtering,pngfile_post_phyla_filtering)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
322
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
323 ### convert phyloseq object to metagenomeSeq - preparing for BIOM output
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
324 metagenomeSeq_obj <- phyloseq_to_metagenomeSeq(physeq_filter_post_transform)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
325 metagenomeSeq_biom <- MRexperiment2biom(metagenomeSeq_obj)
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
326
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
327 ## write biom file
910739de7b80 planemo upload commit 555f91999a1164a4420492126fa7713c89e3c5f5-dirty
simon-gladman
parents:
diff changeset
328 write_biom(metagenomeSeq_biom, biom_file=options$outbiom)