annotate rdata_extractor.R @ 1:5184899fd06e draft default tip

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