Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. # Copyright Robert M Flight, 2019
  2. # MIT Licensed. Use at your own risk, with no warranty.
  3. #' extract raw method
  4. #'
  5. #' Given a Thermo ".raw" file, attempts to extract the "method"
  6. #' definition from a translated hexdump of the file.
  7. #'
  8. #' @param in_file The Thermo raw file to extract
  9. #' @param output_type string, data.frame or json
  10. #'
  11. #' @export
  12. #' @return string or data.frame
  13. extract_raw_method = function(in_file, output_type = "data.frame"){
  14. in_file2 = normalizePath(in_file)
  15.  
  16. # performs a hexdump of the binary file, with translation,
  17. # and captures it in a text string
  18. # Output (on linux) looks like:
  19. #
  20. # 02 .... |translated contents|
  21. # .
  22. # .
  23. sys_data = system2("hexdump", args = c("-C", in_file2), stdout = TRUE)
  24.  
  25. # this pattern extracts the translated contents between the pipe on each line
  26. pipe_pattern = "(?<=\\|)[^|]++(?=\\|)"
  27. pipe_content = stringr::str_extract(sys_data, pipe_pattern)
  28.  
  29. # the output has weird dots embedded. Two or more dots seem to indicate spaces,
  30. # whereas single dots should just be removed. Hence the substitution below
  31. pipe_nodots = gsub("\\.", "", gsub("\\.\\.", " ", pipe_content))
  32.  
  33. # In my experience (with a small number of files), the method
  34. # is normally embedded in the first 2000 translated lines
  35. pipe_pasted = paste(pipe_nodots[1:2000], sep = "", collapse = "")
  36.  
  37. # extract the method definition
  38. method_pattern = "Method Summary.*New Method"
  39. method_text = stringr::str_extract(pipe_pasted, method_pattern)
  40.  
  41. # and split across multiple spaces
  42. method_split = strsplit(method_text, " ")[[1]]
  43.  
  44. # now we push it into a data.frame
  45. has_char = nchar(method_split) > 0
  46. method_split = method_split[has_char]
  47. method_split = method_split[2:(length(method_split) - 1)]
  48.  
  49. method_df = data.frame(Variable = rep("", length(method_split)),
  50. Value = rep("", length(method_split)),
  51. stringsAsFactors = FALSE)
  52.  
  53. for (irow in seq_along(method_split)) {
  54. i_equal = strsplit(method_split[irow], "=")[[1]]
  55. method_df[irow, 1] = trimws(i_equal[1])
  56. if (length(i_equal) == 2) {
  57. method_df[irow, 2] = trimws(i_equal[2])
  58. }
  59. }
  60.  
  61. switch(output_type,
  62. string = return(method_split),
  63. data.frame = return(method_df),
  64. json = return(jsonlite::toJSON(method_df, pretty = TRUE)))
  65.  
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement