Skip to content

Commit

Permalink
Merge branch 'release/v0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
CiaranOMara committed Aug 20, 2017
2 parents afa570b + fce31eb commit 771144c
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 3 deletions.
76 changes: 76 additions & 0 deletions README.md
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.
3 changes: 3 additions & 0 deletions REQUIRE
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
45 changes: 44 additions & 1 deletion src/BedgraphFiles.jl
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
9 changes: 9 additions & 0 deletions test/data.bedgraph
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
32 changes: 30 additions & 2 deletions test/runtests.jl
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

0 comments on commit 771144c

Please sign in to comment.