comparison extractCN.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 3fcbb8030fcc
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("--settings_type",type="character",default=NULL, dest="settings_type"),
17 make_option("--settings_tumor",type="character",default=NULL, dest="settings_tumor"),
18 make_option("--symmetrize",type="character",default=NULL, dest="symmetrize"),
19 make_option("--settings_signal",type="character",default=NULL, dest="settings_signal"),
20 make_option("--settings_snp",type="character",default=NULL, dest="settings_snp"),
21 make_option("--outputlog",type="character",default=NULL, dest="outputlog"),
22 make_option("--log",type="character",default=NULL, dest="log"),
23 make_option("--userid",type="character",default=NULL, dest="userid")
24 );
25
26 opt_parser = OptionParser(option_list=option_list);
27 opt = parse_args(opt_parser);
28
29 if(is.null(opt$input)){
30 print_help(opt_parser)
31 stop("input required.", call.=FALSE)
32 }
33
34 #loading libraries
35
36 chrom=opt$chrom
37 input=opt$input
38 tmp_dir=opt$new_file_path
39 output=opt$output
40 settingsType=opt$settings_type
41 tumorcsv=opt$settings_tumor
42 symmetrize=opt$symmetrize
43 signal=opt$settings_signal
44 snp=type.convert(opt$settings_snp)
45 outputlog=opt$outputlog
46 log=opt$log
47 user=opt$userid
48
49 library(MPAgenomics)
50 workdir=file.path(tmp_dir, "mpagenomics",user)
51 setwd(workdir)
52
53 inputDataset=read.table(file=input,stringsAsFactors=FALSE)
54 dataset=inputDataset[1,2]
55
56 if (outputlog){
57 sinklog <- file(log, open = "wt")
58 sink(sinklog ,type = "output")
59 sink(sinklog, type = "message")
60 }
61
62
63 if (grepl("all",tolower(chrom)) | chrom=="None") {
64 chrom_vec=c(1:25)
65 } else {
66 chrom_tmp <- strsplit(chrom,",")
67 chrom_vecstring <-unlist(chrom_tmp)
68 chrom_vec <- as.numeric(chrom_vecstring)
69 }
70 if (signal == "CN")
71 {
72 if (settingsType == "dataset") {
73 if (tumorcsv== "None")
74 {
75 CN=getCopyNumberSignal(dataset,chromosome=chrom_vec, onlySNP=snp)
76
77 } else {
78 CN=getCopyNumberSignal(dataset,chromosome=chrom_vec, normalTumorArray=tumorcsv, onlySNP=snp)
79 }
80 } else {
81 input_tmp <- strsplit(settingsType,",")
82 input_tmp_vecstring <-unlist(input_tmp)
83 input_vecstring = sub("^([^.]*).*", "\\1", input_tmp_vecstring)
84 if (tumorcsv== "None")
85 {
86 CN=getCopyNumberSignal(dataset,chromosome=chrom_vec, listOfFiles=input_vecstring, onlySNP=snp)
87 } else {
88 CN=getCopyNumberSignal(dataset,chromosome=chrom_vec, normalTumorArray=tumorcsv, listOfFiles=input_vecstring, onlySNP=snp )
89 }
90 }
91
92 list_chr=names(CN)
93 CN_global=data.frame(check.names = FALSE)
94 for (i in list_chr) {
95 chr_data=data.frame(CN[[i]],check.names = FALSE)
96 CN_global=rbind(CN_global,chr_data)
97 }
98 names(CN_global)[names(CN_global)=="featureNames"] <- "probeName"
99 write.table(format(CN_global), output, row.names = FALSE, quote = FALSE, sep = "\t")
100
101 } else {
102 if (symmetrize=="TRUE") {
103 if (settingsType == "dataset") {
104 input_vecstring = getListOfFiles(dataset)
105 } else {
106 input_tmp <- strsplit(settingsType,",")
107 input_tmp_vecstring <-unlist(input_tmp)
108 input_vecstring = sub("^([^.]*).*", "\\1", input_tmp_vecstring)
109 }
110
111 symFracB_global=data.frame(check.names = FALSE)
112
113 for (currentFile in input_vecstring) {
114 cat(paste0("extracting signal from ",currentFile,".\n"))
115 currentSymFracB=data.frame()
116 symFracB=getSymFracBSignal(dataset,chromosome=chrom_vec,file=currentFile,normalTumorArray=tumorcsv)
117 list_chr=names(symFracB)
118 for (i in list_chr) {
119 cat(paste0(" extracting ",i,".\n"))
120 chr_data=data.frame(symFracB[[i]]$tumor,check.names = FALSE)
121 currentSymFracB=rbind(currentSymFracB,chr_data)
122
123 }
124 if (is.null(symFracB_global) || nrow(symFracB_global)==0) {
125 symFracB_global=currentSymFracB
126 } else {
127 symFracB_global=cbind(symFracB_global,currentFile=currentSymFracB[[3]])
128 }
129 }
130 names(symFracB_global)[names(symFracB_global)=="featureNames"] <- "probeName"
131
132 write.table(format(symFracB_global), output, row.names = FALSE, quote = FALSE, sep = "\t")
133 } else {
134 if (settingsType == "dataset") {
135 if (tumorcsv== "None")
136 {
137 fracB=getFracBSignal(dataset,chromosome=chrom_vec)
138
139 } else {
140 fracB=getFracBSignal(dataset,chromosome=chrom_vec, normalTumorArray=tumorcsv)
141 }
142 } else {
143 input_tmp <- strsplit(settingsType,",")
144 input_tmp_vecstring <-unlist(input_tmp)
145 input_vecstring = sub("^([^.]*).*", "\\1", input_tmp_vecstring)
146 if (tumorcsv== "None")
147 {
148 fracB=getFracBSignal(dataset,chromosome=chrom_vec, listOfFiles=input_vecstring)
149 } else {
150 fracB=getFracBSignal(dataset,chromosome=chrom_vec, normalTumorArray=tumorcsv, listOfFiles=input_vecstring)
151 }
152 }
153 #formatage des données
154 list_chr=names(fracB)
155 fracB_global=data.frame(check.names = FALSE)
156 for (i in list_chr) {
157 chr_data=data.frame(fracB[[i]]$tumor,check.names = FALSE)
158 fracB_global=rbind(fracB_global,chr_data)
159 }
160 names(fracB_global)[names(fracB_global)=="featureNames"] <- "probeName"
161 write.table(format(fracB_global), output, row.names = FALSE, quote = FALSE, sep = "\t")
162 }
163
164 }
165
166 if (outputlog){
167 sink(type="output")
168 sink(type="message")
169 close(sinklog)
170 }