---
title: "A note on fimo16 in TFutils"
author: "Vincent J. Carey, stvjc at channing.harvard.edu"
date: "`r format(Sys.time(), '%B %d, %Y')`"
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{A note on fimo16}
  %\VignetteEncoding{UTF-8}
output:
  BiocStyle::html_document:
    highlight: pygments
    number_sections: yes
    theme: united
    toc: yes
---

# Introduction

Sequence-based TF affinity scoring can be conducted with the FIMO
suite, see @Sonawane2017.  We have serialized an object with
references to FIMO outputs for 16 TFs.

```{r lkfimo16}
suppressPackageStartupMessages({
library(TFutils)
library(GenomicRanges)
})
fimo16
```

While the token `bed` is used in the filenames, the files are
not actually bed format!

# Importing with `scanTabix`

We can use `reduceByRange` to import selected scans.

```{r lkimp}
if (.Platform$OS.type != "windows") {
 si = TFutils::seqinfo_hg19_chr17
 myg = GRanges("chr17", IRanges(38.07e6,38.09e6), seqinfo=si)
 colnames(fimo16) = fimo16$HGNC 
 lk2 = GenomicFiles::reduceByRange(fimo16[, c("POU2F1", "VDR")],
   MAP=function(r,f) scanTabix(f, param=r))
 str(lk2)
}
```

This result can be massaged into a GRanges or other desirable structure.
`fimo_granges` takes care of this.

```{r domult}
#fimo_ranges = function(gf, query) { # prototypical code
# rowRanges(gf) = query
# ans = GenomicFiles::reduceByRange(gf, MAP=function(r,f) scanTabix(f, param=r))
# ans = unlist(ans, recursive=FALSE)  # drop top list structure
# tabs = lapply(ans, lapply, function(x) {
#     con = textConnection(x)
#     on.exit(close(con))
#     dtf = read.delim(con, h=FALSE, stringsAsFactors=FALSE, sep="\t")
#     colnames(dtf) = c("chr", "start", "end", "rname", "score", "dir", "pval")
#     ans = with(dtf, GRanges(seqnames=chr, IRanges(start, end),
#            rname=rname, score=score, dir=dir, pval=pval))
#     ans
#     })
# GRangesList(unlist(tabs, recursive=FALSE))
#}
if (.Platform$OS.type != "windows") {
 rr = fimo_granges(fimo16[, c("POU2F1", "VDR")], myg)
 rr
}
```

```{r sesss}
sessionInfo()
```