File: getAE.r

package info (click to toggle)
r-bioc-arrayexpress 1.66.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 460 kB
  • sloc: makefile: 2
file content (215 lines) | stat: -rw-r--r-- 6,473 bytes parent folder | download
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
get_request <-function(url){
  qr = URLencode(url)
  response <- GET( qr )
  if(status_code(response) != 200) {
    stop(
      paste(
        "Error running query. Received HTTP error code",
        status_code(response),
        "from server. Please try again later. If you continue to experience 
        problems please contact us 
        at https://www.ebi.ac.uk/about/contact/support/arrayexpress"
      )
    )
  }
  
  json_parsed <- fromJSON(txt = qr) 
  return(json_parsed)
}

download_file <- function(url, filedest, overwrite){
  if (file.exists(filedest) && overwrite == FALSE){
    message(paste(filedest, ' already exists, skipping.', sep =""))
    retVal <- filedest
  }
  else{
    dnld = try(download.file(url, filedest))
    if(inherits(dnld, 'try-error') || file.info(filedest)$size == 0) {
      retVal <- NULL
    } else  {
      retVal <- filedest
    }    
  }
  return(retVal)
}


get_filenames <-function(jsonData, ftpUrl, type){
  subsections = jsonData$section$subsections
  fileNames = data.frame(matrix(ncol = 3, nrow = 0))
  colnames(fileNames) <- c("type", "file", "url")
  for (s in subsections) {
    if (s$type[1] == "Assays and Data"){
      data_files = s$subsections
      break
    }
  }
  
  for (row in 1:nrow(data_files)){
    file_type = data_files[row, "type"]
    files=data_files[row, "files"]
    for (i in files){
      if (is.data.frame(i)){
        df=i
      }
      else{
        for (n in i){
          df=n
        }
      }    
    }
    
    for(f in df$path){
      if (file_type %in% c("Processed Data","MAGE-TAB Files", "Raw Data")) {
        fileNames[nrow(fileNames) + 1,] = c(file_type, f, paste(ftpUrl,"Files",f,sep="/"))
      }
    }
    if (file_type == "Array Designs"){
      for (i in data_files[row, "links"]){
        for (n in i){
          adf_accession = n$url
          if(!is_empty(adf_accession)){
            adf_name = paste(adf_accession, "adf","txt", sep=".")
            adf_link = paste("https://www.ebi.ac.uk/biostudies/files", adf_accession, adf_name, sep="/")
            fileNames[nrow(fileNames) + 1,] = c(file_type, adf_name, adf_link)
          }
        }
      }
    }
  }
  
  if (tolower(type)=='raw' || tolower(type) == 'full'){
    enaAccession <- NULL
    jsonLinks = unlist(jsonData$section$links)
    if(!is_empty(jsonLinks)){
      if (toupper(jsonLinks['attributes.value']) == 'ENA'){
        enaAccession = jsonLinks['url']
      }     
    }
    if (!is_empty(enaAccession)){
      enaUrl=paste("https://www.ebi.ac.uk/ena/portal/api/filereport?accession=",enaAccession,"&download=false&fields=fastq_ftp&format=json&limit=0&result=read_run", sep="")
      enaData= get_request(enaUrl)
      for(i in 1:nrow(enaData)) { 
        ftpLikns = strsplit(enaData[i, "fastq_ftp"], split = ";")
        for (enaFileLinkList in ftpLikns){
          for (enaFileLink in enaFileLinkList){
            enaFileName = sub(".*/", "", enaFileLink)
            fileNames[nrow(fileNames) + 1,] = c("Raw Data", enaFileName, paste("ftp:", enaFileLink, sep="//"))
          }
        }
      }  
    }
    
    if (tolower(type)=='raw'){
      fileNames= fileNames[fileNames$type == "Raw Data", ]
    }
  }
  else if (tolower(type)=='processed'){
    fileNames= fileNames[fileNames$type == "Processed Data", ]
  }
  else if (tolower(type)=='mage'){
    fileNames= fileNames[fileNames$type == "MAGE-TAB Files", ]
  }
  
  
  return(fileNames)
}

getAE = function (accession, path = getwd(), type = "full", extract = TRUE, sourcedir=path, overwrite = FALSE) {
  
  typeOptions <- c("raw", "full", "processed", "mage")
  if (!tolower(type) %in% typeOptions){
    print(paste("Option'",type,"is not a valid option for parameter 'type'", sep=" "))
    return()
  }
  
  baseURL = "https://www.ebi.ac.uk/biostudies/api/v1/studies"
  httpURL = paste(baseURL, accession, sep="/")
  submissionMetaData = get_request(httpURL)
  
  infoURL = paste(baseURL, accession, "info", sep="/")
  infoData = get_request(infoURL)
  ftpLink = infoData$ftpLink
  allFileNames = get_filenames(submissionMetaData, ftpLink, type)
  
  #Download files
  rawArchive <- NULL
  processedArchive <- NULL
  mageTabFiles <- NULL
  adfFiles <- NULL
  
  for (row_file in 1:nrow(allFileNames)){
    
    filedest = paste(path,allFileNames[row_file,"file"],sep="/")
    url = allFileNames[row_file,"url"]
    message(paste("Downloading file: ", allFileNames[row_file,"file"], "\n", sep=""))
    file_ok <- download_file(url, filedest, overwrite)
    
    if (is.null(file_ok)){
      warning(paste(url, " does not exist or is empty. \n"),sep="")
    }
    else{
      if (allFileNames[row_file,"type"] == 'Raw Data'){
        rawArchive <- c(rawArchive, file_ok)
      }
      else if(allFileNames[row_file,"type"] == 'MAGE-TAB Files'){
        
        mageTabFiles <- c(mageTabFiles, file_ok)         
      }
      else if(allFileNames[row_file,"type"] == 'Processed Data'){
        processedArchive <- c(processedArchive, file_ok)
      }
      else if(allFileNames[row_file,"type"] == 'Array Designs'){
        adfFiles <- c(adfFiles, file_ok)          
      }
    }
  }
  
  rawFiles = NULL
  processedFiles = NULL
  
  if(extract){
    message("Unpacking data files")
    if(!is.null(rawArchive))
      rawFiles<-lapply(rawArchive, function(zipfile){
        rFiles = zipfile
        if (file_ext(zipfile) == "zip"){
          rFiles = extract.zip(file = zipfile)
        }
        return(rFiles)
      })
    if(!is.null(processedArchive))
      processedFiles<-lapply(processedArchive, function(zipfile){
        procFiles = zipfile
        if (file_ext(zipfile) == "zip"){
          procFiles = extract.zip(file = zipfile)
        }
        return(procFiles)
      })
    
    if(!is.null(rawFiles))
      rawFiles = unlist(rawFiles)
    if(!is.null(processedFiles))
      processedFiles = unlist(processedFiles)
  }
  
  sdrfFile <- NULL
  idfFile <- NULL
  
  if (!is_empty(mageTabFiles)){
    sdrfFile = mageTabFiles[grep(mageTabFiles, pattern = "sdrf.txt$")]
    idfFile = mageTabFiles[grep(mageTabFiles, pattern = "idf.txt$")]
  }
  
  res = list(path = path, 
             rawFiles = rawFiles,
             rawArchive = rawArchive,
             processedFiles = processedFiles,
             processedArchive = processedArchive,
             mageTabFiles = mageTabFiles, 
             sdrf = sdrfFile,
             idf = idfFile,
             adf = adfFiles,
             dataFiles = allFileNames)
  return(res)
}