annotate src/lib/scriptBiplotTSV.R @ 0:e0b5980139d9

maaslin
author george-weingart
date Tue, 13 May 2014 22:00:40 -0400
parents
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 library(vegan)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
4 library(optparse)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
5
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
6 funcGetCentroidForMetadatum <- function(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
7 ### Given a binary metadatum, calculate the centroid of the samples associated with the metadata value of 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
8 # 1. Get all samples that have the metadata value of 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
9 # 2. Get the x and y coordinates of the selected samples
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
10 # 3. Get the median value for the x and ys
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
11 # 4. Return those coordinates as the centroid's X and Y value
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
12 vfMetadata,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
13 ### Logical or integer (0,1) vector, TRUE or 1 values indicate correspoinding samples in the
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
14 ### mSamplePoints which will be used to define the centroid
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
15 mSamplePoints
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
16 ### Coordinates (columns;n=2) of samples (rows) corresponding to the vfMetadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
17 ){
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
18 # Check the lengths which should be equal
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
19 if(length(vfMetadata)!=nrow(mSamplePoints))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
20 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
21 print(paste("funcGetCentroidForMetadata::Error: Should have received metadata and samples of the same length, received metadata length ",length(vfMetadata)," and sample ",nrow(mSamplePoints)," length.",sep=""))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
22 return( FALSE )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
23 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
24
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
25 # Get all the samples that have the metadata value of 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
26 viMetadataSamples = which(as.integer(vfMetadata)==1)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
27
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
28 # Get the x and y coordinates for the selected samples
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
29 mSelectedPoints = mSamplePoints[viMetadataSamples,]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
30
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
31 # Get the median value for the x and the ys
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
32 if(!is.null(nrow(mSelectedPoints)))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
33 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
34 return( list(x=median(mSelectedPoints[,1],na.rm = TRUE),y=median(mSelectedPoints[,2],na.rm = TRUE)) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
35 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
36 return( list(x=mSelectedPoints[1],y=mSelectedPoints[2]) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
37 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
38 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
39
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
40 funcGetMaximumForMetadatum <- function(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
41 ### Given a continuous metadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
42 ### 1. Use the x and ys from mSamplePoints for coordinates and the metadata value as a height (z)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
43 ### 2. Use lowess to smooth the landscape
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
44 ### 3. Take the maximum of the landscape
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
45 ### 4. Return the coordiantes for the maximum as the centroid
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
46 vdMetadata,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
47 ### Continuous (numeric or integer) metadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
48 mSamplePoints
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
49 ### Coordinates (columns;n=2) of samples (rows) corresponding to the vfMetadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
50 ){
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
51 # Work with data frame
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
52 if(class(mSamplePoints)=="matrix")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
53 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
54 mSamplePoints = data.frame(mSamplePoints)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
55 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
56 # Check the lengths of the dataframes and the metadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
57 if(length(vdMetadata)!=nrow(mSamplePoints))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
58 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
59 print(paste("funcGetMaximumForMetadatum::Error: Should have received metadata and samples of the same length, received metadata length ",length(vdMetadata)," and sample ",nrow(mSamplePoints)," length.",sep=""))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
60 return( FALSE )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
61 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
62
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
63 # Add the metadata value to the points
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
64 mSamplePoints[3] = vdMetadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
65 names(mSamplePoints) = c("x","y","z")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
66
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
67 # Create lowess to smooth the surface
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
68 # And calculate the fitted heights
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
69 # x = sample coordinate 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
70 # y = sample coordinate 2
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
71 # z = metadata value
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
72 loessSamples = loess(z~x*y, data=mSamplePoints, degree = 1, normalize = FALSE, na.action=na.omit)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
73
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
74 # Naively get the max
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
75 vdCoordinates = loessSamples$x[which(loessSamples$y==max(loessSamples$y)),]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
76 return(list(lsmod = loessSamples, x=vdCoordinates[1],y=vdCoordinates[2]))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
77 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
78
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
79 funcMakeShapes <- function(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
80 ### Takes care of defining shapes for the plot
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
81 dfInput,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
82 ### Data frame of metadata measurements
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
83 sShapeBy,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
84 ### The metadata to shape by
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
85 sShapes,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
86 ### List of custom metadata (per level if factor).
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
87 ### Should correspond to the number of levels in shapeBy; the format is level:shape,level:shape for example HighLuminosity:14,LowLuminosity:2,HighPH:10,LowPH:18
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
88 cDefaultShape
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
89 ### Shape to default to if custom shapes are not used
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
90 ){
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
91 lShapes = list()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
92 vsShapeValues = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
93 vsShapeShapes = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
94 vsShapes = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
95 sMetadataId = sShapeBy
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
96
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
97 # Set default shape, color, and color ranges
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
98 if(!is.null(cDefaultShape))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
99 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
100 # Default shape should be an int for the int pch options
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
101 if(!is.na(as.integer(cDefaultShape)))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
102 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
103 cDefaultShape = as.integer(cDefaultShape)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
104 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
105 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
106 cDefaultShape = 16
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
107 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
108
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
109 # Make shapes
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
110 vsShapes = rep(cDefaultShape,nrow(dfInput))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
111
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
112 if(!is.null(sMetadataId))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
113 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
114 if(is.null(sShapes))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
115 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
116 vsShapeValues = unique(dfInput[[sMetadataId]])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
117 vsShapeShapes = 1:length(vsShapeValues)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
118 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
119 # Put the markers in the order of the values)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
120 vsShapeBy = unlist(strsplit(sShapes,","))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
121 for(sShapeBy in vsShapeBy)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
122 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
123 vsShapeByPieces = unlist(strsplit(sShapeBy,":"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
124 lShapes[vsShapeByPieces[1]] = as.integer(vsShapeByPieces[2])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
125 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
126 vsShapeValues = names(lShapes)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
127 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
128
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
129 # Shapes in the correct order
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
130 if(!is.null(sShapes))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
131 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
132 vsShapeShapes = unlist(lapply(vsShapeValues,function(x) lShapes[[x]]))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
133 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
134 vsShapeValues = paste(vsShapeValues)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
135
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
136 # Make the list of shapes
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
137 for(iShape in 1:length(vsShapeValues))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
138 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
139 vsShapes[which(paste(dfInput[[sMetadataId]])==vsShapeValues[iShape])]=vsShapeShapes[iShape]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
140 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
141
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
142 # If they are all numeric characters, make numeric
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
143 viIntNas = which(is.na(as.integer(vsShapes)))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
144 viNas = which(is.na(vsShapes))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
145 if(length(setdiff(viIntNas,viNas))==0)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
146 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
147 vsShapes = as.integer(vsShapes)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
148 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
149 print("funcMakeShapes::Error: Please supply numbers 1-25 for shape in the -y,--shapeBy option")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
150 vsShapeValues = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
151 vsShapeShapes = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
152 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
153 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
154 return(list(PlotShapes=vsShapes,Values=vsShapeValues,Shapes=vsShapeShapes,ID=sMetadataId,DefaultShape=cDefaultShape))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
155 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
156
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
157 ### Global defaults
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
158 c_sDefaultColorBy = NULL
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
159 c_sDefaultColorRange = "orange,cyan"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
160 c_sDefaultTextColor = "black"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
161 c_sDefaultArrowColor = "cyan"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
162 c_sDefaultArrowTextColor = "Blue"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
163 c_sDefaultNAColor = "grey"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
164 c_sDefaultShapeBy = NULL
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
165 c_sDefaultShapes = NULL
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
166 c_sDefaultMarker = "16"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
167 c_sDefaultRotateByMetadata = NULL
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
168 c_sDefaultResizeArrow = 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
169 c_sDefaultTitle = "Custom Biplot of Bugs and Samples - Metadata Plotted with Centroids"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
170 c_sDefaultOutputFile = NULL
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
171
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
172 ### Create command line argument parser
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
173 pArgs <- OptionParser( usage = "%prog last_metadata input.tsv" )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
174
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
175 # Selecting features to plot
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
176 pArgs <- add_option( pArgs, c("-b", "--bugs"), type="character", action="store", default=NULL, dest="sBugs", metavar="BugsToPlot", help="Comma delimited list of data to plot as text. Bug|1,Bug|2")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
177 pArgs <- add_option( pArgs, c("-m", "--metadata"), type="character", action="store", default=NULL, dest="sMetadata", metavar="MetadataToPlot", help="Comma delimited list of metadata to plot as arrows. metadata1,metadata2,metadata3")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
178
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
179 # Colors
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
180 pArgs <- add_option( pArgs, c("-c", "--colorBy"), type="character", action="store", default=c_sDefaultColorBy, dest="sColorBy", metavar="MetadataToColorBy", help="The id of the metadatum to use to make the marker colors. Expected to be a continuous metadata.")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
181 pArgs <- add_option( pArgs, c("-r", "--colorRange"), type="character", action="store", default=c_sDefaultColorRange, dest="sColorRange", metavar="ColorRange", help=paste("Colors used to color the samples; a gradient will be formed between the color.Default=", c_sDefaultColorRange))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
182 pArgs <- add_option( pArgs, c("-t", "--textColor"), type="character", action="store", default=c_sDefaultTextColor, dest="sTextColor", metavar="TextColor", help=paste("The color bug features will be plotted with as text. Default =", c_sDefaultTextColor))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
183 pArgs <- add_option( pArgs, c("-a", "--arrowColor"), type="character", action="store", default=c_sDefaultArrowColor, dest="sArrowColor", metavar="ArrowColor", help=paste("The color metadata features will be plotted with as an arrow and text. Default", c_sDefaultArrowColor))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
184 pArgs <- add_option( pArgs, c("-w", "--arrowTextColor"), type="character", action="store", default=c_sDefaultArrowTextColor, dest="sArrowTextColor", metavar="ArrowTextColor", help=paste("The color for the metadata text ploted by the head of the metadata arrow. Default", c_sDefaultArrowTextColor))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
185 pArgs <- add_option(pArgs, c("-n","--plotNAColor"), type="character", action="store", default=c_sDefaultNAColor, dest="sPlotNAColor", metavar="PlotNAColor", help=paste("Plot NA values as this color. Example -n", c_sDefaultNAColor))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
186
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
187 # Shapes
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
188 pArgs <- add_option( pArgs, c("-y", "--shapeby"), type="character", action="store", default=c_sDefaultShapeBy, dest="sShapeBy", metavar="MetadataToShapeBy", help="The metadata to use to make marker shapes. Expected to be a discrete metadatum. An example would be -y Environment")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
189 pArgs <- add_option( pArgs, c("-s", "--shapes"), type="character", action="store", default=c_sDefaultShapes, dest="sShapes", metavar="ShapesForPlotting", help="This is to be used to specify the shapes to use for plotting. Can use numbers recognized by R as shapes (see pch). Should correspond to the number of levels in shapeBy; the format is level:shape,level:shape for example HighLuminosity:14,LowLuminosity:2,HighPH:10,LowPH:18 . Need to specify -y/--shapeBy for this option to work.")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
190 pArgs <- add_option( pArgs, c("-d", "--defaultMarker"), type="character", action="store", default=c_sDefaultMarker, dest="sDefaultMarker", metavar="DefaultColorMarker", help="Default shape for markers which are not otherwise indicated in --shapes, can be used for unspecified values or NA. Must not be a shape in --shapes.")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
191
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
192 # Plot manipulations
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
193 pArgs <- add_option( pArgs, c("-e","--rotateByMetadata"), type="character", action="store", default=c_sDefaultRotateByMetadata, dest="sRotateByMetadata", metavar="RotateByMetadata", help="Rotate the ordination by a metadata. Give both the metadata and value to weight it by. The larger the weight, the more the ordination is influenced by the metadata. If the metadata is continuous, use the metadata id; if the metadata is discrete, the ordination will be by one of the levels so use the metadata ID and level seperated by a '_'. Discrete example -e Environment_HighLumninosity,100 ; Continuous example -e Environment,100 .")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
194 pArgs <- add_option( pArgs, c("-z","--resizeArrow"), type="numeric", action="store", default=c_sDefaultResizeArrow, dest="dResizeArrow", metavar="ArrowScaleFactor", help="A constant to multiple the length of the arrow to expand or shorten all arrows together. This will not change the angle of the arrow nor the relative length of arrows to each other.")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
195
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
196 # Misc
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
197 pArgs <- add_option( pArgs, c("-i", "--title"), type="character", action="store", default=c_sDefaultTitle, dest="sTitle", metavar="Title", help="This is the title text to add to the plot.")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
198 pArgs <- add_option( pArgs, c("-o", "--outputfile"), type="character", action="store", default=c_sDefaultOutputFile, dest="sOutputFileName", metavar="OutputFile", help="This is the name for the output pdf file. If an output file is not given, an output file name is made based on the input file name.")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
199
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
200 funcDoBiplot <- function(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
201 ### Perform biplot. Samples are markers, bugs are text, and metadata are text with arrows. Markers and bugs are dtermined usiing NMDS and Bray-Curtis dissimilarity. Metadata are placed on the ordination in one of two ways: 1. Factor data - for each level take the ordination points for the samples that have that level and plot the metadata text at the average orindation point. 2. For continuous data - make a landscape (x and y form ordination of the points) and z (height) as the metadata value. Use a lowess line to get the fitted values for z and take the max of the landscape. Plot the metadata text at that smoothed max.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
202 sBugs,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
203 ### Comma delimited list of data to plot as text. Bug|1,Bug|2
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
204 sMetadata,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
205 ### Comma delimited list of metadata to plot as arrows. metadata1,metadata2,metadata3.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
206 sColorBy = c_sDefaultColorBy,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
207 ### The id of the metadatum to use to make the marker colors. Expected to be a continuous metadata.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
208 sColorRange = c_sDefaultColorRange,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
209 ### Colors used to color the samples; a gradient will be formed between the color. Example orange,cyan
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
210 sTextColor = c_sDefaultTextColor,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
211 ### The color bug features will be plotted with as text. Example black
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
212 sArrowColor = c_sDefaultArrowColor,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
213 ### The color metadata features will be plotted with as an arrow and text. Example cyan
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
214 sArrowTextColor = c_sDefaultArrowTextColor,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
215 ### The color for the metadata text ploted by the head of the metadata arrow. Example Blue
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
216 sPlotNAColor = c_sDefaultNAColor,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
217 ### Plot NA values as this color. Example grey
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
218 sShapeBy = c_sDefaultShapeBy,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
219 ### The metadata to use to make marker shapes. Expected to be a discrete metadatum.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
220 sShapes = c_sDefaultShapes,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
221 ### This is to be used to specify the shapes to use for plotting. Can use numbers recognized by R as shapes (see pch). Should correspond to the number of levels in shapeBy; the format is level:shape,level:shape for example HighLuminosity:14,LowLuminosity:2,HighPH:10,LowPH:18 . Works with sShapesBy.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
222 sDefaultMarker = c_sDefaultMarker,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
223 ### The default marker shape to use if shapes are not otherwise indicated.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
224 sRotateByMetadata = c_sDefaultRotateByMetadata,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
225 ### Metadata and value to rotate by. example Environment_HighLumninosity,100
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
226 dResizeArrow = c_sDefaultResizeArrow,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
227 ### Scale factor to resize tthe metadata arrows
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
228 sTitle = c_sDefaultTitle,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
229 ### The title for the figure.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
230 sInputFileName,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
231 ### File to input (tsv file: tab separated, row = sample file)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
232 sLastMetadata,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
233 ### Last metadata that seperates data and metadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
234 sOutputFileName = c_sDefaultOutputFile
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
235 ### The file name to save the figure.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
236 ){
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
237 print("IN Biplot")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
238 # Define the colors
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
239 vsColorRange = c("blue","orange")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
240 cDefaultColor = "black"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
241 if(!is.null(sColorRange))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
242 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
243 vsColorRange = unlist(strsplit(sColorRange,","))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
244 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
245
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
246 # List of bugs to plot
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
247 # If there is a list it needs to be more than one.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
248 vsBugsToPlot = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
249 if(!is.null(sBugs))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
250 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
251 vsBugsToPlot = unlist(strsplit(sBugs,","))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
252 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
253
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
254 print("vsBugsToPlot")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
255 print(vsBugsToPlot)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
256 # Metadata to plot
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
257 vsMetadata = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
258 if(!is.null(sMetadata))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
259 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
260 vsMetadata = unlist(strsplit(sMetadata,","))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
261 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
262
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
263 print("vsMetadata")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
264 print(vsMetadata)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
265 ### Load table
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
266 if(class(sInputFileName)=="character")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
267 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
268 dfInput = read.table(sInputFileName, sep = "\t", header=TRUE)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
269 names(dfInput) = unlist(lapply(names(dfInput),function(x) gsub(".","|",x,fixed=TRUE)))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
270 row.names(dfInput) = dfInput[,1]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
271 dfInput = dfInput[-1]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
272 } else {dfInput = sInputFileName}
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
273
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
274 ### Get positions of all metadata or all data
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
275 iLastMetadata = which(names(dfInput)==sLastMetadata)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
276 viMetadata = 1:iLastMetadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
277 viData = (iLastMetadata+1):ncol(dfInput)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
278
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
279 ### Dummy the metadata if discontinuous
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
280 ### Leave the continous metadata alone but include
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
281 listMetadata = list()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
282 vsRowNames = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
283 viContinuousMetadata = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
284 for(i in viMetadata)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
285 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
286 print( names( dfInput )[i] )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
287 vCurMetadata = unlist(dfInput[i])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
288 if( ( is.numeric(vCurMetadata)||is.integer(vCurMetadata) ) && ( length( unique( vCurMetadata ) ) >= c_iNonFactorLevelThreshold ) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
289 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
290 vCurMetadata[which(is.na(vCurMetadata))] = mean(vCurMetadata,na.rm=TRUE)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
291 listMetadata[[length(listMetadata)+1]] = vCurMetadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
292 vsRowNames = c(vsRowNames,names(dfInput)[i])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
293 viContinuousMetadata = c(viContinuousMetadata,length(listMetadata))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
294 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
295 vCurMetadata = as.factor(vCurMetadata)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
296 vsLevels = levels(vCurMetadata)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
297 for(sLevel in vsLevels)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
298 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
299 vNewMetadata = rep(0,length(vCurMetadata))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
300 vNewMetadata[which(vCurMetadata == sLevel)] = 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
301 listMetadata[[length(listMetadata)+1]] = vNewMetadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
302 vsRowNames = c(vsRowNames,paste(names(dfInput)[i],sLevel,sep="_"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
303 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
304 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
305 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
306
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
307 # Convert to data frame
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
308 dfDummyMetadata = as.data.frame(sapply(listMetadata,rbind))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
309 names(dfDummyMetadata) = vsRowNames
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
310 iNumberMetadata = ncol(dfDummyMetadata)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
311
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
312 # Data to use in ordination in NMDS
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
313 # All cleaned bug data
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
314 dfData = dfInput[viData]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
315
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
316 # If rotating the ordination by a metadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
317 # 1. Add in the metadata as a bug
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
318 # 2. Multiply the bug by the weight
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
319 # 3. Push this through the NMDS
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
320 if(!is.null(sRotateByMetadata))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
321 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
322 vsRotateMetadata = unlist(strsplit(sRotateByMetadata,","))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
323 sMetadata = vsRotateMetadata[1]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
324 dWeight = as.numeric(vsRotateMetadata[2])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
325 sOrdinationMetadata = dfDummyMetadata[sMetadata]*dWeight
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
326 dfData[sMetadata] = sOrdinationMetadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
327 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
328
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
329 # Run NMDS on bug data (Default B-C)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
330 # Will have species and points because working off of raw data
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
331 mNMDSData = metaMDS(dfData,k=2)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
332
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
333 ## Make shapes
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
334 # Defines the shapes and the metadata they are based on
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
335 # Metadata to use as shapes
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
336 lShapeInfo = funcMakeShapes(dfInput=dfInput, sShapeBy=sShapeBy, sShapes=sShapes, cDefaultShape=sDefaultMarker)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
337
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
338 sMetadataShape = lShapeInfo[["ID"]]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
339 vsShapeValues = lShapeInfo[["Values"]]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
340 vsShapeShapes = lShapeInfo[["Shapes"]]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
341 vsShapes = lShapeInfo[["PlotShapes"]]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
342 cDefaultShape = lShapeInfo[["DefaultShape"]]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
343
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
344 # Colors
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
345 vsColors = rep(cDefaultColor,nrow(dfInput))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
346 vsColorValues = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
347 vsColorRBG = c()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
348 if(!is.null(sColorBy))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
349 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
350 vsColorValues = paste(sort(unique(unlist(dfInput[[sColorBy]])),na.last=TRUE))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
351 iLengthColorValues = length(vsColorValues)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
352
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
353 vsColorRBG = lapply(1:iLengthColorValues/iLengthColorValues,colorRamp(vsColorRange))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
354 vsColorRBG = unlist(lapply(vsColorRBG, function(x) rgb(x[1]/255,x[2]/255,x[3]/255)))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
355
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
356 for(iColor in 1:length(vsColorRBG))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
357 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
358 vsColors[which(paste(dfInput[[sColorBy]])==vsColorValues[iColor])]=vsColorRBG[iColor]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
359 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
360
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
361 #If NAs are seperately given color, then color here
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
362 if(!is.null(sPlotNAColor))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
363 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
364 vsColors[which(is.na(dfInput[[sColorBy]]))] = sPlotNAColor
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
365 vsColorRBG[which(vsColorValues=="NA")] = sPlotNAColor
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
366 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
367 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
368
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
369 print("names(dfDummyMetadata)")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
370 print(names(dfDummyMetadata))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
371
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
372 # Reduce the bugs down to the ones in the list to be plotted
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
373 viBugsToPlot = which(row.names(mNMDSData$species) %in% vsBugsToPlot)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
374 viMetadataDummy = which(names(dfDummyMetadata) %in% vsMetadata)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
375
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
376 print("viBugsToPlot")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
377 print(viBugsToPlot)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
378 print("viMetadataDummy")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
379 print(names(dfDummyMetadata)[viMetadataDummy])
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
380
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
381 # Build the matrix of metadata coordinates
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
382 mMetadataCoordinates = matrix(rep(NA, iNumberMetadata*2),nrow=iNumberMetadata)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
383 for( i in 1:iNumberMetadata )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
384 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
385 lxReturn = NA
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
386 if( i %in% viContinuousMetadata )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
387 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
388 lxReturn = funcGetMaximumForMetadatum(dfDummyMetadata[[i]],mNMDSData$points)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
389 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
390 lxReturn = funcGetCentroidForMetadatum(dfDummyMetadata[[i]],mNMDSData$points)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
391 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
392 mMetadataCoordinates[i,] = c(lxReturn$x,lxReturn$y)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
393 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
394 row.names(mMetadataCoordinates) = vsRowNames
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
395
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
396 # Plot the biplot with the centroid constructed metadata coordinates
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
397 if(length(viMetadataDummy)==0)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
398 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
399 viMetadataDummy = 1:nrow(mMetadataCoordinates)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
400 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
401
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
402 # Plot samples
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
403 # Make output name
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
404 if(is.null(sOutputFileName))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
405 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
406 viPeriods = which(sInputFileName==".")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
407 if(length(viPeriods)>0)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
408 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
409 sOutputFileName = paste(OutputFileName[1:viPeriods[length(viPeriods)]],"pdf",sep=".")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
410 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
411 sOutputFileName = paste(sInputFileName,"pdf",sep=".")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
412 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
413 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
414
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
415 pdf(sOutputFileName, useDingbats=FALSE)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
416 plot(mNMDSData$points, xlab=paste("NMDS1","Stress=",mNMDSData$stress), ylab="NMDS2", pch=vsShapes, col=vsColors)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
417 title(sTitle,line=3)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
418
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
419 # Plot Bugs
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
420 mPlotBugs = mNMDSData$species[viBugsToPlot,]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
421 if(length(viBugsToPlot)==1)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
422 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
423 text(x=mPlotBugs[1],y=mPlotBugs[2],labels=row.names(mNMDSData$species)[viBugsToPlot],col=sTextColor)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
424 } else if(length(viBugsToPlot)>1){
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
425 text(x=mPlotBugs[,1],y=mPlotBugs[,2],labels=row.names(mNMDSData$species)[viBugsToPlot],col=sTextColor)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
426 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
427
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
428 # Add alternative axes
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
429 axis(3, col=sArrowColor)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
430 axis(4, col=sArrowColor)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
431 box(col = "black")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
432
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
433 # Plot Metadata
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
434 if(length(viMetadataDummy)>0)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
435 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
436 for(i in viMetadataDummy)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
437 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
438 curCoordinates = mMetadataCoordinates[i,]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
439 curCoordinates = curCoordinates * dResizeArrow
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
440 # Plot Arrow
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
441 arrows(0,0, curCoordinates[1] * 0.8, curCoordinates[2] * 0.8, col=sArrowColor, length=0.1 )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
442 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
443 # Plot text
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
444 if(length(viMetadataDummy)==1)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
445 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
446 text(x=mMetadataCoordinates[viMetadataDummy,][1]*dResizeArrow*0.8, y=mMetadataCoordinates[viMetadataDummy,][2]*dResizeArrow*0.8, labels=row.names(mMetadataCoordinates)[viMetadataDummy],col=sArrowTextColor)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
447 } else {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
448 text(x=mMetadataCoordinates[viMetadataDummy,1]*dResizeArrow*0.8, y=mMetadataCoordinates[viMetadataDummy,2]*dResizeArrow*0.8, labels=row.names(mMetadataCoordinates)[viMetadataDummy],col=sArrowTextColor)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
449 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
450 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
451
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
452 # Create Legend
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
453 # The text default is the colorMetadata_level (one per level) plus the ShapeMetadata_level (one per level)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
454 # The color default is already determined colors plus grey for shapes.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
455 sLegendText = c(paste(vsColorValues,sColorBy,sep="_"),paste(sMetadataShape,vsShapeValues,sep="_"))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
456 sLegendColors = c(vsColorRBG,rep(cDefaultColor,length(vsShapeValues)))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
457
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
458 # If the color values are numeric
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
459 # Too many values may be given in the legend (given they may be a continuous range of values)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
460 # To reduce this they are summarized instead, given the colors and values for the extreme ends.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
461 if( !sum( is.na( as.numeric( vsColorValues[ which( !is.na( vsColorValues ) ) ] ) ) ) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
462 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
463 vdNumericColors = as.numeric( vsColorValues )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
464 vdNumericColors = vdNumericColors[ which( !is.na( vdNumericColors ) ) ]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
465 vdSortedNumericColors = sort( vdNumericColors )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
466 sLegendText = c( paste( sColorBy, vdSortedNumericColors[ 1 ], sep="_" ),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
467 paste( sColorBy, vdSortedNumericColors[ length(vdSortedNumericColors) ], sep="_" ),
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
468 paste( sMetadataShape, vsShapeValues, sep="_" ) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
469 sLegendColors = c(vsColorRBG[ which( vdNumericColors == vdSortedNumericColors[ 1 ] )[ 1 ] ],
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
470 vsColorRBG[ which( vdNumericColors == vdSortedNumericColors[ length( vdSortedNumericColors ) ] )[ 1 ] ],
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
471 rep(cDefaultColor,length(vsShapeValues)))
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
472 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
473 sLegendShapes = c( rep( cDefaultShape, length( sLegendText ) - length( vsShapeShapes ) ), vsShapeShapes )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
474
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
475 # If any legend text was constructed then make the legend.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
476 if( length( sLegendText ) >0 )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
477 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
478 legend( "topright", legend = sLegendText, pch = sLegendShapes, col = sLegendColors )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
479 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
480
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
481 # Original biplot call if you want to check the custom plotting of the script
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
482 # There will be one difference where the biplot call scales an axis, this one does not. In relation to the axes, the points, text and arrows should still match.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
483 # Axes to the top and right are for the arrow, others are for markers and bug names.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
484 #biplot(mNMDSData$points,mMetadataCoordinates[viMetadataDummy,],xlabs=vsShapes,xlab=paste("MDS1","Stress=",mNMDSData$stress),main="Biplot function Bugs and Sampes - Metadata Plotted with Centroids")
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
485 dev.off()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
486 }
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
487
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
488 # This is the equivalent of __name__ == "__main__" in Python.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
489 # That is, if it's true we're being called as a command line script;
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
490 # if it's false, we're being sourced or otherwise included, such as for
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
491 # library or inlinedocs.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
492 if( identical( environment( ), globalenv( ) ) &&
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
493 !length( grep( "^source\\(", sys.calls( ) ) ) )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
494 {
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
495 lsArgs <- parse_args( pArgs, positional_arguments=TRUE )
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
496
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
497 funcDoBiplot(
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
498 sBugs = lsArgs$options$sBugs,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
499 sMetadata = lsArgs$options$sMetadata,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
500 sColorBy = lsArgs$options$sColorBy,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
501 sColorRange = lsArgs$options$sColorRange,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
502 sTextColor = lsArgs$options$sTextColor,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
503 sArrowColor = lsArgs$options$sArrowColor,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
504 sArrowTextColor = lsArgs$options$sArrowTextColor,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
505 sPlotNAColor = lsArgs$options$sPlotNAColor,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
506 sShapeBy = lsArgs$options$sShapeBy,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
507 sShapes = lsArgs$options$sShapes,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
508 sDefaultMarker = lsArgs$options$sDefaultMarker,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
509 sRotateByMetadata = lsArgs$options$sRotateByMetadata,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
510 dResizeArrow = lsArgs$options$dResizeArrow,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
511 sTitle = lsArgs$options$sTitle,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
512 sInputFileName = lsArgs$args[2],
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
513 sLastMetadata = lsArgs$args[1],
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
514 sOutputFileName = lsArgs$options$sOutputFileName)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
515 }