Module: UniProt

Extended by:
Resource
Defined in:
lib/rbbt/sources/uniprot.rb

Constant Summary collapse

UNIPROT_TEXT =
"http://www.uniprot.org/uniprot/[PROTEIN].txt"
UNIPROT_FASTA =
"http://www.uniprot.org/uniprot/[PROTEIN].fasta"

Class Method Summary collapse

Class Method Details

.cath(protein) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rbbt/sources/uniprot.rb', line 220

def self.cath(protein)
  text = get_uniprot_entry(protein)

  cath = {}
  text.split(/\n/).each{|l| 
    next unless l =~ /^DR\s+Gene3D; G3DSA:(.*)\./
    id, description, cuantity = $1.split(";").collect{|v| v.strip}
    cath[id] = {:description => description, :cuantity => cuantity}
  }
  cath
end

.cath_domains(protein) ⇒ Object



232
233
234
235
236
237
# File 'lib/rbbt/sources/uniprot.rb', line 232

def self.cath_domains(protein)
  pdbs = pdbs(protein).keys.uniq
  pdbs.collect do |pdb|
    Cath.domains_for_pdb(pdb)
  end.flatten.compact
end

.features(protein) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rbbt/sources/uniprot.rb', line 132

def self.features(protein)
  text = get_uniprot_entry(protein)

  text = text.split(/\n/).select{|line| line =~ /^FT/} * "\n"

  parts = text.split(/^(FT   \w+)/)
  parts.shift

  features = []

  type = nil
  parts.each do |part|
    if part[0..1] == "FT"
      type = part.gsub(/FT\s+/,'')
      next
    end
    value = part.gsub("\nFT", '').gsub(/\s+/, ' ')
    case
    when value.match(/(\d+) (\d+) (.*)/)
      start, eend, description = $1, $2, $3
      description.gsub(/^FT\s+/m, '')
    when value.match(/(\d+) (\d+)/)
      start, eend = $1, $2
      description = nil
    else
      Log.debug "Value not understood: #{ value }"
    end


    feature = {
      :type => type,
      :start => start.to_i, 
      :end => eend.to_i, 
      :description => description,
    }

    features << feature
  end

  features
end

.get_uniprot_entry(uniprotids) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rbbt/sources/uniprot.rb', line 39

def self.get_uniprot_entry(uniprotids)
  _array = Array === uniprotids

  uniprotids = [uniprotids] unless Array === uniprotids
  uniprotids = uniprotids.compact.collect{|id| id}

  result_files = FileCache.cache_online_elements(uniprotids, 'uniprot-{ID}.xml') do |ids|
    result = {}
    ids.each do |id|
      begin
        Misc.try3times do

          content = Open.read(UNIPROT_TEXT.sub("[PROTEIN]", id), :wget_options => {:quiet => true}, :nocache => true)

          result[id] = content
        end
      rescue
        Log.error $!.message
      end
    end
    result
  end

  uniprots = {}
  uniprotids.each{|id| uniprots[id] = Open.read(result_files[id]) }

  if _array
    uniprots
  else
    uniprots.values.first
  end
end

.get_uniprot_sequence(uniprotids) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rbbt/sources/uniprot.rb', line 72

def self.get_uniprot_sequence(uniprotids)
  _array = Array === uniprotids

  uniprotids = [uniprotids] unless Array === uniprotids
  uniprotids = uniprotids.compact.collect{|id| id}

  result_files = FileCache.cache_online_elements(uniprotids, 'uniprot-sequence-{ID}') do |ids|
    result = {}
    ids.each do |id|
      begin
        Misc.try3times do

          url = UNIPROT_FASTA.sub "[PROTEIN]", id
          text = Open.read(url, :nocache => true)

          result[id] = text.split(/\n/).select{|line| line !~ /^>/} * ""
        end
      rescue
        Log.error $!.message
      end
    end
    result
  end

  uniprots = {}
  uniprotids.each{|id| uniprots[id] = Open.read(result_files[id]) }

  if _array
    uniprots
  else
    uniprots.values.first
  end
end

.pdbs(protein) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rbbt/sources/uniprot.rb', line 106

def self.pdbs(protein)
  text = get_uniprot_entry(protein)

  pdb = {}

  text.split(/\n/).each{|l| 
    next unless l =~ /^DR\s+PDB; (.*)\./
    id, method, resolution, region = $1.split(";").collect{|v| v.strip}
    begin
      chains, start, eend = region.match(/(\w+)=(\d+)-(\d+)/).values_at(1,2,3)
      start = start.to_i
      eend = eend.to_i
      start, eend = eend, start if start > eend
    rescue
      Log.warn("Error process Uniprot PDB line: #{line}")
      next
    end
    pdb[id.downcase] = {:method => method, :resolution => resolution, :region => (start..eend), :chains => chains}
  }
  pdb
end

.pdbs_covering_aa_position(protein, aa_position) ⇒ Object



239
240
241
242
243
# File 'lib/rbbt/sources/uniprot.rb', line 239

def self.pdbs_covering_aa_position(protein, aa_position)
  UniProt.pdbs(protein).select do |pdb, info|
    info[:region].include? aa_position
  end
end

.sequence(protein) ⇒ Object



128
129
130
# File 'lib/rbbt/sources/uniprot.rb', line 128

def self.sequence(protein)
  get_uniprot_sequence(protein)
end

.variants(protein) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rbbt/sources/uniprot.rb', line 175

def self.variants(protein)
  text = get_uniprot_entry(protein)

  text = text.split(/\n/).select{|line| line =~ /^FT/} * "\n"

  parts = text.split(/^(FT   \w+)/)
  parts.shift

  variants = []

  type = nil
  parts.each do |part|
    if type.nil?
      type = part
    else
      if type !~ /VARIANT/
        type = nil
        next
      end
      type = nil
      
      value = part.gsub("\nFT", '').gsub(/\s+/, ' ')
      # 291 291 K -> E (in sporadic cancers; somatic mutation). /FTId=VAR_045413.
      case
      when value.match(/(\d+) (\d+) ([A-Z])\s*\-\>\s*([A-Z]) (.*)\. \/FTId=(.*)/)
        start, eend, ref, mut, desc, id = $1, $2, $3, $4, $5, $6
      when value.match(/(\d+) (\d+) (.*)\. \/FTId=(.*)/)
        start, eend, ref, mut, desc, id = $1, $2, nil, nil, $3, $4
      else
        Log.debug "Value not understood: #{ value }"
      end
      variants << {
        :start => start, 
        :end => eend, 
        :ref => ref,
        :mut => mut, 
        :desc => desc, 
        :id => id,
      }
    end
  end

  variants
end