Mercurial > repos > sblanck > mpagenomics
comparison selection.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("--response",type="character",default=NULL, dest="response"), | |
17 make_option("--settingsType",type="character",default=NULL, dest="settingsType"), | |
18 make_option("--outputgraph",type="character",default=NULL, dest="outputgraph"), | |
19 make_option("--settingsSnp",type="character",default=NULL, dest="settingsSnp"), | |
20 make_option("--settingsSignal",type="character",default=NULL, dest="settingsSignal"), | |
21 make_option("--settingsLoss",type="character",default=NULL, dest="settingsLoss"), | |
22 make_option("--pdffigures",type="character",default=NULL, dest="pdffigures"), | |
23 make_option("--folds",type="character",default=NULL, dest="folds"), | |
24 make_option("--outputlog",type="character",default=NULL, dest="outputlog"), | |
25 make_option("--log",type="character",default=NULL, dest="log"), | |
26 make_option("--userId",type="character",default=NULL, dest="userid"), | |
27 make_option("--settingsPackage",type="character",default=NULL, dest="settingsPackage") | |
28 ); | |
29 | |
30 opt_parser = OptionParser(option_list=option_list); | |
31 opt = parse_args(opt_parser); | |
32 | |
33 if(is.null(opt$input)){ | |
34 print_help(opt_parser) | |
35 stop("input required.", call.=FALSE) | |
36 } | |
37 | |
38 #loading libraries | |
39 | |
40 | |
41 chrom=opt$chrom | |
42 dataset=opt$input | |
43 dataResponse=opt$response | |
44 output=opt$output | |
45 tmp_dir=opt$new_file_path | |
46 signal=opt$settingsSignal | |
47 settingsType=opt$settingsType | |
48 outputfigures=type.convert(opt$outputgraph) | |
49 snp=type.convert(opt$settingsSnp) | |
50 user=opt$userid | |
51 folds=as.numeric(opt$folds) | |
52 loss=opt$settingsLoss | |
53 log=opt$log | |
54 outputlog=opt$outputlog | |
55 outputgraph=opt$outputgraph | |
56 pdffigures=opt$pdffigures | |
57 package=opt$settingsPackage | |
58 | |
59 | |
60 library(MPAgenomics) | |
61 library(glmnet) | |
62 library(spikeslab) | |
63 library(lars) | |
64 | |
65 inputDataset=read.table(file=dataset,stringsAsFactors=FALSE) | |
66 input=inputDataset[1,2] | |
67 workdir=file.path(tmp_dir, "mpagenomics",user) | |
68 print(workdir) | |
69 setwd(workdir) | |
70 | |
71 if (grepl("all",tolower(chrom)) | chrom=="None") { | |
72 chrom_vec=c(1:25) | |
73 } else { | |
74 chrom_tmp <- strsplit(chrom,",") | |
75 chrom_vecstring <-unlist(chrom_tmp) | |
76 chrom_vec <- as.numeric(chrom_vecstring) | |
77 } | |
78 | |
79 if (outputlog){ | |
80 sinklog <- file(log, open = "wt") | |
81 sink(sinklog ,type = "output") | |
82 sink(sinklog, type = "message") | |
83 } | |
84 | |
85 if (settingsType == "tumor") { | |
86 if (signal=="CN") { | |
87 res=markerSelection(input,dataResponse, chromosome=chrom_vec, signal=signal, normalTumorArray=tumor, onlySNP=snp, loss=loss, plot=outputfigures, nbFolds=folds, pkg=package) | |
88 } else { | |
89 res=markerSelection(input,dataResponse, chromosome=chrom_vec,signal=signal,normalTumorArray=tumor, loss=loss, plot=outputfigures, nbFolds=folds,pkg=package) | |
90 } | |
91 } else { | |
92 if (signal=="CN") { | |
93 res=markerSelection(input,dataResponse, chromosome=chrom_vec, signal=signal, onlySNP=snp, loss=loss, plot=outputfigures, nbFolds=folds,pkg=package) | |
94 } else { | |
95 res=markerSelection(input,dataResponse, chromosome=chrom_vec, signal=signal, loss=loss, plot=outputfigures, nbFolds=folds,pkg=package) | |
96 } | |
97 } | |
98 | |
99 res | |
100 | |
101 df=data.frame() | |
102 list_chr=names(res) | |
103 markerSelected=FALSE | |
104 | |
105 for (i in list_chr) { | |
106 chr_data=res[[i]] | |
107 len=length(chr_data$markers.index) | |
108 if (len != 0) | |
109 { | |
110 markerSelected=TRUE | |
111 chrdf=data.frame(rep(i,len),chr_data$markers.position,chr_data$markers.index,chr_data$markers.names,chr_data$coefficient) | |
112 df=rbind(df,chrdf) | |
113 } | |
114 } | |
115 | |
116 if (outputgraph){ | |
117 file.rename(file.path(tmp_dir,"mpagenomics",user,"Rplots.pdf"), pdffigures) | |
118 } | |
119 | |
120 if (outputlog){ | |
121 sink(type="output") | |
122 sink(type="message") | |
123 close(sinklog) | |
124 } | |
125 | |
126 if (markerSelected) { | |
127 colnames(df) <- c("chr","position","index","names","coefficient") | |
128 #sink(output) | |
129 #print(format(df),row.names=FALSE) | |
130 #sink() | |
131 write.table(df,output,row.names = FALSE, quote = FALSE, sep = "\t") | |
132 } else | |
133 writeLines("no SNP selected", output) | |
134 | |
135 |