-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathScriptRentrezPackage_EMP.R
More file actions
173 lines (158 loc) · 7.58 KB
/
Copy pathScriptRentrezPackage_EMP.R
File metadata and controls
173 lines (158 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
################################################################################
################################################################################
################################################################################
################################################################################
# NCBI extraction information
# https://docs.ropensci.org/rentrez/articles/rentrez_tutorial.html#getting-summary-data-entrez_summary
################################################################################
################################################################################
################################################################################
################################################################################
# Package Rentrez
library(rentrez)
library(xml2)
################################################################################
################################################################################
################################################################################
################################################################################
# Working Directory
getwd()
setwd("C:YOUR/WORKING/DIRECTORY")
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
# Extract the information in a metadata table
# Lista de Run IDs
#run_ids <- c("ERR2860778", "ERR1524111", "ERR2205480", "ERR139681", "ERR579243", "SRR1021873", "SRR5950850")
run_ids <- readLines("ListRun_EMP.txt")
# Crear un data frame vacío para almacenar los resultados
metadata <- data.frame(
Run = character(),
LIBRARY_STRATEGY = character(),
SPOT_LENGTH = numeric(),
total_bases = numeric(),
BioProject = character(),
BioSample = character(),
size = numeric(),
center_name = character(),
Collection_Date = character(),
DATASTORE_filetype = character(),
DATASTORE_provider = character(),
DATASTORE_region = character(),
Depth = numeric(),
Experiment = character(),
geo_loc_name_country = character(),
host_common_name = character(),
host_latin_name = character(),
INSTRUMENT_MODEL = character(),
lat_lon = character(),
LIBRARY_NAME = character(),
LibraryLayout = character(),
LibrarySelection = character(),
LibrarySource = character(),
SCIENTIFIC_NAME = character(),
Platform = character(),
published = character(),
alias = character(),
STUDY_REF_accession = character(),
env_broad_scale = character(),
env_local_scale = character(),
env_medium = character(),
stringsAsFactors = FALSE
)
# Función para extraer datos con manejo de errores
extraer_valor <- function(xml, xpath) {
nodo <- xml_find_first(xml, xpath)
if (!is.null(nodo) && !is.na(nodo)) {
return(xml_text(nodo))
} else {
return(NA)
}
}
# Procesar cada Run ID
for (run in run_ids) {
cat("Procesando:", run, "\n")
# Buscar el Run ID en la base de datos SRA
search_result <- tryCatch(
entrez_search(db = "sra", term = run),
error = function(e) {
cat("Error en la búsqueda de", run, ":", e$message, "\n")
return(NULL)
}
)
if (!is.null(search_result) && length(search_result$ids) > 0) {
# Obtener el XML con detalles del Run ID
record <- tryCatch(
entrez_fetch(db = "sra", id = search_result$ids, rettype = "xml"),
error = function(e) {
cat("Error al obtener detalles para", run, ":", e$message, "\n")
return(NULL)
}
)
if (!is.null(record)) {
# Parsear el texto XML con xml2
xml <- read_xml(record)
# Extraer datos específicos usando XPath
metadata_row <- data.frame(
Run = run,
LIBRARY_STRATEGY = extraer_valor(xml, "//LIBRARY_STRATEGY"),
SPOT_LENGTH = as.numeric(extraer_valor(xml, "//SPOT_LENGTH")),
total_bases = as.numeric(extraer_valor(xml, "//RUN/@total_bases")),
BioProject = extraer_valor(xml, "//EXTERNAL_ID[@namespace='BioProject']"),
BioSample = extraer_valor(xml, "//EXTERNAL_ID[@namespace='BioSample']"),
size = as.numeric(extraer_valor(xml, "//RUN/@size")),
center_name = extraer_valor(xml, "//CENTER_NAME"),
Collection_Date = extraer_valor(xml, "//SAMPLE_ATTRIBUTE[TAG='collection date']/VALUE"),
DATASTORE_filetype = paste(sapply(xml_find_all(xml, "//CloudFile/@filetype"), xml_text), collapse = ","),
DATASTORE_provider = paste(sapply(xml_find_all(xml, "//CloudFile/@provider"), xml_text), collapse = ","),
DATASTORE_region = paste(sapply(xml_find_all(xml, "//CloudFile/@location"), xml_text), collapse = ","),
Depth = as.numeric(extraer_valor(xml, "//SAMPLE_ATTRIBUTE[TAG='geographic location (depth)']/VALUE")),
Experiment = extraer_valor(xml, "//EXPERIMENT/@accession"),
geo_loc_name_country = extraer_valor(xml, "//SAMPLE_ATTRIBUTE[TAG='geographic location (country and/or sea)']/VALUE"),
host_common_name = extraer_valor(xml, "//SAMPLE_ATTRIBUTE[TAG='common name']/VALUE"),
host_latin_name = extraer_valor(xml, "//SAMPLE_NAME/SCIENTIFIC_NAME"),
INSTRUMENT_MODEL = extraer_valor(xml, "//INSTRUMENT_MODEL"),
lat_lon = paste(extraer_valor(xml, "//SAMPLE_ATTRIBUTE[TAG='geographic location (latitude)']/VALUE"),
extraer_valor(xml, "//SAMPLE_ATTRIBUTE[TAG='geographic location (longitude)']/VALUE"), sep = " "),
LIBRARY_NAME = extraer_valor(xml, "//LIBRARY_NAME"),
LibraryLayout = extraer_valor(xml, "//LIBRARY_LAYOUT/*[1]/name()"),
LibrarySelection = extraer_valor(xml, "//LIBRARY_SELECTION"),
LibrarySource = extraer_valor(xml, "//LIBRARY_SOURCE"),
SCIENTIFIC_NAME = extraer_valor(xml, "//SCIENTIFIC_NAME"),
Platform = extraer_valor(xml, "//PLATFORM/*[1]/name()"),
published = extraer_valor(xml, "//RUN/@published"),
alias = extraer_valor(xml, "//SAMPLE/@alias"),
STUDY_REF_accession = extraer_valor(xml, "//STUDY/@accession"),
env_broad_scale = extraer_valor(xml, "//SAMPLE_ATTRIBUTE[TAG='environment (biome)']/VALUE"),
env_local_scale = extraer_valor(xml, "//SAMPLE_ATTRIBUTE[TAG='environment (feature)']/VALUE"),
env_medium = extraer_valor(xml, "//SAMPLE_ATTRIBUTE[TAG='environment (material)']/VALUE"),
stringsAsFactors = FALSE
)
# Agregar la información al data frame principal
metadata <- rbind(metadata, metadata_row)
} else {
cat("No se pudo procesar el XML para el Run ID:", run, "\n")
}
} else {
cat("No se encontró información para el Run ID:", run, "\n")
}
}
# Check Metadata
#View(metadata)
print("Metadata Done")
# Guardar el resultado en un archivo CSV
output_file <- "metadata_sra_EMP.csv"
write.csv(metadata, output_file, row.names = FALSE)
print("Save Metadata")
# Mostrar un resumen del resultado final
cat("\nProceso completado. Resultados guardados en:", output_file, "\n")
print("(^_^)")
# END (^_^)
################################################################################
################################################################################
################################################################################