annotate phyloseq_nmds.R @ 2:20adf95eb758 draft

Updated phyloseq_nmds.xml to allow for new datatype.
author simon-gladman
date Fri, 20 Jul 2018 00:23:53 -0400
parents e376a618eb9f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
1 library('getopt')
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
2 library('ape')
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
3 suppressPackageStartupMessages(library('phyloseq'))
1
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
4 library(biomformat)
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
5 library(plyr)
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
6 Sys.setenv("DISPLAY"=":1")
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
7 library("ggplot2")
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
8 suppressPackageStartupMessages(library("doParallel"))
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
9 ncores = ceiling(detectCores() * 0.8)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
10 registerDoParallel(cores=ncores)
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
11
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
12 options(warn=-1)
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
13
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
14 theme_set(theme_bw())
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
15
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
16 #http://saml.rilspace.com/creating-a-galaxy-tool-for-r-scripts-that-output-images-and-pdfs
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
17 #http://joey711.github.io/phyloseq-demo/phyloseq-demo.html
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
18 option_specification = matrix(c(
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
19 'otu_table','o',2,'character',
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
20 'tax_table','t',2,'character',
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
21 'meta_table','m',2,'character',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
22 'biom','b',2,'character',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
23 'filter','f',2,'numeric',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
24 'subset','s',2,'character',
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
25 'method','n',2,'character',
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
26 'distance','d',2,'character',
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
27 'kingdom','k',2,'character',
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
28 'plottype','e',2,'numeric',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
29 'cutoff','c',2,'numeric',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
30 'category','g',2,'numeric',
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
31 'keep','p',2,'numeric',
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
32 'outdir','r',2,'character',
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
33 'htmlfile','h',2,'character'
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
34 ),byrow=TRUE,ncol=4);
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
35
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
36
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
37 options <- getopt(option_specification);
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
38 options(bitmapType="cairo")
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
39
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
40 if (!is.null(options$outdir)) {
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
41 # Create the directory
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
42 dir.create(options$outdir,FALSE)
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
43 }
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
44
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
45
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
46
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
47 method<-options$method
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
48 cutoff_value<-options$cutoff
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
49 ### select a kingdom for phyloseq plot (e.g., "phylum")
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
50 #kingdom_str<-colnames(tax_table)[options$kingdom]
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
51 kingdom_str<-options$kingdom
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
52 distance<-options$distance
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
53 keep<-options$keep
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
54 plottype<-options$plottype
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
55 filter<-options$filter
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
56
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
57 ### prepare the directory and file name
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
58 pdffile <- gsub("[ ]+", "", paste(options$outdir,"/pdffile.pdf"))
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
59 pngfile_nmds <- gsub("[ ]+", "", paste(options$outdir,"/nmds.png"))
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
60 pngfile_nmds_facet <- gsub("[ ]+", "", paste(options$outdir,"/nmds_facet.png"))
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
61 htmlfile <- gsub("[ ]+", "", paste(options$htmlfile))
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
62
1
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
63
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
64 ### This function accepts different two different type of BIOM file format
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
65 readBIOM<-function(inBiom){
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
66 tryCatch({
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
67 phyloseq_obj<-import_biom(inBiom,parallel=TRUE)
1
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
68 return(phyloseq_obj)
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
69 },
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
70 error=function(e){
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
71 biom_obj<-read_biom(inBiom)
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
72
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
73 otu_matrix = as(biom_data(biom_obj), "matrix")
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
74 OTU_TABLE = otu_table(otu_matrix, taxa_are_rows=TRUE)
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
75
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
76 taxonomy_matrix = as.matrix(observation_metadata(biom_obj), rownames.force=TRUE)
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
77 TAXONOMY_TABLE = tax_table(taxonomy_matrix)
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
78
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
79 metadata.temp<-sample_metadata(biom_obj)
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
80 METADATA_TABLE<-plyr::ldply(metadata.temp, rbind)
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
81 rownames(METADATA_TABLE)<-as.character(METADATA_TABLE$.id)
1
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
82
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
83 phyloseq_obj = phyloseq(OTU_TABLE, TAXONOMY_TABLE,sample_data(METADATA_TABLE))
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
84 return(phyloseq_obj)
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
85 }
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
86 )
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
87 }
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
88
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
89
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
90 create_OTU_PDF<-function(pdf_file,phyloseq_obj,phyloseq_ord,kingdom_str,htmlfile,pngfile_nmds,pngfile_nmds_facet){
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
91 pdf(pdf_file);
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
92 p1<-plot_ordination(physeq,physeq_ord,type="taxa",color="Phylum",title="taxa")
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
93 print(p1)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
94 p2<-plot_ordination(physeq,physeq_ord,type="taxa",color="Phylum",title="taxa") + facet_wrap(formula(paste('~',kingdom_str)),3)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
95 print(p2)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
96 garbage<-dev.off();
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
97
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
98 #png('nmds.png')
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
99 bitmap(pngfile_nmds,"png16m")
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
100 p3<-plot_ordination(physeq,physeq_ord,type="taxa",color="Phylum",title="taxa")
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
101 print(p3)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
102 garbage<-dev.off()
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
103
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
104 #png('nmds_facet.png')
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
105 bitmap(pngfile_nmds_facet,"png16m")
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
106 p4<-plot_ordination(physeq,physeq_ord,type="taxa",color="Phylum",title="taxa") + facet_wrap(formula(paste('~',kingdom_str)),3)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
107 print(p4)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
108 garbage<-dev.off()
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
109
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
110 create_HTML_1(htmlfile)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
111 }
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
112
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
113 create_SAMPLE_PDF<-function(pdf_file,phyloseq_obj,phyloseq_ord,htmlfile,pngfile_nmds,category_type){
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
114 pdf(pdf_file);
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
115 p <- plot_ordination(physeq_filter, physeq_ord, type="samples", color=category_type)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
116 p <- p + geom_point(aes(fill=category_type)) + geom_point(size=5) + ggtitle("samples")
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
117 print(p)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
118 garbage<-dev.off();
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
119
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
120 #png('nmds.png')
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
121 bitmap(pngfile_nmds,"png16m")
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
122 p1 <- plot_ordination(physeq_filter, physeq_ord, type="samples", color=category_type)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
123 p1 <- p1 + geom_point(aes(fill=category_type)) + geom_point(size=5) + ggtitle("samples")
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
124 print(p1)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
125 garbage<-dev.off();
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
126
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
127 create_HTML_2(htmlfile)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
128 }
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
129
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
130 create_HTML_1<-function(htmlfile){
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
131 htmlfile_handle <- file(htmlfile)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
132 html_output = c('<html><body>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
133 '<table align="center>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
134 '<tr>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
135 '<td valign="middle" style="vertical-align:middle;">',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
136 '<a href="pdffile.pdf"><img src="nmds.png"/></a>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
137 '</td>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
138 '</tr>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
139 '<tr>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
140 '<td valign="middle" style="vertical-align:middle;">',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
141 '<a href="pdffile.pdf"><img src="nmds_facet.png"/></a>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
142 '</td>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
143 '</tr>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
144 '</table>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
145 '</html></body>');
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
146 writeLines(html_output, htmlfile_handle);
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
147 close(htmlfile_handle);
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
148 }
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
149
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
150 create_HTML_2<-function(htmlfile){
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
151 htmlfile_handle <- file(htmlfile)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
152 html_output = c('<html><body>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
153 '<table align="center>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
154 '<tr>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
155 '<td valign="middle" style="vertical-align:middle;">',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
156 '<a href="pdffile.pdf"><img src="nmds.png"/></a>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
157 '</td>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
158 '</tr>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
159 '</table>',
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
160 '</html></body>');
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
161 writeLines(html_output, htmlfile_handle);
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
162 close(htmlfile_handle);
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
163 }
1
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
164
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
165 if(!is.null(options$biom)){
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
166
1
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
167 #physeq<-import_biom(options$biom)
e376a618eb9f Updated phyloseq_nmds.R to allow for non standard BIOM files.
simon-gladman
parents: 0
diff changeset
168 physeq<-readBIOM(options$biom)
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
169
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
170 if(length(rank_names(physeq)) == 8){
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
171 tax_table(physeq) <- tax_table(physeq)[,-1]
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
172 colnames(tax_table(physeq)) <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
173 } else {
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
174 colnames(tax_table(physeq)) <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
175 }
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
176
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
177 ### select column name from sample table for nmds plot
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
178 ## which(colnames(sample_data(biom)) == "vegetation_type_id")
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
179 #category_type<-colnames(sample_data(physeq))[options$subset]
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
180 category_type <- options$subset
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
181
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
182 ### obtain the unique value in the selected column from sample table
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
183 category_option<-unique(sample_data(physeq))[,options$subset]
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
184
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
185 }else{
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
186
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
187 ### read the data into correct data type to create phyloseq object
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
188 otu_table<-as.matrix(read.table(options$otu_table,header=T,sep="\t"))
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
189 tax_table<-as.matrix(read.table(options$tax_table,header=T,sep="\t"))
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
190 sample_table<-read.table(options$meta_table,header=T,sep="\t",stringsAsFactors=F)
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
191
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
192
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
193 ### select column name from sample table for nmds plot
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
194 category_type<-colnames(sample_table)[options$category]
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
195
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
196 ### obtain the unique value in the selected column from sample table
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
197 category_option<-unique(sample_table[,options$category])
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
198
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
199
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
200 ### create a sample object for phyloseq
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
201 sample_object<-sample_data(sample_table)
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
202
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
203 ### create otu object for phyloseq
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
204 OTU<-otu_table(otu_table, taxa_are_rows = TRUE)
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
205
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
206 ### create tax object for phyloseq
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
207 TAX<-tax_table(tax_table)
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
208
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
209 ### create a phyloseq object
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
210 physeq = phyloseq(OTU,TAX,sample_object)
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
211 }
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
212 ### select a kingdom for phyloseq plot (e.g., "phylum")
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
213 #kingdom_str<-colnames(tax_table)[options$kingdom]
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
214 #kingdom_str<-options$kingdom
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
215
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
216
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
217 ### filtering OTUs based on cutoff value (e.g., 5)
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
218 #physeq_temp =genefilter_sample(physeq, filterfun_sample(function(x) x > cutoff_value), A=0.1*nsamples(physeq))
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
219 physeq_temp =genefilter_sample(physeq, filterfun_sample(function(x) x > cutoff_value), A=filter*nsamples(physeq))
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
220
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
221 ### phyloseq object after filtered
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
222 physeq_filter = prune_taxa(physeq_temp, physeq)
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
223
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
224 ## Transform to even sampling depth
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
225 physeq_filter = transform_sample_counts(physeq_filter, function(x) 1E6 * x/sum(x))
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
226
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
227
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
228
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
229 if(plottype == 1){
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
230
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
231 # kingdom_str <- as.numeric(kingdom_str)
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
232 ## Keep only the most abundant five phyla
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
233
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
234 phylum.sum = tapply(taxa_sums(physeq_filter), tax_table(physeq_filter)[, kingdom_str], sum,na.rm=TRUE)
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
235 ### number of most abundance phyla to keep
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
236 topphyla = names(sort(phylum.sum, TRUE))[1:keep]
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
237
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
238 physeq_filter = prune_taxa((tax_table(physeq_filter)[, kingdom_str] %in% topphyla), physeq_filter)
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
239
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
240 }else{
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
241
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
242 otu_table(physeq_filter)<-otu_table(physeq_filter)[,colSums(otu_table(physeq_filter)) > 1]
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
243
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
244 }
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
245 ### select category to plot NMDS
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
246 category_input = get_variable(physeq_filter, category_type) %in% category_option
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
247 sample_data(physeq_filter)$category_input <- factor(category_input)
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
248
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
249 physeq_ord<-ordinate(physeq_filter,method,distance)
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
250
2
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
251 if(plottype == 1){
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
252 #kingdom_str = colnames(tax_table)[2]
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
253 create_OTU_PDF(pdffile,physeq_filter,physeq_ord,kingdom_str,htmlfile,pngfile_nmds,pngfile_nmds_facet)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
254 }else{
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
255 create_SAMPLE_PDF(pdffile,physeq_filter,physeq_ord,htmlfile,pngfile_nmds,category_type)
20adf95eb758 Updated phyloseq_nmds.xml to allow for new datatype.
simon-gladman
parents: 1
diff changeset
256 }
0
b4606394e7ec planemo upload
simon-gladman
parents:
diff changeset
257