Class: Mitab::MitabParser

Inherits:
Object
  • Object
show all
Defined in:
lib/mitab.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ MitabParser

Returns a new instance of MitabParser.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mitab.rb', line 13

def initialize(text)
  @nodes = {}
  @links= []
  @scores = {}
  @mitab
  

  $lines = text.split("\n")

  $interactions = $lines.map{ |l| parse(l)}
  nodeval = @nodes.values

  @links = $interactions

  @mitab = {
    links: $interactions,
    nodes: nodeval,
    ids: nodeval.map { |h| h[:id] },
    taxa: nodeval.reduce([]){ |union, x| union | x[:taxonomy]}.compact,
    scores: @scores.values
  }

end

Instance Attribute Details

Returns the value of attribute links


4
5
6
# File 'lib/mitab.rb', line 4

def links
  @links
end

#mitabObject (readonly)

Returns the value of attribute mitab


4
5
6
# File 'lib/mitab.rb', line 4

def mitab
  @mitab
end

#nodesObject (readonly)

Returns the value of attribute nodes


4
5
6
# File 'lib/mitab.rb', line 4

def nodes
  @nodes
end

#scoresObject (readonly)

Returns the value of attribute scores


4
5
6
# File 'lib/mitab.rb', line 4

def scores
  @scores
end

Instance Method Details

#addScore(score) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mitab.rb', line 51

def addScore(score)
  if( !score[:score].to_f.nan?)
      if(@scores.key?(score[:name]))
          if(@scores[score[:name]][:min].to_f > score[:score].to_f) 
            @scores[score[:name]][:min] = score[:score].to_f
          end
          if(@scores[score[:name]][:max].to_f < score[:score].to_f) 
            @scores[score[:name]][:max] = score[:score].to_f
          end
      else
          @scores[score[:name]] = {name:score[:name], min:score[:score], max:score[:score]}
      end
  end
end

#getNode(idStr, altIdsStr, aliasStr, taxStr) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mitab.rb', line 80

def getNode(idStr, altIdsStr, aliasStr, taxStr)
  gNameAliases = aliasStr.split("|")

  gNameStr = gNameAliases.select{ |gNameAlias| gNameAlias.match($geneName)}

  gNameStr = (gNameStr.nil?) ? gNameAliases.select{ |gNameAlias| gNameAlias.match($geneNameSynonym)} : gNameStr
  ids = idStr.split("|") + altIdsStr.split("|") + aliasStr.split("|")
  ids = ids.map{|x| mapPub(x)}
  id = ids.select{|id| id[:name] == "uniprotkb"}
  node = {
          id: ids[0][:value],
          ids: ids,
          uniprot: (id.nil?) ? '' : id,
          geneName: (gNameStr.nil?) ? '' : gNameStr.map{|gStr| gStr.match($textInTax)[1]},
          altIds: altIdsStr.split('|').map{|x| mapPub(x)},
          taxonomy: taxStr.split('|').uniq{|x| mapTaxonomy(x)},
    }
    return node

    
end

#mapField(fieldStr) ⇒ Object


42
43
44
45
46
47
48
49
# File 'lib/mitab.rb', line 42

def mapField(fieldStr)
  
    if(fieldStr.match($textInQuotes).nil? || fieldStr.match($textInParenthesis).nil?)
        arr = fieldStr.split(':')
        return {name:arr[0], score:arr[1]}
    end
    return {name:fieldStr.match($textInQuotes)[1], value:fieldStr.match($textInParenthesis)[1]}
end

#mapPub(pubStr) ⇒ Object


37
38
39
40
# File 'lib/mitab.rb', line 37

def mapPub(pubStr)
    arr = pubStr.split(':')
    return {name:arr[0], value:arr[1]}
end

#mapScore(scoreStr) ⇒ Object


66
67
68
69
70
71
# File 'lib/mitab.rb', line 66

def mapScore(scoreStr)
  arr = scoreStr.split(':')
  score = {name:arr[0], score:arr[1]}
  addScore(score)
  return score
end

#mapTaxonomy(taxStr) ⇒ Object


73
74
75
76
77
78
# File 'lib/mitab.rb', line 73

def mapTaxonomy(taxStr)

    if(taxStr != '-')
        return (taxStr.match($textInTax).nil?) ? taxStr.split(':')[1] : taxStr.match($textInTax)[1]
    end
end

#parse(line) ⇒ Object


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mitab.rb', line 102

def parse(line)
  if (!line.is_a? String) 
    puts 'MITab cannot parse line '
    return {}
  end
  fields = line.split("\t")
  if(fields.length < 15)
    puts "MITab cannot parse line "
    return {}
  end

  nodeA = getNode(fields[0], fields[2], fields[4], fields[9])
  nodeB = getNode(fields[1], fields[3], fields[5], fields[10])

  interaction = {
      source: nodeA[:id],
      target: nodeB[:id],
      detMethods: fields[6].split('|').map{|x| mapField(x)},
      firstAuthor: fields[7].split('|'),
      publications: fields[8].split('|').map{|x| mapField(x)},
      intTypes: fields[11].split('|').map{|x| mapField(x)},
      sourceDbs: fields[12].split('|').map{|x| mapField(x)},
      intIds: fields[13].split('|').map{|x| mapPub(x)},
      scores: fields[14].split('|').map{|x| mapScore(x)},
    }



  @nodes[nodeA[:id]] = nodeA
  @nodes[nodeB[:id]] = nodeB

  nodeval = @nodes.values

  return interaction
end

138
139
140
# File 'lib/mitab.rb', line 138

def print
  puts @mitab
end