annotate src/Maaslin.R @ 8:e9677425c6c3 default tip

Updated the structure of the libraries
author george.weingart@gmail.com
date Mon, 09 Feb 2015 12:17:40 -0500
parents e0b5980139d9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
1 #!/usr/bin/env Rscript
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
2 #####################################################################################
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
3 #Copyright (C) <2012>
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
4 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
5 #Permission is hereby granted, free of charge, to any person obtaining a copy of
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
6 #this software and associated documentation files (the "Software"), to deal in the
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
7 #Software without restriction, including without limitation the rights to use, copy,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
8 #modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
9 #and to permit persons to whom the Software is furnished to do so, subject to
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
10 #the following conditions:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
11 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
12 #The above copyright notice and this permission notice shall be included in all copies
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
13 #or substantial portions of the Software.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
14 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
15 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
16 #INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
17 #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
18 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
19 #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
20 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
21 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
22 # This file is a component of the MaAsLin (Multivariate Associations Using Linear Models),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
23 # authored by the Huttenhower lab at the Harvard School of Public Health
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
24 # (contact Timothy Tickle, ttickle@hsph.harvard.edu).
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
25 #####################################################################################
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
26
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
27 inlinedocs <- function(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
28 ##author<< Curtis Huttenhower <chuttenh@hsph.harvard.edu> and Timothy Tickle <ttickle@hsph.harvard.edu>
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
29 ##description<< Main driver script. Should be called to perform MaAsLin Analysis.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
30 ) { return( pArgs ) }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
31
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
32
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
33 ### Install packages if not already installed
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
34 vDepLibrary = c("agricolae", "gam", "gamlss", "gbm", "glmnet", "inlinedocs", "logging", "MASS", "nlme", "optparse", "outliers", "penalized", "pscl", "robustbase", "testthat")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
35 for(sDepLibrary in vDepLibrary)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
36 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
37 if(! require(sDepLibrary, character.only=TRUE) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
38 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
39 install.packages(pkgs=sDepLibrary, repos="http://cran.us.r-project.org")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
40 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
41 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
42
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
43 ### Logging class
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
44 suppressMessages(library( logging, warn.conflicts=FALSE, quietly=TRUE, verbose=FALSE))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
45 ### Class for commandline argument processing
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
46 suppressMessages(library( optparse, warn.conflicts=FALSE, quietly=TRUE, verbose=FALSE))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
47
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
48
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
49 ### Create command line argument parser
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
50 pArgs <- OptionParser( usage = "%prog [options] <output.txt> <data.tsv>" )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
51
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
52 # Input files for MaAsLin
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
53 ## Data configuration file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
54 pArgs <- add_option( pArgs, c("-i", "--input_config"), type="character", action="store", dest="strInputConfig", metavar="data.read.config", help="Optional configuration file describing data input format.")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
55 ## Data manipulation/normalization file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
56 pArgs <- add_option( pArgs, c("-I", "--input_process"), type="character", action="store", dest="strInputR", metavar="data.R", help="Optional configuration script normalizing or processing data.")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
57
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
58 # Settings for MaAsLin
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
59 ## Maximum false discovery rate
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
60 pArgs <- add_option( pArgs, c("-d", "--fdr"), type="double", action="store", dest="dSignificanceLevel", default=0.25, metavar="significance", help="The threshold to use for significance for the generated q-values (BH FDR). Anything equal to or lower than this is significant. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
61 ## Minimum feature relative abundance filtering
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
62 pArgs <- add_option( pArgs, c("-r", "--minRelativeAbundance"), type="double", action="store", dest="dMinAbd", default=0.0001, metavar="minRelativeAbundance", help="The minimum relative abundance allowed in the data. Values below this are removed and imputed as the median of the sample data. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
63 ## Minimum feature prevalence filtering
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
64 pArgs <- add_option( pArgs, c("-p", "--minPrevalence"), type="double", action="store", dest="dMinSamp", default=0.1, metavar="minPrevalence", help="The minimum percentage of samples a feature can have abundance in before being removed. Also is the minimum percentage of samples a metadata can have that are not NA before being removed. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
65 ## Fence for outlier, if not set Grubbs test is used
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
66 pArgs <- add_option( pArgs, c("-o", "--outlierFence"), type="double", action="store", dest="dOutlierFence", default=0, metavar="outlierFence", help="Outliers are defined as this number times the interquartile range added/subtracted from the 3rd/1st quartiles respectively. If set to 0 (default), outliers are defined by the Grubbs test. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
67 ## Significance for Grubbs test
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
68 pArgs <- add_option(pArgs, c("-G","--grubbsSig"), type="double", action="store", dest="dPOutlier", default=0.05, metavar="grubbsAlpha", help="This is the significance cuttoff used to indicate an outlier or not. The closer to zero, the more significant an outlier must be to be removed. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
69 ## Fixed (not random) covariates
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
70 pArgs <- add_option( pArgs, c("-R","--random"), type="character", action="store", dest="strRandomCovariates", default=NULL, metavar="fixed", help="These metadata will be treated as random covariates. Comma delimited data feature names. These features must be listed in the read.config file. Example '-R RandomMetadata1,RandomMetadata2'. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
71 ## Change the type of correction fo rmultiple corrections
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
72 pArgs <- add_option( pArgs, c("-T","--testingCorrection"), type="character", action="store", dest="strMultTestCorrection", default="BH", metavar="multipleTestingCorrection", help="This indicates which multiple hypothesis testing method will be used, available are holm, hochberg, hommel, bonferroni, BH, BY. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
73 ## Use a zero inflated model of the inference method indicate in -m
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
74 pArgs <- add_option( pArgs, c("-z","--doZeroInfated"), type="logical", action="store_true", default = FALSE, dest="fZeroInflated", metavar="fZeroInflated", help="If true, the zero inflated version of the inference model indicated in -m is used. For instance if using lm, zero-inflated regression on a gaussian distribution is used. [Default %default].")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
75
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
76 # Arguments used in validation of MaAsLin
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
77 ## Model selection (enumerate) c("none","boost","penalized","forward","backward")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
78 pArgs <- add_option( pArgs, c("-s", "--selection"), type="character", action="store", dest="strModelSelection", default="boost", metavar="model_selection", help="Indicates which of the variable selection techniques to use. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
79 ## Argument indicating which method should be ran (enumerate) c("univariate","lm","neg_binomial","quasi")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
80 pArgs <- add_option( pArgs, c("-m", "--method"), type="character", action="store", dest="strMethod", default="lm", metavar="analysis_method", help="Indicates which of the statistical inference methods to run. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
81 ## Argument indicating which link function is used c("none","asinsqrt")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
82 pArgs <- add_option( pArgs, c("-l", "--link"), type="character", action="store", dest="strTransform", default="asinsqrt", metavar="transform_method", help="Indicates which link or transformation to use with a glm, if glm is not selected this argument will be set to none. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
83 pArgs <- add_option( pArgs, c("-Q","--NoQC"), type="logical", action="store_true", default=FALSE, dest="fNoQC", metavar="Do_Not_Run_QC", help="Indicates if the quality control will be ran on the metadata/data. Default is true. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
84
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
85 # Arguments to suppress MaAsLin actions on certain data
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
86 ## Do not perform model selection on the following data
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
87 pArgs <- add_option( pArgs, c("-F","--forced"), type="character", action="store", dest="strForcedPredictors", default=NULL, metavar="forced_predictors", help="Metadata features that will be forced into the model seperated by commas. These features must be listed in the read.config file. Example '-F Metadata2,Metadata6,Metadata10'. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
88 ## Do not impute the following
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
89 pArgs <- add_option( pArgs, c("-n","--noImpute"), type="character", action="store", dest="strNoImpute", default=NULL, metavar="no_impute", help="These data will not be imputed. Comma delimited data feature names. Example '-n Feature1,Feature4,Feature6'. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
90
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
91 #Miscellaneouse arguments
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
92 ### Argument to control logging (enumerate)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
93 strDefaultLogging = "DEBUG"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
94 pArgs <- add_option( pArgs, c("-v", "--verbosity"), type="character", action="store", dest="strVerbosity", default=strDefaultLogging, metavar="verbosity", help="Logging verbosity [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
95 ### Run maaslin without creating a log file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
96 pArgs <- add_option( pArgs, c("-O","--omitLogFile"), type="logical", action="store_true", default=FALSE, dest="fOmitLogFile", metavar="omitlogfile",help="Including this flag will stop the creation of the output log file. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
97 ### Argument for inverting background to black
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
98 pArgs <- add_option( pArgs, c("-t", "--invert"), type="logical", action="store_true", dest="fInvert", default=FALSE, metavar="invert", help="When given, flag indicates to invert the background of figures to black. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
99 ### Selection Frequency
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
100 pArgs <- add_option( pArgs, c("-f","--selectionFrequency"), type="double", action="store", dest="dSelectionFrequency", default=NA, metavar="selectionFrequency", help="Selection Frequency for boosting (max 1 will remove almost everything). Interpreted as requiring boosting to select metadata 100% percent of the time (or less if given a number that is less). Value should be between 1 (100%) and 0 (0%), NA (default is determined by data size).")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
101 ### All v All
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
102 pArgs <- add_option( pArgs, c("-a","--allvall"), type="logical", action="store_true", dest="fAllvAll", default=FALSE, metavar="compare_all", help="When given, the flag indicates that each fixed covariate that is not indicated as Forced is compared once at a time per data feature (bug). Made to be used with the -F option to specify one part of the model while allowing the other to cycle through a group of covariates. Does not affect Random covariates, which are always included when specified. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
103 pArgs <- add_option( pArgs, c("-N","--PlotNA"), type="logical", action="store_true", default=FALSE, dest="fPlotNA", metavar="plotNAs",help="Plot data that was originally NA, by default they are not plotted. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
104 ### Alternative methodology settings
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
105 pArgs <- add_option( pArgs, c("-A","--pAlpha"), type="double", action="store", dest="dPenalizedAlpha", default=0.95, metavar="PenalizedAlpha",help="The alpha for penalization (1.0=L1 regularization, LASSO; 0.0=L2 regularization, ridge regression. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
106 ### Pass an alternative library dir
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
107 pArgs <- add_option( pArgs, c("-L", "--libdir"), action="store", dest="sAlternativeLibraryLocation", default=file.path( "","usr","share","biobakery" ), metavar="AlternativeLibraryDirectory", help="An alternative location to find the lib directory. This dir and children will be searched for the first maaslin/src/lib dir.")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
108
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
109 ### Misc biplot arguments
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
110 pArgs <- add_option( pArgs, c("-M","--BiplotMetadataScale"), type="double", action="store", dest="dBiplotMetadataScale", default=1, metavar="scaleForMetadata", help="A real number used to scale the metadata labels on the biplot (otherwise a default will be selected from the data). [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
111 pArgs <- add_option( pArgs, c("-C", "--BiplotColor"), type="character", action="store", dest="strBiplotColor", default=NULL, metavar="BiplotColorCovariate", help="A continuous metadata that will be used to color samples in the biplot ordination plot (otherwise a default will be selected from the data). Example Age [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
112 pArgs <- add_option( pArgs, c("-S", "--BiplotShapeBy"), type="character", action="store", dest="strBiplotShapeBy", default=NULL, metavar="BiplotShapeCovariate", help="A discontinuous metadata that will be used to indicate shapes of samples in the Biplot ordination plot (otherwise a default will be selected from the data). Example Sex [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
113 pArgs <- add_option( pArgs, c("-P", "--BiplotPlotFeatures"), type="character", action="store", dest="strBiplotPlotFeatures", default=NULL, metavar="BiplotFeaturesToPlot", help="Metadata and data features to plot (otherwise a default will be selected from the data). Comma Delimited.")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
114 pArgs <- add_option( pArgs, c("-D", "--BiplotRotateMetadata"), type="character", action="store", dest="sRotateByMetadata", default=NULL, metavar="BiplotRotateMetadata", help="Metadata to use to rotate the biplot. Format 'Metadata,value'. 'Age,0.5' . [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
115 pArgs <- add_option( pArgs, c("-B", "--BiplotShapes"), type="character", action="store", dest="sShapes", default=NULL, metavar="BiplotShapes", help="Specify shapes specifically for metadata or metadata values. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
116 pArgs <- add_option( pArgs, c("-b", "--BugCount"), type="integer", action="store", dest="iNumberBugs", default=3, metavar="PlottedBugCount", help="The number of bugs automatically selected from the data to plot. [Default %default]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
117 pArgs <- add_option( pArgs, c("-E", "--MetadataCount"), type="integer", action="store", dest="iNumberMetadata", default=NULL, metavar="PlottedMetadataCount", help="The number of metadata automatically selected from the data to plot. [Default all significant metadata and minimum is 1]")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
118
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
119 #pArgs <- add_option( pArgs, c("-c","--MFAFeatureCount"), type="integer", action="store", dest="iMFAMaxFeatures", default=3, metavar="maxMFAFeature", help="Number of features or number of bugs to plot (default=3; 3 metadata and 3 data).")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
120
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
121 main <- function(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
122 ### The main function manages the following:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
123 ### 1. Optparse arguments are checked
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
124 ### 2. A logger is created if requested in the optional arguments
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
125 ### 3. The custom R script is sourced. This is the input *.R script named
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
126 ### the same as the input *.pcl file. This script contains custom formating
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
127 ### of data and function calls to the MFA visualization.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
128 ### 4. Matrices are written to the project folder as they are read in seperately as metadata and data and merged together.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
129 ### 5. Data is cleaned with custom filtering if supplied in the *.R script.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
130 ### 6. Transformations occur if indicated by the optional arguments
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
131 ### 7. Standard quality control is performed on data
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
132 ### 8. Cleaned metadata and data are written to output project for documentation.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
133 ### 9. A regularization method is ran (boosting by default).
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
134 ### 10. An analysis method is performed on the model (optionally boosted model).
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
135 ### 11. Data is summarized and PDFs are created for significant associations
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
136 ### (those whose q-values {BH FDR correction} are <= the threshold given in the optional arguments.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
137 pArgs
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
138 ### Parsed commandline arguments
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
139 ){
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
140 lsArgs <- parse_args( pArgs, positional_arguments = TRUE )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
141 #logdebug("lsArgs", c_logrMaaslin)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
142 #logdebug(paste(lsArgs,sep=" "), c_logrMaaslin)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
143
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
144 # Parse parameters
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
145 lsForcedParameters = NULL
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
146 if(!is.null(lsArgs$options$strForcedPredictors))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
147 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
148 lsForcedParameters = unlist(strsplit(lsArgs$options$strForcedPredictors,","))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
149 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
150 xNoImpute = NULL
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
151 if(!is.null(lsArgs$options$strNoImpute))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
152 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
153 xNoImpute = unlist(strsplit(lsArgs$options$strNoImpute,"[,]"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
154 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
155 lsRandomCovariates = NULL
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
156 if(!is.null(lsArgs$options$strRandomCovariates))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
157 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
158 lsRandomCovariates = unlist(strsplit(lsArgs$options$strRandomCovariates,"[,]"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
159 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
160 lsFeaturesToPlot = NULL
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
161 if(!is.null(lsArgs$options$strBiplotPlotFeatures))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
162 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
163 lsFeaturesToPlot = unlist(strsplit(lsArgs$options$strBiplotPlotFeatures,"[,]"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
164 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
165
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
166 #If logging is not an allowable value, inform user and set to INFO
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
167 if(length(intersect(names(loglevels), c(lsArgs$options$strVerbosity))) == 0)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
168 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
169 print(paste("Maaslin::Error. Did not understand the value given for logging, please use any of the following: DEBUG,INFO,WARN,ERROR."))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
170 print(paste("Maaslin::Warning. Setting logging value to \"",strDefaultLogging,"\"."))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
171 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
172
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
173 # Do not allow mixed effect models and zero inflated models, don't have implemented
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
174 if(lsArgs$options$fZeroInflated && !is.null(lsArgs$options$strRandomCovariates))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
175 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
176 stop("MaAsLin Error:: The combination of zero inflated models and mixed effects models are not supported.")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
177 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
178
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
179 ### Create logger
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
180 c_logrMaaslin <- getLogger( "maaslin" )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
181 addHandler( writeToConsole, c_logrMaaslin )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
182 setLevel( lsArgs$options$strVerbosity, c_logrMaaslin )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
183
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
184 #Get positional arguments
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
185 if( length( lsArgs$args ) != 2 ) { stop( print_help( pArgs ) ) }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
186 ### Output file name
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
187 strOutputTXT <- lsArgs$args[1]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
188 ### Input TSV data file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
189 strInputTSV <- lsArgs$args[2]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
190
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
191 # Get analysis method options
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
192 # includes data transformations, model selection/regularization, regression models/links
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
193 lsArgs$options$strModelSelection = tolower(lsArgs$options$strModelSelection)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
194 if(!lsArgs$options$strModelSelection %in% c("none","boost","penalized","forward","backward"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
195 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
196 logerror(paste("Received an invalid value for the selection argument, received '",lsArgs$options$strModelSelection,"'"), c_logrMaaslin)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
197 stop( print_help( pArgs ) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
198 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
199 lsArgs$options$strMethod = tolower(lsArgs$options$strMethod)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
200 if(!lsArgs$options$strMethod %in% c("univariate","lm","neg_binomial","quasi"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
201 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
202 logerror(paste("Received an invalid value for the method argument, received '",lsArgs$options$strMethod,"'"), c_logrMaaslin)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
203 stop( print_help( pArgs ) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
204 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
205 lsArgs$options$strTransform = tolower(lsArgs$options$strTransform)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
206 if(!lsArgs$options$strTransform %in% c("none","asinsqrt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
207 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
208 logerror(paste("Received an invalid value for the transform/link argument, received '",lsArgs$options$strTransform,"'"), c_logrMaaslin)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
209 stop( print_help( pArgs ) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
210 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
211
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
212 if(!lsArgs$options$strMultTestCorrection %in% c("holm", "hochberg", "hommel", "bonferroni", "BH", "BY"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
213 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
214 logerror(paste("Received an invalid value for the multiple testing correction argument, received '",lsArgs$options$strMultTestCorrection,"'"), c_logrMaaslin)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
215 stop( print_help( pArgs ) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
216 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
217
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
218 ### Necessary local import files
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
219 ### Check to make sure the lib is in the expected place (where the script is)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
220 ### if not, then try the alternative lib location
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
221 ### This will happen if, for instance the script is linked or
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
222 ### on the path.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
223 # Get the first choice relative path
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
224 initial.options <- commandArgs(trailingOnly = FALSE)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
225 script.name <- sub("--file=", "", initial.options[grep("--file=", initial.options)])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
226 strDir = file.path( dirname( script.name ), "lib" )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
227 # If this does not have the lib file then go for the alt lib
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
228 if( !file.exists(strDir) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
229 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
230 lsPotentialListLocations = dir( path = lsArgs$options$sAlternativeLibraryLocation, pattern = "lib", recursive = TRUE, include.dirs = TRUE)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
231 if( length( lsPotentialListLocations ) > 0 )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
232 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
233 sLibraryPath = file.path( "maaslin","src","lib" )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
234 iLibraryPathLength = nchar( sLibraryPath )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
235 for( strSearchDir in lsPotentialListLocations )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
236 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
237 # Looking for the path where the end of the path is equal to the library path given earlier
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
238 # Also checks before hand to make sure the path is atleast as long as the library path so no errors occur
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
239 if ( substring( strSearchDir, 1 + nchar( strSearchDir ) - iLibraryPathLength ) == sLibraryPath )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
240 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
241 strDir = file.path( lsArgs$options$sAlternativeLibraryLocation, strSearchDir )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
242 break
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
243 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
244 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
245 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
246 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
247
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
248 strSelf = basename( script.name )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
249 for( strR in dir( strDir, pattern = "*.R$" ) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
250 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
251 if( strR == strSelf ) {next}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
252 source( file.path( strDir, strR ) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
253 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
254
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
255 # Get analysis modules
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
256 afuncVariableAnalysis = funcGetAnalysisMethods(lsArgs$options$strModelSelection,lsArgs$options$strTransform,lsArgs$options$strMethod,lsArgs$options$fZeroInflated)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
257
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
258 # Set up parameters for variable selection
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
259 lxParameters = list(dFreq=lsArgs$options$dSelectionFrequency, dPAlpha=lsArgs$options$dPenalizedAlpha)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
260 if((lsArgs$options$strMethod == "lm")||(lsArgs$options$strMethod == "univariate"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
261 { lxParameters$sFamily = "gaussian"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
262 } else if(lsArgs$options$strMethod == "neg_binomial"){ lxParameters$sFamily = "binomial"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
263 } else if(lsArgs$options$strMethod == "quasi"){ lxParameters$sFamily = "poisson"}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
264
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
265 #Indicate start
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
266 logdebug("Start MaAsLin", c_logrMaaslin)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
267 #Log commandline arguments
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
268 logdebug("Commandline Arguments", c_logrMaaslin)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
269 logdebug(lsArgs, c_logrMaaslin)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
270
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
271 ### Output directory for the study based on the requested output file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
272 outputDirectory = dirname(strOutputTXT)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
273 ### Base name for the project based on the read.config name
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
274 strBase <- sub("\\.[^.]*$", "", basename(strInputTSV))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
275
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
276 ### Sources in the custom script
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
277 ### If the custom script is not there then
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
278 ### defaults are used and no custom scripts are ran
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
279 funcSourceScript <- function(strFunctionPath)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
280 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
281 #If is specified, set up the custom func clean variable
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
282 #If the custom script is null then return
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
283 if(is.null(strFunctionPath)){return(NULL)}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
284
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
285 #Check to make sure the file exists
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
286 if(file.exists(strFunctionPath))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
287 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
288 #Read in the file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
289 source(strFunctionPath)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
290 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
291 #Handle when the file does not exist
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
292 stop(paste("MaAsLin Error: A custom data manipulation script was indicated but was not found at the file path: ",strFunctionPath,sep=""))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
293 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
294 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
295
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
296 #Read file
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
297 inputFileData = funcReadMatrices(lsArgs$options$strInputConfig, strInputTSV, log=TRUE)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
298 if(is.null(inputFileData[[c_strMatrixMetadata]])) { names(inputFileData)[1] <- c_strMatrixMetadata }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
299 if(is.null(inputFileData[[c_strMatrixData]])) { names(inputFileData)[2] <- c_strMatrixData }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
300
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
301 #Metadata and bug names
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
302 lsOriginalMetadataNames = names(inputFileData[[c_strMatrixMetadata]])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
303 lsOriginalFeatureNames = names(inputFileData[[c_strMatrixData]])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
304
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
305 #Dimensions of the datasets
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
306 liMetaData = dim(inputFileData[[c_strMatrixMetadata]])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
307 liData = dim(inputFileData[[c_strMatrixData]])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
308
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
309 #Merge data files together
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
310 frmeData = merge(inputFileData[[c_strMatrixMetadata]],inputFileData[[c_strMatrixData]],by.x=0,by.y=0)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
311 #Reset rownames
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
312 row.names(frmeData) = frmeData[[1]]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
313 frmeData = frmeData[-1]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
314
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
315 #Write QC files only in certain modes of verbosity
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
316 # Read in and merge files
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
317 if( c_logrMaaslin$level <= loglevels["DEBUG"] ) {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
318 # If the QC internal file does not exist, make
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
319 strQCDir = file.path(outputDirectory,"QC")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
320 dir.create(strQCDir, showWarnings = FALSE)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
321 # Write metadata matrix before merge
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
322 funcWriteMatrices(dataFrameList=list(Metadata = inputFileData[[c_strMatrixMetadata]]), saveFileList=c(file.path(strQCDir,"metadata.tsv")), configureFileName=c(file.path(strQCDir,"metadata.read.config")), acharDelimiter="\t")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
323 # Write data matrix before merge
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
324 funcWriteMatrices(dataFrameList=list(Data = inputFileData[[c_strMatrixData]]), saveFileList=c(file.path(strQCDir,"data.tsv")), configureFileName=c(file.path(strQCDir,"data.read.config")), acharDelimiter="\t")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
325 #Record the data as it has been read
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
326 funcWriteMatrices(dataFrameList=list(Merged = frmeData), saveFileList=c(file.path(strQCDir,"read-Merged.tsv")), configureFileName=c(file.path(strQCDir,"read-Merged.read.config")), acharDelimiter="\t")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
327 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
328
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
329 #Data needed for the MaAsLin environment
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
330 #List of lists (one entry per file)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
331 #Is contained by a container of itself
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
332 #lslsData = list()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
333 #List
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
334 lsData = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
335
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
336 #List of metadata indicies
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
337 aiMetadata = c(1:liMetaData[2])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
338 lsData$aiMetadata = aiMetadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
339 #List of data indicies
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
340 aiData = c(1:liData[2])+liMetaData[2]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
341 lsData$aiData = aiData
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
342 #Add a list to hold qc metrics and counts
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
343 lsData$lsQCCounts$aiDataInitial = aiData
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
344 lsData$lsQCCounts$aiMetadataInitial = aiMetadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
345
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
346 #Raw data
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
347 lsData$frmeRaw = frmeData
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
348
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
349 #Load script if it exists, stop on error
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
350 funcProcess <- NULL
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
351 if(!is.null(funcSourceScript(lsArgs$options$strInputR))){funcProcess <- get(c_strCustomProcessFunction)}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
352
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
353 #Clean the data and update the current data list to the cleaned data list
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
354 funcTransformData = afuncVariableAnalysis[[c_iTransform]]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
355 lsQCCounts = list(aiDataCleaned = c(), aiMetadataCleaned = c())
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
356 lsRet = list(frmeData=frmeData, aiData=aiData, aiMetadata=aiMetadata, lsQCCounts=lsQCCounts, liNaIndices=c())
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
357
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
358 viNotTransformedDataIndices = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
359 if(!lsArgs$options$fNoQC)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
360 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
361 c_logrMaaslin$info( "Running quality control." )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
362 lsRet = funcClean( frmeData=frmeData, funcDataProcess=funcProcess, aiMetadata=aiMetadata, aiData=aiData, lsQCCounts=lsData$lsQCCounts, astrNoImpute=xNoImpute, dMinSamp = lsArgs$options$dMinSamp, dMinAbd = lsArgs$options$dMinAbd, dFence=lsArgs$options$dOutlierFence, funcTransform=funcTransformData, dPOutlier=lsArgs$options$dPOutlier)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
363
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
364 viNotTransformedDataIndices = lsRet$viNotTransformedData
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
365
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
366 #If using a count based model make sure all are integer (QCing can add in numeric values during interpolation for example)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
367 if(lsArgs$options$strMethod %in% c_vCountBasedModels)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
368 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
369 c_logrMaaslin$info( "Assuring the data matrix is integer." )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
370 for(iDataIndex in aiData)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
371 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
372 lsRet$frmeData[ iDataIndex ] = round( lsRet$frmeData[ iDataIndex ] )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
373 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
374 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
375 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
376 c_logrMaaslin$info( "Not running quality control, attempting transform." )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
377 ### Need to do transform if the QC is not performed
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
378 iTransformed = 0
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
379 for(iDataIndex in aiData)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
380 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
381 if( ! funcTransformIncreasesOutliers( lsRet$frmeData[iDataIndex], funcTransformData ) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
382 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
383 lsRet$frmeData[iDataIndex]=funcTransformData(lsRet$frmeData[iDataIndex])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
384 iTransformed = iTransformed + 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
385 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
386 viNotTransformedDataIndices = c(viNotTransformedDataIndices, iDataIndex)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
387 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
388 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
389 c_logrMaaslin$info(paste("Number of features transformed = ", iTransformed))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
390 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
391
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
392 logdebug("lsRet", c_logrMaaslin)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
393 logdebug(format(lsRet), c_logrMaaslin)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
394 #Update the variables after cleaning
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
395 lsRet$frmeRaw = frmeData
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
396 lsRet$lsQCCounts$aiDataCleaned = lsRet$aiData
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
397 lsRet$lsQCCounts$aiMetadataCleaned = lsRet$aiMetadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
398
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
399 #Add List of metadata string names
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
400 astrMetadata = colnames(lsRet$frmeData)[lsRet$aiMetadata]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
401 lsRet$astrMetadata = astrMetadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
402
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
403 # If plotting NA data reset the NA metadata indices to empty so they will not be excluded
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
404 if(lsArgs$options$fPlotNA)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
405 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
406 lsRet$liNaIndices = list()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
407 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
408
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
409 #Write QC files only in certain modes of verbosity
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
410 if( c_logrMaaslin$level <= loglevels["DEBUG"] ) {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
411 #Record the data after cleaning
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
412 funcWriteMatrices(dataFrameList=list(Cleaned = lsRet$frmeData[union(lsRet$aiMetadata,lsRet$aiData)]), saveFileList=c(file.path(strQCDir,"read_cleaned.tsv")), configureFileName=c(file.path(strQCDir,"read_cleaned.read.config")), acharDelimiter="\t") }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
413
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
414 #These variables will be used to count how many features get analysed
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
415 lsRet$lsQCCounts$iBoosts = 0
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
416 lsRet$lsQCCounts$iBoostErrors = 0
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
417 lsRet$lsQCCounts$iNoTerms = 0
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
418 lsRet$lsQCCounts$iLms = 0
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
419
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
420 #Indicate if the residuals plots should occur
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
421 fDoRPlot=TRUE
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
422 #Should not occur for univariates
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
423 if(lsArgs$options$strMethod %in% c("univariate")){ fDoRPlot=FALSE }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
424
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
425 #Run analysis
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
426 alsRetBugs = funcBugs( frmeData=lsRet$frmeData, lsData=lsRet, aiMetadata=lsRet$aiMetadata, aiData=lsRet$aiData, aiNotTransformedData=viNotTransformedDataIndices, strData=strBase, dSig=lsArgs$options$dSignificanceLevel, fInvert=lsArgs$options$fInvert,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
427 strDirOut=outputDirectory, funcReg=afuncVariableAnalysis[[c_iSelection]], funcTransform=funcTransformData, funcUnTransform=afuncVariableAnalysis[[c_iUnTransform]], lsNonPenalizedPredictors=lsForcedParameters,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
428 funcAnalysis=afuncVariableAnalysis[[c_iAnalysis]], lsRandomCovariates=lsRandomCovariates, funcGetResults=afuncVariableAnalysis[[c_iResults]], fDoRPlot=fDoRPlot, fOmitLogFile=lsArgs$options$fOmitLogFile,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
429 fAllvAll=lsArgs$options$fAllvAll, liNaIndices=lsRet$liNaIndices, lxParameters=lxParameters, strTestingCorrection=lsArgs$options$strMultTestCorrection,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
430 fIsUnivariate=afuncVariableAnalysis[[c_iIsUnivariate]], fZeroInflated=lsArgs$options$fZeroInflated )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
431
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
432 #Write QC files only in certain modes of verbosity
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
433 if( c_logrMaaslin$level <= loglevels["DEBUG"] ) {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
434 funcWriteQCReport(strProcessFileName=file.path(strQCDir,"ProcessQC.txt"), lsQCData=alsRetBugs$lsQCCounts, liDataDim=liData, liMetadataDim=liMetaData)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
435
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
436 ### Write out the parameters used in the run
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
437 unlink(file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
438 funcWrite("Parameters used in the MaAsLin run", file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
439 funcWrite(paste("Optional input read.config file=",lsArgs$options$strInputConfig), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
440 funcWrite(paste("Optional R file=",lsArgs$options$strInputR), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
441 funcWrite(paste("FDR threshold for pdf generation=",lsArgs$options$dSignificanceLevel), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
442 funcWrite(paste("Minimum relative abundance=",lsArgs$options$dMinAbd), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
443 funcWrite(paste("Minimum percentage of samples with measurements=",lsArgs$options$dMinSamp), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
444 funcWrite(paste("The fence used to define outliers with a quantile based analysis. If set to 0, the Grubbs test was used=",lsArgs$options$dOutlierFence), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
445 funcWrite(paste("Ignore if the Grubbs test was not used. The significance level used as a cut-off to define outliers=",lsArgs$options$dPOutlier), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
446 funcWrite(paste("These covariates are treated as random covariates and not fixed covariates=",lsArgs$options$strRandomCovariates), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
447 funcWrite(paste("The type of multiple testing correction used=",lsArgs$options$strMultTestCorrection), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
448 funcWrite(paste("Zero inflated inference models were turned on=",lsArgs$options$fZeroInflated), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
449 funcWrite(paste("Feature selection step=",lsArgs$options$strModelSelection), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
450 funcWrite(paste("Statistical inference step=",lsArgs$options$strMethod), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
451 funcWrite(paste("Numeric transform used=",lsArgs$options$strTransform), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
452 funcWrite(paste("Quality control was run=",!lsArgs$options$fNoQC), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
453 funcWrite(paste("These covariates were forced into each model=",lsArgs$options$strForcedPredictors), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
454 funcWrite(paste("These features' data were not changed by QC processes=",lsArgs$options$strNoImpute), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
455 funcWrite(paste("Output verbosity=",lsArgs$options$strVerbosity), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
456 funcWrite(paste("Log file was generated=",!lsArgs$options$fOmitLogFile), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
457 funcWrite(paste("Data plots were inverted=",lsArgs$options$fInvert), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
458 funcWrite(paste("Ignore unless boosting was used. The threshold for the rel.inf used to select features=",lsArgs$options$dSelectionFrequency), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
459 funcWrite(paste("All verses all inference method was used=",lsArgs$options$fAllvAll), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
460 funcWrite(paste("Ignore unless penalized feature selection was used. Alpha to determine the type of penalty=",lsArgs$options$dPenalizedAlpha), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
461 funcWrite(paste("Biplot parameter, user defined metadata scale=",lsArgs$options$dBiplotMetadataScale), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
462 funcWrite(paste("Biplot parameter, user defined metadata used to color the plot=",lsArgs$options$strBiplotColor), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
463 funcWrite(paste("Biplot parameter, user defined metadata used to dictate the shapes of the plot markers=",lsArgs$options$strBiplotShapeBy), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
464 funcWrite(paste("Biplot parameter, user defined user requested features to plot=",lsArgs$options$strBiplotPlotFeatures), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
465 funcWrite(paste("Biplot parameter, user defined metadata used to rotate the plot ordination=",lsArgs$options$sRotateByMetadata), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
466 funcWrite(paste("Biplot parameter, user defined custom shapes for metadata=",lsArgs$options$sShapes), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
467 funcWrite(paste("Biplot parameter, user defined number of bugs to plot =",lsArgs$options$iNumberBugs), file.path(strQCDir,"Run_Parameters.txt"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
468 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
469
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
470 ### Write summary table
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
471 # Summarize output files based on a keyword and a significance threshold
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
472 # Look for less than or equal to the threshold (appropriate for p-value and q-value type measurements)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
473 # DfSummary is sorted by the q.value when it is returned
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
474 dfSummary = funcSummarizeDirectory(astrOutputDirectory=outputDirectory,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
475 strBaseName=strBase,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
476 astrSummaryFileName=file.path(outputDirectory,paste(strBase,c_sSummaryFileSuffix, sep="")),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
477 astrKeyword=c_strKeywordEvaluatedForInclusion,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
478 afSignificanceLevel=lsArgs$options$dSignificanceLevel)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
479
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
480 if( !is.null( dfSummary ) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
481 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
482 ### Start biplot
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
483 # Get metadata of interest and reduce to default size
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
484 lsSigMetadata = unique(dfSummary[[1]])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
485 if( is.null( lsArgs$options$iNumberMetadata ) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
486 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
487 lsSigMetadata = lsSigMetadata[ 1:length( lsSigMetadata ) ]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
488 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
489 lsSigMetadata = lsSigMetadata[ 1:min( length( lsSigMetadata ), max( lsArgs$options$iNumberMetadata, 1 ) ) ]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
490 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
491
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
492 # Convert to indices (ordered numerically here)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
493 liSigMetadata = which( colnames( lsRet$frmeData ) %in% lsSigMetadata )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
494
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
495 # Get bugs of interest and reduce to default size
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
496 lsSigBugs = unique(dfSummary[[2]])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
497
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
498 # Reduce the bugs to the right size
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
499 if(lsArgs$options$iNumberBugs < 1)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
500 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
501 lsSigBugs = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
502 } else if( is.null( lsArgs$options$iNumberBugs ) ) {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
503 lsSigBugs = lsSigBugs[ 1 : length( lsSigBugs ) ]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
504 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
505 lsSigBugs = lsSigBugs[ 1 : lsArgs$options$iNumberBugs ]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
506 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
507
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
508 # Set color by and shape by features if not given
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
509 # Selects the continuous (for color) and factor (for shape) data with the most significant association
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
510 if(is.null(lsArgs$options$strBiplotColor)||is.null(lsArgs$options$strBiplotShapeBy))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
511 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
512 for(sMetadata in lsSigMetadata)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
513 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
514 if(is.factor(lsRet$frmeRaw[[sMetadata]]))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
515 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
516 if(is.null(lsArgs$options$strBiplotShapeBy))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
517 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
518 lsArgs$options$strBiplotShapeBy = sMetadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
519 if(!is.null(lsArgs$options$strBiplotColor))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
520 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
521 break
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
522 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
523 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
524 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
525 if(is.numeric(lsRet$frmeRaw[[sMetadata]]))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
526 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
527 if(is.null(lsArgs$options$strBiplotColor))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
528 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
529 lsArgs$options$strBiplotColor = sMetadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
530 if(!is.null(lsArgs$options$strBiplotShapeBy))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
531 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
532 break
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
533 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
534 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
535 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
536 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
537 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
538
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
539 #If a user defines a feature, make sure it is in the bugs/data indices
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
540 if(!is.null(lsFeaturesToPlot) || !is.null(lsArgs$options$strBiplotColor) || !is.null(lsArgs$options$strBiplotShapeBy))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
541 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
542 lsCombinedFeaturesToPlot = unique(c(lsFeaturesToPlot,lsArgs$options$strBiplotColor,lsArgs$options$strBiplotShapeBy))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
543 lsCombinedFeaturesToPlot = lsCombinedFeaturesToPlot[!is.null(lsCombinedFeaturesToPlot)]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
544
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
545 # If bugs to plot were given then do not use the significant bugs from the MaAsLin output which is default
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
546 if(!is.null(lsFeaturesToPlot))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
547 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
548 lsSigBugs = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
549 liSigMetadata = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
550 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
551 liSigMetadata = unique(c(liSigMetadata,which(colnames(lsRet$frmeData) %in% setdiff(lsCombinedFeaturesToPlot, lsOriginalFeatureNames))))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
552 lsSigBugs = unique(c(lsSigBugs, intersect(lsCombinedFeaturesToPlot, lsOriginalFeatureNames)))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
553 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
554
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
555 # Convert bug names and metadata names to comma delimited strings
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
556 vsBugs = paste(lsSigBugs,sep=",",collapse=",")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
557 vsMetadata = paste(colnames(lsRet$frmeData)[liSigMetadata],sep=",",collapse=",")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
558 vsMetadataByLevel = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
559
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
560 # Possibly remove the NA levels depending on the preferences
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
561 vsRemoveNA = c(NA, "NA", "na", "Na", "nA")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
562 if(!lsArgs$options$fPlotNA){ vsRemoveNA = c() }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
563 for(aiMetadataIndex in liSigMetadata)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
564 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
565 lxCurMetadata = lsRet$frmeData[[aiMetadataIndex]]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
566 sCurName = names(lsRet$frmeData[aiMetadataIndex])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
567 if(is.factor(lxCurMetadata))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
568 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
569 vsMetadataByLevel = c(vsMetadataByLevel,paste(sCurName, setdiff( levels(lxCurMetadata), vsRemoveNA),sep="_"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
570 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
571 vsMetadataByLevel = c(vsMetadataByLevel,sCurName)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
572 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
573 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
574
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
575 # If NAs should not be plotted, make them the background color
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
576 # Unless explicitly asked to be plotted
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
577 sPlotNAColor = "white"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
578 if(lsArgs$options$fInvert){sPlotNAColor = "black"}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
579 if(lsArgs$options$fPlotNA){sPlotNAColor = "grey"}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
580 sLastMetadata = lsOriginalMetadataNames[max(which(lsOriginalMetadataNames %in% names(lsRet$frmeData)))]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
581
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
582 # Plot biplot
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
583 logdebug("PlotBiplot:Started")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
584 funcDoBiplot(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
585 sBugs = vsBugs,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
586 sMetadata = vsMetadataByLevel,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
587 sColorBy = lsArgs$options$strBiplotColor,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
588 sPlotNAColor = sPlotNAColor,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
589 sShapeBy = lsArgs$options$strBiplotShapeBy,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
590 sShapes = lsArgs$options$sShapes,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
591 sDefaultMarker = "16",
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
592 sRotateByMetadata = lsArgs$options$sRotateByMetadata,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
593 dResizeArrow = lsArgs$options$dBiplotMetadataScale,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
594 sInputFileName = lsRet$frmeRaw,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
595 sLastMetadata = sLastMetadata,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
596 sOutputFileName = file.path(outputDirectory,paste(strBase,"-biplot.pdf",sep="")))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
597 logdebug("PlotBiplot:Stopped")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
598 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
599 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
600
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
601 # This is the equivalent of __name__ == "__main__" in Python.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
602 # That is, if it's true we're being called as a command line script;
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
603 # if it's false, we're being sourced or otherwise included, such as for
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
604 # library or inlinedocs.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
605 if( identical( environment( ), globalenv( ) ) &&
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
606 !length( grep( "^source\\(", sys.calls( ) ) ) ) {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
607 main( pArgs ) }