Skip to content

Commit 8e3c8c7

Browse files
committed
fixed function
1 parent ff3c60e commit 8e3c8c7

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

R/BioMartData.R

+25-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
library(magrittr)
22
library(biomaRt)
3+
library(R6)
34

5+
# Placeholder for maybe_create_directory
6+
maybe_create_directory <- function(filepath) {
7+
if (!dir.exists(filepath)) {
8+
dir.create(filepath, recursive = TRUE)
9+
}
10+
}
11+
12+
# Placeholder for log_message
13+
log_message <- function(message) {
14+
cat(paste0("[INFO] ", message, "\n"))
15+
}
416

517
#' BioMartData R6 class
618
#'
@@ -30,7 +42,7 @@ library(biomaRt)
3042
#' biomart$get_data(chromosomes = c("1", "2"), filepath = ".")
3143
#'
3244
#' @export
33-
BioMartData <- R6::R6Class(
45+
BioMartData <- R6Class(
3446
"BioMartData",
3547
public = list(
3648
ensembl = NULL,
@@ -48,30 +60,28 @@ BioMartData <- R6::R6Class(
4860
self$chromosomes <- getBM(attributes = 'chromosome_name', mart = self$ensembl)[, 1]
4961
},
5062

51-
5263
#' @description Fetch gene data from Ensembl BioMart.
5364
#' @param chromosomes Character vector (default NULL). The chromosomes for which data will be fetched.
5465
#' @param filepath Character string (default PROTEOMES_PATH). Path where the fetched data will be stored.
5566
get_data = function(chromosomes = NULL, filepath = PROTEOMES_PATH) {
56-
5767
maybe_create_directory(filepath)
58-
68+
5969
if (!is.null(chromosomes)) {
6070
chromosomes <- intersect(chromosomes, self$chromosomes)
6171
} else {
6272
chromosomes <- self$chromosomes
6373
}
64-
74+
6575
log_message("Fetching data for chromosomes...")
6676
data_list <- list()
6777
file_list <- list() # Create a list to hold the filenames
68-
78+
6979
for (chrom in chromosomes) {
7080
log_message(paste("Fetching data for chromosome", chrom))
7181
tryCatch({
7282
annotLookup <- getBM(
7383
filters = c("chromosome_name"),
74-
values = list(chromosome),
84+
values = list(chrom),
7585
mart = self$ensembl,
7686
attributes = c(
7787
'external_gene_name',
@@ -83,27 +93,27 @@ BioMartData <- R6::R6Class(
8393
),
8494
uniqueRows = TRUE
8595
)
86-
96+
8797
annotLookup <- subset(annotLookup, uniprot_gn_id != '')
8898
filename <- file.path(filepath, paste0("annotLookup_", self$dataset, "_", chrom, ".csv"))
8999
write.csv(annotLookup, filename, row.names = FALSE)
90100
data_list[[chrom]] <- annotLookup
91101
file_list <- c(file_list, filename) # Add the filename to the list
92102
},
93103
error = function(e) {
94-
log_message(paste("Failed to fetch data for chromosome", chromosome, "due to error:", e))
104+
log_message(paste("Failed to fetch data for chromosome", chrom, "due to error:", e))
95105
})
96106
}
97-
107+
98108
log_message("Data fetching completed.")
99-
109+
100110
self$combined_data <- do.call(rbind, data_list)
101111
self$combined_data <- self$combined_data[!(self$combined_data$peptide == "Sequence unavailable"), ]
102-
112+
103113
self$combined_data <- self$combined_data[, c("external_gene_name", "uniprot_gn_id", "chromosome_name", "start_position", "end_position", "peptide")]
104114

105115
colnames(self$combined_data) <- c("GeneName", "SwissID", "chromosome", "start", "end", "seq")
106-
116+
107117
combined_filename <- file.path(filepath, paste0(self$dataset, "_combined.csv"))
108118
file_list <- c(file_list, combined_filename)
109119
write.csv(self$combined_data, combined_filename, row.names = FALSE)
@@ -112,18 +122,17 @@ BioMartData <- R6::R6Class(
112122
quoted_values <- paste0('"', column, '"')
113123
return(quoted_values)
114124
})
115-
125+
116126
quoted_filename <- file.path(filepath, paste0(self$dataset, ".csv"))
117127
write.csv(self$combined_data, quoted_filename, row.names = FALSE, quote = FALSE)
118128

119129
# Remove the temporary files
120130
log_message(paste("Removing temporary files:", file_list))
121-
131+
122132
full_file_paths <- normalizePath(file.path(file_list), mustWork = FALSE)
123133
file.remove(full_file_paths)
124134

125135
log_message("Downloading proteome completed.")
126-
127136
}
128137
)
129138
)

0 commit comments

Comments
 (0)