Module: UniProt

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

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.cath(protein) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
# File 'lib/rbbt/sources/uniprot.rb', line 290

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



302
303
304
305
306
307
# File 'lib/rbbt/sources/uniprot.rb', line 302

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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/rbbt/sources/uniprot.rb', line 195

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, description = $1, $2, $3
      description.gsub(/^FT\s+/m, '')
    when value.match(/^\s+(\d+) (\d+)/)
      start, eend = $1, $2
      description = nil
    when value.match(/(\d+) (.*)/)
      start, description = $1, $2
      eend = start
      description.gsub(/^FT\s+/m, '')
    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_organism_ids(url, organism = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rbbt/sources/uniprot.rb', line 12

def self.get_organism_ids(url, organism = nil)
  tsv = {}
  fields = []
  TSV.traverse url, :type => :array, :bar => "Extracting UniProt IDs #{organism}" do |line|
    uni, type, value = line.split("\t")
    fields << type unless fields.include?(type)
    pos = fields.index type

    values = tsv[uni] 
    values = [] if values.nil?
    values[pos] ||= []
    values[pos] << value
    tsv[uni] = values
  end
  fields =  fields.collect do |field|
    case field
    when "Gene_Name"
      "Associated Gene Name"
    when "Ensembl"
      "Ensembl Gene ID"
    when "Ensembl_TRS"
      "Ensembl Transcript ID"
    when "Ensembl_PRO"
      "Ensembl Protein ID"
    when "GeneID"
      "Entrez Gene ID"
    else
      field
    end
  end

  new = TSV.setup({}, :key_field => "UniProt/SwissProt Accession", :fields => fields, :type => :double, :namespace => organism)
  num_fields = fields.length
  tsv.each do |k,values|
    new_values = [nil] * num_fields
    new_values = values
    num_fields.times do |i|
      new_values[i] = [] if new_values[i].nil?
    end
    new[k] = new_values
  end

  new
end

.get_uniprot_entry(uniprotids) ⇒ 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
# File 'lib/rbbt/sources/uniprot.rb', line 102

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



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
# File 'lib/rbbt/sources/uniprot.rb', line 135

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

  uniprotids = [uniprotids] unless _array
  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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/rbbt/sources/uniprot.rb', line 169

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: #{l}")
      next
    end
    pdb[id.downcase] = {:method => method, :resolution => resolution, :region => (start..eend), :chains => chains}
  }
  pdb
end

.pdbs_covering_aa_position(protein, aa_position) ⇒ Object



309
310
311
312
313
# File 'lib/rbbt/sources/uniprot.rb', line 309

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



191
192
193
# File 'lib/rbbt/sources/uniprot.rb', line 191

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

.variants(protein) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/rbbt/sources/uniprot.rb', line 245

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