| 
									
										
										
										
											2021-12-05 13:23:00 +01:00
										 |  |  | library(data.table) | 
					
						
							|  |  |  | library(httr) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ensembl_api_url <- "https://rest.ensembl.org" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #' Perform a request to the Ensembl REST API. | 
					
						
							|  |  |  | ensembl_request <- function(api_path) { | 
					
						
							| 
									
										
										
										
											2022-05-26 12:42:19 +02:00
										 |  |  |   content(stop_for_status(GET( | 
					
						
							|  |  |  |     paste0(ensembl_api_url, api_path), | 
					
						
							|  |  |  |     content_type_json() | 
					
						
							|  |  |  |   ))) | 
					
						
							| 
									
										
										
										
											2021-12-05 13:23:00 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #' Get IDs of all available vertebrates. | 
					
						
							|  |  |  | get_species_ids <- function() { | 
					
						
							| 
									
										
										
										
											2022-05-26 12:42:19 +02:00
										 |  |  |   species <- ensembl_request("/info/species")$species | 
					
						
							|  |  |  |   sapply(species, function(species) species$name) | 
					
						
							| 
									
										
										
										
											2021-12-05 13:23:00 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #' Get all chromosomes names for a species. | 
					
						
							|  |  |  | get_species_chromosomes <- function(species_id) { | 
					
						
							| 
									
										
										
										
											2022-05-26 12:42:19 +02:00
										 |  |  |   chromosomes <- unlist(ensembl_request( | 
					
						
							|  |  |  |     paste0("/info/assembly/", species_id) | 
					
						
							|  |  |  |   )$karyotype) | 
					
						
							| 
									
										
										
										
											2021-12-05 13:23:00 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #' Get a vector of all available unqiue chromosome names. | 
					
						
							|  |  |  | #' | 
					
						
							|  |  |  | #' There are multiple names for mitochondrial DNA which have to be removed | 
					
						
							|  |  |  | #' manually, unfortunately. | 
					
						
							|  |  |  | get_all_chromosomes <- function() { | 
					
						
							| 
									
										
										
										
											2022-05-26 12:42:19 +02:00
										 |  |  |   chromosomes <- sapply(get_species_ids(), get_species_chromosomes) | 
					
						
							|  |  |  |   unique(unlist(chromosomes)) | 
					
						
							| 
									
										
										
										
											2021-12-05 13:23:00 +01:00
										 |  |  | } |