## ----setup, include=FALSE--------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)

## ----load, warning=FALSE, message=FALSE------------------------------------
library(InterMineR)
listMines()

## ----humanmine, warning=FALSE, message=FALSE-------------------------------

# load HumaMine
im <- initInterMine(mine=listMines()["HumanMine"])
im


## ----get_queries, warning=FALSE, message=FALSE-----------------------------
# Get template (collection of pre-defined queries)
template = getTemplates(im)
head(template)

## ----gene_templates, warning=FALSE, message=FALSE--------------------------

# Get gene-related templates
template[grep("gene", template$name, ignore.case=TRUE),]


## ----gene_orthologs, warning=FALSE, message=FALSE--------------------------
# Query for gene orthologs
queryGeneOrth = getTemplateQuery(
  im = im, 
  name = "Gene_Orth"
)
queryGeneOrth


## ----getModel, warning=FALSE, message=FALSE--------------------------------

model <- getModel(im)
head(model)


## ----gene_type_data, warning=FALSE, message=FALSE--------------------------

model[which(model$type=="Gene"),]


## ----homologue_type, warning=FALSE, message=FALSE--------------------------

model[which(model$type=="Homologue"),]


## ----run_geneorth, warning=FALSE, message=FALSE----------------------------

resGeneOrth <- runQuery(im, queryGeneOrth)
head(resGeneOrth)


## ----modify_query, warning=FALSE, message=FALSE----------------------------

# modify directly the value of the first constraint from the list query
queryGeneOrth$where[[1]][["value"]] <- "ABO"

# or modify the value of the first constraint from the list query with setConstraints
queryGeneOrth$where = setConstraints(
  modifyQueryConstraints = queryGeneOrth,
  m.index = 1,
  values = list("ABO")
)

queryGeneOrth$where


## ----modify_query2, warning=FALSE, message=FALSE---------------------------

resGeneOrth <- runQuery(im, queryGeneOrth)
head(resGeneOrth)


## ----new_constraint, warning=FALSE, message=FALSE--------------------------
newConstraint <- list(
  path=c("Gene.homologues.homologue.organism.shortName"),
  op=c("!="), 
  value=c("H. sapiens"), 
  code=c("B")
)

queryGeneOrth$where[[2]] <- newConstraint
queryGeneOrth$where


## ----new_constraint2, warning=FALSE, message=FALSE-------------------------
resGeneOrth <- runQuery(im, queryGeneOrth)
resGeneOrth

## ----add_column, warning=FALSE, message=FALSE------------------------------
model[which(model$type=="Homologue"),]

## ----add_column2, warning=FALSE, message=FALSE-----------------------------
model[which(model$type=="DataSet"),]

## ----add_column3, warning=FALSE, message=FALSE-----------------------------

# use setQuery function which will create an InterMineR-class query
queryGeneOrth.InterMineR = setQuery(
  inheritQuery = queryGeneOrth,
  select = c(queryGeneOrth$select, 
             "Gene.homologues.dataSets.name")
  )

getSelect(queryGeneOrth.InterMineR)
#queryGeneOrth.InterMineR@select

# or assign new column directly to the existing list query
queryGeneOrth$select[[6]] <- "Gene.homologues.dataSets.name"
queryGeneOrth$select

# run queries
resGeneOrth.InterMineR <- runQuery(im, queryGeneOrth.InterMineR)
resGeneOrth <- runQuery(im, queryGeneOrth)

all(resGeneOrth == resGeneOrth.InterMineR)

head(resGeneOrth, 3)


## ----constrant_logic, warning=FALSE, message=FALSE-------------------------

queryGeneOrth$constraintLogic <- "A and B"
queryGeneOrth$constraintLogic


## ----constrant_logic2, warning=FALSE, message=FALSE------------------------

resGeneOrth <- runQuery(im, queryGeneOrth)
resGeneOrth


## ----gene_go, warning=FALSE, message=FALSE---------------------------------

queryGeneGO <- getTemplateQuery(im, "Gene_GO")
queryGeneGO


## ----gene_go2, warning=FALSE, message=FALSE--------------------------------

queryGeneGO$select <- queryGeneGO$select[2:5]
queryGeneGO$select


## ----gene_go3, warning=FALSE, message=FALSE--------------------------------
queryGeneGO$where[[1]][["value"]] <- "ABO"
queryGeneGO$where

## ----gene_go4, warning=FALSE, message=FALSE--------------------------------
resGeneGO <- runQuery(im, queryGeneGO )
head(resGeneGO)

## ----metal_ion_binding, warning=FALSE, message=FALSE-----------------------
queryGOGene <- getTemplateQuery(im, "GOterm_Gene")
queryGOGene

## ----metal_ion_binding2, warning=FALSE, message=FALSE----------------------
queryGOGene$select <- queryGOGene$select[2:5]
queryGOGene$select

## ----metal_ion_binding3, warning=FALSE, message=FALSE----------------------

queryGOGene$where[[1]]$value = "metal ion binding"
queryGOGene$where


## ----metal_ion_binding4, warning=FALSE, message=FALSE----------------------

resGOGene <- runQuery(im, queryGOGene )
head(resGOGene)


## ----neighbor_genes, warning=FALSE, message=FALSE--------------------------

queryGeneLoc = getTemplateQuery(im, "Gene_Location")
queryGeneLoc$where[[2]][["value"]] = "ABCA6"
resGeneLoc= runQuery(im, queryGeneLoc)

resGeneLoc


## ----neighbor_genes2, warning=FALSE, message=FALSE-------------------------

# set constraints
constraints = setConstraints(
  paths = c(
    "Gene.chromosome.primaryIdentifier",
    "Gene.locations.start",
    "Gene.locations.end",
    "Gene.organism.name"
  ),
  operators = c(
    "=",
    ">=",
    "<=",
    "="
  ),
  values = list(
    resGeneLoc[1, "Gene.chromosome.primaryIdentifier"],
    as.character(as.numeric(resGeneLoc[1, "Gene.locations.start"])-50000),
    as.character(as.numeric(resGeneLoc[1, "Gene.locations.end"])+50000),
    "Homo sapiens"
  )
)

# set InterMineR-class query
queryNeighborGene = setQuery(
  select = c("Gene.primaryIdentifier", 
             "Gene.symbol",
             "Gene.chromosome.primaryIdentifier",
             "Gene.locations.start", 
             "Gene.locations.end", 
             "Gene.locations.strand"),
  where = constraints
)

summary(queryNeighborGene)


## ----neighbor_genes6, warning=FALSE, message=FALSE-------------------------

resNeighborGene <- runQuery(im, queryNeighborGene)
resNeighborGene


## ----neighbor_genes7, warning=FALSE, message=FALSE-------------------------

resNeighborGene$Gene.locations.strand[which(resNeighborGene$Gene.locations.strand==1)]="+"

resNeighborGene$Gene.locations.strand[which(resNeighborGene$Gene.locations.strand==-1)]="-"

gene.idx = which(nchar(resNeighborGene$Gene.symbol)==0)

resNeighborGene$Gene.symbol[gene.idx]=resNeighborGene$Gene.primaryIdentifier[gene.idx]

## ----load_gviz, warning=FALSE, message=FALSE-------------------------------
require(Gviz)

## ----plottracks, warning=FALSE, message=FALSE------------------------------
annTrack = AnnotationTrack(
  start=resNeighborGene$Gene.locations.start,
  end=resNeighborGene$Gene.locations.end,
  strand=resNeighborGene$Gene.locations.strand,
  chromosome=resNeighborGene$Gene.chromosome.primaryIdentifier[1],
  genome="GRCh38", 
  name="around ABCA6",
  id=resNeighborGene$Gene.symbol)

gtr <- GenomeAxisTrack()
itr <- IdeogramTrack(genome="hg38", chromosome="chr17")

plotTracks(list(gtr, itr, annTrack), shape="box", showFeatureId=TRUE, fontcolor="black")


## ----sessioInfo------------------------------------------------------------

sessionInfo()


