GenomicsBootCamp

Extract gene content

Mar 9th, 2021
1,612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.05 KB | None | 0 0
  1. ######################################
  2. # Explore genomics regions           #
  3. ######################################
  4.  
  5. # Script file for the video "Hunt for genes! | How to extract gene content of genomic regions" on the Genomics Boot Camp YouTube channel
  6.  
  7. # Clear workspace
  8. rm(list = ls())
  9. # Set working directory
  10. setwd("d:/analysis/2020_GenomicsBootCamp_Demo/")
  11.  
  12. # load packages
  13. if (!require("tidyverse")) {
  14.   install.packages("tidyverse", dependencies = TRUE)
  15.   library(tidyverse)
  16. }
  17. if (!require("qqman")) {
  18.   install.packages("qqman", dependencies = TRUE)
  19.   library(qqman)
  20. }
  21.  
  22.  
  23. #load and visualize Fst results
  24. data <- read_delim("FstResults.fst", delim = "\t") %>%
  25.   drop_na()
  26.  
  27. manhattan(data, chr = "CHR", bp = "POS", p = "FST", logp = F, suggestiveline = 0.8)
  28.  
  29.  
  30. #########################
  31. # Regions of interest (RoI)
  32. #########################
  33.  
  34. # Required input data format: chromosome:startPosition:endPosition, e.g. "1:13280734:31789988"
  35.  
  36. segmentsBiomart <- data %>%
  37.   filter(FST >= 0.8) %>%
  38.   mutate(RoI = paste(CHR,POS-250000,POS+250000,sep = ":")) %>%
  39.   dplyr::select(RoI) %>%
  40.   t()
  41.  
  42. write_delim(as.data.frame(t(segmentsBiomart)), "RoI4BioMart.txt", col_names = F,delim = ",")
  43.  
  44. ################################
  45. # Extract genomic regions with biomaRt
  46. ################################
  47.  
  48. if (!requireNamespace("BiocManager", quietly = TRUE))
  49.   install.packages("BiocManager")
  50. BiocManager::install("biomaRt")
  51.  
  52. library("biomaRt")
  53.  
  54. browseVignettes("biomaRt")
  55.  
  56. listMarts(host="www.ensembl.org")
  57. ensembl <- useMart("ensembl")
  58. datasets <- listDatasets(ensembl) ## to show names of all databases
  59.  
  60. ## protein coding genes - for all genes detele "biotype"
  61. ensembl = useMart(biomart="ENSEMBL_MART_ENSEMBL",dataset="chircus_gene_ensembl")
  62.  
  63. attributes = listAttributes(ensembl) ## to show name of all possibilities
  64.  
  65. filters = listFilters(ensembl)
  66.  
  67. biomaRt_results=getBM(attributes = c("ensembl_gene_id", "external_gene_name", "gene_biotype",
  68.                                    "go_id", "name_1006", "definition_1006", "go_linkage_type", "namespace_1003",
  69.                                    "kegg_enzyme",
  70.                                    "chromosome_name", "start_position", "end_position","description"),
  71.                     filters = c("chromosomal_region","biotype"),
  72.                     values =list(chromosomal_region=segmentsBiomart,biotype="protein_coding"),
  73.                     mart =ensembl)
  74.  
  75. #write out results
  76. # take care, with a lot of attributes and segments the txt files could be large
  77. write_delim(biomaRt_results, "resultsFromBioMart.txt", delim = ";")
  78.  
  79. ################################
  80. # post-processing of biomaRt results
  81. ################################
  82.  
  83. # read back results to extract gene names or follow up with post processing
  84. # biomaRt_results <- read_delim("resultsFromBioMart.txt", delim = ";")
  85.  
  86. # example - list of gene names
  87. geneNames <- biomaRt_results %>%
  88.   drop_na(external_gene_name) %>%
  89.   dplyr::select(external_gene_name) %>%
  90.   unique() %>%
  91.   write_delim("listOfGenes_Fst_AngGum.txt", delim = ";")
  92.  
Advertisement
Add Comment
Please, Sign In to add comment