-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,77 @@ | ||
# BedgraphFiles | ||
|
||
[![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active) | ||
[![Build Status](https://travis-ci.org/CiaranOMara/BedgraphFiles.jl.svg?branch=master)](https://travis-ci.org/CiaranOMara/BedgraphFiles.jl) | ||
[![Build status](https://ci.appveyor.com/api/projects/status/jny2ep4u3cmly8pj/branch/master?svg=true)](https://ci.appveyor.com/project/CiaranOMara/Bedgraphfiles-jl/branch/master) | ||
[![BedgraphFiles](http://pkg.julialang.org/badges/BedgraphFiles_0.6.svg)](http://pkg.julialang.org/?pkg=BedgraphFiles) | ||
[![codecov.io](http://codecov.io/github/CiaranOMara/BedgraphFiles.jl/coverage.svg?branch=master)](http://codecov.io/github/CiaranOMara/BedgraphFiles.jl?branch=master) | ||
|
||
## Overview | ||
|
||
This package provides load and save support for [Bedgraph files](https://github.com/CiaranOMara/Bedgraph.jl) | ||
under the [FileIO.jl](https://github.com/JuliaIO/FileIO.jl) package. | ||
|
||
This package is largely -- if not completely -- inspired by the work of [David Anthoff](https://github.com/davidanthoff). | ||
|
||
## Installation | ||
|
||
Use Pkg.add("BedgraphFiles") in Julia to install BedgraphFiles and its dependencies. | ||
|
||
## Usage | ||
|
||
### Load a Bedgraph file | ||
|
||
To read a Bedgraph file into a ``DataFrame``, use the following Julia code: | ||
|
||
````julia | ||
using FileIO, BedgraphFiles, DataFrames | ||
|
||
df = DataFrame(load("data.bedgraph")) | ||
```` | ||
|
||
The call to ``load`` returns a ``struct`` that is an [IterableTable.jl](https://github.com/davidanthoff/IterableTables.jl), so it can be passed to any function that can handle iterable tables, i.e. all the sinks in [IterableTable.jl](https://github.com/davidanthoff/IterableTables.jl). Here are some examples of materializing a Bedgraph file into data structures that are not a ``DataFrame``: | ||
|
||
````julia | ||
using FileIO, BedgraphFiles, DataTables, IndexedTables, Gadfly | ||
|
||
# Load into a DataTable | ||
dt = DataTable(load("data.bedgraph")) | ||
|
||
# Load into an IndexedTable | ||
it = IndexedTable(load("data.bedgraph")) | ||
|
||
# Plot directly with Gadfly | ||
plot(load("data.bedgraph"), x=:a, y=:b, Geom.line) | ||
```` | ||
|
||
### Save a Bedgraph file | ||
|
||
The following code saves any iterable table as a Bedgraph file: | ||
````julia | ||
using FileIO, BedgraphFiles | ||
|
||
save("output.bedgraph", it) | ||
```` | ||
This will work as long as ``it`` is any of the types supported as sources in [IterableTables.jl](https://github.com/davidanthoff/IterableTables.jl). | ||
|
||
### Using the pipe syntax | ||
|
||
Both ``load`` and ``save`` also support the pipe syntax. For example, to load a Bedgraph file into a ``DataFrame``, one can use the following code: | ||
|
||
````julia | ||
using FileIO, BedgraphFiles, DataFrame | ||
|
||
df = load("data.bedgraph") |> DataFrame | ||
```` | ||
|
||
To save an iterable table, one can use the following form: | ||
|
||
````julia | ||
using FileIO, BedgraphFiles, DataFrame | ||
|
||
df = # Aquire a DataFrame somehow | ||
|
||
df |> save("output.bedgraph") | ||
```` | ||
|
||
The pipe syntax is especially useful when combining it with [Query.jl](https://github.com/davidanthoff/Query.jl) queries, for example one can easily load a Bedgraph file, pipe it into a query, then pipe it to the ``save`` function to store the results in a new file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
julia 0.6 | ||
IterableTables 0.4.2 | ||
FileIO 0.5.1 | ||
DataFrames 0.10.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,48 @@ | ||
module BedgraphFiles | ||
|
||
# package code goes here | ||
# using Bedgraph | ||
using IterableTables, DataValues, DataFrames | ||
import FileIO | ||
|
||
struct BedgraphFile | ||
filename::String | ||
end | ||
|
||
function load(f::FileIO.File{FileIO.format"Bedgraph"}) | ||
return BedgraphFile(f.filename) | ||
end | ||
|
||
IterableTables.isiterable(x::BedgraphFile) = true | ||
IterableTables.isiterabletable(x::BedgraphFile) = true | ||
|
||
function IterableTables.getiterator(file::BedgraphFile) | ||
|
||
# df = Bedgraph.read(file.filename, DataFrame) | ||
|
||
data = readdlm(file.filename) | ||
|
||
df = DataFrame(Chromosome=data[:,1], Start=data[:,2], End=data[:,3], Value=data[:,4]) | ||
|
||
it = getiterator(df) | ||
|
||
return it | ||
end | ||
|
||
function save(f::FileIO.File{FileIO.format"Bedgraph"}, data) | ||
isiterabletable(data) || error("Can't write this data to a Bedgraph file.") | ||
|
||
it = getiterator(data) | ||
|
||
# ds = IterableTables.get_datastreams_source(it) | ||
try | ||
# Bedgraph.write(f.filename, ds) | ||
output = [convert(Array,it.df[:Chromosome]) convert(Array,it.df[:Start]) convert(Array,it.df[:End]) convert(Array,it.df[:Value]) ] | ||
|
||
writedlm(f.filename, output) | ||
# finally | ||
# Data.close!(ds) | ||
end | ||
|
||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
chr19 49302000 49302300 -1.0 | ||
chr19 49302300 49302600 -0.75 | ||
chr19 49302600 49302900 -0.50 | ||
chr19 49302900 49303200 -0.25 | ||
chr19 49303200 49303500 0.0 | ||
chr19 49303500 49303800 0.25 | ||
chr19 49303800 49304100 0.50 | ||
chr19 49304100 49304400 0.75 | ||
chr19 49304400 49304700 1.00 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,33 @@ | ||
using FileIO | ||
using IterableTables | ||
using BedgraphFiles | ||
using DataFrames | ||
using Base.Test | ||
|
||
# write your own tests here | ||
@test 1 == 2 | ||
add_format(format"Bedgraph", (), [".bedgraph"], [:BedgraphFiles]) | ||
|
||
@testset "BedgraphFiles" begin | ||
|
||
df = load(joinpath(@__DIR__, "data.bedgraph")) |> DataFrame | ||
|
||
@test size(df) == (9,4) | ||
|
||
@test df[:Chromosome] == ["chr19", "chr19", "chr19", "chr19", "chr19", "chr19", "chr19", "chr19", "chr19"] | ||
@test df[:Start] == [49302000, 49302300, 49302600, 49302900, 49303200, 49303500, 49303800, 49304100, 49304400] | ||
@test df[:End] == [49302300, 49302600, 49302900, 49303200, 49303500, 49303800, 49304100, 49304400, 49304700] | ||
@test df[:Value] == [-1.0, -0.75, -0.50, -0.25, 0.0, 0.25, 0.50, 0.75, 1.00] | ||
|
||
output_filename = tempname() * ".bedgraph" | ||
|
||
try | ||
df |> save(output_filename) | ||
|
||
df2 = load(output_filename) |> DataFrame | ||
|
||
@test df == df2 | ||
finally | ||
gc() | ||
rm(output_filename) | ||
end | ||
|
||
end |