annotate NmrPreprocessing_script.R @ 2:5e64657b4fe5 draft

planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
author lecorguille
date Wed, 28 Mar 2018 08:05:12 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1 ## ==========================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
2 # Internal functions
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
3 ## ==========================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
4
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
5 # beginTreatment
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
6 beginTreatment <- function(name, Signal_data = NULL, Signal_info = NULL,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
7 force.real = FALSE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
8
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
9 cat("Begin", name, "\n")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
10
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
11
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
12 # Formatting the Signal_data and Signal_info -----------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
13
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
14 vec <- is.vector(Signal_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
15 if (vec) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
16 Signal_data <- vec2mat(Signal_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
17 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
18 if (is.vector(Signal_info)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
19 Signal_info <- vec2mat(Signal_info)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
20 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
21 if (!is.null(Signal_data)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
22 if (!is.matrix(Signal_data)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
23 stop("Signal_data is not a matrix.")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
24 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
25 if (!is.complex(Signal_data) && !is.numeric(Signal_data)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
26 stop("Signal_data contains non-numerical values.")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
27 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
28 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
29 if (!is.null(Signal_info) && !is.matrix(Signal_info)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
30 stop("Signal_info is not a matrix.")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
31 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
32
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
33
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
34 Original_data <- Signal_data
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
35
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
36 # Extract the real part of the spectrum ---------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
37
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
38 if (force.real) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
39 if (is.complex(Signal_data)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
40 Signal_data <- Re(Signal_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
41 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
42 # The signal is numeric Im(Signal_data) is zero anyway so let's avoid
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
43 # using complex(real=...,imaginary=0) which would give a complex signal
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
44 # in endTreatment()
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
45 force.real <- FALSE
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
46 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
47 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
48
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
49
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
50 # Return the formatted data and metadata entries --------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
51
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
52 return(list(start = proc.time(), vec = vec, force.real = force.real,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
53 Original_data = Original_data, Signal_data = Signal_data, Signal_info = Signal_info))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
54 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
55
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
56 # endTreatment
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
57 endTreatment <- function(name, begin_info, Signal_data) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
58
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
59 # begin_info: object outputted from beginTreatment
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
60
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
61
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
62 # Formatting the entries and printing process time -----------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
63 end_time <- proc.time() # record it as soon as possible
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
64 start_time <- begin_info[["start"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
65 delta_time <- end_time - start_time
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
66 delta <- delta_time[]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
67 cat("End", name, "\n")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
68 cat("It lasted", round(delta["user.self"], 3), "s user time,", round(delta["sys.self"],3),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
69 "s system time and", round(delta["elapsed"], 3), "s elapsed time.\n")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
70
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
71
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
72 if (begin_info[["force.real"]]) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
73 # The imaginary part is left untouched
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
74 i <- complex(real = 0, imaginary = 1)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
75 Signal_data <- Signal_data + i * Im(begin_info[["Original_data"]])
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
76 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
77
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
78 if (begin_info[["vec"]]) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
79 Signal_data <- Signal_data[1, ]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
80 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
81
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
82 # Return the formatted data and metadata entries --------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
83 return(Signal_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
84 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
85
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
86 # checkArg
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
87 checkArg <- function(arg, checks, can.be.null=FALSE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
88 check.list <- list(bool=c(is.logical, "a boolean"),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
89 int =c(function(x){x%%1==0}, "an integer"),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
90 num =c(is.numeric, "a numeric"),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
91 str =c(is.character, "a string"),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
92 pos =c(function(x){x>0}, "positive"),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
93 pos0=c(function(x){x>=0}, "positive or zero"),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
94 l1 =c(function(x){length(x)==1}, "of length 1")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
95 )
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
96 if (is.null(arg)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
97 if (!can.be.null) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
98 stop(deparse(substitute(arg)), " is null.")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
99 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
100 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
101 if (is.matrix(arg)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
102 stop(deparse(substitute(arg)), " is not scalar.")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
103 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
104 for (c in checks) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
105 if (!check.list[[c]][[1]](arg)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
106 stop(deparse(substitute(arg)), " is not ", check.list[[c]][[2]], ".")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
107 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
108 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
109 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
110 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
111
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
112 # getArg
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
113 getArg <- function(arg, info, argname, can.be.absent=FALSE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
114 if (is.null(arg)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
115 start <- paste("impossible to get argument", argname, "it was not given directly and");
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
116 if (!is.matrix(info)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
117 stop(paste(start, "the info matrix was not given"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
118 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
119 if (!(argname %in% colnames(info))) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
120 if (can.be.absent) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
121 return(NULL)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
122 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
123 stop(paste(start, "is not in the info matrix"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
124 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
125 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
126 if (nrow(info) < 1) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
127 stop(paste(start, "the info matrix has no row"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
128 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
129 arg <- info[1,argname]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
130 if (is.na(arg)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
131 stop(paste(start, "it is NA in the info matrix"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
132 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
133 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
134 return(arg)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
135 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
136
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
137 # binarySearch
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
138 binarySearch <- function(a, target, lower = TRUE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
139 # search the index i in a such that a[i] == target
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
140 # if it doesn't exists and lower, it searches the closer a[i] such that a[i] < target
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
141 # if !lower, it seraches the closer a[i] such that a[i] > target
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
142 # a should be monotone but can be increasing or decreasing
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
143
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
144 # if a is increasing INVARIANT: a[amin] < target < a[amax]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
145 N <- length(a)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
146 if ((a[N] - target) * (a[N] - a[1]) <= 0) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
147 return(N)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
148 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
149 if ((a[1] - target) * (a[N] - a[1]) >= 0) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
150 return(1)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
151 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
152 amin <- 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
153 amax <- N
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
154 while (amin + 1 < amax) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
155 amid <- floor((amin + amax)/2)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
156 if ((a[amid] - target) * (a[amax] - a[amid]) < 0) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
157 amin <- amid
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
158 } else if ((a[amid] - target) * (a[amax] - a[amid]) > 0) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
159 amax <- amid
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
160 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
161 # a[amid] == a[amax] or a[amid] == target In both cases, a[amid] ==
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
162 # target
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
163 return(amid)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
164 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
165 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
166 if (xor(lower, a[amin] > a[amax])) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
167 # (lower && a[amin] < a[amax]) || (!lower && a[min] > a[max])
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
168 # If increasing and we want the lower, we take amin
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
169 # If decreasing and we want the bigger, we take amin too
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
170 return(amin)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
171 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
172 return(amax)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
173 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
174 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
175
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
176 # Interpol
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
177 Interpol <- function(t, y) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
178 # y: sample
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
179 # t : warping function
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
180
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
181 m <- length(y)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
182 # t <= m-1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
183 # because if t > m-1, y[ti+1] will be NA when we compute g
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
184 valid <- 1 <= t & t <= m-1 # FIXME it was '<' in Bubble v2
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
185 s <- (1:m)[valid]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
186 ti <- floor(t[s])
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
187 tr <- t[s] - ti
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
188 g <- y[ti + 1] - y[ti]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
189 f <- y[ti] + tr * g
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
190 list(f=f, s=s, g=g)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
191 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
192
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
193 # vec2mat
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
194 vec2mat <- function(vec) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
195 return(matrix(vec, nrow = 1, dimnames = list(c(1), names(vec))))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
196
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
197 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
198
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
199 # binarySearch
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
200 binarySearch <- function(a, target, lower = TRUE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
201 # search the index i in a such that a[i] == target
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
202 # if it doesn't exists and lower, it searches the closer a[i] such that a[i] < target
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
203 # if !lower, it seraches the closer a[i] such that a[i] > target
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
204 # a should be monotone but can be increasing or decreasing
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
205
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
206 # if a is increasing INVARIANT: a[amin] < target < a[amax]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
207 N <- length(a)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
208 if ((a[N] - target) * (a[N] - a[1]) <= 0) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
209 return(N)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
210 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
211 if ((a[1] - target) * (a[N] - a[1]) >= 0) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
212 return(1)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
213 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
214 amin <- 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
215 amax <- N
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
216 while (amin + 1 < amax) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
217 amid <- floor((amin + amax)/2)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
218 if ((a[amid] - target) * (a[amax] - a[amid]) < 0) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
219 amin <- amid
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
220 } else if ((a[amid] - target) * (a[amax] - a[amid]) > 0) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
221 amax <- amid
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
222 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
223 # a[amid] == a[amax] or a[amid] == target In both cases, a[amid] ==
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
224 # target
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
225 return(amid)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
226 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
227 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
228 if (xor(lower, a[amin] > a[amax])) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
229 # (lower && a[amin] < a[amax]) || (!lower && a[min] > a[max])
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
230 # If increasing and we want the lower, we take amin
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
231 # If decreasing and we want the bigger, we take amin too
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
232 return(amin)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
233 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
234 return(amax)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
235 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
236 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
237
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
238
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
239 # indexInterval
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
240 indexInterval <- function (a, from, to, inclusive=TRUE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
241 # If inclusive and from <= to, we need to take the lower
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
242 # If not inclusive and from > to, we need to take the lower too
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
243 lowerFrom <- xor(inclusive, from > to)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
244 fromIndex <- binarySearch(a, from, lowerFrom)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
245 toIndex <- binarySearch(a, to, !lowerFrom)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
246 return(fromIndex:toIndex)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
247 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
248
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
249
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
250
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
251 ## ==========================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
252 # GroupDelayCorrection
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
253 ## ==========================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
254 GroupDelayCorrection <- function(Fid_data, Fid_info = NULL, group_delay = NULL) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
255
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
256
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
257 # Data initialisation and checks ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
258
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
259 begin_info <- beginTreatment("GroupDelayCorrection", Fid_data, Fid_info)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
260 Fid_data <- begin_info[["Signal_data"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
261 dimension_names <- dimnames(Fid_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
262 Fid_info <- begin_info[["Signal_info"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
263 checkArg(group_delay, c("num", "pos0"), can.be.null = TRUE)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
264 # if Fid_info and group_delay are NULL, getArg will generate an error
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
265
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
266 group_delay <- getArg(group_delay, Fid_info, "GRPDLY", can.be.absent = TRUE)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
267
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
268 if (is.null(group_delay)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
269
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
270 # See DetermineBrukerDigitalFilter.m in matNMR MATLAB library
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
271 group_delay_matrix <- matrix(c(44.75, 46, 46.311, 33.5, 36.5, 36.53, 66.625,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
272 48, 47.87, 59.0833, 50.1667, 50.229, 68.5625, 53.25, 53.289, 60.375,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
273 69.5, 69.551, 69.5313, 72.25, 71.6, 61.0208, 70.1667, 70.184, 70.0156,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
274 72.75, 72.138, 61.3438, 70.5, 70.528, 70.2578, 73, 72.348, 61.5052, 70.6667,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
275 70.7, 70.3789, 72.5, 72.524, 61.5859, 71.3333, NA, 70.4395, 72.25, NA,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
276 61.6263, 71.6667, NA, 70.4697, 72.125, NA, 61.6465, 71.8333, NA, 70.4849,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
277 72.0625, NA, 61.6566, 71.9167, NA, 70.4924, 72.0313, NA), nrow = 21,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
278 ncol = 3, byrow = TRUE, dimnames = list(c(2, 3, 4, 6, 8, 12, 16, 24,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
279 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536, 2048),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
280 c(10, 11, 12)))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
281 decim <- Fid_info[1, "DECIM"]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
282 dspfvs <- Fid_info[1, "DSPFVS"]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
283 if (!(toString(decim) %in% rownames(group_delay_matrix))) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
284 stop(paste("Invalid DECIM", decim, "it should be one of", rownames(group_delay_matrix)))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
285 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
286 if (!(toString(dspfvs) %in% colnames(group_delay_matrix))) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
287 stop(paste("Invalid DSPFVS", dspfvs, "it should be one of", colnames(group_delay_matrix)))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
288 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
289 group_delay <- group_delay_matrix[toString(decim), toString(dspfvs)]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
290 if (is.na(group_delay)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
291 stop(paste("Invalid DECIM", decim, "for DSPFVS", dspfvs))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
292 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
293 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
294 m <- ncol(Fid_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
295 n <- nrow(Fid_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
296
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
297 # GroupDelayCorrection ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
298
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
299 # We do the shifting in the Fourier domain because the shift can be non-integer.
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
300 # That way we automatically have the circular behaviour of the shift and the
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
301 # interpolation if it is non-integer.
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
302
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
303 Spectrum <- t(stats::mvfft(t(Fid_data)))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
304
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
305 # Spectrum <- FourierTransform(Fid_data, Fid_info)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
306 p <- ceiling(m/2)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
307 new_index <- c((p + 1):m, 1:p)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
308 Spectrum <- Spectrum[,new_index]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
309 Spectrum <- matrix(data = Spectrum, ncol = m, nrow = n)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
310
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
311 Omega <- (0:(m - 1))/m
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
312 i <- complex(real = 0, imaginary = 1)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
313
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
314 if (n>1) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
315 Spectrum <- sweep(Spectrum, MARGIN = 2, exp(i * group_delay * 2 * pi * Omega), `*`)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
316 Spectrum <- Spectrum[,new_index]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
317 }else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
318 Spectrum <- Spectrum* exp(i * group_delay * 2 * pi * Omega)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
319 Spectrum <- Spectrum[new_index]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
320 Spectrum <- matrix(data = Spectrum, ncol = m, nrow = n)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
321 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
322
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
323
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
324 Fid_data <- t(stats::mvfft(t(Spectrum), inverse = TRUE))/m
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
325 colnames(Fid_data) <- dimension_names[[2]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
326 rownames(Fid_data) <- dimension_names[[1]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
327
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
328 # Data finalisation ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
329
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
330 return(endTreatment("GroupDelayCorrection", begin_info, Fid_data))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
331 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
332
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
333 ## ==========================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
334 # SolventSuppression
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
335 ## ==========================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
336 SolventSuppression <- function(Fid_data, lambda.ss = 1e+06, ptw.ss = TRUE,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
337 plotSolvent = F, returnSolvent = F) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
338
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
339 # Data initialisation and checks ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
340
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
341 begin_info <- beginTreatment("SolventSuppression", Fid_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
342 Fid_data <- begin_info[["Signal_data"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
343 checkArg(ptw.ss, c("bool"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
344 checkArg(lambda.ss, c("num", "pos0"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
345
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
346
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
347 # difsm function definition for the smoother -----------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
348
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
349 if (ptw.ss) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
350 # Use of the function in ptw that smoothes signals with a finite difference
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
351 # penalty of order 2
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
352 difsm <- ptw::difsm
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
353 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
354 # Or manual implementation based on sparse matrices for large data series (cf.
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
355 # Eilers, 2003. 'A perfect smoother')
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
356 difsm <- function(y, d = 2, lambda) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
357
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
358 m <- length(y)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
359 # Sparse identity matrix m x m
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
360 E <- Matrix::Diagonal(m)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
361 D <- Matrix::diff(E, differences = d)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
362 A <- E + lambda.ss * Matrix::t(D) %*% D
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
363 # base::chol does not take into account that A is sparse and is extremely slow
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
364 C <- Matrix::chol(A)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
365 x <- Matrix::solve(C, Matrix::solve(Matrix::t(C), y))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
366 return(as.numeric(x))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
367 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
368 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
369
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
370 # Solvent Suppression ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
371
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
372 n <- dim(Fid_data)[1]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
373 if (returnSolvent) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
374 SolventRe <- Fid_data
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
375 SolventIm <- Fid_data
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
376 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
377 for (i in 1:n) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
378 FidRe <- Re(Fid_data[i, ])
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
379 FidIm <- Im(Fid_data[i, ])
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
380 solventRe <- difsm(y = FidRe, lambda = lambda.ss)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
381 solventIm <- difsm(y = FidIm, lambda = lambda.ss)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
382
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
383 if (plotSolvent) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
384 m <- length(FidRe)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
385 graphics::plot(1:m, FidRe, type = "l", col = "red")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
386 graphics::lines(1:m, solventRe, type = "l", col = "blue")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
387 graphics::plot(1:m, FidIm, type = "l", col = "red")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
388 graphics::lines(1:m, solventIm, type = "l", col = "blue")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
389 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
390 FidRe <- FidRe - solventRe
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
391 FidIm <- FidIm - solventIm
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
392 Fid_data[i, ] <- complex(real = FidRe, imaginary = FidIm)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
393 if (returnSolvent) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
394 SolventRe[i, ] <- solventRe
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
395 SolventIm[i, ] <- solventIm
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
396 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
397 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
398
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
399
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
400 # Data finalisation ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
401
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
402 Fid_data <- endTreatment("SolventSuppression", begin_info, Fid_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
403 if (returnSolvent) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
404 return(list(Fid_data = Fid_data, SolventRe = SolventRe, SolventIm = SolventIm))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
405 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
406 return(Fid_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
407 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
408 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
409
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
410
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
411 ## ==========================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
412 # Apodization
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
413 # =============================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
414 Apodization <- function(Fid_data, Fid_info = NULL, DT = NULL,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
415 type.apod = c("exp","cos2", "blockexp", "blockcos2",
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
416 "gauss", "hanning", "hamming"), phase = 0, rectRatio = 1/2,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
417 gaussLB = 1, expLB = 1, plotWindow = F, returnFactor = F) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
418
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
419 # Data initialisation and checks ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
420 begin_info <- beginTreatment("Apodization", Fid_data, Fid_info)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
421 Fid_data <- begin_info[["Signal_data"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
422 Fid_info <- begin_info[["Signal_info"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
423 # Data check
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
424 type.apod <- match.arg(type.apod)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
425 checkArg(DT, c("num", "pos"), can.be.null = TRUE)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
426 checkArg(phase, c("num"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
427
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
428 # Apodization ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
429 DT <- getArg(DT, Fid_info, "DT") # Dwell Time
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
430 m <- ncol(Fid_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
431 t <- (1:m) * DT # Time
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
432 rectSize <- ceiling(rectRatio * m)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
433 gaussLB <- (gaussLB/(sqrt(8 * log(2))))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
434 # Define the types of apodization:
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
435 switch(type.apod, exp = {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
436 # exponential
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
437 Factor <- exp(-expLB * t)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
438 }, cos2 = {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
439 # cos^2
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
440 c <- cos((1:m) * pi/(2 * m) - phase * pi/2)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
441 Factor <- c * c
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
442 }, blockexp = {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
443 # block and exponential
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
444 Factor <- c(rep.int(1, rectSize), rep.int(0, m - rectSize))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
445 # | rectSize | 1 ___________ | \ 0 \____
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
446 Factor[(rectSize + 1):m] <- exp(-expLB * t[1:(m - rectSize)])
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
447 }, blockcos2 = {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
448 # block and cos^2
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
449 Factor <- c(rep.int(1, rectSize), rep.int(0, m - rectSize))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
450 c <- cos((1:(m - rectSize)) * pi/(2 * (m - rectSize)))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
451 Factor[(rectSize + 1):m] <- c * c
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
452 }, gauss = {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
453 # gaussian
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
454 Factor <- exp(-(gaussLB * t)^2/2)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
455 Factor <- Factor/max(Factor)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
456 }, hanning = {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
457 # Hanning
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
458 Factor <- 0.5 + 0.5 * cos((1:m) * pi/m - phase * pi)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
459 }, hamming = {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
460 # Hamming
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
461 Factor <- 0.54 + 0.46 * cos((1:m) * pi/m - phase * pi)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
462 })
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
463 if (plotWindow) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
464 graphics::plot(1:m, Factor, "l")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
465 # dev.off() # device independent, it is the responsability of the
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
466 # caller to do it
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
467 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
468 # Apply the apodization factor on the spectra
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
469 Fid_data <- sweep(Fid_data, MARGIN = 2, Factor, `*`)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
470
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
471 # Data finalisation ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
472 Fid_data <- endTreatment("Apodization", begin_info, Fid_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
473 if (returnFactor) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
474 return(list(Fid_data = Fid_data, Factor = Factor))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
475 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
476 return(Fid_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
477 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
478 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
479
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
480
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
481 ## ====================================================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
482 # FourierTransform
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
483 ## ====================================================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
484
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
485
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
486 # fftshift1D2D
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
487 fftshift1D2D <- function(x) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
488 vec <- F
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
489 if (is.vector(x)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
490 x <- vec2mat(x)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
491 vec <- T
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
492 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
493 m <- dim(x)[2]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
494 p <- ceiling(m/2)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
495 new_index <- c((p + 1):m, 1:p)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
496 y <- x[, new_index, drop = vec]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
497 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
498
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
499 # FourierTransform
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
500 FourierTransform <- function(Fid_data, Fid_info = NULL, SW_h = NULL, SW = NULL, O1 = NULL, reverse.axis = TRUE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
501
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
502 # Data initialisation and checks ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
503 begin_info <- beginTreatment("FourierTransform", Fid_data, Fid_info)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
504 Fid_data <- begin_info[["Signal_data"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
505 Fid_info <- begin_info[["Signal_info"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
506
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
507 m <- ncol(Fid_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
508 n <- nrow(Fid_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
509
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
510 if (is.null(SW_h)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
511 SW_h <- getArg(SW_h, Fid_info, "SW_h")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
512 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
513
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
514 if (is.null(SW)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
515 SW <- getArg(SW, Fid_info, "SW") # Sweep Width in ppm (semi frequency scale in ppm)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
516 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
517
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
518
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
519 if (is.null(O1)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
520 O1 <- getArg(O1, Fid_info, "O1")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
521 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
522
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
523
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
524 checkArg(reverse.axis, c("bool"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
525
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
526 # Fourier Transformation ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
527 # mvfft does the unnormalized fourier transform (see ?mvfft), so we need divide
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
528 # by m. It does not matter a lot in our case since the spectrum will be
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
529 # normalized.
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
530
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
531 # FT
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
532 RawSpect_data <- fftshift1D2D(t(stats::mvfft(t(Fid_data))))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
533 # recover the frequencies values
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
534 f <- ((0:(m - 1)) - floor(m/2)) * Fid_info[1, "SW_h"]/(m-1)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
535
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
536 if(reverse.axis == TRUE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
537 revind <- rev(1:m)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
538 RawSpect_data <- RawSpect_data[,revind] # reverse the spectrum
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
539 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
540
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
541 RawSpect_data <- matrix(RawSpect_data, nrow = n, ncol = m)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
542 colnames(RawSpect_data) <- f
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
543 rownames(RawSpect_data) <- rownames(Fid_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
544
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
545 # PPM conversion ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
546
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
547 # The Sweep Width has to be the same since the column names are the same
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
548
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
549 ppmInterval <- SW/(m-1)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
550
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
551 O1index = round((m+1)/2+O1*(m - 1) / SW_h)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
552
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
553 end <- O1index - m
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
554 start <- O1index -1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
555 ppmScale <- (start:end) * ppmInterval
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
556 RawSpect_data <- matrix(RawSpect_data, nrow = n, ncol = -(end - start) + 1, dimnames =
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
557 list(rownames(RawSpect_data), ppmScale))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
558
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
559
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
560 # Data finalisation ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
561 return(endTreatment("FourierTransform", begin_info, RawSpect_data))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
562 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
563
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
564 ## ====================================================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
565 # InternalReferencing
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
566 ## ====================================================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
567
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
568 InternalReferencing <- function(Spectrum_data, Fid_info, method = c("max", "thres"),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
569 range = c("nearvalue", "all", "window"), ppm.value = 0,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
570 direction = "left", shiftHandling = c("zerofilling", "cut",
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
571 "NAfilling", "circular"), c = 2, pc = 0.02, fromto.RC = NULL,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
572 ppm.ir = TRUE, rowindex_graph = NULL) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
573
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
574
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
575
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
576 # Data initialisation and checks ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
577
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
578 begin_info <- beginTreatment("InternalReferencing", Spectrum_data, Fid_info)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
579 Spectrum_data <- begin_info[["Signal_data"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
580 Fid_info <- begin_info[["Signal_info"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
581
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
582
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
583 # Check input arguments
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
584 range <- match.arg(range)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
585 shiftHandling <- match.arg(shiftHandling)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
586 method <- match.arg(method)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
587 plots <- NULL
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
588
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
589
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
590 checkArg(ppm.ir, c("bool"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
591 checkArg(unlist(fromto.RC), c("num"), can.be.null = TRUE)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
592 checkArg(pc, c("num"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
593 checkArg(ppm.value, c("num"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
594 checkArg(rowindex_graph, "num", can.be.null = TRUE)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
595
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
596 # fromto.RC
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
597 if (!is.null(fromto.RC)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
598 diff <- diff(unlist(fromto.RC))[1:length(diff(unlist(fromto.RC)))%%2 !=0]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
599 for (i in 1:length(diff)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
600 if (diff[i] >= 0) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
601 fromto <- c(fromto.RC[[i]][2], fromto.RC[[i]][1])
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
602 fromto.RC[[i]] <- fromto
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
603 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
604 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
605 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
606
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
607
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
608 # findTMSPpeak function ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
609 findTMSPpeak <- function(ft, c = 2, direction = "left") {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
610 ft <- Re(ft) # extraction de la partie réelle
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
611 N <- length(ft)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
612 if (direction == "left") {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
613 newindex <- rev(1:N)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
614 ft <- rev(ft)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
615 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
616 thres <- 99999
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
617 i <- 1000 # Start at point 1000 to find the peak
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
618 vect <- ft[1:i]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
619
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
620 while (vect[i] <= (c * thres)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
621 cumsd <- stats::sd(vect)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
622 cummean <- mean(vect)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
623 thres <- cummean + 3 * cumsd
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
624 i <- i + 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
625 vect <- ft[1:i]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
626 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
627 if (direction == "left") {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
628 v <- newindex[i]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
629 } else {v <- i}
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
630
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
631 if (is.na(v)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
632 warning("No peak found, need to lower the threshold.")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
633 return(NA)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
634 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
635 # recherche dans les 1% de points suivants du max trouve pour etre au sommet du
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
636 # pic
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
637 d <- which.max(ft[v:(v + N * 0.01)])
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
638 new.peak <- v + d - 1 # nouveau pic du TMSP si d > 0
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
639
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
640 if (names(which.max(ft[v:(v + N * 0.01)])) != names(which.max(ft[v:(v + N * 0.03)]))) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
641 # recherche dans les 3% de points suivants du max trouve pour eviter un faux
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
642 # positif
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
643 warning("the TMSP peak might be located further away, increase the threshold to check.")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
644 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
645 return(new.peak)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
646 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
647 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
648
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
649
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
650 # Apply the method ('thres' or 'max') on spectra
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
651 # ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
652
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
653 n <- nrow(Spectrum_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
654 m <- ncol(Spectrum_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
655
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
656 # The Sweep Width has to be the same since the column names are the same
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
657 SW <- Fid_info[1, "SW"] # Sweep Width in ppm (semi frequency scale in ppm)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
658 ppmInterval <- SW/(m-1)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
659
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
660 if (range == "all") {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
661 Data <- Spectrum_data
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
662 } else { # range = "nearvalue" or "window"
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
663
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
664 if (range == "nearvalue") {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
665 fromto.RC <- list(c(-(SW * pc)/2 + ppm.value, (SW * pc)/2 + ppm.value)) # automatic fromto values in ppm
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
666 colindex <- as.numeric(colnames(Spectrum_data))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
667 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
668 if (ppm.ir == TRUE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
669 colindex <- as.numeric(colnames(Spectrum_data))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
670 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
671 colindex <- 1:m
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
672 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
673 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
674
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
675
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
676 Int <- vector("list", length(fromto.RC))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
677 for (i in 1:length(fromto.RC)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
678 Int[[i]] <- indexInterval(colindex, from = fromto.RC[[i]][1],
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
679 to = fromto.RC[[i]][2], inclusive = TRUE)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
680 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
681
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
682 vector <- rep(0, m)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
683 vector[unlist(Int)] <- 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
684 if (n > 1) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
685 Data <- sweep(Spectrum_data, MARGIN = 2, FUN = "*", vector) # Cropped_Spectrum
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
686 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
687 Data <- Spectrum_data * vector
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
688 } # Cropped_Spectrum
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
689 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
690
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
691
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
692 if (method == "thres") {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
693 TMSPpeaks <- apply(Data, 1, findTMSPpeak, c = c, direction = direction)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
694 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
695 TMSPpeaks <- apply(abs(Re(Data)), 1, which.max)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
696 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
697
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
698 # TMSPpeaks is an column index
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
699 maxpeak <- max(TMSPpeaks)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
700 minpeak <- min(TMSPpeaks)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
701
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
702
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
703
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
704 # Shift spectra according to the TMSPpeaks found --------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
705 # Depends on the shiftHandling
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
706
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
707 if (shiftHandling %in% c("zerofilling", "NAfilling", "cut")) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
708 fill <- NA
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
709 if (shiftHandling == "zerofilling") {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
710 fill <- 0
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
711 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
712
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
713 start <- maxpeak - 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
714 end <- minpeak - m
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
715
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
716 ppmScale <- (start:end) * ppmInterval
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
717
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
718 # check if ppm.value is in the ppmScale interval
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
719 if(ppm.value < min(ppmScale) | ppm.value > max(ppmScale)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
720 warning("ppm.value = ", ppm.value, " is not in the ppm interval [",
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
721 round(min(ppmScale),2), ",", round(max(ppmScale),2), "], and is set to its default ppm.value 0")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
722 ppm.value = 0
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
723 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
724
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
725 ppmScale <- ppmScale + ppm.value
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
726
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
727 Spectrum_data_calib <- matrix(fill, nrow = n, ncol = -(end - start) + 1,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
728 dimnames = list(rownames(Spectrum_data), ppmScale))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
729 for (i in 1:n) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
730 shift <- (1 - TMSPpeaks[i]) + start
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
731 Spectrum_data_calib[i, (1 + shift):(m + shift)] <- Spectrum_data[i, ]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
732 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
733
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
734 if (shiftHandling == "cut") {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
735 Spectrum_data_calib = as.matrix(stats::na.omit(t(Spectrum_data_calib)))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
736 Spectrum_data_calib = t(Spectrum_data_calib)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
737 base::attr(Spectrum_data_calib, "na.action") <- NULL
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
738 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
739
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
740
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
741 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
742 # circular
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
743 start <- 1 - maxpeak
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
744 end <- m - maxpeak
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
745
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
746 ppmScale <- (start:end) * ppmInterval
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
747
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
748 # check if ppm.value in is the ppmScale interval
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
749 if(ppm.value < min(ppmScale) | ppm.value > max(ppmScale)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
750 warning("ppm.value = ", ppm.value, " is not in the ppm interval [",
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
751 round(min(ppmScale),2), ",", round(max(ppmScale),2), "], and is set to its default ppm.value 0")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
752 ppm.value = 0
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
753 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
754 ppmScale <- ppmScale + ppm.value
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
755
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
756 Spectrum_data_calib <- matrix(nrow=n, ncol=end-start+1,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
757 dimnames=list(rownames(Spectrum_data), ppmScale))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
758 for (i in 1:n) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
759 shift <- (maxpeak-TMSPpeaks[i])
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
760 Spectrum_data_calib[i,(1+shift):m] <- Spectrum_data[i,1:(m-shift)]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
761 if (shift > 0) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
762 Spectrum_data_calib[i,1:shift] <- Spectrum_data[i,(m-shift+1):m]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
763 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
764 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
765 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
766
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
767
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
768
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
769
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
770 # Plot of the spectra ---------------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
771
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
772 ppm = xstart = value = xend = Legend = NULL # only for R CMD check
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
773
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
774
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
775 # with the search zone for TMSP and the location of the peaks just found
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
776 if (!is.null(rowindex_graph)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
777
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
778 if (range == "window") {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
779 if (ppm.ir == TRUE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
780 fromto <- fromto.RC
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
781 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
782 fromto <- list()
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
783 idcol <- as.numeric(colnames(Spectrum_data))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
784 for (i in 1:length(fromto.RC)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
785 fromto[[i]] <- as.numeric(colnames(Spectrum_data))[fromto.RC[[i]]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
786 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
787 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
788 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
789 fromto <- fromto.RC
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
790 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
791
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
792 # TMSPloc in ppm
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
793 TMSPloc <- as.numeric(colnames(Spectrum_data))[TMSPpeaks[rowindex_graph]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
794
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
795 # num plot per window
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
796 num.stacked <- 6
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
797
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
798 # rectanglar bands of color for the search zone
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
799 rects <- data.frame(xstart = sapply(fromto, function(x) x[[1]]),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
800 xend = sapply(fromto, function(x) x[[2]]),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
801 Legend = "TMSP search zone and location")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
802
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
803 # vlines for TMSP peak
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
804 addlines <- data.frame(rowname = rownames(Spectrum_data)[rowindex_graph],TMSPloc)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
805
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
806 nn <- length(rowindex_graph)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
807 i <- 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
808 j <- 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
809 plots <- vector(mode = "list", length = ceiling(nn/num.stacked))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
810
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
811 while (i <= nn) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
812
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
813 last <- min(i + num.stacked - 1, nn)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
814
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
815 melted <- reshape2::melt(Re(Spectrum_data[i:last, ]),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
816 varnames = c("rowname", "ppm"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
817
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
818 plots[[j]] <- ggplot2::ggplot() + ggplot2::theme_bw() +
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
819 ggplot2::geom_line(data = melted,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
820 ggplot2::aes(x = ppm, y = value)) +
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
821 ggplot2::geom_rect(data = rects, ggplot2::aes(xmin = xstart, xmax = xend,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
822 ymin = -Inf, ymax = Inf, fill = Legend), alpha = 0.4) +
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
823 ggplot2::facet_grid(rowname ~ ., scales = "free_y") +
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
824 ggplot2::theme(legend.position = "none") +
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
825 ggplot2::geom_vline(data = addlines, ggplot2::aes(xintercept = TMSPloc),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
826 color = "red", show.legend = TRUE) +
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
827 ggplot2::ggtitle("TMSP peak search zone and location") +
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
828 ggplot2::theme(legend.position = "top", legend.text = ggplot2::element_text())
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
829
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
830
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
831
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
832 if ((melted[1, "ppm"] - melted[(dim(melted)[1]), "ppm"]) > 0) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
833 plots[[j]] <- plots[[j]] + ggplot2::scale_x_reverse()
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
834 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
835
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
836 i <- last + 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
837 j <- j + 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
838 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
839
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
840 plots
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
841 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
842
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
843
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
844 # Return the results ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
845 Spectrum_data <- endTreatment("InternalReferencing", begin_info, Spectrum_data_calib)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
846
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
847 if (is.null(plots)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
848 return(Spectrum_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
849 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
850 return(list(Spectrum_data = Spectrum_data, plots = plots))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
851 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
852
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
853 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
854
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
855 ## ====================================================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
856 # ZeroOrderPhaseCorrection
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
857 ## ====================================================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
858
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
859 ZeroOrderPhaseCorrection <- function(Spectrum_data, type.zopc = c("rms", "manual", "max"),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
860 plot_rms = NULL, returnAngle = FALSE, createWindow = TRUE,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
861 angle = NULL, plot_spectra = FALSE,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
862 ppm.zopc = TRUE, exclude.zopc = list(c(5.1,4.5))) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
863
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
864
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
865 # Data initialisation and checks ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
866
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
867 # Entry arguments definition:
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
868 # plot_rms : graph of rms criterion returnAngle : if TRUE, returns avector of
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
869 # optimal angles createWindow : for plot_rms plots angle : If angle is not NULL,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
870 # spectra are rotated according to the angle vector values
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
871 # plot_spectra : if TRUE, plot rotated spectra
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
872
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
873
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
874
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
875 begin_info <- beginTreatment("ZeroOrderPhaseCorrection", Spectrum_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
876 Spectrum_data <- begin_info[["Signal_data"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
877 n <- nrow(Spectrum_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
878 m <- ncol(Spectrum_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
879
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
880 rnames <- rownames(Spectrum_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
881
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
882 # Check input arguments
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
883 type.zopc <- match.arg(type.zopc)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
884 checkArg(ppm.zopc, c("bool"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
885 checkArg(unlist(exclude.zopc), c("num"), can.be.null = TRUE)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
886
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
887
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
888 # type.zopc in c("max", "rms") -----------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
889 if (type.zopc %in% c("max", "rms")) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
890 # angle is found by optimization
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
891
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
892 # rms function to be optimised
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
893 rms <- function(ang, y, meth = c("max", "rms")) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
894 # if (debug_plot) { graphics::abline(v=ang, col='gray60') }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
895 roty <- y * exp(complex(real = 0, imaginary = ang)) # spectrum rotation
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
896 Rey <- Re(roty)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
897
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
898 if (meth == "rms") {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
899 ReyPos <- Rey[Rey >= 0] # select positive intensities
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
900 POSss <- sum((ReyPos)^2, na.rm = TRUE) # SS for positive intensities
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
901 ss <- sum((Rey)^2, na.rm = TRUE) # SS for all intensities
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
902 return(POSss/ss) # criterion : SS for positive values / SS for all intensities
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
903 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
904 maxi <- max(Rey, na.rm = TRUE)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
905 return(maxi)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
906 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
907 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
908
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
909
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
910 # Define the interval where to search for (by defining Data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
911 if (is.null(exclude.zopc)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
912 Data <- Spectrum_data
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
913 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
914
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
915 # if ppm.zopc == TRUE, then exclude.zopc is in the colnames values, else, in the column
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
916 # index
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
917 if (ppm.zopc == TRUE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
918 colindex <- as.numeric(colnames(Spectrum_data))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
919 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
920 colindex <- 1:m
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
921 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
922
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
923 # Second check for the argument exclude.zopc
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
924 diff <- diff(unlist(exclude.zopc))[1:length(diff(unlist(exclude.zopc)))%%2 !=0]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
925 for (i in 1:length(diff)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
926 if (ppm.zopc == TRUE & diff[i] >= 0) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
927 stop(paste("Invalid region removal because from <= to in ppm.zopc"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
928 } else if (ppm.zopc == FALSE & diff[i] <= 0) {stop(paste("Invalid region removal because from >= to in column index"))}
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
929 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
930
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
931
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
932 Int <- vector("list", length(exclude.zopc))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
933 for (i in 1:length(exclude.zopc)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
934 Int[[i]] <- indexInterval(colindex, from = exclude.zopc[[i]][1],
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
935 to = exclude.zopc[[i]][2], inclusive = TRUE)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
936 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
937
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
938 vector <- rep(1, m)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
939 vector[unlist(Int)] <- 0
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
940 if (n > 1) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
941 Data <- sweep(Spectrum_data, MARGIN = 2, FUN = "*", vector) # Cropped_Spectrum
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
942 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
943 Data <- Spectrum_data * vector
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
944 } # Cropped_Spectrum
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
945 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
946
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
947
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
948 # angles computation
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
949 Angle <- c()
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
950 for (k in 1:n)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
951 {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
952 # The function is rms is periodic (period 2pi) and it seems that there is a phase
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
953 # x such that rms is unimodal (i.e. decreasing then increasing) on the interval
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
954 # [x; x+2pi]. However, if we do the optimization for example on [x-pi; x+pi],
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
955 # instead of being decreasing then increasing, it might be increasing then
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
956 # decreasing in which case optimize, thinking it is a valley will have to choose
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
957 # between the left or the right of this hill and if it chooses wrong, it will end
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
958 # up at like x-pi while the minimum is close to x+pi.
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
959
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
960 # Supposing that rms is unimodal, the classical 1D unimodal optimization will
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
961 # work in either [-pi;pi] or [0;2pi] (this is not easy to be convinced by that I
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
962 # agree) and we can check which one it is simply by the following trick
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
963
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
964 f0 <- rms(0, Data[k, ],meth = type.zopc)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
965 fpi <- rms(pi, Data[k, ], meth = type.zopc)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
966 if (f0 < fpi) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
967 interval <- c(-pi, pi)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
968 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
969 interval <- c(0, 2 * pi)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
970 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
971
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
972 # graphs of rms criteria
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
973 debug_plot <- F # rms should not plot anything now, only when called by optimize
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
974 if (!is.null(plot_rms) && rnames[k] %in% plot_rms) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
975 x <- seq(min(interval), max(interval), length.out = 100)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
976 y <- rep(1, 100)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
977 for (K in (1:100)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
978 y[K] <- rms(x[K], Data[k, ], meth = type.zopc)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
979 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
980 if (createWindow == TRUE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
981 grDevices::dev.new(noRStudioGD = FALSE)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
982 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
983 graphics::plot(x, y, main = paste("Criterion maximization \n",
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
984 rownames(Data)[k]), ylim = c(0, 1.1),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
985 ylab = "positiveness criterion", xlab = "angle ")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
986 debug_plot <- T
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
987 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
988
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
989 # Best angle
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
990 best <- stats::optimize(rms, interval = interval, maximum = TRUE,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
991 y = Data[k,], meth = type.zopc)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
992 ang <- best[["maximum"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
993
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
994
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
995 if (debug_plot) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
996 graphics::abline(v = ang, col = "black")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
997 graphics::text(x = (ang+0.1*ang), y = (y[ang]-0.1*y[ang]), labels = round(ang, 3))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
998 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
999
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1000 # Spectrum rotation
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1001 Spectrum_data[k, ] <- Spectrum_data[k, ] * exp(complex(real = 0, imaginary = ang))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1002 Angle <- c(Angle, ang)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1003 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1004
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1005
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1006
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1007
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1008 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1009 # type.zopc is "manual" -------------------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1010 # if Angle is already specified and no optimisation is needed
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1011 Angle <- angle
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1012
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1013 if (!is.vector(angle)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1014 stop("angle is not a vector")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1015 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1016
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1017 if (!is.numeric(angle)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1018 stop("angle is not a numeric")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1019 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1020
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1021 if (length(angle) != n) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1022 stop(paste("angle has length", length(angle), "and there are", n, "spectra to rotate."))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1023 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1024 for (k in 1:n) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1025 Spectrum_data[k, ] <- Spectrum_data[k, ] * exp(complex(real = 0, imaginary = - angle[k]))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1026 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1027 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1028
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1029
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1030 # Draw spectra
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1031 if (plot_spectra == TRUE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1032 nn <- ceiling(n/4)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1033 i <- 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1034 for (k in 1:nn) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1035 if (createWindow == TRUE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1036 grDevices::dev.new(noRStudioGD = FALSE)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1037 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1038 graphics::par(mfrow = c(4, 2))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1039 while (i <= n) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1040 last <- min(i + 4 - 1, n)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1041 graphics::plot(Re(Spectrum_data[i, ]), type = "l", ylab = "intensity",
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1042 xlab = "Index", main = paste0(rownames(Spectrum_data)[i], " - Real part"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1043 graphics::plot(Im(Spectrum_data[i, ]), type = "l", ylab = "intensity",
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1044 xlab = "Index", main = paste0(rownames(Spectrum_data)[i], " - Imaginary part"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1045 i <- i + 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1046 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1047 i <- last + 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1048 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1049 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1050
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1051
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1052 # Data finalisation ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1053
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1054 Spectrum_data <- endTreatment("ZeroOrderPhaseCorrection", begin_info, Spectrum_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1055 if (returnAngle) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1056 return(list(Spectrum_data = Spectrum_data, Angle = Angle))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1057 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1058 return(Spectrum_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1059 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1060 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1061
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1062
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1063 ## ====================================================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1064 # Baseline Correction
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1065 ## ====================================================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1066 BaselineCorrection <- function(Spectrum_data, ptw.bc = TRUE, maxIter = 42,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1067 lambda.bc = 1e+07, p.bc = 0.05, eps = 1e-08,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1068 ppm.bc = TRUE, exclude.bc = list(c(5.1,4.5)),
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1069 returnBaseline = F) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1070
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1071 # Data initialisation ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1072 begin_info <- beginTreatment("BaselineCorrection", Spectrum_data, force.real = T)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1073 Spectrum_data <- begin_info[["Signal_data"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1074 p <- p.bc
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1075 lambda <- lambda.bc
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1076 n <- dim(Spectrum_data)[1]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1077 m <- dim(Spectrum_data)[2]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1078
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1079
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1080 # Data check
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1081 checkArg(ptw.bc, c("bool"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1082 checkArg(maxIter, c("int", "pos"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1083 checkArg(lambda, c("num", "pos0"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1084 checkArg(p.bc, c("num", "pos0"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1085 checkArg(eps, c("num", "pos0"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1086 checkArg(returnBaseline, c("bool"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1087 checkArg(ppm.bc, c("bool"))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1088 checkArg(unlist(exclude.bc), c("num"), can.be.null = TRUE)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1089
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1090 # Define the interval where to search for (by defining Data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1091 if (is.null(exclude.bc)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1092 exclude_index <- NULL
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1093 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1094 # if ppm.bc == TRUE, then exclude.bc is in the colnames values, else, in the column
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1095 # index
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1096 if (ppm.bc == TRUE) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1097 colindex <- as.numeric(colnames(Spectrum_data))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1098 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1099 colindex <- 1:m
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1100 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1101
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1102 Int <- vector("list", length(exclude.bc))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1103 for (i in 1:length(exclude.bc)) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1104 Int[[i]] <- indexInterval(colindex, from = exclude.bc[[i]][1],
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1105 to = exclude.bc[[i]][2], inclusive = TRUE)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1106 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1107 exclude_index <- unlist(Int)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1108 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1109
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1110 # Baseline Correction implementation definition ----------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1111
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1112 # 2 Ways: either use the function asysm from the ptw package or by
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1113 # built-in functions
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1114 if (ptw.bc) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1115 asysm <- ptw::asysm
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1116 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1117 difsmw <- function(y, lambda, w, d) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1118 # Weighted smoothing with a finite difference penalty cf Eilers, 2003.
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1119 # (A perfect smoother)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1120 # y: signal to be smoothed
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1121 # lambda: smoothing parameter
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1122 # w: weights (use0 zeros for missing values)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1123 # d: order of differences in penalty (generally 2)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1124 m <- length(y)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1125 W <- Matrix::Diagonal(x=w)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1126 E <- Matrix::Diagonal(m)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1127 D <- Matrix::diff(E, differences = d)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1128 C <- Matrix::chol(W + lambda * t(D) %*% D)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1129 x <- Matrix::solve(C, Matrix::solve(t(C), w * y))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1130 return(as.numeric(x))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1131
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1132 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1133 asysm <- function(y, lambda, p, eps, exclude_index) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1134 # Baseline estimation with asymmetric least squares
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1135 # y: signal
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1136 # lambda: smoothing parameter (generally 1e5 to 1e8)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1137 # p: asymmetry parameter (generally 0.001)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1138 # d: order of differences in penalty (generally 2)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1139 # eps: 1e-8 in ptw package
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1140 m <- length(y)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1141 w <- rep(1, m)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1142 i <- 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1143 repeat {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1144 z <- difsmw(y, lambda, w, d = 2)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1145 w0 <- w
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1146 p_vect <- rep((1-p), m) # if y <= z + eps
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1147 p_vect[y > z + eps | y < 0] <- p # if y > z + eps | y < 0
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1148 if(!is.null(exclude_index)){
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1149 p_vect[exclude_index] <- 0 # if exclude area
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1150 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1151
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1152 w <- p_vect
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1153 # w <- p * (y > z + eps | y < 0) + (1 - p) * (y <= z + eps)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1154
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1155 if (sum(abs(w - w0)) == 0) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1156 break
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1157 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1158 i <- i + 1
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1159 if (i > maxIter) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1160 warning("cannot find Baseline estimation in asysm")
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1161 break
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1162 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1163 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1164 return(z)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1165 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1166 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1167
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1168 # Baseline estimation ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1169 Baseline <- matrix(NA, nrow = nrow(Spectrum_data), ncol = ncol(Spectrum_data))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1170
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1171 # for (k in 1:n) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1172 # Baseline[k, ] <- asysm(y = Spectrum_data[k, ], lambda = lambda, p = p, eps = eps)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1173
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1174 if (ptw.bc ){
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1175 Baseline <- apply(Spectrum_data,1, asysm, lambda = lambda, p = p,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1176 eps = eps)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1177 }else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1178 Baseline <- apply(Spectrum_data,1, asysm, lambda = lambda, p = p,
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1179 eps = eps, exclude_index = exclude_index)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1180 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1181
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1182
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1183 Spectrum_data <- Spectrum_data - t(Baseline)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1184 # }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1185
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1186 # Data finalisation ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1187 Spectrum_data <- endTreatment("BaselineCorrection", begin_info, Spectrum_data) # FIXME create removeImaginary filter ??
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1188
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1189 if (returnBaseline) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1190 return(list(Spectrum_data = Spectrum_data, Baseline = Baseline))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1191 } else {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1192 return(Spectrum_data)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1193 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1194 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1195
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1196
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1197
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1198 ## ====================================================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1199 # NegativeValuesZeroing
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1200 ## ====================================================
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1201
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1202 NegativeValuesZeroing <- function(Spectrum_data) {
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1203 # Data initialisation and checks ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1204 begin_info <- beginTreatment("NegativeValuesZeroing", Spectrum_data, force.real = T)
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1205 Spectrum_data <- begin_info[["Signal_data"]]
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1206
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1207 # NegativeValuesZeroing ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1208 Spectrum_data[Spectrum_data < 0] <- 0
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1209
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1210 # Data finalisation ----------------------------------------------
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1211 return(endTreatment("NegativeValuesZeroing", begin_info, Spectrum_data))
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1212 }
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1213
5e64657b4fe5 planemo upload for repository https://github.com/workflow4metabolomics/nmr_preprocessing commit 22ca8782d7c4c0211e13c95b425d4f29f53f995e
lecorguille
parents:
diff changeset
1214