comparison segcall.R @ 0:4d539083cf7f draft

planemo upload for repository https://github.com/sblanck/MPAgenomics4Galaxy/tree/master/mpagenomics_wrappers commit 689d0d8dc899a683ee18700ef385753559850233-dirty
author sblanck
date Tue, 12 May 2020 10:40:36 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4d539083cf7f
1 #!/usr/bin/env Rscript
2 # setup R error handling to go to stderr
3 options( show.error.messages=F, error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } )
4
5 # we need that to not crash galaxy with an UTF8 error on German LC settings.
6 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
7
8 library("optparse")
9
10 ##### Read options
11 option_list=list(
12 make_option("--chrom",type="character",default=NULL, dest="chrom"),
13 make_option("--input",type="character",default=NULL, dest="input"),
14 make_option("--output",type="character",default=NULL, dest="output"),
15 make_option("--new_file_path",type="character",default=NULL, dest="new_file_path"),
16 make_option("--nbcall",type="character",default=NULL, dest="nbcall"),
17 make_option("--settingsType",type="character",default=NULL, dest="settingsType"),
18 make_option("--outputgraph",type="character",default=NULL, dest="outputgraph"),
19 make_option("--snp",type="character",default=NULL, dest="snp"),
20 make_option("--zipfigures",type="character",default=NULL, dest="zipfigures"),
21 make_option("--settingsTypeTumor",type="character",default=NULL, dest="settingsTypeTumor"),
22 make_option("--cellularity",type="character",default=NULL, dest="cellularity"),
23 make_option("--outputlog",type="character",default=NULL, dest="outputlog"),
24 make_option("--log",type="character",default=NULL, dest="log"),
25 make_option("--userid",type="character",default=NULL, dest="userid"),
26 make_option("--method",type="character",default=NULL, dest="method")
27 );
28
29 opt_parser = OptionParser(option_list=option_list);
30 opt = parse_args(opt_parser);
31
32 if(is.null(opt$input)){
33 print_help(opt_parser)
34 stop("input required.", call.=FALSE)
35 }
36
37 #loading libraries
38
39 chrom=opt$chrom
40 datasetFile=opt$input
41 output=opt$output
42 tmp_dir=opt$new_file_path
43 nbcall=as.numeric(opt$nbcall)
44 settingsType=opt$settingsType
45 outputfigures=type.convert(opt$outputgraph)
46 snp=type.convert(opt$snp)
47 tumorcsv=opt$settingsTypeTumor
48 cellularity=as.numeric(opt$cellularity)
49 user=opt$userid
50 method=opt$method
51 log=opt$log
52 outputlog=opt$outputlog
53 outputgraph=opt$outputgraph
54 zipfigures=opt$zipfigures
55
56 library(MPAgenomics)
57 workdir=file.path(tmp_dir, "mpagenomics",user)
58 setwd(workdir)
59
60 if (grepl("all",tolower(chrom)) | chrom=="None") {
61 chrom_vec=c(1:25)
62 } else {
63 chrom_tmp <- strsplit(chrom,",")
64 chrom_vecstring <-unlist(chrom_tmp)
65 chrom_vec <- as.numeric(chrom_vecstring)
66 }
67
68
69 if (outputlog){
70 sinklog <- file(log, open = "wt")
71 sink(sinklog ,type = "output")
72 sink(sinklog, type = "message")
73 }
74
75
76 inputDataset=read.table(file=datasetFile,stringsAsFactors=FALSE)
77 dataset=inputDataset[1,2]
78
79 fig_dir = file.path("mpagenomics", user, "figures", dataset, "segmentation","CN")
80 abs_fig_dir = file.path(tmp_dir, fig_dir)
81
82 if (outputgraph) {
83 if (dir.exists(abs_fig_dir)) {
84 system(paste0("rm -r ", abs_fig_dir))
85 }
86 }
87
88 if (settingsType == 'dataset') {
89 if (tumorcsv== "none")
90 {
91 segcall=cnSegCallingProcess(dataset,chromosome=chrom_vec, nclass=nbcall, savePlot=outputfigures,onlySNP=snp, cellularity=cellularity, method=method)
92 } else {
93 segcall=cnSegCallingProcess(dataset,chromosome=chrom_vec, normalTumorArray=tumorcsv, nclass=nbcall, savePlot=outputfigures,onlySNP=snp, cellularity=cellularity, method=method)
94 }
95 } else {
96 input_tmp <- strsplit(settingsType,",")
97 input_tmp_vecstring <-unlist(input_tmp)
98 input_vecstring = sub("^([^.]*).*", "\\1", input_tmp_vecstring)
99 if (tumorcsv== "none")
100 {
101 segcall=cnSegCallingProcess(dataset,chromosome=chrom_vec, listOfFiles=input_vecstring, nclass=nbcall, savePlot=outputfigures, onlySNP=snp, cellularity=cellularity, method=method)
102 } else {
103 segcall=cnSegCallingProcess(dataset,chromosome=chrom_vec, normalTumorArray=tumorcsv, listOfFiles=input_vecstring, nclass=nbcall, savePlot=outputfigures, onlySNP=snp, cellularity=cellularity, method=method)
104 }
105 }
106
107
108 write.table(format(segcall),output,row.names = FALSE, quote=FALSE, sep = "\t")
109
110
111 if (outputgraph) {
112 setwd(abs_fig_dir)
113 files2zip <- dir(abs_fig_dir)
114 zip(zipfile = "figures.zip", files = files2zip)
115 file.rename("figures.zip",zipfigures)
116 }
117
118 if (outputlog){
119 sink(type="output")
120 sink(type="message")
121 close(sinklog)
122 }
123 #write.fwf(segcall,output,rownames = FALSE, quote=FALSE, sep = "\t")
124