Class: ScbiGo::GoListReducer

Inherits:
GeneOntology show all
Defined in:
lib/scbi_go/go_list_reducer.rb

Instance Attribute Summary

Attributes inherited from GeneOntology

#base_terms, #gos, #header

Instance Method Summary collapse

Methods inherited from GeneOntology

#add_go_term, #find_go, #go_list_to_terms, #initialize

Constructor Details

This class inherits a constructor from ScbiGo::GeneOntology

Instance Method Details

#all_matching_branches(go_list) ⇒ Object

return all branches that match with the elements of go_list is the same as reduce_branches, but returning whole branches



52
53
54
55
56
57
58
59
60
61
# File 'lib/scbi_go/go_list_reducer.rb', line 52

def all_matching_branches(go_list)
    leaves = reduce_branches(go_list)
    res=[]

    leaves.each do |leave|
      res += leave.all_branches_to_top
    end

    return simplify_branches(res)
end

#best_matching_branches(go_list) ⇒ Object

get the branch/es that match more elements in the go_list



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/scbi_go/go_list_reducer.rb', line 64

def best_matching_branches(go_list)
    matching_count = {}

    terms=go_list_to_terms(go_list)

    leaves = reduce_branches(go_list)

    branches = leaves.first.all_branches_to_top

    branches.each do |branch|
      intersect_count = (branch & terms).count

      if intersect_count>0
        matching_count[intersect_count] = [] if matching_count[intersect_count].nil?
        matching_count[intersect_count] << branch
      end
    end

    res = []
    if m=matching_count.keys.max
      res = matching_count[m]
    end

    return res
end

#reduce_branches(go_list) ⇒ Object

given a go_list, returns the leaves of all the branches defined by them



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/scbi_go/go_list_reducer.rb', line 29

def reduce_branches(go_list)
  terms = go_list_to_terms(go_list)
  
  res=[]
  branches=[]

  if !terms.empty?
    #get all branches for each terms
    terms.each do |term|
     branches += term.all_branches_to_top
    end
    
    branches=simplify_branches(branches)

    #return first unique element of each branch
    res=branches.map{|b| b.first}.uniq
  end

  return res
end

#simplify_branches(branches) ⇒ Object

return the subset of branches not contained in another branch of the list



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/scbi_go/go_list_reducer.rb', line 6

def simplify_branches(branches)
  res=[]

  # inverse sort branches by length
  sorted_b = branches.sort{|b1,b2| -b1.length<=>-b2.length}

  # simplify by iteration from larger to smaller

  sorted_b.each do |branch|
    # select other branches in res that contains all elements of this branch
    matches=res.select{|e| (e & branch).length==branch.length}

    # add it to res if no match found
    if matches.empty?
      res << branch
    end
  end
  
  return res
end