Mercurial > repos > qfabrepo > metadegalaxy_phyloseq_deseq2
comparison phyloseq_2_deseq2.r @ 0:1f0569e94be4 draft
"planemo upload for repository https://github.com/QFAB-Bioinformatics/metaDEGalaxy/tree/master/phyloseq_2_deseq2 commit 8bd68662b72404f6291e9628327dcb109b5fa55e"
author | qfabrepo |
---|---|
date | Mon, 14 Sep 2020 08:20:41 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1f0569e94be4 |
---|---|
1 library('getopt') | |
2 suppressPackageStartupMessages(library('phyloseq')) | |
3 suppressPackageStartupMessages(library('DESeq2')) | |
4 | |
5 options(warn= -1) | |
6 | |
7 option_specification = matrix(c( | |
8 'biomfile','b',2,'character', | |
9 'metafile','m',2,'character', | |
10 'factor','f',2,'numeric', | |
11 'test','t',2,'character', | |
12 'fitType','T',2,'character', | |
13 'cutoff','c','2','double', | |
14 'outdir','o',2,'character', | |
15 'result','r',2,'character', | |
16 'normalisedResult','n','2','character' | |
17 ),byrow=TRUE,ncol=4); | |
18 | |
19 | |
20 options <- getopt(option_specification); | |
21 options(bitmapType="cairo") | |
22 | |
23 | |
24 if (!is.null(options$outdir)) { | |
25 # Create the directory | |
26 dir.create(options$outdir,FALSE) | |
27 } | |
28 | |
29 | |
30 galaxy_biom <- import_biom(options$biomfile) | |
31 galaxy_map <- import_qiime_sample_data(options$metafile) | |
32 tax_col_norm <- c("Kingdom","Phylum","Class","Order","Family","Genus","Species") | |
33 tax_col_extra <- c("None","Kingdom","Phylum","Class","Order","Family","Genus","Species") | |
34 | |
35 number.of.tax.rank<-length(colnames(tax_table(galaxy_biom))) | |
36 | |
37 if( number.of.tax.rank == 7){ | |
38 colnames(tax_table(galaxy_biom)) <- tax_col_norm | |
39 }else{ | |
40 colnames(tax_table(galaxy_biom)) <- tax_col_extra | |
41 } | |
42 | |
43 | |
44 AIP_galaxy <- merge_phyloseq(galaxy_biom,galaxy_map) | |
45 | |
46 | |
47 Infactor<-colnames(galaxy_map)[options$factor] | |
48 method<-options$test | |
49 Type<-options$fitType | |
50 cutoff<-options$cutoff | |
51 | |
52 | |
53 | |
54 suppressMessages(deseq2_obj<-phyloseq_to_deseq2(AIP_galaxy,as.formula(paste('~',Infactor,sep="")))) | |
55 gm_mean = function(x, na.rm=TRUE){ | |
56 exp(sum(log(x[x > 0]), na.rm=na.rm) / length(x)) | |
57 } | |
58 geoMeans = apply(counts(deseq2_obj), 1, gm_mean) | |
59 deseq2_obj = estimateSizeFactors(deseq2_obj, geoMeans = geoMeans) | |
60 | |
61 | |
62 | |
63 ### Normalisation | |
64 deseq2_obj_norm<-counts(deseq2_obj,normalized=T) | |
65 deseq2_obj_norm.out<-as.data.frame(cbind("OTUID"=rownames(deseq2_obj_norm),deseq2_obj_norm)) | |
66 write.table(deseq2_obj_norm.out,file=options$normalisedResult,col.names=T,row.names=F,quote=F,sep="\t") | |
67 | |
68 | |
69 ### Normalisation and DE analysis | |
70 suppressMessages(deseq2_obj_DE<-DESeq(deseq2_obj,test=method,fitType=Type)) | |
71 res = results(deseq2_obj_DE,cooksCutoff = FALSE) | |
72 | |
73 significant.table <-res[which(res$padj < cutoff),] | |
74 | |
75 if(nrow(significant.table) == 0){ | |
76 out_message <-"no significant result found!" | |
77 write(out_message,file=options$result,sep="\t") | |
78 quit("yes") | |
79 } | |
80 | |
81 significant.table <- cbind(as(significant.table,"data.frame"), as(tax_table(AIP_galaxy)[rownames(significant.table),],"matrix")) | |
82 | |
83 significant.table.out<-as.data.frame(cbind("OTUID"=rownames(significant.table),significant.table)) | |
84 | |
85 write.table(format(significant.table.out, digits=4, scientific=F),file=options$result,col.names=T,row.names=F,quote=F,sep="\t") | |
86 |