## ----style, echo = FALSE, results = 'asis', message=FALSE--------------------- BiocStyle::markdown() ##----支持腹部--------------------------------------------------------------------------------------------------------------------库(AnnotationFilter)支持filters()## ----符号滤波器------------------------------------------------------------------------------------------------------------------------------------------------库(AnnotationFilter)Smbl <-Symbolfilter(“ Bcl2”)smbl ## ----符号startswith -------------------------------------------------------------------------------------------------------------------------------------------- smbl <- SymbolFilter("BCL2", condition = "startsWith") smbl ## ----convert-expression--------------------------------------------------------------------------------------------------AnnotationFilter(〜符号==“ bcl2”)smbl ## ---- convert-multi-expression ------------------------------------------------------------------------------------------------------- flt < - AnnotationFilter(〜符号==“ bcl2”&tx_biotype ==“ protein_coding”)flt ## ---------------------询问 - - - - - - - - - - - - - - - - - - - - - - - - --------------- ##定义第一对过滤器的过滤器查询。afl1 < - AnnotationFilterList(Symbolfilter(“ Bcl2l11”),TXBIOTYPEFILTER(“ NONSENSE_MEDIADIAD_DECAY”))##定义第二个过滤器对(应组合括号。Afl2)##现在将逻辑或afl < - AnnotationFilterList(afl1,afl2,logicop =“ |”)结合在一起------------------------------------------------------------- ##定义一个简单的基因表Gene < - data.frame(gene_id = 1:10,符号= c(字母[1:9],“ b”),seq_name = paste0(“ chr”,c(1,4,4,4,4,8,1,1,2、5、3,“ x”,4)),弦乐器= false)基因## ----简单符号-------------------------------------------------------------------------------------------------------------- smbl <- SymbolFilter("b") ## ----简单符号条件---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- condition(smbl) ## ----simple-symbol-value------------------------------------------------------ value(smbl) ## ----simple-symbol-field------------------------------------------------------ field(smbl) ## ----doMatch------------------------------------------------------------------ doMatch <- function(x, filter) { do.call(condition(filter), list(x[, field(filter)], value(filter))) } ## Apply this function doMatch(gene, smbl) ## ----doExtract---------------------------------------------------------------- doExtract <- function(x, filter) { x[doMatch(x, filter), ] } ## Apply it on the data doExtract(gene, smbl) ## ----doMatch-formula---------------------------------------------------------- doMatch <- function(x, filter) { if (is(filter, "formula")) filter <- AnnotationFilter(filter) do.call(condition(filter), list(x[, field(filter)], value(filter))) } doExtract(gene, ~ gene_id == '2') ## ----orgDb, message = FALSE--------------------------------------------------- ## Load the required packages library(org.Hs.eg.db) library(RSQLite) ## Get the database connection dbcon <- org.Hs.eg_dbconn() ## What tables do we have? dbListTables(dbcon) ## ----gene_info---------------------------------------------------------------- ## What fields are there in the gene_info table? dbListFields(dbcon, "gene_info") ## ----doExtractSQL------------------------------------------------------------- doExtractGene <- function(x, filter) { gene <- dbGetQuery(x, "select * from gene_info") doExtract(gene, filter) } ## Extract all entries for BCL2 bcl2 <- doExtractGene(dbcon, SymbolFilter("BCL2")) bcl2 ## ----simpleSQL---------------------------------------------------------------- ## Define a simple function that covers some condition conversion conditionForSQL <- function(x) { switch(x, "==" = "=", x) } ## Define a function to translate a filter into an SQL where condition. ## Character values have to be quoted. where <- function(x) { if (is(x, "CharacterFilter")) value <- paste0("'", value(x), "'") else value <- value(x) paste0(field(x), conditionForSQL(condition(x)), value) } ## Now "translate" a filter using this function where(SeqNameFilter("Y")) ## ----doExtractGene2----------------------------------------------------------- ## Define a function that doExtractGene2 <- function(x, filter) { if (is(filter, "formula")) filter <- AnnotationFilter(filter) query <- paste0("select * from gene_info where ", where(filter)) dbGetQuery(x, query) } bcl2 <- doExtractGene2(dbcon, ~ symbol == "BCL2") bcl2 ## ----performance-------------------------------------------------------------- system.time(doExtractGene(dbcon, ~ symbol == "BCL2")) system.time(doExtractGene2(dbcon, ~ symbol == "BCL2")) ## ----symbol-overwrite--------------------------------------------------------- ## Default method from AnnotationFilter: field(SymbolFilter("a")) ## Overwrite the default method. setMethod("field", "SymbolFilter", function(object, ...) "hgnc_symbol") ## Call to field returns now the "correct" database column field(SymbolFilter("a")) ## ----si----------------------------------------------------------------------- sessionInfo()