bio-tabix 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rvmrc ADDED
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
+ environment_id="ruby-1.9.2-p290@tabix"
8
+
9
+ #
10
+ # First we attempt to load the desired environment directly from the environment
11
+ # file. This is very fast and efficient compared to running through the entire
12
+ # CLI and selector. If you want feedback on which environment was used then
13
+ # insert the word 'use' after --create as this triggers verbose mode.
14
+ #
15
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
16
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
17
+ then
18
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
19
+
20
+ if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
21
+ then
22
+ . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
23
+ fi
24
+ else
25
+ # If the environment file has not yet been created, use the RVM CLI to select.
26
+ if ! rvm --create "$environment_id"
27
+ then
28
+ echo "Failed to create RVM environment ''."
29
+ fi
30
+ fi
31
+
32
+ #
33
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
34
+ # it be automatically loaded. Uncomment the following and adjust the filename if
35
+ # necessary.
36
+ #
37
+ # filename=".gems"
38
+ # if [[ -s "$filename" ]] ; then
39
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
40
+ # fi
41
+
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ gem "ffi"
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "shoulda"
9
+ gem "rdoc"
10
+ gem "bundler"
11
+ gem "jeweler"
12
+ gem "simplecov"
13
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Nicholas A. Thrower
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,82 @@
1
+ = bio-tabix
2
+
3
+ Ruby binding for the tabix file indexing routines within the samtools package
4
+ http://samtools.sourceforge.net/
5
+
6
+ Tabix provides utilities for indexing and subsequently querying regions of interest from large tab delimited files.
7
+
8
+ Files are indexed on three columns: [group, pos1, pos2] and must be position sorted
9
+
10
+ This gem was modeled on the bio-samtools gem: https://github.com/helios/bioruby-samtools
11
+
12
+ == Installation
13
+ gem install bio-tabix
14
+
15
+ == Usage
16
+ === Compress your file
17
+ The following command will run the bgzip utility.
18
+ You must supply input and output filenames. If you don't compress your
19
+ data first, it will be compressed when the file is opened appending '.bgzf' to the filename.
20
+ Bio::Tabix::TFile.compress(in_file,compressed_file)
21
+
22
+ === Index the file
23
+ Build an index by supplying the required columns.
24
+ If you don't create an index the open method will use the default options.
25
+ The opts parameter takes a hash of index build options
26
+ - :s => sequence/group column [1]
27
+ - :b => beginning range column [2]
28
+ - :e => ending range column. Can equal :b [3]
29
+ - :meta_char => comment character [#]
30
+ - :line_skip => number of initial lines to ignore [0]
31
+ tabix_file = Bio::Tabix::TFile.build_index(compressed_file, {:s => group_col, :b => pos1_col, :e => pos2_col})
32
+
33
+
34
+ === Open your file
35
+ Create a new TFile instance and open it.
36
+ tabix_file = Bio::Tabix::TFile.open(compressed_file)
37
+
38
+ === Create a proc or lambda
39
+ This will be called with the value of each fileline
40
+ my_func = lambda do |line|
41
+ # convert text to array and print column index 3
42
+ puts line.split("\t")[3]
43
+ end
44
+
45
+ === Process a region
46
+ Choose a group and range for your function and process it
47
+ tabix_file.process_region('g2', 4, 10, my_func)
48
+
49
+ == Example
50
+ === File
51
+ g1 1 3 a1 b1
52
+ g1 2 3 a2 b2
53
+ g1 3 4 a3 b3
54
+ ...
55
+ g2 1 2 A1 B1
56
+ g2 2 4 A2 B2
57
+ g2 3 5 A3 B3
58
+ g2 4 6 A4 B4
59
+ ...
60
+
61
+ === Output
62
+ A2
63
+ A3
64
+ A4
65
+
66
+ == Dependencies:
67
+ - FFI (http://github.com/ffi/ffi)
68
+
69
+ == Contributing to bio-tabix
70
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
71
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
72
+ * Fork the project.
73
+ * Start a feature/bugfix branch.
74
+ * Commit and push until you are happy with your contribution.
75
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
76
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
77
+
78
+ == Copyright
79
+
80
+ Copyright (c) 2012 Nicholas A Thrower. See LICENSE.txt for
81
+ further details.
82
+
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "bio-tabix"
18
+ gem.homepage = "http://github.com/throwern/bio-tabix"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Ruby binding for samtools tabix}
21
+ gem.description = %Q{Tabix file indexing routines from the samtools package http://samtools.sourceforge.net/}
22
+ gem.email = "throwern@msu.edu"
23
+ gem.authors = ["throwern"]
24
+ # dependencies defined in Gemfile
25
+ gem.extensions = "ext/tabix/mkrf_conf.rb"
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ # require 'rcov/rcovtask'
37
+ # Rcov::RcovTask.new do |test|
38
+ # test.libs << 'test'
39
+ # test.pattern = 'test/**/test_*.rb'
40
+ # test.verbose = true
41
+ # test.rcov_opts << '--exclude "gems/*"'
42
+ # end
43
+ desc "Code coverage detail"
44
+ task :simplecov do
45
+ ENV['COVERAGE'] = "true"
46
+ Rake::Task['spec'].execute
47
+ end
48
+
49
+ task :default => :test
50
+
51
+ require 'rdoc/task'
52
+ Rake::RDocTask.new do |rdoc|
53
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
54
+
55
+ rdoc.rdoc_dir = 'rdoc'
56
+ rdoc.title = "bio-tabix #{version}"
57
+ rdoc.rdoc_files.include('README*')
58
+ rdoc.rdoc_files.include('lib/**/*.rb')
59
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,72 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "bio-tabix"
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["throwern"]
12
+ s.date = "2012-05-07"
13
+ s.description = "Tabix file indexing routines from the samtools package http://samtools.sourceforge.net/"
14
+ s.email = "throwern@msu.edu"
15
+ s.extensions = ["ext/tabix/mkrf_conf.rb"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".rvmrc",
23
+ "Gemfile",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bio-tabix.gemspec",
29
+ "ext/tabix/Rakefile",
30
+ "ext/tabix/mkrf_conf.rb",
31
+ "lib/bio-tabix.rb",
32
+ "lib/bio/tabix/Version",
33
+ "lib/bio/tabix/binding.rb",
34
+ "lib/bio/tabix/library.rb",
35
+ "lib/bio/tabix/t_file.rb",
36
+ "test/helper.rb",
37
+ "test/test_bio-tabix.rb"
38
+ ]
39
+ s.homepage = "http://github.com/throwern/bio-tabix"
40
+ s.licenses = ["MIT"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = "1.8.24"
43
+ s.summary = "Ruby binding for samtools tabix"
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<ffi>, [">= 0"])
50
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
51
+ s.add_development_dependency(%q<rdoc>, [">= 0"])
52
+ s.add_development_dependency(%q<bundler>, [">= 0"])
53
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
54
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<ffi>, [">= 0"])
57
+ s.add_dependency(%q<shoulda>, [">= 0"])
58
+ s.add_dependency(%q<rdoc>, [">= 0"])
59
+ s.add_dependency(%q<bundler>, [">= 0"])
60
+ s.add_dependency(%q<jeweler>, [">= 0"])
61
+ s.add_dependency(%q<simplecov>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<ffi>, [">= 0"])
65
+ s.add_dependency(%q<shoulda>, [">= 0"])
66
+ s.add_dependency(%q<rdoc>, [">= 0"])
67
+ s.add_dependency(%q<bundler>, [">= 0"])
68
+ s.add_dependency(%q<jeweler>, [">= 0"])
69
+ s.add_dependency(%q<simplecov>, [">= 0"])
70
+ end
71
+ end
72
+
@@ -0,0 +1,2 @@
1
+ placeholder
2
+
@@ -0,0 +1,73 @@
1
+ #(c) Copyright 2012 Nicholas A Thrower. All Rights Reserved.
2
+ # Derivative of https://github.com/helios/bioruby-samtools/blob/master/ext/mkrf_conf.rb
3
+ # create Rakefile for shared library compilation
4
+
5
+ path = File.expand_path(File.dirname(__FILE__))
6
+
7
+ path_external = File.join(path, "../../lib/bio/tabix/")
8
+
9
+ version = File.open(File.join(path_external,"Version"),'r')
10
+ Version = version.read
11
+ version.close
12
+
13
+ url = "http://samtools.svn.sourceforge.net/viewvc/samtools/trunk/tabix/?view=tar"
14
+ TabixFile = "tabix-trunk.tar"
15
+
16
+ File.open(File.join(path,"Rakefile"),"w") do |rakefile|
17
+ rakefile.write <<-RAKE
18
+ require 'rbconfig'
19
+ require 'open-uri'
20
+ require 'fileutils'
21
+ include FileUtils::Verbose
22
+ require 'rake/clean'
23
+
24
+ URL = "#{url}"
25
+
26
+ task :download do
27
+ open(URL) do |uri|
28
+ File.open("#{TabixFile}",'wb') do |fout|
29
+ fout.write(uri.read)
30
+ end #fout
31
+ end #uri
32
+ end
33
+
34
+ task :compile do
35
+ sh "tar xvf #{TabixFile}"
36
+ cd("tabix") do
37
+ #sh "patch < ../Makefile-bioruby.patch"
38
+ sh "make"
39
+ cp("libtabix.a","#{path_external}")
40
+ case Config::CONFIG['host_os']
41
+ when /linux/
42
+ sh "make libtabix.so.1"
43
+ cp("libtabix.so.1","#{path_external}")
44
+ when /darwin/
45
+ sh "make libtabix.1.dylib"
46
+ cp("libtabix.1.dylib","#{path_external}")
47
+ else raise NotImplementedError, "Tabix not supported on your platform"
48
+ end #case
49
+ cp("tabix", "#{path}/../../lib/bio/tabix")
50
+ chmod 0755, "#{path}/../../lib/bio/tabix/tabix"
51
+ cp("bgzip", "#{path}/../../lib/bio/tabix")
52
+ chmod 0755, "#{path}/../../lib/bio/tabix/bgzip"
53
+ end #cd
54
+ end
55
+
56
+ task :clean do
57
+ # cd("tabix-#{Version}") do
58
+ # sh "make clean"
59
+ # end
60
+ # rm("#{TabixFile}")
61
+ # rm_rf("tabix-#{Version}")
62
+ cd("tabix") do
63
+ sh "make clean"
64
+ end
65
+ rm("#{TabixFile}")
66
+ rm_rf("tabix")
67
+ end
68
+
69
+ task :default => [:download, :compile, :clean]
70
+
71
+ RAKE
72
+
73
+ end
@@ -0,0 +1,2 @@
1
+ require 'ffi'
2
+ require 'bio/tabix/t_file'
@@ -0,0 +1 @@
1
+ 0.2.5
@@ -0,0 +1,100 @@
1
+ # == binding.rb
2
+ # This file contains the ffi binding declarations for the tabix api
3
+ # See https://github.com/ffi/ffi and http://samtools.sourceforge.net/tabix.shtml for details
4
+ #
5
+ # == Contact
6
+ #
7
+ # Author:: Nicholas A. Thrower
8
+ # Copyright:: Copyright (c) 2012 Nicholas A Thrower
9
+ # License:: See LICENSE.txt for more details
10
+ #
11
+
12
+ # -
13
+ module Bio
14
+ # -
15
+ module Tabix
16
+ # Ruby binding for the tabix file indexing routines within the samtools package http://samtools.sourceforge.net/
17
+ module Binding
18
+ require 'bio/tabix/library'
19
+ extend FFI::Library
20
+ ffi_lib Bio::Tabix::Library.filename
21
+
22
+ # CLASSES
23
+
24
+ # Custom string storage
25
+ # member of the IterT class
26
+ class KString < FFI::Struct
27
+ layout(
28
+ :l,:size_t,
29
+ :m,:size_t,
30
+ :s,:string
31
+ )
32
+ end
33
+
34
+ # File pointers to text and index data
35
+ # created by ti_open
36
+ # used by ti_read, ti_query, ti_close
37
+ class TabixT < FFI::Struct
38
+ layout(
39
+ :fp, :pointer,
40
+ :idx, :pointer,
41
+ :fn, :string,
42
+ :fnidx, :string
43
+ )
44
+ end
45
+
46
+ # Iteratator for monitoring the query progress
47
+ # created by the ti_query method
48
+ # used by ti_read
49
+ class IterT < FFI::Struct
50
+ layout(
51
+ :from_first,:int,
52
+ :tid, :int,
53
+ :beg, :int,
54
+ :end, :int,
55
+ :n_off, :int,
56
+ :i, :int,
57
+ :finished, :int,
58
+ :curr_off, :uint64,
59
+ :str, KString,
60
+ :idx, :pointer,
61
+ :off, :pointer
62
+ )
63
+ end
64
+ # Index configuration
65
+ # used by ti_index_build2
66
+ class ConfT < FFI::Struct
67
+ layout(
68
+ :preset, :int32,
69
+ :sc, :int32,
70
+ :bc, :int32,
71
+ :ec, :int32,
72
+ :meta_char, :int32,
73
+ :line_skip, :int32
74
+ )
75
+ # convenience method to access attributes
76
+ def get_hash
77
+ {
78
+ :preset => self[:preset],
79
+ :sc => self[:sc],
80
+ :bc => self[:bc],
81
+ :ec => self[:ec],
82
+ :meta_char => self[:meta_char],
83
+ :line_skip => self[:line_skip]
84
+ }
85
+ end
86
+ end
87
+ # FUNCTIONS # PARAMETER(S) : RETURN
88
+ attach_function :ti_open, [:string, :string], :pointer # filename, idxname (or 0) : TabixT*
89
+ attach_function :ti_read, [:pointer, :pointer, :pointer], :string # TabixT*, ti_iter_t, len : string
90
+ attach_function :ti_query, [:pointer,:string,:int,:int], IterT # TabixT*, name, beg, end : IterT
91
+ attach_function :ti_close, [:pointer], :void # TabixT*
92
+ attach_function :ti_iter_destroy, [IterT], :void # ti_iter_t
93
+ attach_function :ti_index_build2, [:string,:pointer,:string], :int # filename, ti_conf_t, idxname (or 0) : 0/-1
94
+ attach_function :bgzf_is_bgzf, [:string], :int # filename, : 1/0
95
+ attach_function :ti_seqname, [:pointer,:pointer],:pointer # ti_index_t*, int*(count) : char**
96
+ attach_function :ti_index_load,[:string],:pointer # filename(no idx suffix) : ti_index_t*
97
+ attach_function :ti_get_conf,[:pointer],:pointer # ti_index_t* : ti_conf_t*
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,38 @@
1
+ # == library.rb
2
+ # This file contains the Library Class for retrieving platform specific library names
3
+ #
4
+ # == Contact
5
+ #
6
+ # Author:: Nicholas A. Thrower
7
+ # Copyright:: Copyright (c) 2012 Nicholas A Thrower
8
+ # License:: See LICENSE.txt for more details
9
+ #
10
+
11
+ # -
12
+ module Bio
13
+ # -
14
+ module Tabix
15
+ # Cross-platform library naming
16
+ class Library
17
+ # return the platform specific library name
18
+ def self.filename
19
+ lib_os = case RUBY_PLATFORM
20
+ when /linux/
21
+ 'so.1'
22
+ when /darwin/
23
+ '1.dylib'
24
+ else
25
+ case RUBY_DESCRIPTION
26
+ when /darwin.*java/
27
+ '1.dylib'
28
+ when /linux.*java/
29
+ 'so.1'
30
+ else raise NotImplementedError, "Tabix not supported on your platform"
31
+ end
32
+ end
33
+
34
+ File.join(File.expand_path(File.dirname(__FILE__)),"libtabix.#{lib_os}")
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,158 @@
1
+ # == t_file.rb
2
+ # This file contains the TFile class used to interact with the tabix api
3
+ #
4
+ # == Contact
5
+ #
6
+ # Author:: Nicholas A. Thrower
7
+ # Copyright:: Copyright (c) 2012 Nicholas A Thrower
8
+ # License:: See LICENSE.txt for more details
9
+ #
10
+
11
+ # -
12
+ module Bio
13
+ # -
14
+ module Tabix
15
+ # The TFile class manages compressing, indexing, opening and parsing tab delimited files.
16
+ # The file must be position sorted prior to indexing.
17
+ class TFile
18
+ require 'bio/tabix/binding'
19
+ include Bio::Tabix::Binding
20
+ # ascii or compressed file name
21
+ attr_accessor :file
22
+ # index name
23
+ attr_accessor :index
24
+ # TabixT created from open index
25
+ attr_accessor :t_file
26
+ # pointer to TabixT
27
+ attr_accessor :t_file_p
28
+ # index build options
29
+ attr_accessor :options
30
+ # compresses the fi into fo using bgzip
31
+ def self.compress(fi, fo)
32
+ `#{File.join(File.expand_path(File.dirname(__FILE__)),'bgzip')} -c #{fi} > #{fo}`
33
+ end
34
+ # Builds an index from the supplied filename and options
35
+ # - :s => sequence/group column [1]
36
+ # - :b => beginning range column [2]
37
+ # - :e => ending range column. Can equal :b. [3]
38
+ # - :meta_char => comment character [#]
39
+ # - :line_skip => number of initial lines to ignore [0]
40
+ def self.build_index(f, opts={})
41
+ conf = ConfT.new
42
+ conf[:preset]=0
43
+ conf[:sc]=opts[:s] || 1
44
+ conf[:bc]=opts[:b] || 2
45
+ conf[:ec]=opts[:e] || 3
46
+ conf[:meta_char]=('#'||opts[:c][0]).ord
47
+ conf[:line_skip]=(0||opts[:S]).to_i
48
+ unless(Bio::Tabix::Binding.bgzf_is_bgzf(f)==1)
49
+ puts "Compressing..."
50
+ self.class.compress(f,f+".bgzf")
51
+ f=f+".bgzf"
52
+ end
53
+ puts "Indexing with #{conf.get_hash}..."
54
+ Bio::Tabix::Binding.ti_index_build2(f,conf,f+".tbi")
55
+ end
56
+ # convenience method to create a new Tabix instance and open it.
57
+ def self.open(*args)
58
+ self.new(*args).open
59
+ end
60
+ # Returns a new TFile. If the file is not compressed, a new compressed
61
+ # file will be created with compress[compress]. If the index is not present
62
+ # a new index will be created with build_index[build_index].
63
+ def initialize(f, opts={})
64
+ @file = f
65
+ @options = opts
66
+ @index = file+".tbi"
67
+ return self
68
+ end
69
+ # opens the file checking for compression and corresponding index.
70
+ def open
71
+ # check existing
72
+ if(@t_file)
73
+ puts "Already open, closing and re-opening"
74
+ self.close
75
+ end
76
+ # check datafile
77
+ if file =~ /http:\/\/|ftp:\/\//
78
+ puts "Expecting remote file: #{file}"
79
+ else
80
+ raise "FileNotFound #{file}" unless(File.exist?(file))
81
+ unless(bgzf_is_bgzf(file)==1)
82
+ unless(bgzf_is_bgzf(file+".bgzf")==1)
83
+ puts "Input does not look like a bgzip compressed file. Attempting compression..."
84
+ self.class.compress(file,file+".bgzf")
85
+ end
86
+ @file = file+".bgzf"
87
+ end
88
+ end
89
+ # check index
90
+ if index =~ /http:\/\/|ftp:\/\//
91
+ puts "Expecting remote index: #{index}"
92
+ elsif !File.exist?(index)
93
+ puts "Index #{index} not found. Building..."
94
+ self.class.build_index(file,options)
95
+ end
96
+ # open
97
+ @t_file_p = ti_open(file,index)
98
+ raise "FileAcessError #{file}" if @t_file_p.null?
99
+ @t_file = TabixT.new(@t_file_p)
100
+ return self
101
+ end
102
+ # closes the TabixT file
103
+ def close
104
+ if(@t_file_p)
105
+ begin
106
+ ti_close(@t_file_p)
107
+ @t_file_p = nil
108
+ rescue
109
+ puts "Error closing file"
110
+ end
111
+ end
112
+ end
113
+ # returns an array of the group names found in the index
114
+ def groups
115
+ load_index
116
+ g_num = FFI::MemoryPointer.new(:int)
117
+ g_ptr = ti_seqname(t_file[:idx],g_num)
118
+ return [] if g_ptr.null? || g_num.null?
119
+ g_ptr.get_array_of_string(0, g_num.read_int).compact
120
+ end
121
+ # returns the header (skipped lines + comments)
122
+ def header
123
+ load_index
124
+ conf = ConfT.new(ti_get_conf(t_file[:idx]))
125
+ iter = IterT.new(ti_query(t_file_p,nil,0,1))
126
+ len = FFI::MemoryPointer.new(:int)
127
+ str = ""
128
+ while( (s = ti_read(t_file_p, iter, len)) )
129
+ break if(s[0].ord != conf[:meta_char])
130
+ str << s
131
+ str << "\n"
132
+ end
133
+ ti_iter_destroy(iter)
134
+ @header = str
135
+ end
136
+ # Iterates over the supplied region calling user_proc on each item
137
+ # a region is defined by a group name and range(pos1 - pos2)
138
+ # all overlapping intervals within the group will be processed in order
139
+ def process_region(group, pos1, pos2, user_proc)
140
+ iter = IterT.new(ti_query(t_file_p,group,pos1,pos2))
141
+ len = FFI::MemoryPointer.new(:int)
142
+ while( (s = ti_read(t_file_p, iter, len)) )
143
+ user_proc.call(s,len)
144
+ end
145
+ ti_iter_destroy(iter)
146
+ end
147
+
148
+ private
149
+ def load_index
150
+ if t_file[:idx].null?
151
+ t_file[:idx] = ti_index_load(t_file[:fn])
152
+ end
153
+ raise "Index Load Error" if t_file[:idx].null?
154
+ end
155
+
156
+ end#Index class
157
+ end#Tabix module
158
+ end#Bio module
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'bio-tabix'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestBioTabix < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bio-tabix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - throwern
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ffi
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: shoulda
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rdoc
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Tabix file indexing routines from the samtools package http://samtools.sourceforge.net/
111
+ email: throwern@msu.edu
112
+ executables: []
113
+ extensions:
114
+ - ext/tabix/mkrf_conf.rb
115
+ extra_rdoc_files:
116
+ - LICENSE.txt
117
+ - README.rdoc
118
+ files:
119
+ - .document
120
+ - .rvmrc
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.rdoc
124
+ - Rakefile
125
+ - VERSION
126
+ - bio-tabix.gemspec
127
+ - ext/tabix/Rakefile
128
+ - ext/tabix/mkrf_conf.rb
129
+ - lib/bio-tabix.rb
130
+ - lib/bio/tabix/Version
131
+ - lib/bio/tabix/binding.rb
132
+ - lib/bio/tabix/library.rb
133
+ - lib/bio/tabix/t_file.rb
134
+ - test/helper.rb
135
+ - test/test_bio-tabix.rb
136
+ homepage: http://github.com/throwern/bio-tabix
137
+ licenses:
138
+ - MIT
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ segments:
150
+ - 0
151
+ hash: -2955396723264547364
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 1.8.24
161
+ signing_key:
162
+ specification_version: 3
163
+ summary: Ruby binding for samtools tabix
164
+ test_files: []