Skip to content

Commit

Permalink
-Added the IDs to test
Browse files Browse the repository at this point in the history
-Capacity to calculate the average coverage of certain region without unnecesary allocation.
  • Loading branch information
homonecloco committed Aug 23, 2010
1 parent 91a67ac commit fd0cd99
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
13 changes: 7 additions & 6 deletions Rakefile
@@ -1,10 +1,11 @@
require 'echoe' #require 'echoe'


Echoe.new("samtools-ruby", version=0.1) do |p| #Echoe.new("samtools-ruby", version=0.1) do |p|
p.author = "Ricardo H. Ramirez-Gonzalez" # p.author = "Ricardo H. Ramirez-Gonzalez"
p.summary = "Binder of samtools for ruby, on the top of FFI. " # p.summary = "Binder of samtools for ruby, on the top of FFI. "
p.url = "http://github.com/homonecloco/samtools-ruby" # p.url = "http://github.com/homonecloco/samtools-ruby"
end # p.platform = Gem
#end




desc "Basic test" desc "Basic test"
Expand Down
28 changes: 27 additions & 1 deletion lib/bio/db/sam.rb
Expand Up @@ -102,12 +102,38 @@ def load_reference()


end end


def average_coverage(chromosome, qstart, len)
reference = fetch_reference(chromosome, qstart, qstart+len)
len = reference.length if len > reference.length
#p qend.to_s + "-" + qstart.to_s + "framesize " + (qend - qstart).to_s
coverages = Array.new(len, 0)

avg_proc = Proc.new do |alignment|
last = qstart + len
first = qstart
last = alignment.calend if last > alignment.calend
first = alignment.pos if first < alignment.pos
p first
first.upto(last-1) { |i|
coverages[first-i] = 1 + coverages[first-i] }
end

fetch_with_function(chromosome, qstart, qstart+len, avg_proc)
p coverages
total = 0
0.upto(len-1) { |i| total= total + coverages[i] }
avg_cov = total.to_f / len
#LibC.free referemce
avg_cov
end

def fetch_reference(chromosome, qstart,qend) def fetch_reference(chromosome, qstart,qend)
load_reference unless @fasta_index.nil? || @fasta_index.null? load_reference if @fasta_index.nil? || @fasta_index.null?
query = query_string(chromosome, qstart,qend) query = query_string(chromosome, qstart,qend)
len = FFI::MemoryPointer.new :int len = FFI::MemoryPointer.new :int
reference = Bio::DB::SAM::Tools.fai_fetch(@fasta_index, query, len) reference = Bio::DB::SAM::Tools.fai_fetch(@fasta_index, query, len)
raise SAMException.new(), "Unable to get sequence for reference: "+query if reference.nil? raise SAMException.new(), "Unable to get sequence for reference: "+query if reference.nil?

reference reference
end end


Expand Down
35 changes: 35 additions & 0 deletions test/basictest.rb
Expand Up @@ -87,6 +87,30 @@ def test_class_binary_read_no_close
assert(true, "BINARY file openend but not closed") assert(true, "BINARY file openend but not closed")
end end


def test_read_coverage
sam = Bio::DB::Sam.new({:bam=>@testBAMFile, :fasta=>@testReference})
sam.open
File.open( @test_folder +"/ids2.txt", "r") do |file|
puts "file opened"
file.each_line{|line|
fetching = line.split(' ')[0]
puts "fetching: " + fetching
sam.load_reference
seq = sam.fetch_reference(fetching, 0, 16000)
# puts seq
# puts seq.length
als = sam.fetch(fetching, 0, seq.length)
# p als
if als.length() > 0 then
p fetching
p als
end
}

end
sam.close
assert(true, "Finish")
end
# def test_read_TAM_as_BAM # def test_read_TAM_as_BAM
# begin # begin
# sam = Bio::DB::Sam.new({:bam=>@testTAMFile}) # sam = Bio::DB::Sam.new({:bam=>@testTAMFile})
Expand Down Expand Up @@ -228,6 +252,17 @@ def test_load_feature
p fs p fs
assert(true, "Loaded as features") assert(true, "Loaded as features")
end end

def test_avg_coverage
sam = Bio::DB::Sam.new({:fasta=>@testReference, :bam=>@testBAMFile })
sam.open
cov = sam.average_coverage("chr_1", 60, 30)
p "Coverage: " + cov.to_s
sam.close
assert(true, "Average coverage ran")
assert(3 == cov, "The coverage is 3")
end



end end


Expand Down
1 change: 1 addition & 0 deletions test/samples/small/ids2.txt
@@ -0,0 +1 @@
chr_1

0 comments on commit fd0cd99

Please sign in to comment.