0

I have a data.table with 1 Million rows with each cell looking like this:

ENST00000408384 // ENSEMBL // ncrna:miRNA chromosome:GRCh37:1:30366:30503:1 gene:ENSG00000221311 gene_biotype:miRNA transcript_biotype:miRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000469289 // ENSEMBL // havana:known chromosome:GRCh38:1:30267:31109:1 gene:ENSG00000243485 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000473358 // ENSEMBL // havana:known chromosome:GRCh38:1:29554:31097:1 gene:ENSG00000243485 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000002840 // Havana transcript // novel transcript[gene_biotype:lincRNA transcript_biotype:lincRNA] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000002841 // Havana transcript // novel transcript[gene_biotype:lincRNA transcript_biotype:lincRNA] // chr1 // 100 // 100 // 0 // --- // 0

I need to extract what comes immediately after "gene_biotype:" (in this case it would "miRNA"). How to do that?

I tried finding a solution with stringR and regex and gave up after several hours. Appreciate your help. Thanks.

2
  • 2
    There is gene_biotype:lincRNA as well. Don't you want to take that also? Commented Dec 30, 2020 at 19:10
  • sure, this is also necessary. Thanks for the comment. Commented Dec 30, 2020 at 19:21

3 Answers 3

1

You could try regmatches with regexpr.

regmatches(x, regexpr("(?<=gene_biotype\\:)\\w*", x, perl=TRUE))
# [1] "miRNA"

Data:

x <- "
ENST00000408384 // ENSEMBL // ncrna:miRNA chromosome:GRCh37:1:30366:30503:1 gene:ENSG00000221311 gene_biotype:miRNA transcript_biotype:miRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000469289 // ENSEMBL // havana:known chromosome:GRCh38:1:30267:31109:1 gene:ENSG00000243485 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000473358 // ENSEMBL // havana:known chromosome:GRCh38:1:29554:31097:1 gene:ENSG00000243485 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000002840 // Havana transcript // novel transcript[gene_biotype:lincRNA transcript_biotype:lincRNA] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000002841 // Havana transcript // novel transcript[gene_biotype:lincRNA transcript_biotype:lincRNA] // chr1 // 100 // 100 // 0 // --- // 0
"
Sign up to request clarification or add additional context in comments.

Comments

1

We can use regex lookaround with str_extract

library(stringr)
str_extract(df1$col1, "(?<=gene_biotype:)\\w+")
#[1] "miRNA"

If we need all the elements, use str_extract_all

str_extract_all(df1$col1, "(?<=gene_biotype:)\\w+")
#[[1]]
#[1] "miRNA"   "lincRNA" "lincRNA" "lincRNA" "lincRNA"

data

df1 <- structure(list(col1 = "\nENST00000408384 // ENSEMBL // ncrna:miRNA chromosome:GRCh37:1:30366:30503:1 gene:ENSG00000221311 gene_biotype:miRNA transcript_biotype:miRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000469289 // ENSEMBL // havana:known chromosome:GRCh38:1:30267:31109:1 gene:ENSG00000243485 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000473358 // ENSEMBL // havana:known chromosome:GRCh38:1:29554:31097:1 gene:ENSG00000243485 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000002840 // Havana transcript // novel transcript[gene_biotype:lincRNA transcript_biotype:lincRNA] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000002841 // Havana transcript // novel transcript[gene_biotype:lincRNA transcript_biotype:lincRNA] // chr1 // 100 // 100 // 0 // --- // 0\n"), class = "data.frame", row.names = c(NA, 
-1L))

Comments

0

Maybe not the best but it works fine.

splited <- strsplit(text," ")[[1]]
splited <-gsub("*.gene_biotype:","",splited )
unique(gsub("gene_biotype:","",splited[grepl("gene_biotype:",splited)]))

gives,

"miRNA"   "lincRNA"

Data:

text <- "ENST00000408384 // ENSEMBL // ncrna:miRNA chromosome:GRCh37:1:30366:30503:1 gene:ENSG00000221311 gene_biotype:miRNA transcript_biotype:miRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000469289 // ENSEMBL // havana:known chromosome:GRCh38:1:30267:31109:1 gene:ENSG00000243485 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// ENST00000473358 // ENSEMBL // havana:known chromosome:GRCh38:1:29554:31097:1 gene:ENSG00000243485 gene_biotype:lincRNA transcript_biotype:lincRNA // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000002840 // Havana transcript // novel transcript[gene_biotype:lincRNA transcript_biotype:lincRNA] // chr1 // 100 // 100 // 0 // --- // 0 /// OTTHUMT00000002841 // Havana transcript // novel transcript[gene_biotype:lincRNA transcript_biotype:lincRNA] // chr1 // 100 // 100 // 0 // --- // 0"

Comments