lib/transrate/comparative_metrics.rb
# coverage of contigs that have reciprocal hits
# divided by
# number of reciprocal targets
coverage of contigs that have reciprocal hits
divided by
number of reciprocal targets
coverage of contigs that have reciprocal hits divided by number of reciprocal targets
def ortholog_hit_ratio crbblast
      return @ortholog_hit_ratio unless @ortholog_hit_ratio.nil?
      targets = Hash.new
      crbblast.reciprocals.each_pair do |key, list|
        list.each do |hit|
          targets[hit.target] ||= [] 
          targets[hit.target] << hit
        end
      end
      @n_refs_with_recip = targets.size
      total_coverage=0
      total_length=0
      targets.each_pair do |key, list|
        blocks = []
        target_length = 0
        list.each do |hit|
          target_length = hit.tlen
          if crbblast.target_is_prot
            target_length *= 3
            start, stop = [hit.tstart*3, hit.tend*3].minmax
          else
            start, stop = [hit.tstart, hit.tend].minmax
          end
          if blocks.empty?
            blocks << [start, stop]
          else
            found=false
            blocks.each do |block|
              o = overlap(block[0], block[1], start, stop)
              if o == 0 
                found=true
              elsif o == 1 
                block[0] = start
                found=true
              elsif o == 2 
                block[1] = stop
                found=true
              elsif o == 3 
                block[0] = start
                block[1] = stop
                found=true
              # elsif o == 4 
              end
            end
            if !found
              blocks << [start, stop]
            end
          end
        end
        blocks.each_with_index do |block_a,a|
          blocks.each_with_index do |block_b,b|
            if a!=b
              o = overlap(block_a[0], block_a[1], block_b[0], block_b[1])
              if o == 0 
                block_b[0]=-1
                block_b[1]=-1
              elsif o == 1 
                block_a[0] = block_b[0]
                block_b[0] = -1
                block_b[1] = -1
              elsif o == 2 
                block_a[1] = block_b[1]
                block_b[0] = -1
                block_b[1] = -1
              elsif o == 3 
                block_a[0] = block_b[0]
                block_a[1] = block_b[1]
                block_b[0] = -1
                block_b[1] = -1
              elsif o == 4 
                block_b[0] = -1
                block_b[1] = -1
              end
            end
          end 
        end 
        length_of_coverage=0
        blocks.each do |block|
          if block[0] and block[1]
            if block[0]>=0 and block[1]>=0
              length_of_coverage += block[1] - block[0] + 1
            end
          else
            puts "error: key = #{key}, #{blocks}"
          end
        end
        cov = [0.25, 0.5, 0.75, 0.85, 0.95]
        @cov ||= [0, 0, 0, 0, 0]
        p = length_of_coverage / target_length.to_f
        cov.each_with_index do |c, i|
          if p >= c
            @cov[i] +=1
          end
        end
        total_coverage += length_of_coverage
        total_length += target_length
      end
      return ortholog_hit_ratio = total_coverage / total_length.to_f
    end
def ortholog_hit_ratio crbblast return @ortholog_hit_ratio unless @ortholog_hit_ratio . nil? targets = Hash . new crbblast . reciprocals . each_pair do | key , list | list . each do | hit | targets [ hit . target ] ||= [ ] targets [ hit . target ] << hit end end @n_refs_with_recip = targets . size total_coverage = 0 total_length = 0 targets . each_pair do | key , list | blocks = [ ] target_length = 0 list . each do | hit | target_length = hit . tlen if crbblast . target_is_prot target_length *= 3 start , stop = [ hit . tstart * 3 , hit . tend * 3 ] . minmax else start , stop = [ hit . tstart , hit . tend ] . minmax end if blocks . empty? blocks << [ start , stop ] else found = false blocks . each do | block | o = overlap ( block [ 0 ] , block [ 1 ] , start , stop ) if o == 0 found = true elsif o == 1 block [ 0 ] = start found = true elsif o == 2 block [ 1 ] = stop found = true elsif o == 3 block [ 0 ] = start block [ 1 ] = stop found = true end end if ! found blocks << [ start , stop ] end end end blocks . each_with_index do | block_a , a | blocks . each_with_index do | block_b , b | if a != b o = overlap ( block_a [ 0 ] , block_a [ 1 ] , block_b [ 0 ] , block_b [ 1 ] ) if o == 0 block_b [ 0 ] = - 1 block_b [ 1 ] = - 1 elsif o == 1 block_a [ 0 ] = block_b [ 0 ] block_b [ 0 ] = - 1 block_b [ 1 ] = - 1 elsif o == 2 block_a [ 1 ] = block_b [ 1 ] block_b [ 0 ] = - 1 block_b [ 1 ] = - 1 elsif o == 3 block_a [ 0 ] = block_b [ 0 ] block_a [ 1 ] = block_b [ 1 ] block_b [ 0 ] = - 1 block_b [ 1 ] = - 1 elsif o == 4 block_b [ 0 ] = - 1 block_b [ 1 ] = - 1 end end end end length_of_coverage = 0 blocks . each do | block | if block [ 0 ] and block [ 1 ] if block [ 0 ] >= 0 and block [ 1 ] >= 0 length_of_coverage += block [ 1 ] - block [ 0 ] + 1 end else puts "error: key = #{key}, #{blocks}" end end cov = [ 0.25 , 0.5 , 0.75 , 0.85 , 0.95 ] @cov ||= [ 0 , 0 , 0 , 0 , 0 ] p = length_of_coverage / target_length . to_f cov . each_with_index do | c , i | if p >= c @cov [ i ] += 1 end end total_coverage += length_of_coverage total_length += target_length end return ortholog_hit_ratio = total_coverage / total_length . to_f end
coverage of contigs that have reciprocal hits
divided by
number of reciprocal targets
coverage of contigs that have reciprocal hits divided by number of reciprocal targets
# if key doesn't exist add it with a [] # if query overlaps with any block extend that block # perfect overlap # partial overlap # partial overlap # full overlap # elsif o == 4 # full overlap # nothing # elsif o == 5 || o == 6 # no overlap # if any blocks now overlap then extend one block and remove # the other # perfect overlap # partial overlap # partial overlap # full overlap # full overlap # elsif o == 5 || o == 6# no overlap # do nothing # elsif  # no overlap # do nothing # each_with_index b # each_with_index a # sum blocks to find total coverage
crbblast
<not_specific>