Skip to content

Commit 2e40b92

Browse files
mikesiodamsioda
authored andcommitted
Bug Fixes (#70)
* R script format updates + bug fix * add verbose to rsync command * cleanup * fix R script pageNum bug
1 parent 1d4b4f1 commit 2e40b92

10 files changed

Lines changed: 199 additions & 239 deletions

File tree

resources/R/BioLockJ_Lib.R

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,25 @@ addNamedVectorElement <- function( v, name, value ) {
88
}
99

1010
# Add a page number in the lower right corner of the page
11-
addPageNumber <- function (pageNum, line=0){
12-
mtext(pageNum, side=1, outer=TRUE, line=line, adj = 1)
13-
# optional return value
14-
if (is.numeric(pageNum) ) pageNum + 1
11+
addPageNumber <- function( pageNum ){
12+
mtext (pageNum, side=1, outer=TRUE, adj=1 )
1513
}
1614

1715
# Add text to the bottom of the page, centered
1816
addPageFooter <- function(text, line=0){
19-
mtext(text, side=1, outer=TRUE, line=line, adj = .5)
17+
mtext(text, side=1, outer=TRUE, line=line, adj=0.5)
2018
}
2119

2220
# Add a page title
23-
addPageTitle <- function (main, level=NULL, line=2){
24-
mtext(main, side=3, outer = TRUE, font=par("font.main"), cex=par("cex.main"), line=line)
25-
if (!is.null(level)){
26-
titlePart2 = displayLevel( level )
27-
title(main=titlePart2, outer = TRUE, line=(line-1))
28-
}
21+
addPageTitle <- function( main ) {
22+
mtext(main, side=3, outer=TRUE, font=par("font.main"), cex=par("cex.main"), line=1)
2923
}
3024

3125
# Return P value formated with sprintf as defined in MASTER Config r.pValFormat, otherwise use %1.2g default
3226
displayCalc <- function( pval ) {
3327
return( paste( sprintf(getProperty("r.pValFormat", "%1.2g"), pval) ) )
3428
}
3529

36-
displayLevel <- function(level){
37-
return( str_to_title( paste(level,"Level") ) )
38-
}
39-
4030
# Return TRUE if BioLock property r.debug=Y, otherwise return FALSE
4131
doDebug <- function() {
4232
return( getProperty( "r.debug", FALSE ) )
@@ -250,6 +240,11 @@ getReportFields <- function() {
250240
return( c( getBinaryFields(), getNominalFields(), getNumericFields() ) )
251241
}
252242

243+
# Display R^2 label and value
244+
displayR2 <- function( val ) {
245+
return( bquote( paste( R^2, ": ", .( displayCalc( val ) ) ) ) )
246+
}
247+
253248
# Return the most recent stats file at the given level based on the suffix returned by statsFileSuffix()
254249
getStatsTable <- function( level, parametric=NULL, adjusted=TRUE ) {
255250
statsFile = pipelineFile( paste0( level, "_", statsFileSuffix( parametric, adjusted ), "$" ) )

resources/R/BioLockJ_MAIN.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ getModuleScript <- function() {
5656
initial.options = commandArgs(trailingOnly=FALSE)
5757
script.name <- sub("--file=", "", initial.options[grep("--file=", initial.options)])
5858
if( length( script.name ) == 0 ) {
59-
6059
if( init( getInteractiveMain() ) ) {
6160
script.name = getInteractiveMain()
6261
}

resources/R/R_PlotMds.R

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Module script for: biolockj.module.report.r.R_PlotMds
22

33
# Import vegan library for distance plot support
4-
# Main function generates 3 MDS plots for each attribute at each level in taxaLevels()
4+
# Main function generates numAxis MDS plots for each field at each level in taxaLevels()
55
main <- function() {
66
importLibs( c( "vegan" ) )
7-
numAxis = getProperty("r_PlotMds.numAxis")
87
mdsFields = getProperty( "r_PlotMds.reportFields", c( getBinaryFields(), getNominalFields() ) )
98

109
for( level in taxaLevels() ) {
@@ -13,8 +12,10 @@ main <- function() {
1312
metaTable = getMetaData( level )
1413
if( is.null(countTable) || is.null(metaTable) ) { next }
1514
if( doDebug() ) sink( getLogFile( level ) )
15+
logInfo( "mdsFields", mdsFields )
1616

1717
myMDS = capscale( countTable~1, distance=getProperty("r_PlotMds.distance") )
18+
numAxis = min( c( getProperty("r_PlotMds.numAxis"), ncol(myMDS$CA$u) ) )
1819
metaColColors = getColorsByCategory( metaTable )
1920

2021
pcoaFileName = paste0( getPath( file.path(getModuleDir(), "temp"), paste0(level, "_pcoa") ), ".tsv" )
@@ -25,57 +26,42 @@ main <- function() {
2526
write.table( data.frame(mds=names(myMDS$CA$eig), eig=myMDS$CA$eig), file=eigenFileName, col.names=FALSE, row.names=FALSE, sep="\t")
2627
logInfo( "Save Eigen value table", pcoaFileName )
2728

28-
# Make plots
2929
outputFile = paste0( getPath( getOutputDir(), paste0(level, "_MDS.pdf" ) ) )
3030
pdf( outputFile, paper="letter", width=7.5, height=10.5 )
31-
par(mfrow=c(3, 2), las=1, oma=c(1,0,2,1), mar=c(5, 4, 2, 2), cex=.95)
32-
percentVariance = as.numeric(eigenvals(myMDS)/sum( eigenvals(myMDS) ) ) * 100
31+
par( mfrow=c(3, 2), las=1, oma=c(1,0,2,1), mar=c(5, 4, 2, 2), cex=0.95 )
32+
perVariance = as.numeric(eigenvals(myMDS)/sum( eigenvals(myMDS) ) ) * 100
3333
pageNum = 0
3434

3535
for( field in mdsFields ){
36-
logInfo( "mdsFields", mdsFields )
37-
pageNum = pageNum + 1
38-
metaColVals = as.character(metaTable[,field])
39-
logInfo( "metaColVals", metaColVals )
40-
par(mfrow = par("mfrow"), cex = par("cex"))
41-
att = as.factor(metaColVals)
42-
colorKey = metaColColors[[field]]
43-
logInfo( c( "Using colors: ", paste(colorKey, "for", names(colorKey), collapse= ", ")) )
44-
position = 1
45-
pageNum = 1
46-
numAxis = min(c(numAxis, ncol(myMDS$CA$u)))
47-
for (x in 1:(numAxis-1)) {
48-
for (y in (x+1):numAxis) {
49-
if (position > prod(par("mfrow") ) ) {
50-
position = 1
51-
pageNum = pageNum + 1
52-
}
53-
pch=getProperty("r.pch", 20)
36+
par(mfrow = par("mfrow"), cex = par("cex"))
37+
position = 1
38+
39+
metaColVals = as.character(metaTable[,field])
40+
colorKey = metaColColors[[field]]
41+
42+
logInfo( "metaColVals", metaColVals )
43+
logInfo( c( "Using colors: ", paste(colorKey, "for", names(colorKey), collapse= ", ")) )
44+
45+
for( x in 1: (numAxis-1) ) {
46+
for( y in (x+1): numAxis ) {
47+
5448
plot( myMDS$CA$u[,x], myMDS$CA$u[,y], main=paste("Axes", x, "vs", y),
55-
xlab=getMdsLabel( x, percentVariance[x] ),
56-
ylab=getMdsLabel( y, percentVariance[y] ),
57-
cex=1.2, pch=pch, col=colorKey[metaColVals] )
49+
xlab=getMdsLabel( x, perVariance[x] ),
50+
ylab=getMdsLabel( y, perVariance[y] ),
51+
cex=1.2, pch=getProperty("r.pch", 20), col=colorKey[metaColVals] )
52+
53+
54+
if( position == 1 || position > prod( par("mfrow") ) ) {
55+
position = 1
56+
pageNum = pageNum + 1
57+
addHeaderFooter( field, level, pageNum )
58+
}
59+
5860
position = position + 1
59-
if ( position == 2 ){
60-
addPageTitle( field, line=1 )
61-
addPageNumber( pageNum )
62-
addPageFooter( "Multidimensional Scaling" )
63-
# put this plot at the upper right position
64-
# that puts the legend in a nice white space, and it makes axis 1 in line with itself in two plots (same for axis3)
65-
plotRelativeVariance(percentVariance, numAxis)
66-
position = position + 1
67-
title( displayLevel( level ) )
68-
# Add legend
69-
legendKey = colorKey
70-
legendLabels = paste0(names(legendKey), " (n=", table(metaColVals)[names(legendKey)], ")")
71-
legendKey = legendKey[ order(table(metaColVals)[names(colorKey)]) ]
72-
maxInLegend = 6
73-
if (length(colorKey) > (maxInLegend + 1)){
74-
legendKey = c( colorKey[ 1:maxInLegend], NA)
75-
numDropped = length(colorKey) - length(legendKey) + 1
76-
legendLabels = c(legendLabels[1:maxInLegend], paste("(", numDropped, "other labels )"))
77-
}
78-
legend(x="topright", title=field, legend = legendLabels, col=legendKey, pch=pch, bty="n")
61+
62+
if( position == 2 ) {
63+
plotRelativeVariance( field, metaColVals, perVariance, level, numAxis, colorKey )
64+
position = position + 1
7965
}
8066
}
8167
}
@@ -85,19 +71,42 @@ main <- function() {
8571
}
8672
}
8773

74+
# Add page title + footer with page number
75+
addHeaderFooter <- function( field, level, pageNum ) {
76+
addPageTitle( field )
77+
addPageNumber( pageNum )
78+
addPageFooter( paste( str_to_title( level ), "Multidimensional Scaling" ) )
79+
}
80+
81+
# Get variance plot label as percentage
8882
getMdsLabel <- function( axisNum, variance ) {
89-
return( paste0("Axis ", axisNum, " (", paste0( round( variance ), "%)" ) ) )
83+
return( paste0("Axis ", axisNum, " ( ", paste0( round( variance ), "% )" ) ) )
9084
}
9185

92-
plotRelativeVariance <- function(percentVariance, numAxis){
93-
numBars = min(c(length(percentVariance), 6)) # arbitrary choice, don't show more than 6
86+
# This plot is always put in the upper right corner of the page
87+
plotRelativeVariance <- function( field, metaColVals, perVariance, level, numAxis, colorKey ){
88+
numBars = min( c(length(perVariance), maxInLegend) )
9489
numBars = max(numBars, numAxis)
95-
heights = percentVariance[1:numBars]
90+
heights = perVariance[1:numBars]
9691
bp = barplot(heights, col="dodgerblue1", ylim=c(0,100), names=1:numBars,
9792
xlab="Axis", ylab="Variance" )
9893
labels = round(heights)
9994
near0 = which(labels < 1)
10095
labels[near0] = "<1"
101-
if (numBars <= 6){ labels = paste(labels, "%") }
96+
if( numBars <= maxInLegend ) labels = paste( labels, "%" )
10297
text(x=bp, y=heights, labels = labels, pos=3, xpd=TRUE)
98+
title( str_to_title( level ) )
99+
100+
legendKey = colorKey
101+
legendLabels = paste0(names(legendKey), " (n=", table(metaColVals)[names(legendKey)], ")")
102+
legendKey = legendKey[ order(table(metaColVals)[names(colorKey)]) ]
103+
104+
if (length(colorKey) > (maxInLegend + 1) ){
105+
legendKey = c( colorKey[ 1:maxInLegend], NA)
106+
numDropped = length(colorKey) - length(legendKey) + 1
107+
legendLabels = c(legendLabels[1:maxInLegend], paste("(", numDropped, "other labels )"))
108+
}
109+
legend( "topright", title=field, legend=legendLabels, cex=0.8, col=legendKey, pch=getProperty("r.pch", 20), bty="n" )
103110
}
111+
112+
maxInLegend = 6

resources/R/R_PlotOtus.R

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ addBoxPlot <- function( item, taxaVals, metaVals, barColors )
1616
vertical=TRUE, pch=getProperty("r.pch"), add=TRUE )
1717
}
1818

19+
# Display plot heading with 2 pvalues + R^2 effect size
1920
plotHeading <- function( parPval, nonParPval, r2, field ) {
2021
HEAD_1 = 0.2; HEAD_2 = 1.4; LEFT = 0; RIGHT = 1; TOP = 3;
21-
22-
title1_A = paste( "Adj.", getTestName( field ), "P-value:", displayCalc( parPval ) )
23-
title1_B = bquote( paste( R^2, ": ", .( displayCalc( r2 ) ) ) )
22+
title1 = paste( "Adj.", getTestName( field ), "P-value:", displayCalc( parPval ) )
2423
title2 = paste( "Adj.", getTestName( field, FALSE ), "P-value:", displayCalc( nonParPval ) )
25-
26-
mtext( title1_A, TOP, HEAD_1, col=getColor( parPval ), cex=0.75, adj=LEFT )
27-
mtext( title1_B, TOP, HEAD_1, cex=0.75, adj=RIGHT )
24+
mtext( title1, TOP, HEAD_1, col=getColor( parPval ), cex=0.75, adj=LEFT )
25+
mtext( displayR2( r2 ), TOP, HEAD_1, cex=0.75, adj=RIGHT )
2826
mtext( title2, TOP, HEAD_2, col=getColor( nonParPval ), cex=0.75, adj=LEFT )
2927
}
3028

@@ -48,12 +46,9 @@ getBoxPlotLabels <- function( labels ) {
4846
getCexAxis <- function( labels=NULL, returnMax=FALSE, returnMin=FALSE) {
4947
cexAxisMax = 1
5048
cexAxisMin = 0.65
51-
if (returnMax){
52-
return(cexAxisMax)
53-
}
54-
if (returnMin){
55-
return(cexAxisMin)
56-
}
49+
if ( returnMax ) return( cexAxisMax )
50+
if ( returnMin ) return( cexAxisMin )
51+
5752
nchars = sum(nchar(labels)) + length(labels) - 1
5853
if( nchars < r.plotWidth ) return( cexAxisMax )
5954
if( nchars < ( r.plotWidth +7 ) ) return( 0.9 )
@@ -86,58 +81,56 @@ main <- function() {
8681
binaryCols = getBinaryFields()
8782
nominalCols = getNominalFields()
8883
numericCols = getNumericFields()
89-
9084
logInfo( "binaryCols", binaryCols )
9185
logInfo( "nominalCols", nominalCols )
9286
logInfo( "numericCols", numericCols )
93-
94-
reportCols = getReportFields()
95-
87+
9688
parStats = getStatsTable( level, TRUE )
9789
nonParStats = getStatsTable( level, FALSE )
9890
r2Stats = getStatsTable( level )
99-
10091
metaColColors = getColorsByCategory( metaTable )
10192

10293
outputFile = getPath( getOutputDir(), paste0(level, "_OTU_plots.pdf") )
10394
pdf( outputFile, paper="letter", width=7, height=10.5 )
104-
10595
par(mfrow=c(3, 2), las=1, oma=c(1.2,1,4.5,0), mar=c(5, 4, 3, 2), cex=1)
10696
pageNum = 0
10797

10898
# if r.rareOtuThreshold > 1, cutoffValue is an absolute threshold, otherwise it's a % of countTable rows
10999
cutoffValue = getProperty("r.rareOtuThreshold", 1)
110-
if( cutoffValue < 1 ) cutoffValue = cutoffValue * nrow(countTable)
100+
if( cutoffValue < 1 ) cutoffValue = cutoffValue * nrow( countTable )
111101

112-
for( item in names(countTable) ) {
102+
for( item in names( countTable ) ) {
113103
if( sum( countTable[,item] > 0 ) >= cutoffValue ) {
114-
par( mfrow = par("mfrow") ) # step to next pageNum, even if the last page is not full
115-
position = 1
116-
pageNum = pageNum + 1
104+
105+
# Every item starts a new page
106+
par( mfrow = par("mfrow") )
107+
position = 0
108+
117109
taxaVals = countTable[,item]
118110

119-
for( meta in reportCols ) {
111+
for( meta in getReportFields() ) {
112+
120113
metaVals = metaTable[,meta]
121114
if( meta %in% binaryCols || meta %in% nominalCols ) {
122-
logInfo( c( "Plot Box-Plot [", item, "~", meta, "]" ) )
115+
logInfo( c( "Add Box-Plot [", item, "~", meta, "]" ) )
123116
addBoxPlot( item, taxaVals, metaVals, metaColColors[[meta]] )
124117
}
125-
else if( meta %in% numericCols ) {
126-
logInfo( c( "Plot Scatter-Plot [", item, "~", meta, "]" ) )
118+
else {
119+
logInfo( c( "Add Scatter-Plot [", item, "~", meta, "]" ) )
127120
addScatterPlot( item, taxaVals, metaVals )
128121
}
129122

130-
plotHeading( parStats[ item, meta ], nonParStats[ item, meta ], r2Stats[ item, meta], meta )
123+
plotHeading( parStats[item, meta], nonParStats[item, meta], r2Stats[item, meta], meta )
131124
mtext( meta, side=1, font=1, cex=1, line=2.5 )
132125
position = position + 1
133126

134-
if(position == 2) {
135-
addPageTitle( item )
136-
addPageNumber( pageNum )
137-
}
138-
if( position > prod( par("mfrow") ) ) {
139-
position = 1
140-
pageNum = pageNum + 1
127+
if( position == 1 ) {
128+
pageNum = pageNum + 1
129+
addHeaderFooter( item, level, pageNum )
130+
} else if( position > prod( par("mfrow") ) ) {
131+
position = 1
132+
pageNum = pageNum + 1
133+
addHeaderFooter( item, level, pageNum )
141134
}
142135
}
143136
}
@@ -147,4 +140,11 @@ main <- function() {
147140
}
148141
}
149142

143+
# Add page title + footer with page number
144+
addHeaderFooter <- function( item, level, pageNum ) {
145+
addPageTitle( item )
146+
addPageNumber( pageNum )
147+
addPageFooter( paste( str_to_title( level ), "Taxa Plots" ) )
148+
}
149+
150150
r.plotWidth=23

0 commit comments

Comments
 (0)