~ubuntu-branches/ubuntu/trusty/qiime/trusty

« back to all changes in this revision

Viewing changes to qiime/support_files/R/permdisp.r

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2013-06-17 18:28:26 UTC
  • mfrom: (9.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20130617182826-376az5ad080a0sfe
Tags: 1.7.0+dfsg-1
Upload preparations done for BioLinux to Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Runs vegan function betadisper on QIIME distance matrix
 
2
# usage:
 
3
# R --slave --args --source_dir $QIIME_HOME/qiime/support_files/R/ -d unifrac.txt -m Fasting_Map.txt -c Treatment -o permdisp < permdisp.r
 
4
#
 
5
# print help string:
 
6
# R --slave --args -h --source_dir $QIIME_HOME/qiime/support_files/R/ < permdisp.r
 
7
#
 
8
# Requires command-line param --source_dir pointing to QIIME R source dir
 
9
 
 
10
# load libraries and source files
 
11
args <- commandArgs(trailingOnly=TRUE)
 
12
if(!is.element('--source_dir', args)){
 
13
    stop("\n\nPlease use '--source_dir' to specify the R source code directory.\n\n")
 
14
}
 
15
sourcedir <- args[which(args == '--source_dir') + 1]
 
16
source(sprintf('%s/loaddata.r',sourcedir))
 
17
source(sprintf('%s/util.r',sourcedir))
 
18
load.library('optparse')
 
19
load.library('vegan')
 
20
 
 
21
# make option list and parse command line
 
22
option_list <- list(
 
23
    make_option(c("--source_dir"), type="character",
 
24
        help="Path to R source directory [required]."),
 
25
    make_option(c("-d", "--distmat"), type="character",
 
26
        help="Input distance matrix [required]."),
 
27
    make_option(c("-m", "--mapfile"), type="character",
 
28
        help="Input metadata mapping file [required]."),
 
29
    make_option(c("-c", "--category"), type="character",
 
30
        help="Metadata column header giving cluster IDs [required]."),
 
31
    make_option(c("-n", "--num_permutations"), type="integer", default=999,
 
32
        help="Number of permutations [default %default]."),
 
33
    make_option(c("-o", "--outdir"), type="character", default='.',
 
34
        help="Output directory [default %default]")
 
35
)
 
36
opts <- parse_args(OptionParser(option_list=option_list), args=args)
 
37
 
 
38
# error checking
 
39
if(is.null(opts$mapfile)) stop('Please supply a mapping file.')
 
40
if(is.null(opts$category)) stop('Please supply a mapping file header.')
 
41
if(is.null(opts$distmat)) stop('Please supply a distance matrix.')
 
42
 
 
43
# create output directory if needed
 
44
if(opts$outdir != ".") dir.create(opts$outdir,showWarnings=FALSE, recursive=TRUE)
 
45
 
 
46
# load qiime data
 
47
map <- load.qiime.mapping.file(opts$mapfile)
 
48
distmat <- load.qiime.distance.matrix(opts$distmat)
 
49
qiime.data <- remove.nonoverlapping.samples(map=map, distmat=distmat)
 
50
 
 
51
# Run betadisper.
 
52
mod <- betadisper(as.dist(qiime.data$distmat), qiime.data$map[[opts$category]])
 
53
 
 
54
# Perform parametric test.
 
55
results1 <- anova(mod)
 
56
 
 
57
# Perform nonparametric test.
 
58
results2 <- permutest(mod, pairwise=TRUE,
 
59
                      control=permControl(nperm=opts$num_permutations))
 
60
 
 
61
# write output file
 
62
filepath <- sprintf('%s/permdisp_results.txt',opts$outdir)
 
63
sink(filepath)
 
64
print(results1)
 
65
print(results2)
 
66
sink(NULL)