2
|
1 #!/usr/bin/Rscript
|
|
2
|
|
3 ## --------------------
|
|
4 ## prints all arguments as msg
|
|
5 ## --------------------
|
|
6 catMsg <- function( msg=c() ){
|
|
7 cat( MAIN_NAME, paste( msg, collapse="" ), "\n", sep='')
|
|
8 }
|
|
9 ## --------------------
|
28
|
10 ## return the location of this script
|
2
|
11 ## --------------------
|
|
12 getScriptPath <- function(){
|
|
13 cmd.args <- commandArgs()
|
|
14 m <- regexpr("(?<=^--file=).+", cmd.args, perl=TRUE)
|
|
15 script.dir <- dirname(regmatches(cmd.args, m))
|
42
|
16 if( length(script.dir) == 0 ) stop("[ERR] Can't determine script dir: please call the script with Rscript\n")
|
|
17 if( length(script.dir) > 1 ) stop("[ERR] Can't determine script dir: more than one '--file' argument detected\n")
|
2
|
18 return(script.dir)
|
|
19 }
|
|
20 ## --------------------
|
28
|
21 ## Some html creation functions
|
2
|
22 ## --------------------
|
|
23 htmlTableRow <- function( string_array=c() ){
|
|
24 td_cells <- ''
|
|
25 for ( i in string_array ){
|
|
26 td_cells <- paste( td_cells, '<td>', i, '</td>', sep='' )
|
|
27 }
|
|
28 return( paste( "<tr>", td_cells, "</tr>") )
|
|
29 }
|
|
30 htmlLink <- function( path, desc="LINK" ){
|
|
31 return( paste( '<a href="', path, '">', desc, "</a>", sep='') )
|
|
32 }
|
|
33 ## --------------------
|
28
|
34 ## constructs a list with input bam file info
|
2
|
35 ## --------------------
|
|
36 makeBamFileList <- function( paths, names ){
|
|
37 tmp <- list()
|
|
38 l1 <- length(paths)
|
|
39 l2 <- length(names)
|
42
|
40 if ( l1 != l2 ) stop( "Unequal amount of bam-paths (", l1, ") and -names (", l2, ") in makeBamFileList!!!\n" )
|
2
|
41 for ( i in 1:length(paths) ){
|
|
42 path <- paths[i]
|
|
43 name <- names[i]
|
|
44 file <- basename(path)
|
|
45
|
|
46 tmp[[ file ]] <- name
|
|
47 tmp[[ 'all_paths' ]] <- c( tmp[[ 'all_paths' ]], path )
|
|
48 tmp[[ 'all_files' ]] <- c( tmp[[ 'all_files' ]], file )
|
|
49 tmp[[ 'all_names' ]] <- c( tmp[[ 'all_names' ]], name )
|
|
50 }
|
|
51 return( tmp )
|
|
52 }
|
|
53
|
|
54 ## --------------------
|
28
|
55 ## copied code for extracting the regions by segment call status
|
2
|
56 ## --------------------
|
59
|
57 fuseRegions <- function( cgh, onlyCalled=T ) {
|
|
58 if ( ncol(cgh) > 1 ) stop('Please specify which sample...')
|
2
|
59
|
59
|
60 data <- data.frame( cgh@featureData@data[,1:3], calls(cgh), copynumber(cgh), segmented(cgh), check.names=FALSE, stringsAsFactors=FALSE)
|
|
61 colnames( data ) <- c( "chr", "start", "end", "call", "log2", "segmentval" )
|
2
|
62
|
|
63 fused.data <- data.frame()
|
|
64 curr.bin <- 1
|
59
|
65 for ( chr in unique( data$chr ) ) {
|
2
|
66
|
59
|
67 chr.data <- data[ data$chr == chr, ]
|
|
68 prev.bin <- curr.bin
|
|
69 prev.call <- chr.data[ 1, 'call' ]
|
|
70 prev.log2 <- chr.data[ 1, 'log2' ]
|
|
71 prev.segm <- chr.data[ 1, 'segmentval' ]
|
|
72 start <- chr.data[ 1, 'start' ]
|
2
|
73
|
|
74 if ( nrow(chr.data) > 1) {
|
|
75 for ( i in 2:nrow(chr.data) ) {
|
|
76
|
59
|
77 curr.bin <- curr.bin + 1
|
2
|
78 curr.call <- chr.data[ i, 'call']
|
|
79 curr.segm <- chr.data[ i, 'segmentval']
|
|
80
|
|
81 if ( curr.segm != prev.segm ) {
|
|
82
|
|
83 fused.data <- rbind( fused.data, data.frame( chr=chr, start=start, end=chr.data[ i-1, 'end'], call=prev.call, segmentval=round(prev.segm, digits=DECIMALS) ) )
|
|
84 if ( prev.call != 0 ){
|
42
|
85 catMsg( c(" ...found called/segmented region (", chr, ':', start, ' call=', prev.call, ' segment=', prev.segm, ")" ) )
|
2
|
86 }
|
|
87 prev.call <- curr.call
|
|
88 prev.segm <- curr.segm
|
59
|
89 prev.bin <- curr.bin
|
|
90 start <- chr.data[ i, 'start']
|
2
|
91 }
|
|
92 }
|
|
93 fused.data <- rbind( fused.data, data.frame( chr=chr, start=start, end=chr.data[ i-1, 'end'], call=prev.call, segmentval=round(prev.segm, digits=DECIMALS) ) )
|
59
|
94 }else{
|
2
|
95 fused.data <- rbind( fused.data, data.frame( chr=chr, start=start, end=chr.data[ i-1, 'end'], call=prev.call, segmentval=round(prev.segm, digits=DECIMALS) ) )
|
|
96 }
|
|
97 }
|
59
|
98 ## if requested remove the regions with call status 0 (= normal)
|
2
|
99 if ( onlyCalled == T ){
|
|
100 fused.data <- fused.data[ fused.data$call != 0, ]
|
|
101 }
|
|
102 fused.data
|
|
103 }
|
|
104
|
|
105 ## DESC: takes the output of fuse.regions and outputs a txt file per sample
|
|
106 outputRegionsFromList <- function ( regionsList, outputBasename, outputDir="./" ){
|
|
107 if ( missing(regionsList) ) stop( 'Please provide regionsList...' )
|
|
108 if ( missing(outputBasename) ) stop( 'Please provide outputBasename...' )
|
|
109 if ( !is.list(regionsList) ) stop( 'Input not a list...?' )
|
|
110 if ( length(regionsList) < 1 ) stop( 'List seems empty...?' )
|
42
|
111 if ( file.exists( outputDir ) ) catMsg( c(" Using dir ", outputDir, " for output") )
|
2
|
112 else dir.create( outputDir )
|
|
113 outFiles <- list()
|
|
114
|
|
115 ## have to set R output options otherwise scientific method is used at some point
|
|
116 options( "scipen"=100 )
|
|
117
|
|
118 sampleCount <- length( regionsList )
|
|
119 sampleNames <- names( regionsList )
|
|
120 bedgraphColumns <- c( 'chr', 'start', 'end', 'segmentval' )
|
30
|
121
|
42
|
122 catMsg( c( " There are ", sampleCount, " samples found in input list") )
|
2
|
123
|
|
124 for ( sample in sampleNames ){
|
42
|
125 catMsg( c(" Working on sample ", sample ) )
|
2
|
126 regionCount <- nrow( regionsList[[sample]] )
|
|
127
|
|
128 outSampleBase <- paste( outputBasename, '_', sample, '_QDNAseqRegions', sep='')
|
|
129 outBedgraphFile <- paste( outSampleBase, '.bedGraph', sep="" )
|
|
130 outBedgraphPath <- paste( outputDir, '/', outBedgraphFile, sep="" )
|
|
131
|
|
132 ## ---------- BEDGRAPH ----------
|
|
133 txt <- paste( "track type=bedGraph color=0,100,0 altColor=255,0,0 name=", sample,"_segmReg description=segmented_regions_from_QDNAseq\n", sep="")
|
|
134 sink( outBedgraphPath )
|
|
135 cat( txt )
|
|
136 sink()
|
|
137 write.table( regionsList[[sample]][,bedgraphColumns], outBedgraphPath, quote=F, sep="\t", row.names=F, append=T, col.names=F)
|
59
|
138 outFiles[[sample]] <- c( outBedgraphFile )
|
2
|
139 }
|
|
140 outFiles
|
|
141 }
|
|
142
|
|
143 ## ==================================================
|
|
144 ## Start of analysis
|
|
145 ## ==================================================
|
42
|
146 MAIN_NAME <- '[INFO] '
|
2
|
147 TOOL_PATH <- getScriptPath()
|
|
148 CSS_FILE <- paste( TOOL_PATH, '/QDNAseq.css', sep="" )
|
|
149 DECIMALS <- 3
|
|
150 WEB_LINK <- 'http://www.bioconductor.org/packages/release/bioc/html/QDNAseq.html'
|
59
|
151 PURE_CSS <- 'http://yui.yahooapis.com/pure/0.5.0/pure-min.css'
|
54
|
152 PAR_SET <- list( pch=22 )
|
2
|
153
|
|
154 catMsg( "Starting QDNAseq wrapper" )
|
|
155 catMsg( "Loading R libraries" )
|
|
156 suppressWarnings( suppressMessages( library( QDNAseq, quietly = TRUE ) ) )
|
|
157 suppressWarnings( suppressMessages( library( CGHcall, quietly = TRUE ) ) )
|
25
|
158
|
2
|
159 ## only one param: the tmp config file
|
|
160 cmdLineArgs <- commandArgs(TRUE)
|
|
161 config <- cmdLineArgs[1]
|
|
162
|
|
163 ## sourcing the config file will load all input params
|
59
|
164 ## many variables are imported via sourced "config"
|
2
|
165 source( config )
|
|
166
|
42
|
167 PLOT_RES <- min( PLOT_WIDTH, PLOT_HEIGHT ) / 6.3 # desparate try to make png text scale well, damn you R...!
|
2
|
168 if ( doOutputCallsRds == TRUE ){ doCall <- TRUE }
|
|
169
|
42
|
170 systemUser <- system("whoami",T)
|
|
171 qdnaseqVersion <- packageDescription( "QDNAseq" )$Version
|
59
|
172 rVersion <- R.version.string
|
42
|
173 catMsg( c("Analysis run with user: ", systemUser ) )
|
|
174 catMsg( c("QDNAseq version loaded: ", qdnaseqVersion) )
|
59
|
175 catMsg( c( rVersion ) )
|
42
|
176
|
2
|
177 ## get the comma separated list of chromosomes to exclude
|
|
178 excludeChrs <- unlist( strsplit( excludeChrsString, ",") )
|
|
179
|
|
180 ## ------------------------
|
|
181 ## DEBUG
|
|
182 #catMsg( "PARAM" )
|
|
183 #catMsg( galaxy_path )
|
|
184 #catMsg( repos_path )
|
|
185 #catMsg( instal_path )
|
|
186 ## /DEBUG
|
|
187 ## ------------------------
|
|
188
|
59
|
189 ## help msg for running script without wrapper still needs work!
|
28
|
190 #if ( length(cmdLineArgs) == 0 || cmdLineArgs[1] == "-h" || cmdLineArgs[1] == "--help"){
|
42
|
191 # catMsg( c( "Usage: ", paste(params_help,sep=",") ) )
|
28
|
192 # quit( save = 'no', status=0, runLast=F )
|
|
193 #}
|
2
|
194
|
|
195 if ( !file.exists( outputPath) ){
|
|
196 dir.create( outputPath )
|
|
197 }
|
|
198
|
|
199 ## because we circumvent params that galaxy can save, we want to
|
|
200 ## copy source config file to output dir to include it in output zip
|
|
201 file.copy( config, paste(outputPath, 'qdnaseq_config_file.R', sep='/') )
|
|
202
|
|
203 ## ------------------------
|
|
204 ## construct output file-names and -paths
|
|
205 ## ------------------------
|
|
206 htmlOutputName <- 'index.html'
|
59
|
207 gzipOutputName <- paste( 'QDNAseqResults_', binSize, 'kbp_', outputName, '.zip', sep='' )
|
2
|
208 robjReadCoName <- paste( binSize, 'kbp_QDNAseqReadCounts.rds', sep='')
|
|
209 robjCopyNrName <- paste( binSize, 'kbp_QDNAseqCopyNumbers.rds', sep='')
|
|
210 robjCalledName <- paste( binSize, 'kbp_QDNAseqCalledSegments.rds', sep='')
|
|
211 regiOutputName <- paste( binSize, 'kbp_QDNAseqRegions.rds', sep='')
|
|
212 igvCopyNrName <- paste( binSize, 'kbp_QDNAseq-normalized.igv', sep='')
|
42
|
213 noiseImgName <- paste( 'QDNAseqNoisePlot.png', sep='')
|
|
214 freqImgName <- paste( 'QDNAseqFrequencyPlot.png', sep='')
|
2
|
215
|
42
|
216 gzipOutputPath <- paste( outputPath, '/', gzipOutputName, sep='')
|
|
217 htmlOutputPath <- paste( outputPath, '/', htmlOutputName, sep='')
|
|
218 robjReadCoPath <- paste( outputPath, '/', robjReadCoName, sep='')
|
|
219 robjCopyNrPath <- paste( outputPath, '/', robjCopyNrName, sep='')
|
|
220 robjCalledPath <- paste( outputPath, '/', robjCalledName, sep='')
|
|
221 robjRegionPath <- paste( outputPath, '/', regiOutputName, sep='')
|
|
222 igvCopyNrPath <- paste( outputPath, '/', igvCopyNrName, sep='')
|
|
223 noiseImgPath <- paste( outputPath, '/', noiseImgName, sep='' )
|
|
224 freqImgPath <- paste( outputPath, '/', freqImgName, sep='' )
|
2
|
225
|
59
|
226 ## setup bam filelist for easy retrieval later
|
|
227 fileList <- makeBamFileList( bamsPaths, bamsNames )
|
|
228 bamCount <- length( fileList[[ 'all_paths' ]] )
|
2
|
229
|
|
230 ## ------------------------
|
|
231 ## performing QDNAseq analysis steps
|
|
232 ## ------------------------
|
59
|
233 if ( debug ){ ## in case of debug just use inbuilt LGG data for speedup
|
2
|
234 data(LGG150)
|
|
235 readCounts <- LGG150
|
59
|
236 binSize <- 15
|
|
237 bamsPaths <- c( "BUILD_IN_DATA_LGG150")
|
|
238 bamsNames <- c( "LGG150")
|
|
239 fileList <- makeBamFileList( bamsPaths, bamsNames )
|
|
240 bamCount <- length( fileList[[ 'all_paths' ]] )
|
2
|
241 }else{
|
|
242 if ( nchar(binAnnotations) == 0 ){
|
|
243 binAnnotations <- getBinAnnotations( binSize=binSize, type=experimentType )
|
|
244 }else{
|
|
245 ## if user provided file, check if correct class
|
|
246 if ( class(binAnnotations)[1] != 'AnnotatedDataFrame' ){
|
|
247 stop( "Provided binAnnotations file is not of class 'AnnotatedDataFrame'\n" )
|
|
248 }
|
|
249 binAnnotations <- readRDS( binAnnotations )
|
|
250 }
|
|
251 ## provide bamnames because in galaxy everyting is called "dataset_###"
|
|
252 readCounts <- binReadCounts( binAnnotations, bamfiles=fileList[[ 'all_paths' ]], bamnames=bamsNames )
|
|
253 }
|
|
254
|
|
255 readCountsFiltered <- applyFilters( readCounts, residual=TRUE, blacklist=filterBlacklistedBins, mappability=mappabilityCutoff, chromosomes=excludeChrs )
|
|
256 readCountsFiltered <- estimateCorrection( readCountsFiltered )
|
|
257 copyNumbers <- correctBins( readCountsFiltered )
|
|
258 copyNumbersNormalized <- normalizeBins( copyNumbers )
|
|
259 copyNumbersSmooth <- smoothOutlierBins( copyNumbersNormalized )
|
28
|
260 sampleNames <- readCountsFiltered@phenoData@data$name
|
2
|
261
|
|
262 ## save objects to output dir
|
42
|
263 saveRDS( readCountsFiltered, robjReadCoPath );
|
29
|
264 saveRDS( copyNumbersSmooth, robjCopyNrPath );
|
42
|
265 exportBins( copyNumbersSmooth, file=igvCopyNrPath, format="igv" )
|
2
|
266
|
|
267 ## also save objects for galaxy history output if requested
|
|
268 if ( doOutputReadcountsRds ){
|
|
269 saveRDS( readCountsFiltered, readCountsDatasetFile );
|
|
270 }
|
|
271 if ( doOutputCopynumbersRds ){
|
|
272 saveRDS( copyNumbersSmooth, copyNumbersDatasetFile );
|
|
273 }
|
59
|
274 if ( doOutputCopynumbersIgv ){
|
|
275 saveRDS( copyNumbersSmooth, copyNumbersIgvDatasetFile );
|
|
276 }
|
2
|
277
|
|
278 ## proceed with calling if requested
|
|
279 if ( doCall ){
|
|
280 copyNumbersSegmented <- segmentBins( copyNumbersSmooth, undo.splits=undoSplits, undo.SD=undoSD )
|
|
281 copyNumbersSegmented <- normalizeSegmentedBins( copyNumbersSegmented )
|
|
282 copyNumbersCalled <- callBins( copyNumbersSegmented )
|
|
283 cgh <- makeCgh( copyNumbersCalled )
|
|
284 saveRDS( copyNumbersCalled, robjCalledPath );
|
|
285 if ( doOutputCallsRds ){
|
|
286 saveRDS( copyNumbersCalled, calledSegmentsDatasetFile );
|
|
287 }
|
59
|
288 ## actual calls are not included in exportBins output, so not used now
|
|
289 #exportBins( copyNumbersCalled, file=igvCalledPath, format="igv")
|
2
|
290 }
|
|
291
|
28
|
292
|
2
|
293
|
|
294 ## ------------------------
|
|
295 ## create output files
|
|
296 ## ------------------------
|
|
297 plotted_images <- list() # to keep track of images for later linking
|
|
298 regions <- list() # will contain the (called) segments
|
|
299
|
53
|
300 png( noiseImgPath, width=PLOT_HEIGHT, height=PLOT_HEIGHT, res=PLOT_RES );
|
|
301 par( PAR_SET )
|
42
|
302 noisePlot( readCountsFiltered, col="darkgreen" )
|
2
|
303 dev.off()
|
|
304
|
42
|
305 if ( doCall ){
|
|
306 png( freqImgPath, width=PLOT_WIDTH, height=PLOT_HEIGHT, res=PLOT_RES );
|
53
|
307 par( PAR_SET )
|
42
|
308 frequencyPlot( copyNumbersCalled )
|
|
309 dev.off()
|
|
310 }
|
|
311
|
2
|
312 for (i in 1:length(sampleNames) ){
|
|
313 sample <- sampleNames[i]
|
|
314 usedReads <- readCountsFiltered@phenoData@data$used.reads[i]
|
|
315 catMsg( c("Creating plots for sample: ", sample ) )
|
|
316
|
|
317 type <- 'CopyNumbers'
|
|
318 img_file <- paste( sample, '_', binSize, 'kbp_QDNAseq', type, '.png', sep='')
|
|
319 img_file_path <- paste( outputPath, '/', img_file, sep='' )
|
42
|
320 png( img_file_path, width=PLOT_WIDTH, height=PLOT_HEIGHT, res=PLOT_RES );
|
53
|
321 par( PAR_SET )
|
42
|
322 plot( copyNumbersSmooth[ ,sample ] );
|
|
323 dev.off()
|
2
|
324 plotted_images[[ sample ]][[ type ]] <- img_file
|
|
325
|
|
326 if ( doCall ){
|
|
327 type <- 'Called'
|
|
328 img_file <- paste( sample, '_', binSize, 'kbp_QDNAseq', type, '.png', sep='')
|
|
329 img_file_path <- paste( outputPath, '/', img_file, sep='' )
|
42
|
330 png( img_file_path, width=PLOT_WIDTH, height=PLOT_HEIGHT, res=PLOT_RES );
|
53
|
331 par( PAR_SET )
|
2
|
332 plot( copyNumbersCalled[ ,sample ] );
|
|
333 dev.off()
|
|
334 plotted_images[[ sample ]][[ type ]] <- img_file
|
|
335
|
42
|
336 catMsg( c(" Fusing regions of sample: ", sample) )
|
59
|
337 regions[[ sample ]] <- fuseRegions( cgh[, sample] )
|
2
|
338 region_count <- nrow( data.frame( regions[[ sample ]] ) )
|
42
|
339 catMsg( c( ' sample "', sample, '" has ', region_count, " regions" ) )
|
2
|
340 plotted_images[[ sample ]][[ 'region_count' ]] <- region_count
|
|
341 }
|
|
342
|
30
|
343 ## add USED read counts
|
2
|
344 plotted_images[[ sample ]][[ 'usedReads' ]] <- usedReads
|
|
345 }
|
|
346
|
|
347 if ( doCall ){
|
|
348 saveRDS( regions, robjRegionPath )
|
|
349 printedFiles <- outputRegionsFromList( regions, outputBasename=outputName, outputDir=outputPath )
|
|
350 }
|
|
351
|
|
352 ## ------------------------
|
|
353 ## prepare output
|
|
354 ## ------------------------
|
42
|
355 catMsg( "...zipping output")
|
2
|
356 zip_cmd <- paste( "zip -j", gzipOutputPath, paste(outputPath,'/*',sep='') ) ## -j is for removing dirs from the tree
|
|
357 system( zip_cmd )
|
|
358
|
|
359 ## ------------------------
|
|
360 ## get filesizes for report
|
|
361 ## ------------------------
|
59
|
362 zippedSize <- paste( round( file.info( gzipOutputPath )[["size"]] / 1e+6, digits=2 ), 'MB' )
|
|
363 readCoSize <- paste( round( file.info( robjReadCoPath )[["size"]] / 1e+6, digits=2 ), 'MB' )
|
|
364 copyNrSize <- paste( round( file.info( robjCopyNrPath )[["size"]] / 1e+6, digits=2 ), 'MB' )
|
|
365 calledSize <- paste( round( file.info( robjCalledPath )[["size"]] / 1e+6, digits=2 ), 'MB' )
|
|
366 regionSize <- paste( round( file.info( robjRegionPath )[["size"]] / 1e+6, digits=2 ), 'MB' )
|
|
367 igvCopyNrSize <- paste( round( file.info( igvCopyNrPath )[["size"]] / 1e+6, digits=2 ), 'MB' )
|
2
|
368
|
|
369 ## ------------------------
|
|
370 ## creating html output to be linked to from the middle galaxy pane
|
|
371 ## ------------------------
|
|
372
|
|
373 #sink( file = outputHtml, type = "output" )
|
|
374 sink( file = htmlOutputPath, type = "output" )
|
|
375 cat( "<html>\n")
|
|
376 cat( "<head>\n")
|
|
377 #cat( '<link rel="stylesheet" type="text/css" href="test.css" media="screen" />', "\n" )
|
59
|
378 cat( "\t", '<title>QDNAseq Report | ', outputName,'</title>', "\n", sep='' )
|
|
379 cat( "\t", '<link rel="stylesheet" href="', PURE_CSS, '">', "\n", sep='' )
|
|
380 cat( "\t<style>\n", sep='')
|
|
381 ## include CSS into html file, makes it more portable
|
|
382 cat( paste( "\t\t", '/* the css here originates from ', CSS_FILE,' */', "\n"), sep='' )
|
|
383 cat( "\t\t", readLines( CSS_FILE ), sep="\n\t\t" )
|
2
|
384 #cat( "\t\th1 {color:red;}", "\n")
|
59
|
385 cat( "\n\t</style>\n" )
|
2
|
386
|
|
387 cat( "\n</head>\n")
|
|
388 cat( "\n<body>\n")
|
|
389
|
|
390 cat( "<h1>QDNAseq Report</h1>", "\n")
|
|
391
|
|
392 cat( '<h3 class="qdnaseq">About this analysis</h3>', "\n")
|
59
|
393 cat( '<p>This page provides access to all results. To have a local copy of this report just download the <a href="', gzipOutputName, '" class="button">zipfile</a> with all output (', zippedSize, ')</p>', "\n", sep='')
|
2
|
394
|
|
395 ## ------------------------
|
|
396 ## table with general info
|
|
397 ## ------------------------
|
|
398 cat( '<h3 class="qdnaseq">Settings</h3><p>', "\n")
|
59
|
399 cat( '<table class="pure-table pure-table-striped"><tbody>' )
|
2
|
400 cat( htmlTableRow( c( "AnalysisName", outputName ) ) )
|
59
|
401 cat( htmlTableRow( c( "AnalysisDate", as.character(Sys.time()) ) ) )
|
2
|
402 cat( htmlTableRow( c( "BinSize (kb)", binSize ) ) )
|
59
|
403 cat( htmlTableRow( c( "R info", rVersion ) ) )
|
|
404 cat( htmlTableRow( c( "QDNAseq info", qdnaseqVersion ) ) )
|
2
|
405
|
59
|
406 sampleStrings <- c()
|
2
|
407 for ( galaxyName in fileList[[ 'all_files' ]] ){
|
|
408 sampleName <- fileList[[ galaxyName ]]
|
59
|
409 sampleStrings <- c( sampleStrings, paste( galaxyName, ' (', sampleName, ')', sep='' ) )
|
2
|
410 }
|
59
|
411 cat( htmlTableRow( c( "InputBams", paste( sampleStrings, collapse=", ") ) ) )
|
|
412
|
2
|
413 cat( "</tbody></table></p>", "\n")
|
|
414
|
|
415 ## ------------------------
|
|
416 ## list with links to all output files
|
|
417 ## ------------------------
|
59
|
418
|
2
|
419
|
|
420 cat( '<h3 class="qdnaseq">Output files</h3><p>', "\n")
|
|
421 cat( '<dl>', "\n" )
|
|
422 cat( '<dt>', htmlLink( path=robjReadCoName, robjReadCoName ), '</dt>', "\n" )
|
|
423 cat( '<dd>QDNAseq object with read counts per bin, ', readCoSize,'</dd>', "\n" )
|
59
|
424
|
2
|
425
|
|
426 cat( '<dt>', htmlLink( path=robjCopyNrName, robjCopyNrName ), '</dt>', "\n" )
|
|
427 cat( '<dd>QDNAseq object with copy numbers per bin, ', copyNrSize,'</dd>', "\n" )
|
|
428
|
31
|
429 cat( '<dt>', htmlLink( path=igvCopyNrName, igvCopyNrName ), '</dt>', "\n" )
|
|
430 cat( '<dd>IGV formatted text file with copy numbers per bin, ', igvCopyNrSize,'</dd>', "\n" )
|
|
431
|
2
|
432 if ( doCall ){
|
|
433 cat( '<dt>', htmlLink( path=robjCalledName, robjCalledName ), '</dt>', "\n" )
|
|
434 cat( '<dd>QDNAseq object with segment and call status per bin, ', calledSize,'</dd>', "\n" )
|
|
435
|
|
436 cat( '<dt>', htmlLink( path=regiOutputName, regiOutputName ), '</dt>', "\n" )
|
|
437 cat( '<dd>list with segmented/called regions for each sample, ', regionSize, '</dd>', "\n" )
|
|
438 }
|
|
439 cat( '</dl></p>', "\n" )
|
|
440
|
59
|
441 r_code <- '## start own downstream analysis in R with:'
|
|
442 r_code <- c( r_code, 'library( QDNAseq )' )
|
|
443 r_code <- c( r_code, paste( 'readRDS( ', robjReadCoName, ' ) -> readCounts', sep="") )
|
|
444
|
42
|
445 ## r_code shows example code on how to load output files
|
25
|
446 #cat( r_code, "</p>\n", sep="\n")
|
2
|
447 cat( '<p>See ', htmlLink( WEB_LINK, 'the bioconductor QDNAseq documentation' ), ' for more information on how to work with these files</p>', "\n", sep='' )
|
59
|
448 cat( '<pre>', paste( r_code, collapse="\n" ), '</pre>', "\n", sep='' )
|
2
|
449
|
|
450 ## ------------------------
|
|
451 ## table with links to files
|
|
452 ## ------------------------
|
|
453 cat( '<h3 class="qdnaseq">Results: overview</h3><p>', "\n")
|
|
454 plots_html <- ''
|
|
455
|
40
|
456 cat( '<table class="pure-table pure-table-striped"><thead><tr><th>Sample / File</th><th>CopyNr</th><th>Calls</th><th>Reads</th><th>Regions</th><th>Files</th></tr></thead><tbody>' )
|
2
|
457
|
|
458 for ( bam_file in sampleNames ){
|
|
459
|
|
460 #width <- 600; height <- 240
|
|
461 width <- PLOT_WIDTH; height <- PLOT_HEIGHT
|
|
462 width_t <- 100; height_t <- 40
|
|
463
|
|
464 ## add thumbnails to table with links to anchors on html page
|
|
465 copy_img <- plotted_images[[ bam_file ]][[ 'CopyNumbers' ]]
|
|
466 usedReads <- plotted_images[[ bam_file ]][[ 'usedReads' ]]
|
|
467 usedReads <- format( as.integer(usedReads), digits=4, decimal.mark=".", big.mark="," )
|
|
468
|
|
469 html_copy_thumb <- htmlLink( path=paste('#', copy_img, sep=''), paste('<img src="',copy_img,'" alt="', bam_file, '" width="', width_t, '" height="', height_t, '">', sep='') )
|
|
470 html_copy_img <- htmlLink( path=copy_img, paste('<img id="', copy_img,'" src="',copy_img,'" alt="',bam_file, '" width="', width, '" height="', height, '">', sep='') )
|
|
471 html_call_thumb <- 'NA'
|
|
472 html_call_img <- ''
|
|
473 html_bedGraph <- 'NA'
|
|
474 region_count <- 'NA'
|
|
475
|
|
476 if ( doCall ){
|
|
477 call_img <- plotted_images[[ bam_file ]][[ 'Called' ]]
|
|
478 region_count <- plotted_images[[ bam_file ]][[ 'region_count' ]]
|
|
479 html_call_thumb <- htmlLink( path=paste('#', call_img, sep=''), paste('<img src="', call_img, '" alt="', bam_file, '" width="', width_t,'" height="', height_t,'">', sep='') )
|
|
480
|
59
|
481 ## list structure with output files just is an array per BAM
|
|
482 html_bedGraph <- htmlLink( path=printedFiles[[ bam_file ]][1], 'bedGraph' )
|
2
|
483 html_call_img <- htmlLink( path=call_img, paste('<img id="', call_img,'" src="', call_img,'" alt="', bam_file, '" width="', width, '" height="', height,'">', sep='') )
|
|
484 }
|
|
485
|
|
486 ## add info to overview table, including small thumbnails
|
59
|
487 cat( htmlTableRow( c(bam_file, html_copy_thumb, html_call_thumb, usedReads, region_count, paste( html_bedGraph, sep=", ")) ), "\n" )
|
2
|
488 ## now include (large) images in html page
|
|
489 plots_html <- paste( plots_html, html_copy_img, "\n", html_call_img, "\n<hr \\>\n", sep='' )
|
|
490 }
|
|
491 cat( "</tbody></table></p>", "\n")
|
|
492
|
42
|
493 ## ------------------------
|
|
494 ## create the noise and frequency plots
|
|
495 ## ------------------------
|
|
496 html_noise_img <- htmlLink(
|
|
497 path=noiseImgName,
|
55
|
498 paste('<img id="', noiseImgName,'" src="', noiseImgName, '" width="', height/2, '" height="', height/2, '" alt="NoisePlot">', sep='')
|
42
|
499 )
|
|
500 html_freq_img <- htmlLink(
|
|
501 path=freqImgName,
|
55
|
502 paste('<img id="', freqImgName,'" src="', freqImgName, '" width="', width/2, '" height="', height/2, ' alt="FrequenceyPlot">', sep='')
|
42
|
503 )
|
56
|
504 ## there only is a frequency plot when data is called
|
|
505 if ( !doCall ){ html_freq_img <- '' }
|
59
|
506
|
|
507 extra_plots_html <- paste( html_noise_img, " \n", html_freq_img, "\n", sep='' )
|
2
|
508
|
|
509 ## ------------------------
|
|
510 ## section with various output shown
|
|
511 ## ------------------------
|
42
|
512 cat( '<h3 class="qdnaseq">Results: Analysis plots</h3><p>', "\n")
|
|
513 cat( extra_plots_html, "\n")
|
|
514 cat( '<h3 class="qdnaseq">Results: Sample plots</h3><p>', "\n")
|
2
|
515 cat( plots_html, "\n")
|
|
516 cat( "\n</p></body>\n")
|
|
517 cat( "\n</html>\n")
|
|
518 sink()
|
|
519
|
|
520 ## ------------------------
|
42
|
521 ## creating main html output for galaxy history
|
2
|
522 ## ------------------------
|
|
523 sink( file = outputHtml, type = "output" )
|
|
524
|
|
525 cat( "<head>", "\n")
|
59
|
526 cat( "\t", '<link rel="stylesheet" href="', PURE_CSS, '">', "\n", sep='' )
|
2
|
527
|
|
528 cat( "<style>", "\n")
|
42
|
529 ## include CSS directly into html file
|
2
|
530 cat( paste( "\t", '/* the css here originates from ', CSS_FILE,' */', "\n") )
|
|
531 cat( paste( "\t", readLines( CSS_FILE, n = -1)), sep="\n" )
|
|
532 cat( "</style>", "\n")
|
|
533 cat( "</head>", "\n")
|
|
534
|
|
535 cat( '<h1>QDNAseq Results (', outputName,')</h1>', "\n", sep="")
|
59
|
536 cat( '<p>Explore <a href="', htmlOutputName, '" class="button">the results</a> directly within galaxy</p>', "\n", sep="")
|
|
537 cat( '<p>Or download a <a href="', gzipOutputName, '" class="button">zipfile</a> with all output (', zippedSize, ')</p>', "\n", sep="" )
|
2
|
538
|
|
539 cat( '<p>The zip file contains all output files, including *.rds files allowing you to load the R copyNumber object(s) and perform further detailed analysis or create your own output for further processing. You can load the rds file with <code class="code">loadRDS(file.rds)</code></p>', "\n", sep="")
|
|
540
|
|
541 sink()
|
|
542
|
42
|
543 ## ------------------------
|
|
544 ## create final zip and quit with status 0 to tell galaxy all was fine
|
|
545 ## ------------------------
|
|
546 catMsg( "zipping all output")
|
|
547 system( paste( "zip -j ", gzipOutputPath, paste(outputPath,'/', htmlOutputName, sep='') ) )
|
|
548 catMsg( "done" )
|
25
|
549 q(status=0)
|