-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileformat.R
132 lines (121 loc) · 4.28 KB
/
fileformat.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
library(rjson)
library(flowCore)
##text file rules:
#fields are sepearted by \t
#each line contains the same number records
#one row for the columns names, the colum names has less than 20 characters
#the rest of them are real numbers, at least two rows for the data
args<-commandArgs(TRUE)
input<-fromJSON(args[1])
processinput<-function(input)
{
input<-input$input
output<-list()
output$input<-input
output$status<-"failure"
output$fileformat<-"non-fcsfile"
emptyheader<-FALSE
idir<-input$data$filedir
if(!file.exists(idir)){
output$message<-"the input dir does not exist"
return(output)
}
ifile<-file.path(idir,input$data$filename)
if(!file.exists(ifile)){
output$message<-"The input file does not exist"
return(output)
}
odir<-input$output$dir
if(!file.exists(odir)){
output$message<-"the output dir does not exist"
return(output)
}
if(isFCSfile(ifile)){
output$fileformat<-"fcsfile"
x<-read.FCS(ifile,which.lines=c(1,2))
output$colnames<-as.character(colnames(x@exprs))
}else{
##process supposed text file
header<-scan(ifile,"",sep="\t",nlines=1,
strip.white=TRUE,quiet=TRUE,na.strings="")
if(sum(nchar(header)>20)>0){
output$message<-"Some column names have more than 20 characters\n"
return(output)
}else if(sum(nchar(header)==0)>0){
output$message<-"Some column names have zero characters\n"
return(output)
}else if(length(unique(header))!=length(header)){
output$message<-"Some column names are not unique\n"
}else if ( sum(!is.na(suppressWarnings(as.numeric(header))))==length(header) ){
##warning, missing header
header<-paste("column",1:length(header))
emptyheader<-TRUE
}
line1<-scan(ifile,"",sep="\t",nlines=1,skip=1-emptyheader,quiet=TRUE)
line2<-scan(ifile,"",,sep="\t",nlines=1,skip=2-emptyheader,quiet=TRUE)
if((length(header)!=length(line1))||
(length(line2)!=length(line1))){
output$message<-"the file has an unequal number of fields for the first three lines\n"
return(output)
}else{
number1<-as.numeric(line1)
number2<-as.numeric(line2)
if(sum(is.na(number1)>0)|| sum(is.na(number2)>0)){
output$message<-"The second and third lines of the file contain no-numerical entries\n"
return(output)
}
}
output$colnames<-header
}
if(input$mode=="colnames"){
output$status<-"success"
return(output)
}
ofile<-file.path(odir,"data.txt")
if(output$fileformat=="fcsfile"){
x<-read.FCS(ifile)
if(input$mode=="checkall"){
output$numberofevents<-nrow(x@exprs)
output$status<-"success"
return(output)
}else{
write.table(x@exprs,ofile,row.names=FALSE,col.names=TRUE,
sep="\t",quote=FALSE)
output$info<-list(filedir=odir,txtfile="data.txt")
output$status<-"success"
return(output)
}
}else{
x<-read.delim(ifile,sep="\t",colClass="numeric",skip=1-emptyheader,header=FALSE)
colnames(x)<-output$colnames
##first check
if(sum(is.na(x))>0){
output$message<-"There are entries in your that can not be converted to numbers\n"
return(output)
}
if(input$mode=="checkall"){
output$numberofevents<-nrow(x)
output$status<-"success"
return(output)
}else{
write.table(x,ofile,row.names=FALSE,col.names=TRUE,
sep="\t",quote=FALSE)
output$info<-list(filedir=odir,txtfile="data.txt")
output$status<-"success"
return(output)
}
}
output
}
cat("arrives just right before try\n",file=stderr())
output<-try(processinput(input))
if(class(output)=="try-error"){
cat("error in try goes to this\n",file=stderr())
output<-list()
output$input<-input
output$status<-"failure"
output$message<-geterrmessage()
}
output<-list(output=output)
outputstr<-toJSON(output)
write(outputstr,"")