comparison rdata_extractor.R @ 0:10212598b005 draft

Uploaded
author mnhn65mo
date Tue, 07 Aug 2018 06:02:29 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:10212598b005
1 #!/usr/bin/env Rscript
2 #Use a Rdata file and attributes to extract
3 #Get every argument and write a file with its values(s)
4
5
6 #get the rdata file
7 args = commandArgs(trailingOnly=TRUE)
8 rdata<-load(args[1])
9 rdata<-get(rdata)
10 sum<-summary(rdata)
11
12 #get the selected attributes to explore
13 attributes_selected <- commandArgs(trailingOnly=TRUE)[2]
14 attributes<-strsplit(attributes_selected, ",") #List of elements
15
16 write.table(sum,file = "summary.tabular",sep='\t',row.names=FALSE)
17 len<-length(attributes[[1]])-1
18 bind<-tail(args,n=1)
19
20 #file type definition
21 file_ext<-function(ext){
22 file<-paste(attributes[[1]][i],ext,sep="") #Filename definition
23 file<-paste("outputs/",file,sep="")
24 return(file)
25 }
26
27 for (i in 1:len){
28 attribute<-attributes[[1]][i] #Get the attribute i
29 if(! any(names(rdata)==attribute)){
30 error<-paste(attribute, " doesn't exist in the RData. Check the inputs files")
31 write(error, stderr())
32 }
33
34 attribute_val<-eval(parse(text=paste("rdata$",attribute,sep=""))) #Extract the value(s)
35
36 if(is.null(attribute_val)){ #Galaxy can't produce output if NULL
37 file<-file_ext(".txt")
38 write("Return NULL value",file=file)
39 next #Exit loop
40 }
41
42 if (typeof(attribute_val)=="list"){ #Need to be corrected, fail in galaxy but not in R
43 if(length(attribute_val)=="0"){
44 file<-file_ext(".txt")
45 sink(file=file)
46 print("Empty list :") #If the list is empty without element, file is empty and an error occur in galaxy
47 print(attribute_val)
48 sink()
49 next
50 }else{
51 attribute_val<-as.data.frame(do.call(rbind, attribute_val))
52 file<-file_ext(".tabular")
53 write.table(attribute_val,file=file,row.names=FALSE)
54 next
55 }
56 }else if (typeof(attribute_val)=="language"){ #OK
57 attribute_val<-toString(attribute_val,width = NULL)
58 file<-file_ext(".txt")
59 write(attribute_val,file=file)
60 next
61 }
62 file<-file_ext(".tabular")
63 dataframe<-as.data.frame(attribute_val)
64 names(dataframe)<-attribute
65 if(bind=="nobind"){
66 write.table(dataframe,file=file,row.names=FALSE,sep=" ")
67 }else{
68 if(!exists("alldataframe")){
69 alldataframe<-dataframe
70 }else{
71 alldataframe<-cbind(alldataframe, dataframe)
72 }
73 }
74 }
75
76 if(exists("alldataframe")&&bind=="bind"){
77 write.table(alldataframe,file=file,row.names=FALSE,sep=" ")
78 }
79 q('no')