Skip to content

Instantly share code, notes, and snippets.

@crazyhottommy
Created October 26, 2016 16:18
Show Gist options
  • Select an option

  • Save crazyhottommy/72e17157d7aaea245fce645c9a2c66b1 to your computer and use it in GitHub Desktop.

Select an option

Save crazyhottommy/72e17157d7aaea245fce645c9a2c66b1 to your computer and use it in GitHub Desktop.
```r
library(TxDb.Hsapiens.UCSC.hg19.knownGene)
UCSC.hg19<- TxDb.Hsapiens.UCSC.hg19.knownGene
hg19.genes<- genes(UCSC.hg19)
transcriptsBy(UCSC.hg19, "gene")
library("org.Hs.eg.db")
## note that dplyr and AnnotationDbi both have a function called select
## use dplyr::select when use dplyr
gene_symbol<- AnnotationDbi::select(org.Hs.eg.db, keys=hg19.genes$gene_id,
columns="SYMBOL", keytype="ENTREZID")
all.equal(hg19.genes$gene_id, gene_symbol$ENTREZID)
hg19.genes$gene_id<- gene_symbol$SYMBOL
hg19.genes
## import the chrosome arm file
library(rtracklayer)
chrom.arm<- import("~/annotations/human/hg19_UCSC_genome/chrom_pq_arm.bed", format = "BED")
gene.hits<- findOverlaps(hg19.genes, chrom.arm, ignore.strand = TRUE)
queryHits(gene.hits)
## error below
genes.df <- as.data.frame(hg19.genes[queryHits(gene.hits)])
## error below
hg19.genes[queryHits(gene.hits)]$id<- chrom.arm[subjectHits(gene.hits)]$name
## one gene expands the p and q arm, suspicious https://support.bioconductor.org/p/88725/
## turns out this non-coding gene has two transcripts very far away.
hg19.genes[9349]
findOverlaps(hg19.genes[9349], chrom.arm)
chrom.arm[c(17,18)]
##########
### modify this gene first
hg19.genes.mod<- hg19.genes
hg19.genes.mod['286297']<- GRanges("chr9", ranges= IRanges(start = 42844370, end = 42859085), strand = "-", gene_id = "LOC286297")
gene.hits<- findOverlaps(hg19.genes.mod, chrom.arm, ignore.strand = TRUE)
queryHits(gene.hits)
genes.df <- as.data.frame(hg19.genes.mod[queryHits(gene.hits)])
genes.df$arm<- chrom.arm[subjectHits(gene.hits)]$name
library(dplyr)
gene_count_arm<- genes.df %>% group_by(seqnames, arm) %>% summarise(gene_count = n())
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment