|
15 | 15 | #' #-------------------------# |
16 | 16 | #' ## NON-PAIRED ## |
17 | 17 | #' #-------------------------# |
18 | | -#' # Load required library |
19 | 18 | #' library(data.table) |
20 | | -#' |
| 19 | +#' |
21 | 20 | #' # Define parameters and variables |
22 | | -#' sample_ids <- c("S1_A", "S2_A", "S3_A", "S4_B", "S5_B", "S6_B") |
| 21 | +#' sample_ids <- c("S1", "S2", "S3", "S4", "S5", "S6") |
| 22 | +#' groups <- c("A", "A", "B", "B", "C", "C") |
23 | 23 | #' feature_ids <- c("Feature1", "Feature2", "Feature3") |
24 | | -#' |
| 24 | +#' |
25 | 25 | #' # Simulated abundance matrix (features x samples) |
26 | 26 | #' abundances <- matrix( |
27 | 27 | #' c( |
28 | | -#' # Feature1 (e.g. GenusA) |
29 | | -#' 100, 120, 110, 55, 60, 65, |
30 | | -#' # Feature2 (e.g. GenusB) |
31 | | -#' 50, 65, 60, 130, 120, 125, |
32 | | -#' # Feature3 (e.g. GenusC) |
33 | | -#' 80, 85, 90, 80, 85, 90 |
| 28 | +#' 100, 120, 110, 55, 60, 65, |
| 29 | +#' 50, 65, 60, 130, 120, 125, |
| 30 | +#' 80, 85, 90, 80, 85, 90 |
34 | 31 | #' ), |
35 | 32 | #' nrow = 3, byrow = TRUE, |
36 | 33 | #' dimnames = list(feature_ids, sample_ids) |
37 | 34 | #' ) |
38 | | -#' |
39 | | -#' # A wide table with columns as samples, rows as features |
40 | | -#' # And an additional column as the feature_rank, a column for feature comparison. |
41 | | -#' mock_data <- data.table( |
42 | | -#' Genus = feature_ids, # feature_rank column (e.g. "Genus") |
43 | | -#' S1_A = abundances[ , 1], |
44 | | -#' S2_A = abundances[ , 2], |
45 | | -#' S3_A = abundances[ , 3], |
46 | | -#' S4_B = abundances[ , 4], |
47 | | -#' S5_B = abundances[ , 5], |
48 | | -#' S6_B = abundances[ , 6] |
49 | | -#' ) |
50 | | -#' print(mock_data) |
51 | | -#' |
52 | | -#' # It uses substring matching, and multiple conditions can be used |
| 35 | +#' |
| 36 | +#' # Convert to a data.table |
| 37 | +#' mock_data <- OmicFlow::matrix_to_dtable(abundances) |
| 38 | +#' mock_data$Genus <- feature_ids |
| 39 | +#' |
| 40 | +#' # It uses exact matching and multiple conditions are allowed. |
53 | 41 | #' res <- foldchange( |
54 | 42 | #' data = mock_data, |
55 | 43 | #' feature_rank = "Genus", |
56 | | -#' condition_A = c("_A", "_B"), |
57 | | -#' condition_B = c("_B", "_A"), |
58 | | -#' |
59 | | -#' # This can also be a column wherein, conditions A and B are present |
60 | | -#' condition_labels = sample_ids, |
| 44 | +#' condition_A = c("A", "B"), |
| 45 | +#' condition_B = c("B", "C"), |
| 46 | +#' condition_labels = groups, |
61 | 47 | #' paired = FALSE |
62 | 48 | #' ) |
63 | 49 | #' print(res) |
|
67 | 53 | #' #---------------------# |
68 | 54 | #' library(data.table) |
69 | 55 | #' |
70 | | -#' # Define paired sample ids for 3 pairs: |
71 | | -#' paired_ids <- paste0("Pair", 1:3) |
72 | | -#' |
73 | | -#' # Features: |
74 | | -#' feature_ids <- c("Feature1", "Feature2", "Feature3") |
75 | | -#' |
76 | | -#' # Simulate abundances for each paired sample: |
77 | | -#' # For each pair, we have two samples: condition A and condition B. |
78 | | -#' # Make sure the length of condition A and condition B are the same! |
79 | | -#' |
80 | | -#' # Construct the data.table with features as rows |
81 | | -#' mock_data_paired <- data.table( |
82 | | -#' Genus = feature_ids, |
83 | | -#' Pair1_A = c(100, 50, 80), |
84 | | -#' Pair1_B = c(60, 130, 75), |
85 | | -#' Pair2_A = c(120, 65, 85), |
86 | | -#' Pair2_B = c(60, 120, 90), |
87 | | -#' Pair3_A = c(110, 60, 90), |
88 | | -#' Pair3_B = c(65, 125, 85) |
89 | | -#' ) |
| 56 | +#' # In the paired case both conditions A and B must be of the same length! |
| 57 | +#' # We re-use the above mock_data and only change group labels |
| 58 | +#' groups <- c("A", "A", "B", "B", "A", "B") |
90 | 59 | #' |
91 | 60 | #' res <- foldchange( |
92 | | -#' data = mock_data_paired, |
| 61 | +#' data = mock_data, |
93 | 62 | #' feature_rank = "Genus", |
94 | | -#' condition_A = c("_A", "_B"), |
95 | | -#' condition_B = c("_B", "_A"), |
96 | | -#' |
97 | | -#' # This can also be a column wherein, conditions A and B are present |
98 | | -#' condition_labels = names(mock_data_paired)[-1], |
| 63 | +#' condition_A = c("A"), |
| 64 | +#' condition_B = c("B"), |
| 65 | +#' condition_labels = groups, |
99 | 66 | #' paired = TRUE |
100 | 67 | #' ) |
101 | 68 | #' print(res) |
|
0 commit comments