extractAnnotations.R 52.1 KB
Newer Older
Emma Schymanski's avatar
Emma Schymanski committed
1
2
3
4
5
6
7
8
# Functions for Extracting Annotations Data from PubChem
# Established to get Metabolites/Metabolism Information from HSDB (and others)
# As yet not thoroughly tested on other annotation sources or types
# Emma Schymanski, 1 May 2020 (based off earlier files from late Apr. 2020)
# In collaboration with PubChem (Evan Bolton, Paul Thiessen, Jeff Zhang)

#depends
library(RChemMass)
Emma Schymanski's avatar
Emma Schymanski committed
9
#setwd("C:/DATA/PubChem/")
Emma Schymanski's avatar
Emma Schymanski committed
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


#' Retrieve Number of Pages for Annotation JSON file
#' 
#' Retrieves the number of pages for annotations files. As this requires
#' retrieving the entire JSON file (up to 1000 entries), this can take 
#' some time (hence large default timeout). Thanks to Paul Thiessen 
#' and Evan Bolton from PubChem team for assistance. 
#' 
#' @usage getPcAnno.TotalPages(source, heading, timeout=100)
#' 
#' @param source Data source name of the annotations data, e.g. \code{"HSDB"}
#' @param heading Annotation category desired, e.g. \code{"Metabolism/Metabolites"}
#' @param timeout The timeout, in seconds.  
#' @return The number of pages
#' 
#' @details 
#' This is primarily a supporter function for retrieving full annotation data
#' 
#' @author Emma Schymanski <emma.schymanski@@uni.lu>
#' 
#' @references 
#' PubChem: \url{http://pubchem.ncbi.nlm.nih.gov/} 
#' 
#' PubChem Data Sources (with annotations):
#' \url{https://pubchem.ncbi.nlm.nih.gov/source/#type=Annotations}
#' 
#' @seealso \code{\link{getMSInfo.files}}
#' 
#' @examples
#' n_pages <- getPcAnno.TotalPages('HSDB','Metabolism/Metabolites')
Emma Schymanski's avatar
Emma Schymanski committed
41
#' n_pages <- getPcAnno.TotalPages("NORMAN Suspect List Exchange","Transformations")
Emma Schymanski's avatar
Emma Schymanski committed
42
43
44
45
46
47
48
49
#' 
#' @export
getPcAnno.TotalPages <- function(source, heading, timeout=100) {
  # set URL
  baseURL <- 'https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/annotations/heading/JSON/?source='
  midURL <- '&heading='
  endURL <- '&heading_type=Compound&page=1'
  url <- paste0(baseURL, source, midURL, heading, endURL)
Emma Schymanski's avatar
Emma Schymanski committed
50
51
  #Example:
  #https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/annotations/heading/JSON/?source=NORMAN%20Suspect%20List%20Exchange&heading_type=Compound&heading=Transformations&page=1
Emma Schymanski's avatar
Emma Schymanski committed
52
53
54
55
56
  
  errorvar <- 0
  currEnvir <- environment()
  
  tryCatch(
Emma Schymanski's avatar
Emma Schymanski committed
57
58
59
60
61
    #data <- getURL(URLencode(url),timeout=timeout),
    {
      res <- GET(URLencode(url))
      data <- httr::content(res, type="text", encoding="UTF-8")
    },
Emma Schymanski's avatar
Emma Schymanski committed
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
    error=function(e){
      currEnvir$errorvar <- 1
    })
  
  if(errorvar){
    return(NA)
  }
  
  r <- fromJSON(data)
  
  if(!is.null(r$Fault)) {
    return(NA)
  }
  
  n_pages <- r$Annotations$TotalPages
  return(n_pages)
  
}


#' Retrieve Annotation Details from Metabolism/Metabolites Sections
#' 
#' Retrieve the annotation information from the Metabolism/Metabolites 
#' section of data sources, per page. As this requires
#' retrieving the entire JSON file (up to 1000 entries), this can take 
#' some time (hence large default timeout). Thanks to Paul Thiessen 
#' and Evan Bolton from PubChem team for assistance. 
#' 
#' @usage getPcAnno.Metabolism(source, heading, page=1, file_name="", timeout=100)
#' 
#' @param source Data source name of the annotations data, e.g. \code{"HSDB"}
#' @param heading Annotation category desired, e.g. \code{"Metabolism/Metabolites"}
#' @param page Page number to retrieve. Use \link{getPcAnno.TotalPages} to 
#' determine page numbers available
#' @param file_name If empty, \code{"heading_source_pageX.csv"} is created, 
#' overwriting any existing file of the same name in the directory. 
#' @param timeout The timeout, in seconds. Should be generous as these are large files
#' @return file_name of a saved multi-column CSV containing Source Name, Source ID, 
#' Source Chemical Name, Reference text, Description text and 
#' pipe-separated CIDs of the metabolites (\code{tp_cids}).
#' 
#' @details 
#' This function may work for other annotation headings, but has not been 
#' tested beyond Metabolism/Metabolites for HSDB.
#' 
#' @author Emma Schymanski <emma.schymanski@@uni.lu>
#' 
#' @references 
#' PubChem: \url{http://pubchem.ncbi.nlm.nih.gov/} 
#' 
#' PubChem Data Sources (with annotations):
#' \url{https://pubchem.ncbi.nlm.nih.gov/source/#type=Annotations}
#' 
#' @seealso \code{\link{getPcAnno.TotalPages}}, \code{\link{getPcAnno.SourceCIDs}}
#' 
#' @examples
#' # This retrieves only the first page
#' getPcAnno.Metabolism(source = 'HSDB',heading = 'Metabolism/Metabolites')
#' # This will retrieve all (in single files)
#' n_pages <- getPcAnno.TotalPages('HSDB','Metabolism/Metabolites')
#' for (i in 1:n_pages) {
#'   getPcAnno.Metabolism(source = 'HSDB',heading = 'Metabolism/Metabolites', page=i)
#' }
#' # merge together
#' HSDB_data <- read.csv("Metabolism_Metabolites_HSDB_page1.csv")
#' HSDB_data2 <- read.csv("Metabolism_Metabolites_HSDB_page2.csv")
#' HSDB_data_all <- merge(HSDB_data,HSDB_data2,all.x=TRUE,all.y=TRUE)
#' 
#' for (i in 3:n_pages) {
#'     file_name <- paste0("Metabolism_Metabolites_HSDB_page",i,".csv")
#'     HSDB_data2 <- read.csv(file_name)
#'     HSDB_data_all <- merge(HSDB_data_all,HSDB_data2,all.x=TRUE,all.y=TRUE)
#'     }
#' 
#' @export
getPcAnno.Metabolism <- function(source, heading, page=1, file_name="", timeout=100) {
  # set file_name if not set...
  if (nchar(file_name) < 4) {
    file_name <- paste0(sub("/","_",heading), "_", source, "_page", page,".csv")
  }
  # set URL
  baseURL <- 'https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/annotations/heading/JSON/?source='
  #source <- 'HSDB'
  midURL <- '&heading='
  #heading <- 'Metabolism/Metabolites'
  #endURL <- '&heading_type=Compound&page=1&response_type=display'
  endURL <- '&heading_type=Compound&page='
  url <- paste0(baseURL, source, midURL, heading, endURL, page)
  
  errorvar <- 0
  currEnvir <- environment()
  
  tryCatch(
Emma Schymanski's avatar
Emma Schymanski committed
155
156
157
158
159
    #data <- getURL(URLencode(url),timeout=timeout),
    {
      res <- GET(URLencode(url))
      data <- httr::content(res, type="text", encoding="UTF-8")
    },
Emma Schymanski's avatar
Emma Schymanski committed
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
    error=function(e){
      currEnvir$errorvar <- 1
    })
  
  if(errorvar){
    return(NA)
  }
  
  r <- fromJSON(data)
  
  if(!is.null(r$Fault)) {
    return(NA)
  }
  
  n_annos <- length(r$Annotations$Annotation) #1000 per page
  source_name <- vector(mode="character",length=n_annos)
  source_ID <- vector(mode="character",length=n_annos)
  source_chemical_name <- vector(mode="character",length=n_annos)
  desc_text <- vector(mode="character",length=n_annos)
  ref_text <- vector(mode="character",length=n_annos)
  tp_cids <- vector(mode="character",length=n_annos)
  n <- 1
  
  for (i in 1:n_annos) {
    #for (i in 1:3) {
    l1 <- r$Annotations$Annotation[[i]]
    n_refs <- length(l1$Data)
    
    for (k in 1:n_refs) {
      #print source name
      source_name[n] <- l1$SourceName #HSDB
      source_ID[n] <- l1$SourceID #ID from source
      source_chemical_name[n] <- l1$Name # name of chemical from source
      #extract next layer
      l2 <- l1$Data[[k]]
      if (length(l2$Reference)>0) {
        ref_text[n] <- l2$Reference[1] # reference
      }
      # get next layer
      l3 <- l2$Value$StringWithMarkup[[1]]
      n_cids <- length(l3$Markup)
      desc_text[n] <- l3$String # description mentioning metabolites ... 
      # loop over n_cids ... 
      cids <- ""
      if (n_cids > 0) {
        for (j in 1:n_cids) {
          cid <- sub("CID-","",l3$Markup[[j]]$Extra)
          if (j == 1) {
            cids <- cid
          } else {
            cids <- paste(cids,cid,sep="|")
          }
        }
      }
      tp_cids[n] <- cids
      n <- n+1
    }
  }
  anno_data <- cbind(source_name, source_ID, source_chemical_name, ref_text, desc_text, tp_cids)
  write.csv(anno_data, file_name ,row.names = F)
  return(file_name)
}




#' Retrieve SourceID-CID mappings for HSDB
#' 
#' Retrieve the sourceID-CID mappings for HSDB. As this requires
#' retrieving the entire JSON file (up to 1000 entries), this can take 
#' some time (hence large default timeout). Thanks to Paul Thiessen 
#' and Evan Bolton from PubChem team for assistance. 
#' 
#' @usage getPcAnno.SourceCIDs(source, heading, page=1, file_name="",timeout=100)
#' 
#' @param source Data source name of the annotations data, e.g. \code{"HSDB"}
#' @param heading Heading of the category desired, e.g. 
#' \code{"Hazardous%20Substances%20DataBank%20Number"}
#' @param page Page number to retrieve. Use \link{getPcAnno.TotalPages} to 
#' determine page numbers available
#' @param file_name If empty, \code{"heading_source_pageX.csv"} is created, 
#' overwriting any existing file of the same name in the directory. 
#' @param timeout The timeout, in seconds. Should be generous as these are large files
#' @return file_name of a saved multi-column CSV containing Source Name, Source ID, 
#' Source Chemical Name, the first CID (\code{cids}), the number of CIDs 
#' (\code{n_cids}), pipe-separated CIDs of the metabolites (\code{all_cids}).
#' 
#' @details 
#' Although written more generically, this function is a special case for HSDB, 
#' unless extended to other annotation sources in the future. 
#' 
#' @author Emma Schymanski <emma.schymanski@@uni.lu>
#' 
#' @references 
#' PubChem: \url{http://pubchem.ncbi.nlm.nih.gov/} 
#' 
#' HSDB Annotations:
#' \url{https://pubchem.ncbi.nlm.nih.gov/source/11933#data=Annotations}
#' 
#' @seealso \code{\link{getPcAnno.TotalPages}}, \code{\link{getPcAnno.Metabolism}}
#' 
#' @examples
#' # This retrieves only the first page
#' getPcAnno.SourceCIDs('HSDB','Hazardous%20Substances%20DataBank%20Number')
#' # Get all data
#' n_source_pages <- getPcAnno.TotalPages('HSDB','Hazardous%20Substances%20DataBank%20Number')
#' getPcAnno.SourceCIDs('HSDB','Hazardous%20Substances%20DataBank%20Number',page=1)
#' HSDB_data <- read.csv("HazardousSubstancesDataBankNumber_HSDB_page1.csv")
#' for (i in 2:n_source_pages) {
#'   getPcAnno.SourceCIDs('HSDB','Hazardous%20Substances%20DataBank%20Number',page=i)
#' }
#' HSDB_data <- read.csv("HazardousSubstancesDataBankNumber_HSDB_page1.csv")
#' HSDB_data2 <- read.csv("HazardousSubstancesDataBankNumber_HSDB_page2.csv")
#' HSDB_data_all <- merge(HSDB_data,HSDB_data2,all.x=TRUE,all.y=TRUE)
#' for (i in 3:n_source_pages) {
#'   file_name <- paste0("HazardousSubstancesDataBankNumber_HSDB_page",i,".csv")
#'   HSDB_data2 <- read.csv(file_name)
#'   HSDB_data_all <- merge(HSDB_data_all,HSDB_data2,all.x=TRUE,all.y=TRUE)
#' }
#' 
#' write.csv(HSDB_data_all,"HazardousSubstancesDataBankNumber_HSDB_allpages.csv")
#' 
#' @export
getPcAnno.SourceCIDs <- function(source, heading, page=1, file_name="",timeout=100) {
  # set file_name if not set...
  if (nchar(file_name) < 4) {
    file_name <- paste0(gsub("%20","",heading,fixed=T), "_", source, "_page", page,".csv")
  }
  # set URL
  baseURL <- 'https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/annotations/heading/JSON/?source='
  midURL <- '&heading='
  endURL <- '&heading_type=Compound&page='
  url <- paste0(baseURL, source, midURL, heading, endURL, page)
  
  errorvar <- 0
  currEnvir <- environment()
  
  tryCatch(
Emma Schymanski's avatar
Emma Schymanski committed
298
299
300
301
302
#    data <- getURL(URLencode(url),timeout=timeout),
    {
      res <- GET(URLencode(url))
      data <- httr::content(res, type="text", encoding="UTF-8")
    },
Emma Schymanski's avatar
Emma Schymanski committed
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    error=function(e){
      currEnvir$errorvar <- 1
    })
  
  if(errorvar){
    return(NA)
  }
  
  r <- fromJSON(data)
  
  if(!is.null(r$Fault)) {
    return(NA)
  }
  
  n_annos <- length(r$Annotations$Annotation) #1000 per page
  source_name <- vector(mode="character",length=n_annos)
  source_ID <- vector(mode="character",length=n_annos)
  source_chemical_name <- vector(mode="character",length=n_annos)
  cids <- vector(mode="character",length=n_annos)
  n_cids <- vector(mode="character",length=n_annos)
  all_cids <- vector(mode="character",length=n_annos)
  
  for (i in 1:n_annos) {
    #for (i in 1:3) {
    l1 <- r$Annotations$Annotation[[i]]
    source_name[i] <- l1$SourceName #HSDB
    source_ID[i] <- l1$SourceID #ID from source
    source_chemical_name[i] <- l1$Name # name of chemical from source
    cids_entry <- l1$LinkedRecords$CID
    if (length(cids_entry)>0) {
      cids[i] <- l1$LinkedRecords$CID[1]
      n_cids[i] <- length(cids_entry)
      all_cids[i] <- paste(cids_entry,collapse="|")
    }
  }
  anno_data <- cbind(source_name, source_ID, source_chemical_name, cids, n_cids, all_cids)
  #colnames(anno_data) 
  write.csv(anno_data, file_name ,row.names = F)
  return(file_name)
  
}




#' Retrieve Metabolite CIDs from Extracted Annotation Data 
#' 
#' Retrieve the metabolite CIDs from extracted annotation data and save
#' the extracted annotation information in a CSV. Thanks to Paul Thiessen, 
#' Jeff Zhang and Evan Bolton from PubChem team for assistance. 
#' 
#' @usage getPcAnno.TPcids(cid,anno_csv_name,tp_csv=TRUE)
#' 
#' @param cid PubChem CID to look up the metabolites, e.g. \code{2256}
#' @param anno_csv_name Name of the file containing the extracted annotation 
#' information, retrieved with \link{getPcAnno.TotalPages} 
#' @param tp_csv Default \code{TRUE} writes a CSV with the same name as
#' \code{anno_csv_name_CID.csv"} is created, 
#' overwriting any existing file of the same name in the directory. 
#' @return CIDs list and CSV file of exactly the same format as the input
#' file, only containing entries matching the input CID.
#' 
#' @details 
#' The files that are directly extracted do not contain the CIDs, these 
#' have to be merged. Function outstanding ... 
#' 
#' @author Emma Schymanski <emma.schymanski@@uni.lu>
#' 
#' @references 
#' PubChem: \url{http://pubchem.ncbi.nlm.nih.gov/} 
#' 
#' @seealso \code{\link{getPcAnno.Metabolism}}
#' 
#' @examples
#' #This retrieves only the first page (OK for atrazine)
#' anno_csv_name <- getPcAnno.Metabolism(source = 'HSDB',heading = 'Metabolism/Metabolites')
#' # retrieve the CIDs (saving a CSV as well)
#' getPcAnno.TPcids(2256,anno_csv_name)
#' 
#' @export
getPcAnno.TPcids <- function(cid,anno_csv_name,tp_csv=TRUE) {
  anno_data <- read.csv(anno_csv_name,stringsAsFactors = F)
  tps_index <- which(anno_data$cids==cid)
Emma Schymanski's avatar
Emma Schymanski committed
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
  if (length(tps_index)>0) {
    all_cids <- paste(anno_data$tp_cids[tps_index],collapse="|")
    #  unique_cids <- na.omit(suppressWarnings(as.numeric(unique(strsplit(all_cids,"|",fixed=T)[[1]]))))
    unique_cids <- suppressWarnings(as.numeric(unique(strsplit(all_cids,"|",fixed=T)[[1]])))
    if (tp_csv) {
      tp_file_name <- sub(".csv",paste0("_",cid,".csv"),anno_csv_name)
      anno_data <- anno_data[tps_index,]
      anno_data$selected_tp_cids <- anno_data$tp_cids
      anno_data$use <- FALSE
      anno_data$transformation <- ""
      anno_data$isPredecessor <- TRUE #most cases, the starting entry will be predecessor
      anno_data$comment <- ""
      write.csv(anno_data,tp_file_name,row.names = F)
    } else {
      tp_file_name <- NA
    }
Emma Schymanski's avatar
Emma Schymanski committed
402
403
  } else {
    tp_file_name <- NA
Emma Schymanski's avatar
Emma Schymanski committed
404
    unique_cids <- NA
Emma Schymanski's avatar
Emma Schymanski committed
405
  }
Emma Schymanski's avatar
Emma Schymanski committed
406
407
408
409
410
411
  results <- list()
  results[['TP_File_Name']] <- tp_file_name
  results[['TP_Unique_CIDs']] <- unique_cids
  return(results)
  # 
  # return(unique_cids)
Emma Schymanski's avatar
Emma Schymanski committed
412
413
414
}


Emma Schymanski's avatar
Emma Schymanski committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#' Retrieve CIDs with Transformations Entries (by page)
#' 
#' Retrieve the CIDs with Transformations entries from the 
#' annotation information at the Transformation 
#' section of data sources, per page. As this requires
#' retrieving the entire JSON file (up to 1000 entries), this can take 
#' some time (hence large default timeout). Thanks to Paul Thiessen, 
#' Jeff Zhang and Evan Bolton from PubChem team for assistance. 
#' 
#' @usage getPcAnno.Transformations(source, heading, page=1, file_name="", timeout=100)
#' 
#' @param source Data source name of the annotations data, e.g. \code{"NORMAN Suspect List Exchange"}
#' @param heading Annotation category desired, e.g. \code{"Transformations"}
#' @param page Page number to retrieve. Use \link{getPcAnno.TotalPages} to 
#' determine page numbers available
#' @param timeout The timeout, in seconds. Should be generous as these are large files
#' @return The CIDs mentioned in the record.
#' 
#' @details 
#' This function will only extract CIDs and thus probably only works for
#' Transformations sections (and/or any other section where only CIDs are
#' needed)
#' 
#' @author Emma Schymanski <emma.schymanski@@uni.lu>
#' 
#' @references 
#' PubChem: \url{http://pubchem.ncbi.nlm.nih.gov/} 
#' 
#' PubChem Data Sources (with annotations):
#' \url{https://pubchem.ncbi.nlm.nih.gov/source/#type=Annotations}
#' 
#' Transformations Example: 
#' \url{https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/annotations/heading/JSON/?source=NORMAN%20Suspect%20List%20Exchange&heading_type=Compound&heading=Transformations&page=1}
#' 
#' @seealso \code{\link{getPcAnno.TotalPages}}, \code{\link{getPcAnno.Metabolism}}
#' 
#' @examples
#' # This retrieves only the CIDs from the first page
#' getPcAnno.Transformations(source = 'NORMAN Suspect List Exchange',heading = 'Transformations')
#' # This will retrieve all as a list of CIDs
#' source <- "NORMAN Suspect List Exchange"
#' heading <- "Transformations"
#' n_pages <- getPcAnno.TotalPages(source, heading)
#' for (i in 1:n_pages) {
#'   if (i == 1) {
#'     cids <- getPcAnno.Transformations(source, heading, page=i)
#'   } else {
#'     cids_px <- getPcAnno.Transformations(source, heading, page=i)
#'     cids <- c(cids,cids_px)
#'   }
#' }
#' # May 2021 this was 4730 CIDs
#' 
#' @export
getPcAnno.Transformations <- function(source, heading, page=1, timeout=100) {
  # set URL
  baseURL <- 'https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/annotations/heading/JSON/?source='
  #source <- 'HSDB'
  midURL <- '&heading='
  #heading <- 'Metabolism/Metabolites'
  #endURL <- '&heading_type=Compound&page=1&response_type=display'
  endURL <- '&heading_type=Compound&page='
  url <- paste0(baseURL, source, midURL, heading, endURL, page)
  
  errorvar <- 0
  currEnvir <- environment()
  
  tryCatch(
    #data <- getURL(URLencode(url),timeout=timeout),
    {
      res <- GET(URLencode(url))
      data <- httr::content(res, type="text", encoding="UTF-8")
    },
    error=function(e){
      currEnvir$errorvar <- 1
    })
  
  if(errorvar){
    return(NA)
  }
  
  r <- fromJSON(data)
  
  if(!is.null(r$Fault)) {
    return(NA)
  }
  
  n_annos <- length(r$Annotations$Annotation) #1000 per page
  cids <- vector(mode="character",length=n_annos)
  
  for (i in 1:n_annos) {
    #for (i in 1:3) {
    l1 <- r$Annotations$Annotation[[i]]
    cid <- l1$LinkedRecords$CID
    if (is.null(cid)) {
      cids[i] <- NA
    } else {
      cids[i] <- l1$LinkedRecords$CID
    }
  }
  
  return(cids)
}


Emma Schymanski's avatar
Emma Schymanski committed
520
521
522
523
524
525
526

#' Retrieve Transformations Table from PubChem Transformation Sections
#' 
#' Retrieve the transformation table (CSV) from the Transformation
#' section of individual compounds, by CID. Thanks to Jeff Zhang, Paul Thiessen 
#' and Evan Bolton from PubChem team for assistance. 
#' 
Emma Schymanski's avatar
Emma Schymanski committed
527
#' @usage getPcCand.trans(query, dataset="transformations",file_name="")
Emma Schymanski's avatar
Emma Schymanski committed
528
529
530
#' 
#' @param query CID to retrieve the transformation table, e.g. \code{2256}
#' @param dataset The name of the table behind the scenes, 
Emma Schymanski's avatar
Emma Schymanski committed
531
#' currently \code{"transformations"}. Other values will likely not work. 
Emma Schymanski's avatar
Emma Schymanski committed
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#' @param file_name If empty, \code{"CID_query_dataset.csv"} is created, 
#' overwriting any existing file of the same name in the directory. 
#' @return file_name of a saved multi-column CSV containing the transformation
#' information. 
#' 
#' @details 
#' NOTE: The transformations section is under development and this function may
#' break unexpectedly at any point. Changes in format are also possible.
#' 
#' @author Emma Schymanski <emma.schymanski@@uni.lu>
#' 
#' @references 
#' PubChem: \url{http://pubchem.ncbi.nlm.nih.gov/} 
#' 
#' Example PubChem Data Source (with transformations):
#' \url{https://pubchem.ncbi.nlm.nih.gov/source/23819#data=Annotations}
#' 
#' Example Transformations entry: 
#' \url{https://pubchem.ncbi.nlm.nih.gov/compound/2256#section=Transformations}
#' 
#' @examples
#' getPcCand.trans(1017)
#' getPcCand.trans(2256)
#' # an entry that doesn't have any transformation entry
#' getPcCand.trans(1234567)
#' 
#' @export
Emma Schymanski's avatar
Emma Schymanski committed
559
getPcCand.trans <- function(query, dataset="transformations",file_name="") {
Emma Schymanski's avatar
Emma Schymanski committed
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
  # set file_name if not set...
  if (nchar(file_name) < 4) {
    file_name <- paste0("CID_",query,"_",dataset,".csv")
  }
  
  baseURL <- 'https://pubchem.ncbi.nlm.nih.gov/sdq/sdqagent.cgi?infmt=json&outfmt=csv&query={"download":"*","collection":"'
  #then dataset
  midURL1 <- '","where":{"ands":[{"cid":"'
  #then CID
  midURL2 <- '"}]},"order":["relevancescore,desc"],"start":1,"limit":10000000,"downloadfilename":"'
  #then file_name
  endURL <- '"}'
  url <- paste0(baseURL, dataset, midURL1, query, midURL2, file_name, endURL)
  con <- curl(url)
  content <- suppressWarnings(readLines(con,n=1))
  close(con)
  if (length(grep("Warning",content))==0) {
    download.file(url,file_name,quiet=T)
    print(paste0("CID_",query,"_",dataset,": download successful"))
Emma Schymanski's avatar
Emma Schymanski committed
579
    if (file.exists(file_name)) {
Emma Schymanski's avatar
Emma Schymanski committed
580
      tp_info <- read.csv(file_name,stringsAsFactors = F)
Emma Schymanski's avatar
Emma Schymanski committed
581
582
583
584
585
      unique_cids <- unique(c(tp_info$predecessorcid,tp_info$successorcid))
    } else {
      unique_cids <- NA
    }
    
Emma Schymanski's avatar
Emma Schymanski committed
586
587
  } else {
    print(paste0("CID_",query,"_",dataset,": ",content))
Emma Schymanski's avatar
Emma Schymanski committed
588
    unique_cids <- NA
Emma Schymanski's avatar
Emma Schymanski committed
589
  }
Emma Schymanski's avatar
Emma Schymanski committed
590
591
592
593
594
595
  
  results <- list()
  results[['TP_File_Name']] <- file_name
  results[['TP_Unique_CIDs']] <- unique_cids
  return(results)
  #return(file_name)
Emma Schymanski's avatar
Emma Schymanski committed
596
597
598
599
}



Emma Schymanski's avatar
Emma Schymanski committed
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#' Retrieve Download File from PubChem Queries (text-based search)
#' 
#' Download the results of text-based queries to PubChem. Thanks to Paul Thiessen 
#' and Evan Bolton from PubChem team for assistance. 
#' 
#' @usage getPcCand.query(query,file_name="")
#' 
#' @param query text-based query to search, e.g. \code{"coronavirus"}
#' @param file_name If empty, \code{"PubChem_Candidates_query.csv"} is created, 
#' overwriting any existing file of the same name in the directory. 
#' @return file_name of the multi-column CSV download file containing 
#' the search results. 
#' 
#' @details 
#' NOTE: This was designed to work off small queries and could download
#' huge files for large query terms...this has not been tested.
#' 
#' @author Emma Schymanski <emma.schymanski@@uni.lu>
#' 
#' @references 
#' PubChem: \url{http://pubchem.ncbi.nlm.nih.gov/} 
#' 
#' Example PubChem Text-Based Query:
#' \url{https://pubchem.ncbi.nlm.nih.gov/#query=coronavirus}
#' 
#' @examples
#' getPcCand.query("coronavirus","PC_CoronaQuery.csv")
#' 
#' @export
getPcCand.query <- function(query, file_name="") {
  # set file_name if not set...
  if (nchar(file_name) < 4) {
    file_name <- paste0("PubChem_Candidates_",query,".csv")
  }
  
  baseURL <- 'https://pubchem.ncbi.nlm.nih.gov/sdq/sdqagent.cgi?query={"download":"*","collection":"compound","where":{"ands":{"*":"'
  endURL <- '"}},"start":1,"limit":100000}'
  url <- paste0(baseURL, query, endURL)
  download.file(url,file_name)
  return(file_name)
}


Emma Schymanski's avatar
Emma Schymanski committed
643
644
645
646
647
648
#' Retrieve Information for Mass Spectral Data Processing from CIDs
#' 
#' Retrieve various pieces of information for mass spectral screening, 
#' by CID, via parent CID mappings. Thanks to Paul Thiessen 
#' and Evan Bolton from PubChem team for assistance. 
#' 
Emma Schymanski's avatar
Emma Schymanski committed
649
#' @usage getMSInfo.cid(cid,useParent=TRUE)
Emma Schymanski's avatar
Emma Schymanski committed
650
651
#' 
#' @param cid CID to retrieve information
Emma Schymanski's avatar
Emma Schymanski committed
652
653
#' @param useParent Default \code{TRUE} searches and returns info for
#' parent CID. If \code{FALSE}, retrieves on \code{cid}.
Emma Schymanski's avatar
Emma Schymanski committed
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#' @return list containing the mass spectral screening information. 
#' 
#' @details 
#' This currently relies on functionality in RChemMass. 
#' 
#' @author Emma Schymanski <emma.schymanski@@uni.lu>
#' 
#' @references 
#' PubChem: \url{http://pubchem.ncbi.nlm.nih.gov/} 
#' 
#' @seealso \code{\link{getMSInfo.files}} \code{\link{getMSInfo.cids}}
#' 
#' @examples
#' getMSInfo.cid(2256)
#' # generates NAs when invalid CIDs are given
#' getMSInfo.cid("blah")
#' 
#' @export
Emma Schymanski's avatar
Emma Schymanski committed
672
getMSInfo.cid <- function(cid,useParent=TRUE) {
Emma Schymanski's avatar
Emma Schymanski committed
673
  Input_CID <- cid
Emma Schymanski's avatar
Emma Schymanski committed
674
675
676
677
678
679
  if (useParent) {
    Parent_CID <- getPCIDs.CIDtype(cid,type = "parent",timeout=60)
  } else {
    Parent_CID <- cid
  }
  
Emma Schymanski's avatar
Emma Schymanski committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
  # get properties
  extra_info <- getPCproperty.MF(Parent_CID)
  name_info <- getPCdesc.title(Parent_CID)
  # fill in the gaps
  Name <- name_info$Title
  IUPAC_Name <- extra_info$IUPACName
  SMILES <- extra_info$IsomericSMILES
  InChI <- extra_info$InChI
  InChIKey <- extra_info$InChIKey
  MolecularFormula <- extra_info$MolecularFormula
  ExactMass <- extra_info$MonoisotopicMass
  
  MSInfo <- list()
  MSInfo[['Input_CID']] <- Input_CID
  MSInfo[['Parent_CID']] <- Parent_CID
  MSInfo[['Name']] <- Name
  MSInfo[['IUPAC_Name']] <- IUPAC_Name
  MSInfo[['SMILES']] <- SMILES
  MSInfo[['InChI']] <- InChI
  MSInfo[['InChIKey']] <- InChIKey
  MSInfo[['MolecularFormula']] <- MolecularFormula
  MSInfo[['ExactMass']] <- ExactMass
  return(MSInfo)
}


Emma Schymanski's avatar
Emma Schymanski committed
706

Emma Schymanski's avatar
Emma Schymanski committed
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#' Retrieve Information for Mass Spectral Data Processing from CIDs
#' 
#' Retrieve various pieces of information for mass spectral screening, 
#' by CID, via parent CID mappings. Thanks to Paul Thiessen 
#' and Evan Bolton from PubChem team for assistance. 
#' 
#' @usage getMSInfo.cids(cids,file_name)
#' 
#' @param cids Vector of CIDs to retrieve information
#' @param file_name File name to save the resulting information
#' @return file_name of the resulting multi-column CSV containing the 
#' mass spectral screening information. 
#' 
#' @details 
#' NOTE: This function goes through two series of web retrievals and
#' can take a while to run (several minutes for tens of CIDs)
#' Future improvement will be to retrieve by entire list not 1 by 1
#' This currently relies on functionality in RChemMass. 
#' 
#' @author Emma Schymanski <emma.schymanski@@uni.lu>
#' 
#' @references 
#' PubChem: \url{http://pubchem.ncbi.nlm.nih.gov/} 
#' 
Emma Schymanski's avatar
Emma Schymanski committed
731
#' @seealso \code{\link{getMSInfo.files}} \code{\link{getMSInfo.cid}}
Emma Schymanski's avatar
Emma Schymanski committed
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
#' 
#' @examples
#' test_cids <- c(2256, 124886, 70292631, 136096020, 136114911, NA)
#' # the last 3 cause various problems and have been included deliberately
#' test_file <- getMSInfo.cids(test_cids,"test_MSinfo.csv")
#' 
#' @export
getMSInfo.cids <- function(cids,file_name) {
  n_cids <- length(cids)
  Input_CID <- vector(mode="character",length=n_cids)
  Parent_CID <- vector(mode="character",length=n_cids)
  Name <- vector(mode="character",length=n_cids)
  IUPAC_Name <- vector(mode="character",length=n_cids)
  SMILES <- vector(mode="character",length=n_cids)
  InChI <- vector(mode="character",length=n_cids)
  InChIKey <- vector(mode="character",length=n_cids)
  MolecularFormula <- vector(mode="character",length=n_cids)
  ExactMass <- vector(mode="character",length=n_cids)
  
  for (i in 1:n_cids) {
    # save input
    Input_CID[i] <- cids[i]
    # get parent CID
    Parent_CID[i] <- getPCIDs.CIDtype(cids[i],type = "parent",timeout=60)
    # get properties
    extra_info <- getPCproperty.MF(Parent_CID[i])
    name_info <- getPCdesc.title(Parent_CID[i])
    # fill in the gaps
    Name[i] <- name_info$Title
    IUPAC_Name[i] <- extra_info$IUPACName
    SMILES[i] <- extra_info$IsomericSMILES
    InChI[i] <- extra_info$InChI
    InChIKey[i] <- extra_info$InChIKey
    MolecularFormula[i] <- extra_info$MolecularFormula
    ExactMass[i] <- extra_info$MonoisotopicMass
  }
  MSinfo <- cbind(Input_CID, Parent_CID, Name, IUPAC_Name, SMILES, InChI, InChIKey,
                  MolecularFormula, ExactMass)
  write.csv(MSinfo, file_name ,row.names = F)
  return(file_name)
}



#' Format getMSInfo.cids files to input files for Shinyscreen
#' 
#' Format the getMSInfo.cids output to create input files for 
#' Shinyscreen (compound list, set list) plus a suspect file. 
#' 
#' @usage getMSInfo.files(file_name, set_id,min_mass=50,id_offset=0)
#' 
#' @param file_name File name from \code{getMSInfo.cids}
#' @param set_id The name of the set ID for Shinyscreen. Keep short and 
#' informative, e.g. \code{"atrazine_tps"}
#' @param min_mass The minimum mass to include in the suspect screening. 
#' Default \code{50}; increase to \code{100} if only MS/MS is run 
#' at \code{mz=100-1000}
#' @param id_offset This can be used to adjust the ID number assigned
#' automatically (default starts at 1)
#' @return Silently returns three files named systematically off \code{file_name} 
#' 
#' @details 
#' NOTE: For maximum convenience, run this function directly in future 
#' Shinyscreen dir. 
#' 
#' @author Emma Schymanski <emma.schymanski@@uni.lu>
#' 
#' @references 
#' PubChem: \url{http://pubchem.ncbi.nlm.nih.gov/} 
#' 
#' @seealso \code{\link{getMSInfo.cids}}
#' 
#' @examples
#' test_cids <- c(2256, 124886, 70292631, 136096020, 136114911, NA)
#' # the last 3 cause various problems and have been included deliberately
#' test_file <- getMSInfo.cids(test_cids,"test_MSinfo.csv")
#' getMSInfo.files("test_MSinfo.csv",set_id="atrazine_tp_subset")
#' 
#' @export
getMSInfo.files <- function(file_name, set_id,min_mass=50,id_offset=0) {
  #this takes the output from getMSInfo.cids and creates files for Shinyscreen
  MS_info <- read.csv(file_name,stringsAsFactors = F)
  #check which are NAs - these are only ions - and remove
Emma Schymanski's avatar
Emma Schymanski committed
815
816
817
818
  check_nas <- which(is.na(MS_info$Parent_CID))
  if (length(check_nas)>0) {
    MS_info <- MS_info[-which(is.na(MS_info$Parent_CID)),]
  }
Emma Schymanski's avatar
Emma Schymanski committed
819
820
  #remove duplicates
  MS_info <- unique(MS_info)
Emma Schymanski's avatar
Emma Schymanski committed
821
  # remove duplicate parent CIDs that had different source CIDs
Emma Schymanski's avatar
Emma Schymanski committed
822
823
824
825
  dup_CID_test <- which(duplicated(MS_info$Parent_CID))
  if (length(dup_CID_test)>0) {
    MS_info <- MS_info[-which(duplicated(MS_info$Parent_CID)),]
  }
Emma Schymanski's avatar
Emma Schymanski committed
826
  #remove those below mz=min_mass ... 
Emma Schymanski's avatar
Emma Schymanski committed
827
828
829
830
  mass_test <- which(as.numeric(MS_info$ExactMass)>min_mass)
  if (length(mass_test)>0) {
    MS_info <- MS_info[mass_test,]
  }
Emma Schymanski's avatar
Emma Schymanski committed
831
  #remove any SMILES with wildcards
Emma Schymanski's avatar
Emma Schymanski committed
832
833
834
835
  star_smiles <- grep("*",MS_info$SMILES,fixed=T)
  if (length(star_smiles)>0) {
    MS_info <- MS_info[-grep("*",MS_info$SMILES,fixed=T),]
  }
Emma Schymanski's avatar
Emma Schymanski committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
  # transform into compound list for ShinyScreen
  # minimum fields ID (4-digit), Name, SMILES, RT (can be empty)
  MS_info$ID <- sequence(length(MS_info$Parent_CID))+id_offset
  MS_info$RT <- ""
  MS_info$set <- set_id
  suspect_filename <- paste0(set_id, "_SuspectList.csv")
  cmpdList_filename <- paste0(set_id, "_CmpdList.csv")
  set_filename <- paste0(set_id, "_Set.csv")
  #output CSV with all info
  write.csv(MS_info,suspect_filename,row.names = F)
  # output compound list for ShinyScreen
  #ID, Name, SMILES, RT
  write.csv(MS_info[,c(10,3,5,11)],cmpdList_filename,row.names = F)
  #create set file
  # ID, set_id
  write.csv(MS_info[,c(10,12)], set_filename,row.names = F)
}


Emma Schymanski's avatar
Emma Schymanski committed
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
# Create Transformations table entries from HSDB Directory
createTransFile.hsdb <- function(hsdb_dir, hsdb_structure_file, file_name="",
                                 hsdb_desc="", dataset_DOI="",dataset_desc="",
                                 MSInfo=FALSE) {
  # set the default output file name: 
  if (nchar(file_name) < 4) {
    file_name <- "S68_HSDBTPS_TransformationTable.csv"
  }
  # note: if MSInfo=TRUE, can go off the MSInfo file not the structure file
  
  # set the default descriptions
  if (nchar(hsdb_desc) < 4) {
    hsdb_desc <- paste0("HSDB is a toxicology database that focuses on the ",
                        "toxicology of potentially hazardous chemicals. See ",
                        "https://pubchem.ncbi.nlm.nih.gov/source/11933")
  }
  
  if (nchar(dataset_desc) < 4) {
    dataset_desc <- "Transformation Products extracted from HSDB content in PubChem, validated by LCSB-ECI"
  }
  
  if (nchar(dataset_DOI) < 4) {
    dataset_DOI <- "10.5281/zenodo.3827487"
  }
  
  # get the file listing
  hsdb_files <- list.files(hsdb_dir,pattern="selected.csv",full.names = T)
Emma Schymanski's avatar
Emma Schymanski committed
882
  hsdb_all_tp_info <- ""
Emma Schymanski's avatar
Emma Schymanski committed
883
  
Emma Schymanski's avatar
Emma Schymanski committed
884
  for (i in 1:length(hsdb_files)) {
Emma Schymanski's avatar
Emma Schymanski committed
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
    
    hsdb_selected_file <- hsdb_files[i]
    hsdb_info <- read.csv(hsdb_selected_file, stringsAsFactors = F)
    tp_cids <- getHSDBCIDs.selected(hsdb_info)
    if (length(tp_cids)>0) {
      hsdb_tp_info <- createTPinfo.hsdb(tp_cids, hsdb_selected_file, hsdb_structure_file, 
                                        hsdb_desc, dataset_DOI, dataset_desc,MSInfo)
    } else {
      #    print(paste0("No information to add for ", basename(hsdb_selected_file)))
      # run this just with the file CID, so it's on record that this was done
      hsdb_tp_info <- createTPinfo.hsdb(hsdb_info$cids[1], hsdb_selected_file, 
                                        hsdb_structure_file, 
                                        hsdb_desc, dataset_DOI, dataset_desc,MSInfo)
    }
    
    if (nchar(hsdb_all_tp_info[1]) == 0) {
      hsdb_all_tp_info <- hsdb_tp_info
    } else {
      if (length(tp_cids)>0) {
        hsdb_all_tp_info <- merge(hsdb_all_tp_info,hsdb_tp_info,all.x=T,all.y=T)
      }
    }
    
  }
  write.csv(hsdb_all_tp_info,file_name,row.names = F)
  return(file_name)
}


# This is a small helper function to get only the CIDs from 
# selected records in the manaully-checked HSDB-extracted records
getHSDBCIDs.selected <- function(hsdb_info) {
  hsdb_tp_cids <- hsdb_info$selected_tp_cids[which(hsdb_info$use==TRUE)]
  hsdb_tp_cids <- paste(hsdb_tp_cids,collapse="|")
  hsdb_tp_cids <- suppressWarnings(as.numeric(unique(strsplit(hsdb_tp_cids,"|",fixed=T)[[1]])))
  return(hsdb_tp_cids)
}
Emma Schymanski's avatar
Emma Schymanski committed
922

Emma Schymanski's avatar
Emma Schymanski committed
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938

# a helper function to create the files for the #Transformations table
# from HSDB extracted-data. Although this is was originally set up to 
# export a csv, it now exports information for a larger function. 
createTPinfo.hsdb <- function(hsdb_cids, hsdb_selected_file, hsdb_structure_file, 
                              hsdb_desc, dataset_DOI, dataset_desc,MSInfo=FALSE) {
  # read in the information
  hsdb_info <- read.csv(hsdb_selected_file,stringsAsFactors = F)
  MS_info <- read.csv(hsdb_structure_file,stringsAsFactors = F)
  #select only those that we'll use
  hsdb_info <- hsdb_info[which(hsdb_info$use==T),]
  
  n_cids <- length(hsdb_cids)
  # initiate vectors
  Input_CID <- vector(mode="character",length=n_cids)
  Parent_CID <- vector(mode="character",length=n_cids)
Emma Schymanski's avatar
Emma Schymanski committed
939
940
  Name <- vector(mode="character",length=n_cids)
  IUPAC_Name <- vector(mode="character",length=n_cids)
Emma Schymanski's avatar
Emma Schymanski committed
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
  Predecessor_CID <- vector(mode="character",length=n_cids)
  Predecessor_Name <- vector(mode="character",length=n_cids)
  Successor_CID <- vector(mode="character",length=n_cids)
  Successor_Name <- vector(mode="character",length=n_cids)
  IsPredecessor <- vector(mode="logical",length=n_cids)
  Transformation <- vector(mode="character",length=n_cids)
  Source_ID <- vector(mode="character",length=n_cids)
  Source_CID <- vector(mode="character",length=n_cids)
  Source <- vector(mode="character",length=n_cids)
  Source_Description <- vector(mode="character",length=n_cids)
  Dataset_DOI <- vector(mode="character",length=n_cids)
  Dataset_Description <- vector(mode="character",length=n_cids)
  Evidence_ID <- vector(mode="character",length=n_cids)
  Evidence_Description <- vector(mode="character",length=n_cids)
  
  # take care of the starting CID. If it's in the TP list, remove.
  # we will have to treat this as a special case
  start_cid <- hsdb_info$cids[1]
  i_start_cid <- which(hsdb_cids==start_cid)
  if (length(i_start_cid)>0) {
    hsdb_cids <- hsdb_cids[-i_start_cid]
  }
  # count the number of CIDs now starting CID is removed
  n_cids <- length(hsdb_cids)
  # start the counter, the parent goes at n=1
  n <- 1
  # set the current CID to the start CID
  curr_cid <- start_cid
  #find the index of the start CID in MSInfo
  if (MSInfo) {
    i_MSInfo <- which(MS_info$Input_CID==start_cid)
  } else {
    i_MSInfo <- which(MS_info$PubChemCID==start_cid)
  }
  # if the info doesn't exist, reretrieve, otherwise, get from MS_info
  if (length(i_MSInfo)==0) {
    cid_MSInfo <- getMSInfo.cid(curr_cid)
    Input_CID[n] <- cid_MSInfo$Input_CID
    Parent_CID[n] <- cid_MSInfo$Parent_CID
    Name[n] <- cid_MSInfo$Name
    IUPAC_Name[n] <- cid_MSInfo$IUPAC_Name
  } else {
    if (MSInfo) {
      Input_CID[n] <- MS_info$Input_CID[i_MSInfo]
      Parent_CID[n] <- MS_info$Parent_CID[i_MSInfo]
    } else {
      Input_CID[n] <- MS_info$PubChemCID[i_MSInfo]
      Parent_CID[n] <- MS_info$PubChemCID[i_MSInfo]
    }
    Name[n] <- MS_info$Name[i_MSInfo]
    IUPAC_Name[n] <- MS_info$IUPAC_Name[i_MSInfo]
  }
  Predecessor_CID[n] <- Parent_CID[n]
  Predecessor_Name[n] <- Name[n]
  Successor_CID[n] <- Parent_CID[n]
  Successor_Name[n] <- Name[n]
  IsPredecessor[n] <- NA
  Transformation[n] <- "none"
  Source_ID[n] <- hsdb_info$source_ID[1]
  Source_CID[n] <- hsdb_info$cids[1]
  Source[n] <- hsdb_info$source_name[1]
  Source_Description[n] <- hsdb_desc
  Dataset_DOI[n] <- dataset_DOI
  Dataset_Description[n] <- dataset_desc
  Evidence_ID[n] <- ""
  Evidence_Description[n] <- ""
  
  # go over the rest of the TPs
  for (i in 1:n_cids) {
    # for each CID, find out which reference(s) it is in
    curr_cid <- hsdb_cids[i]
    tp_entries <- grep(curr_cid,hsdb_info$selected_tp_cids,fixed=T)
    if (length(tp_entries)>0) {
      for (j in 1:length(tp_entries)) {
        #add one more to the counter
        n <- n+1
        # get the index of the TP in hsdb_info
        i_tp_entry <- tp_entries[j]
        # get the index of the TP in MS_info
        if (MSInfo) {
          i_MSInfo <- which(MS_info$Input_CID==curr_cid)
        } else {
          i_MSInfo <- which(MS_info$PubChemCID==curr_cid)
        }
        #i_MSInfo <- which(MS_info$Input_CID==curr_cid)
        # if the value is not found, reretrieve the info, otherwise, save
        if (length(i_MSInfo)==0) {
          cid_MSInfo <- getMSInfo.cid(curr_cid)
          Input_CID[n] <- cid_MSInfo$Input_CID
          Parent_CID[n] <- cid_MSInfo$Parent_CID
          Name[n] <- cid_MSInfo$Name
          IUPAC_Name[n] <- cid_MSInfo$IUPAC_Name
        } else {
          if (MSInfo) {
            Input_CID[n] <- MS_info$Input_CID[i_MSInfo]
            Parent_CID[n] <- MS_info$Parent_CID[i_MSInfo]
          } else {
            Input_CID[n] <- MS_info$PubChemCID[i_MSInfo]
            Parent_CID[n] <- MS_info$PubChemCID[i_MSInfo]
          }
          Name[n] <- MS_info$Name[i_MSInfo]
          IUPAC_Name[n] <- MS_info$IUPAC_Name[i_MSInfo]
        }
        if (hsdb_info$isPredecessor[i_tp_entry]) {
          # if the starting CID is predecessor, this entry is a TP
          Predecessor_CID[n] <- Parent_CID[1]
          Predecessor_Name[n] <- Name[1]
          Successor_CID[n] <- Parent_CID[n]
          Successor_Name[n] <- Name[n]
          IsPredecessor[n] <- FALSE
        } else {
          # otherwise this entry is the predecessor
          Predecessor_CID[n] <- Parent_CID[n]
          Predecessor_Name[n] <- Name[n]
          Successor_CID[n] <- Parent_CID[1]
          Successor_Name[n] <- Name[1]
          IsPredecessor[n] <- TRUE
        }
        Transformation[n] <- hsdb_info$transformation[i_tp_entry]
        Source_ID[n] <- hsdb_info$source_ID[1]
        Source_CID[n] <- hsdb_info$cids[1]
        Source[n] <- hsdb_info$source_name[1]
        Source_Description[n] <- hsdb_desc
        Dataset_DOI[n] <- dataset_DOI
        Dataset_Description[n] <- dataset_desc
        Evidence_ID[n] <- ""
        Evidence_Description[n] <- hsdb_info$ref_text[i_tp_entry]
      }
    }
  }
  #PubChem_CID <- Parent_CID
  #Source_CID <- Input_CID
  # create the format for saving
  tp_info <- cbind(#Name, IUPAC_Name, PubChem_CID, SMILES, InChI, InChIKey,
    #MolecularFormula, ExactMass, Source_CID, 
    Predecessor_CID, Predecessor_Name,
    Successor_CID, Successor_Name, #IsPredecessor, 
    Transformation, Source_ID, Source_CID,
    Source, Source_Description, Dataset_DOI, Dataset_Description, 
    Evidence_ID, Evidence_Description)
  #remove duplicate entries
  tp_info <- unique(tp_info)
  #remove NAs if there was no parent CID
  na_entries <- which(is.na(tp_info[,2]))
  if (length(na_entries)>0) {
    tp_info <- tp_info[-which(is.na(tp_info[,2])),]
  }
  # #write the file and return the file name. 
  # write.csv(tp_info, file_name ,row.names = F)
  # return(file_name)
  return(tp_info)
  
}


# # superceded function version, kept for the record. 
# # clean out once other version is thoroughly tested
# createTPinfo.hsdb <- function(hsdb_cids, hsdb_selected_file, MSInfo_file, hsdb_desc, 
#                               dataset_DOI, dataset_desc,file_name="") {
#   # if no filename, autocreate from hsdb_selected_file
#   if (nchar(file_name) < 4) {
#     file_name <- sub(".csv","_allInfo.csv",hsdb_selected_file)
#   }
#   
#   # read in the information
#   hsdb_info <- read.csv(hsdb_selected_file,stringsAsFactors = F)
#   MS_info <- read.csv(MSInfo_file,stringsAsFactors = F)
#   #select only those that we'll use
#   hsdb_info <- hsdb_info[which(hsdb_info$use==T),]
#   
#   n_cids <- length(hsdb_cids)
#   # initiate vectors
#   Input_CID <- vector(mode="character",length=n_cids)
#   Parent_CID <- vector(mode="character",length=n_cids)
#   Predecessor_CID <- vector(mode="character",length=n_cids)
#   Predecessor_Name <- vector(mode="character",length=n_cids)
#   Successor_CID <- vector(mode="character",length=n_cids)
#   Successor_Name <- vector(mode="character",length=n_cids)
#   IsPredecessor <- vector(mode="logical",length=n_cids)
#   Transformation <- vector(mode="character",length=n_cids)
#   Source_ID <- vector(mode="character",length=n_cids)
#   Source_CID <- vector(mode="character",length=n_cids)
#   Source <- vector(mode="character",length=n_cids)
#   Source_Description <- vector(mode="character",length=n_cids)
#   Dataset_DOI <- vector(mode="character",length=n_cids)
#   Dataset_Description <- vector(mode="character",length=n_cids)
#   Evidence_ID <- vector(mode="character",length=n_cids)
#   Evidence_Description <- vector(mode="character",length=n_cids)
#   
#   # take care of the starting CID. If it's in the TP list, remove.
#   # we will have to treat this as a special case
#   start_cid <- hsdb_info$cids[1]
#   i_start_cid <- which(hsdb_cids==start_cid)
#   if (length(i_start_cid)>0) {
#     hsdb_cids <- hsdb_cids[-i_start_cid]
#   }
#   # count the number of CIDs now starting CID is removed
#   n_cids <- length(hsdb_cids)
#   # start the counter, the parent goes at n=1
#   n <- 1
#   # set the current CID to the start CID
#   curr_cid <- start_cid
#   #find the index of the start CID in MSInfo
#   i_MSInfo <- which(MS_info$Input_CID==start_cid)
#   # if the info doesn't exist, reretrieve, otherwise, get from MS_info
#   if (length(i_MSInfo)==0) {
#     cid_MSInfo <- getMSInfo.cid(curr_cid)
#     Input_CID[n] <- cid_MSInfo$Input_CID
#     Parent_CID[n] <- cid_MSInfo$Parent_CID
#     Name[n] <- cid_MSInfo$Name
#     IUPAC_Name[n] <- cid_MSInfo$IUPAC_Name
#   } else {
#     Input_CID[n] <- MS_info$Input_CID[i_MSInfo]
#     Parent_CID[n] <- MS_info$Parent_CID[i_MSInfo]
#     Name[n] <- MS_info$Name[i_MSInfo]
#     IUPAC_Name[n] <- MS_info$IUPAC_Name[i_MSInfo]
#   }
#   Predecessor_CID[n] <- Parent_CID[n]
#   Predecessor_Name[n] <- Name[n]
#   Successor_CID[n] <- Parent_CID[n]
#   Successor_Name[n] <- Name[n]
#   IsPredecessor[n] <- NA
#   Transformation[n] <- "none"
#   Source_ID[n] <- hsdb_info$source_ID[1]
#   Source_CID[n] <- hsdb_info$cids[1]
#   Source[n] <- hsdb_info$source_name[1]
#   Source_Description[n] <- hsdb_desc
#   Dataset_DOI[n] <- dataset_DOI
#   Dataset_Description[n] <- dataset_desc
#   Evidence_ID[n] <- ""
#   Evidence_Description[n] <- ""
#   
#   # go over the rest of the TPs
#   for (i in 1:n_cids) {
#     # for each CID, find out which reference(s) it is in
#     curr_cid <- hsdb_cids[i]
#     tp_entries <- grep(curr_cid,hsdb_info$selected_tp_cids,fixed=T)
#     if (length(tp_entries)>0) {
#       for (j in 1:length(tp_entries)) {
#         #add one more to the counter
#         n <- n+1
#         # get the index of the TP in hsdb_info
#         i_tp_entry <- tp_entries[j]
#         # get the index of the TP in MS_info
#         i_MSInfo <- which(MS_info$Input_CID==curr_cid)
#         # if the value is not found, reretrieve the info, otherwise, save
#         if (length(i_MSInfo)==0) {
#           cid_MSInfo <- getMSInfo.cid(curr_cid)
#           Input_CID[n] <- cid_MSInfo$Input_CID
#           Parent_CID[n] <- cid_MSInfo$Parent_CID
#           Name[n] <- cid_MSInfo$Name
#           IUPAC_Name[n] <- cid_MSInfo$IUPAC_Name
#         } else {
#           Input_CID[n] <- MS_info$Input_CID[i_MSInfo]
#           Parent_CID[n] <- MS_info$Parent_CID[i_MSInfo]
#           Name[n] <- MS_info$Name[i_MSInfo]
#           IUPAC_Name[n] <- MS_info$IUPAC_Name[i_MSInfo]
#         }
#         if (hsdb_info$isPredecessor[i_tp_entry]) {
#           # if the starting CID is predecessor, this entry is a TP
#           Predecessor_CID[n] <- Parent_CID[1]
#           Predecessor_Name[n] <- Name[1]
#           Successor_CID[n] <- Parent_CID[n]
#           Successor_Name[n] <- Name[n]
#           IsPredecessor[n] <- FALSE
#         } else {
#           # otherwise this entry is the predecessor
#           Predecessor_CID[n] <- Parent_CID[n]
#           Predecessor_Name[n] <- Name[n]
#           Successor_CID[n] <- Parent_CID[1]
#           Successor_Name[n] <- Name[1]
#           IsPredecessor[n] <- TRUE
#         }
#         Transformation[n] <- hsdb_info$transformation[i_tp_entry]
#         Source_ID[n] <- hsdb_info$source_ID[1]
#         Source_CID[n] <- hsdb_info$cids[1]
#         Source[n] <- hsdb_info$source_name[1]
#         Source_Description[n] <- hsdb_desc
#         Dataset_DOI[n] <- dataset_DOI
#         Dataset_Description[n] <- dataset_desc
#         Evidence_ID[n] <- ""
#         Evidence_Description[n] <- hsdb_info$ref_text[i_tp_entry]
#       }
#     }
#   }
#   #PubChem_CID <- Parent_CID
#   #Source_CID <- Input_CID
#   # create the format for saving
#   tp_info <- cbind(#Name, IUPAC_Name, PubChem_CID, SMILES, InChI, InChIKey,
#     #MolecularFormula, ExactMass, Source_CID, 
#     Predecessor_CID, Predecessor_Name,
#     Successor_CID, Successor_Name, #IsPredecessor, 
#     Transformation, Source_ID, Source_CID,
#     Source, Source_Description, Dataset_DOI, Dataset_Description, 
#     Evidence_ID, Evidence_Description)
#   #remove duplicate entries
#   tp_info <- unique(tp_info)
#   #remove NAs if there was no parent CID
#   na_entries <- which(is.na(tp_info[,2]))
#   if (length(na_entries)>0) {
#     tp_info <- tp_info[-which(is.na(tp_info[,2])),]
#   }
#   # #write the file and return the file name. 
#   # write.csv(tp_info, file_name ,row.names = F)
#   # return(file_name)
#   return(tp_info)
# }




#NOTE: This function is now superceded by createTPinfo.hsdb
Emma Schymanski's avatar
Emma Schymanski committed
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
createFiles.trans <- function(hsdb_cids, hsdb_selected_file, MSInfo_file, hsdb_desc, 
                              dataset_DOI, dataset_desc,file_name="",
                              startCIDisPredecessor=TRUE) {
  # if no filename, autocreate from hsdb_selected_file
  if (nchar(file_name) < 4) {
    file_name <- sub(".csv","_allInfo.csv",hsdb_selected_file)
  }
  
  # read in the information
  hsdb_info <- read.csv(hsdb_selected_file,stringsAsFactors = F)
  MS_info <- read.csv(MSInfo_file,stringsAsFactors = F)
  #select only those that we'll use
  hsdb_info <- hsdb_info[which(hsdb_info$use==T),]
  
  n_cids <- length(hsdb_cids)
  # initiate vectors
  Input_CID <- vector(mode="character",length=n_cids)
  Parent_CID <- vector(mode="character",length=n_cids)
  Name <- vector(mode="character",length=n_cids)
  IUPAC_Name <- vector(mode="character",length=n_cids)
  SMILES <- vector(mode="character",length=n_cids)
  InChI <- vector(mode="character",length=n_cids)
  InChIKey <- vector(mode="character",length=n_cids)
  MolecularFormula <- vector(mode="character",length=n_cids)
  ExactMass <- vector(mode="character",length=n_cids)
  Predecessor_CID <- vector(mode="character",length=n_cids)
  Successor_CID <- vector(mode="character",length=n_cids)
  IsPredecessor <- vector(mode="logical",length=n_cids)
  Transformation <- vector(mode="character",length=n_cids)
  Source <- vector(mode="character",length=n_cids)
  Source_Description <- vector(mode="character",length=n_cids)
  Dataset_DOI <- vector(mode="character",length=n_cids)
  Dataset_Description <- vector(mode="character",length=n_cids)
  Evidence_DOI <- vector(mode="character",length=n_cids)
  Evidence_Description <- vector(mode="character",length=n_cids)
  
  # take care of the starting CID. If it's in the TP list, remove. 
  # we will have to treat this as a special case
  start_cid <- hsdb_info$cids[1]
  i_start_cid <- which(hsdb_cids==start_cid)
  if (length(i_start_cid)>0) {
    hsdb_cids <- hsdb_cids[-i_start_cid]
  }
  # count the number of CIDs now starting CID is removed
  n_cids <- length(hsdb_cids)
  # start the counter, the parent goes at n=1
  n <- 1
  # set the current CID to the start CID
  curr_cid <- start_cid
  #find the index of the start CID in MSInfo
  i_MSInfo <- which(MS_info$Input_CID==start_cid)
  # if the info doesn't exist, reretrieve, otherwise, get from MS_info
  if (length(i_MSInfo)==0) {
    cid_MSInfo <- getMSInfo.cid(curr_cid)
    Input_CID[n] <- cid_MSInfo$Input_CID
    Parent_CID[n] <- cid_MSInfo$Parent_CID
    Name[n] <- cid_MSInfo$Name
    IUPAC_Name[n] <- cid_MSInfo$IUPAC_Name
    SMILES[n] <- cid_MSInfo$SMILES
    InChI[n] <- cid_MSInfo$InChI
    InChIKey[n] <- cid_MSInfo$InChIKey
    MolecularFormula[n] <- cid_MSInfo$MolecularFormula
    ExactMass[n] <- cid_MSInfo$ExactMass
  } else {
    Input_CID[n] <- MS_info$Input_CID[i_MSInfo]
    Parent_CID[n] <- MS_info$Parent_CID[i_MSInfo]
    Name[n] <- MS_info$Name[i_MSInfo]
    IUPAC_Name[n] <- MS_info$IUPAC_Name[i_MSInfo]
    SMILES[n] <- MS_info$SMILES[i_MSInfo]
    InChI[n] <- MS_info$InChI[i_MSInfo]
    InChIKey[n] <- MS_info$InChIKey[i_MSInfo]
    MolecularFormula[n] <- MS_info$MolecularFormula[i_MSInfo]
    ExactMass[n] <- MS_info$ExactMass[i_MSInfo]
  }
  Predecessor_CID[n] <- Parent_CID[n]
  Successor_CID[n] <- Parent_CID[n]
  IsPredecessor[n] <- startCIDisPredecessor
  Transformation[n] <- "none"
  Source[n] <- hsdb_info$source_name[1]
  Source_Description[n] <- hsdb_desc
  Dataset_DOI[n] <- dataset_DOI
  Dataset_Description[n] <- dataset_desc
  Evidence_DOI[n] <- ""
  Evidence_Description[n] <- ""
  
  # go over the rest of the TPs
  for (i in 1:n_cids) {
    # for each CID, find out which reference(s) it is in
    curr_cid <- hsdb_cids[i]
    tp_entries <- grep(curr_cid,hsdb_info$selected_tp_cids,fixed=T)
    if (length(tp_entries)>0) {
      for (j in 1:length(tp_entries)) {
        #add one more to the counter
        n <- n+1
        # get the index of the TP in hsdb_info
        i_tp_entry <- tp_entries[j]
        # get the index of the TP in MS_info
        i_MSInfo <- which(MS_info$Input_CID==curr_cid)
        # if the value is not found, reretrieve the info, otherwise, save
        if (length(i_MSInfo)==0) {
          cid_MSInfo <- getMSInfo.cid(curr_cid)
          Input_CID[n] <- cid_MSInfo$Input_CID
          Parent_CID[n] <- cid_MSInfo$Parent_CID
          Name[n] <- cid_MSInfo$Name
          IUPAC_Name[n] <- cid_MSInfo$IUPAC_Name
          SMILES[n] <- cid_MSInfo$SMILES
          InChI[n] <- cid_MSInfo$InChI
          InChIKey[n] <- cid_MSInfo$InChIKey
          MolecularFormula[n] <- cid_MSInfo$MolecularFormula
          ExactMass[n] <- cid_MSInfo$ExactMass
        } else {
          Input_CID[n] <- MS_info$Input_CID[i_MSInfo]
          Parent_CID[n] <- MS_info$Parent_CID[i_MSInfo]
          Name[n] <- MS_info$Name[i_MSInfo]
          IUPAC_Name[n] <- MS_info$IUPAC_Name[i_MSInfo]
          SMILES[n] <- MS_info$SMILES[i_MSInfo]
          InChI[n] <- MS_info$InChI[i_MSInfo]
          InChIKey[n] <- MS_info$InChIKey[i_MSInfo]
          MolecularFormula[n] <- MS_info$MolecularFormula[i_MSInfo]
          ExactMass[n] <- MS_info$ExactMass[i_MSInfo]
        }
        if (hsdb_info$isPredecessor[i_tp_entry]) {
          # if the starting CID is predecessor, this entry is a TP
          Predecessor_CID[n] <- Parent_CID[1]
          Successor_CID[n] <- Parent_CID[n]
          IsPredecessor[n] <- FALSE
        } else {
          # otherwise this entry is the predecessor
          Predecessor_CID[n] <- Parent_CID[n]
          Successor_CID[n] <- Parent_CID[1]
          IsPredecessor[n] <- TRUE
        }
        Transformation[n] <- hsdb_info$transformation[i_tp_entry]
        Source[n] <- hsdb_info$source_name[1]
        Source_Description[n] <- hsdb_desc
        Dataset_DOI[n] <- dataset_DOI
        Dataset_Description[n] <- dataset_desc
        Evidence_DOI[n] <- ""
        Evidence_Description[n] <- hsdb_info$ref_text[i_tp_entry]
      }
    }
  }
Emma Schymanski's avatar
Emma Schymanski committed
1395
1396
  PubChem_CID <- Parent_CID
  Source_CID <- Input_CID
Emma Schymanski's avatar
Emma Schymanski committed
1397
  # create the format for saving
Emma Schymanski's avatar
Emma Schymanski committed
1398
1399
1400
  tp_info <- cbind(Name, IUPAC_Name, PubChem_CID, SMILES, InChI, InChIKey,
                   MolecularFormula, ExactMass, Source_CID, 
                   Predecessor_CID, Successor_CID, #IsPredecessor, 
Emma Schymanski's avatar
Emma Schymanski committed
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
                   Transformation, Source, Source_Description, 
                   Dataset_DOI, Dataset_Description, Evidence_DOI, Evidence_Description)
  #remove duplicate entries
  tp_info <- unique(tp_info)
  #remove NAs if there was no parent CID
  na_entries <- which(is.na(tp_info[,2]))
  if (length(na_entries)>0) {
    tp_info <- tp_info[-which(is.na(tp_info[,2])),]
  }
  #write the file and return the file name. 
  write.csv(tp_info, file_name ,row.names = F)
  return(file_name)
  
}


Emma Schymanski's avatar
Emma Schymanski committed
1417

Emma Schymanski's avatar
Emma Schymanski committed
1418
1419
1420
# Missing:
# a function to merge CIDs by source ID - CID mappings
# a function to retrieve source ID / CIDs for data sources with substance depositions
Emma Schymanski's avatar
Emma Schymanski committed
1421
1422
# TODO:
# rewrite getMSInfo.cids to depend on getMSInfo.cid