Module: GEO

Defined in:
lib/MARQ/GEO.rb

Defined Under Namespace

Modules: Eutils

Constant Summary collapse

CACHE_DIR =
File.join(MARQ.cachedir,'GEO')
GEO_SOFT =
"http://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?targ=self&view=full&form=text&acc="
ID_FIX =
{
  :mgi_unigene => proc{|gene| if gene then gene.match(/^Mm./) ? gene : "Mm." + gene end},
  :human_unigene => proc{|gene| if gene then gene.match(/^Hs./) ? gene : "Hs." + gene end},
}
@@formats =
{}
@@r =
nil

Class Method Summary collapse

Class Method Details

.clean(name) ⇒ Object

{{{ Misc Info



274
275
276
# File 'lib/MARQ/GEO.rb', line 274

def self.clean(name)
  name.sub(/_cross_platform/,'') if name
end

.consecutive?(ids) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/MARQ/GEO.rb', line 51

def self.consecutive?(ids)
  ids.collect{|id| id.to_i}.sort[0..19] == (1..20).to_a
end

.dataset_path(dataset, platform = nil) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/MARQ/GEO.rb', line 282

def self.dataset_path(dataset, platform = nil)
  if platform
    return Dir.glob(File.join(platform_path(clean(platform)),"/*/#{ dataset }")).first.match(/(.*)\./)[1]
  else
    files = Dir.glob(File.join(MARQ.datadir, "GEO/GPL*/*/#{ dataset }.*"))
    if files.any?
      return files.first.match(/(.*)\./)[1]
    else
      return ""
    end
  end
end

.dataset_platform(dataset) ⇒ Object



315
316
317
318
# File 'lib/MARQ/GEO.rb', line 315

def self.dataset_platform(dataset)
  dataset_path(dataset).match(/(GPL\d+)/)
  $1
end

.dna_sequence?(ids) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/MARQ/GEO.rb', line 59

def self.dna_sequence?(ids)
  ids.compact.select{|id| ! id.strip.match(/^[ATCG]+$/i)}.empty?
end

.fix_GSE_ids(platform_codes_file, prefix) ⇒ Object

Fix possible discrepancies in ids between series and platforms



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/MARQ/GEO.rb', line 371

def self.fix_GSE_ids(platform_codes_file, prefix)
  platform_codes = File.open(platform_codes_file).collect{|l| l.chomp}
  platform_order = {}
  
  platform_codes.each_with_index{|code, i|
    platform_order[code] = i
  }

  series_codes = File.open(prefix + '.codes').collect{|l| l.chomp}

  platform_positions = platform_order.values_at(*series_codes)

  # Fill with nil for missing positions
  platform_positions[platform_codes.length] ||= nil

  %w(codes t logratios orders pvalues).each{|ext|
    rearange(platform_positions, prefix + '.' + ext)
  }

  Open.write(prefix + '.swap', platform_positions.join("\n"))
end

.GDS_info(name) ⇒ Object



260
261
262
263
264
265
266
267
268
269
# File 'lib/MARQ/GEO.rb', line 260

def self.GDS_info(name)
  begin
    title, description = Open.read(dataset_path(name) + '.description').split(/\n--\n/).values_at(0,1)
    {:title => title.strip, :description => description.strip}
  rescue Exception
    puts $!.message
    {:title => "" , :description => "" }
  end

end

.get_GDS(name, prefix, id_field = nil, id_file = nil) ⇒ Object



102
103
104
# File 'lib/MARQ/GEO.rb', line 102

def self.get_GDS(name, prefix, id_field = nil, id_file = nil)
  r.GEO_GDS_process(name, prefix, id_field, id_file, CACHE_DIR)
end

.get_GPL(name, prefix, id_field = nil) ⇒ Object

{{{ Process



98
99
100
# File 'lib/MARQ/GEO.rb', line 98

def self.get_GPL(name, prefix, id_field = nil)
  r.GEO_GPL_process(name, prefix, id_field, CACHE_DIR)
end

.get_GSE(gsms, conditions, do_log, prefix, id_file = nil, fields = nil, title = nil, description = nil) ⇒ Object



106
107
108
# File 'lib/MARQ/GEO.rb', line 106

def self.get_GSE(gsms, conditions, do_log, prefix, id_file = nil, fields= nil, title = nil, description = nil)
  r.GEO_GSE_process(gsms, conditions, prefix, do_log, id_file, fields, title, description, CACHE_DIR)
end

.get_soft(item) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/MARQ/GEO.rb', line 12

def self.get_soft(item)
  item = item.strip
  cache_file = File.join(CACHE_DIR, item + '.soft')
  if File.exist?( cache_file )
    File.open(cache_file).read
  else
    content = Open.read(GEO_SOFT + item, :nocache => true)
    fout = File.open(cache_file,'w')
    fout.write content
    fout.close
    content
  end
end

.GPL_id_fields(platform) ⇒ Object



171
172
173
174
175
176
# File 'lib/MARQ/GEO.rb', line 171

def self.GPL_id_fields(platform)
  soft = get_soft(platform)
  data = soft.split(/!platform_table_begin/s)[1].collect{|l| l.chomp.split(/\t/)}
  data.shift
  data.shift
end

.GPL_info(platform) ⇒ Object



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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/MARQ/GEO.rb', line 178

def self.GPL_info(platform)
  if !File.exist?(File.join(MARQ.datadir, 'GEO', 'platforms',"#{platform}.yaml"))  &&
     !File.exist?(File.join(MARQ.datadir, 'GEO', 'platforms',"#{platform}.skip")) 
    begin
      if platform =~ /_/
        organism =  GPL_info(platform.match(/(.*?)_/)[1])[:organism]
        
        info =  {
          :organism => organism,
          :title => "Merged platforms #{ platform }",
        }
        return info
      end
      soft = get_soft(platform)


      raise "SOFT file error" if soft !~ /!/

      organisms = soft.scan(/!Platform_organism\s*=\s*(.*)/).collect{|v| v.first.strip}

      if organisms.empty?
        raise "No Organism information" 
      else
        # This might happen actually GPL2529
        organisms.delete('Schizosaccharomyces pombe') if  organisms.include?('Saccharomyces cerevisiae')
        org_name = organisms.first
      end


      title = ""
      if soft.match(/!Platform_title\s*=\s*(.*)/)
        title = $1
      end

      org = Organism.name2org(org_name)
      raise "Organism not identified" if org.nil?

      if soft.match(/!platform_table_begin/)
        data = soft.split(/!platform_table_begin/s)[1].collect{|l| l.chomp.split(/\t/)}
        data.shift
        names = data.shift
        total = data.first.length
        genes = data.sort_by{ rand }[1..1000].collect{|v| v.first}

        id = guessIds(genes,org, names.first)
        other = nil
        other_pos = 0
        other_count = 0
        other_name = 0
        if id.nil? 
          (1..total - 1).to_a.each{|num|
            genes = data.collect{|v| v[num]}
            other = guessIds(genes,org, name = names[num])

            if other && other[1] > other_count
              other_pos = num
              other_count = other[1]
              other_name = names[num]
            end
          }
        end
      else
        raise "Soft file incomplete"
      end

      info = {:organism => org, :BioMart_ID => id ? id.first : nil, :title => title }
      info[:other_ID_field] = [other_pos + 1, other_name] if other_pos > 0


      Open.write(File.join(MARQ.datadir, 'GEO', 'platforms',"#{platform}.yaml"), info.to_yaml)
    rescue Exception
      puts $!.message
      puts $!.backtrace
      Open.write(File.join(MARQ.datadir, 'GEO', 'platforms',"#{platform}.skip"), $!.message)
    end
  end

  raise "Platform info for #{ platform } is not available and could not be automatically produced." if File.exist?(File.join(MARQ.datadir, 'GEO', 'platforms',"#{platform}.skip"))

  YAML::load(File.open(File.join(MARQ.datadir, 'GEO', 'platforms',"#{platform}.yaml")))
end

.GSE_info(series) ⇒ Object



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
137
138
139
140
141
142
143
144
145
146
# File 'lib/MARQ/GEO.rb', line 110

def self.GSE_info(series)
  soft = get_soft(series)
  raise "SOFT file error" if soft !~ /!/

  if match = soft.scan(/!Series_platform_id\s*=?\s*(.*)/)
    platform = match.flatten.collect{|p| p.strip}
  else
    raise "No Platform information" 
  end

  if soft.match(/!Series_title \s*=?\s*(.*)/)
    title = $1
  else
    raise "No Title information" 
  end

  if soft.match(/!Series_summary \s*=?\s*(.*)/)
    matches = soft.scan(/!Series_summary \s*=?\s*(.*)/).to_a
    description = matches.collect{|m| m.to_s.strip.sub(/!Series_summary \s*=?\s*/,'')}.join("\n")
  else
    raise "No Summary information" 
  end

  if soft.match(/!Series_sample_id \s*=?\s*(.*)/)
    matches = soft.scan(/!Series_sample_id \s*=?\s*(.*)/).to_a
    samples = matches.collect{|m| m.to_s.strip.sub(/!Series_sample_id \s*=?\s*/,'')}
  else
    raise "No Summary information" 
  end

  {
    :platform => platform.join("_"),
    :description =>description.strip,
    :title => title.strip,
    :samples => samples,
  }
end

.GSM_info(array) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/MARQ/GEO.rb', line 148

def self.GSM_info(array)
  soft = get_soft(array)

  if soft.match(/!Sample_title\s*=?\s*(.*)/)
    title = $1
  else
    raise "No Title information" 
  end


  if soft.match(/!Sample_description \s*=?\s*(.*)/)
    description = $1
  else
    raise "No Description information" 
  end

  {
    
    :description =>description.strip,
    :title => title.strip,
  }
end

.guessIds(genes, org, name = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/MARQ/GEO.rb', line 70

def self.guessIds(genes,org, name = nil)
  @@formats[org] ||= Organism.id_formats(org)
  if consecutive?(genes) || dna_sequence?(genes) || (numerical?(genes) && (name.nil? || !name.match(/entrez/i)))
    id = nil
  else
    fix = ID_FIX[(org + "_" + name.downcase).to_sym] if name
    if fix
      genes = genes.collect{|gene| fix.call(gene)}
    end
    id = Organism.guessIdFormat(@@formats[org], genes)
  end
  
  id 
end

.has_cross_platform?(dataset = nil, platform = nil) ⇒ Boolean

Returns:

  • (Boolean)


299
300
301
302
303
304
305
306
307
308
# File 'lib/MARQ/GEO.rb', line 299

def self.has_cross_platform?(dataset = nil, platform = nil)
  platform = clean(platform)
  raise "Dataset #{ dataset } not found" if dataset && dataset_path(dataset, platform).nil? 
  raise "Platform #{ platform } not found" if platform && platform_path(platform).nil? 
  if dataset
    File.exists?(dataset_path(dataset, platform) + "_cross_platform.orders")
  else
    Dir.glob(File.join(platform_path(platform), '*', '*_cross_platform.orders')).any?
  end
end

.is_cross_platform?(dataset) ⇒ Boolean

Returns:

  • (Boolean)


295
296
297
# File 'lib/MARQ/GEO.rb', line 295

def self.is_cross_platform?(dataset)
  dataset =~ /_cross_platform/
end

.numerical?(ids) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/MARQ/GEO.rb', line 55

def self.numerical?(ids)
  ids.compact.select{|id| ! id.match(/^\d+$/)}.uniq.length < ids.length.to_f / 10
end

.organism_platforms(organism) ⇒ Object



320
321
322
323
324
325
326
327
# File 'lib/MARQ/GEO.rb', line 320

def self.organism_platforms(organism)
  Dir.glob(File.join(MARQ.datadir, "GEO/GPL*")).collect{|f| 
    File.basename(f)
  }.select{|platform| 
    GPL_info(platform)[:organism] == organism &&
    platform_datasets(platform).any?
  }
end

.platform_datasets(platform) ⇒ Object



311
312
313
# File 'lib/MARQ/GEO.rb', line 311

def self.platform_datasets(platform)
  Dir.glob(File.join(platform_path(platform),"*/*.orders")).collect{|f| File.basename(f).sub(/.orders$/,'')}.select{|d| !is_cross_platform?(d)}
end

.platform_path(platform) ⇒ Object



278
279
280
# File 'lib/MARQ/GEO.rb', line 278

def self.platform_path(platform)
  File.join(MARQ.datadir, "GEO/#{clean(platform)}")
end

.process_GDS(dataset, platform, field = nil) ⇒ Object

{{{ Processing



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/MARQ/GEO.rb', line 331

def self.process_GDS(dataset, platform, field = nil)
  puts "Processing GDS #{ dataset }. Platform #{ platform }"

  puts "-- Original"
  prefix = File.join(platform_path(platform), 'GDS', dataset.to_s)
  GEO.get_GDS(dataset, prefix, field, nil)

  # Was there an error?
  if File.exist?(prefix + '.skip')
    FileUtils.cp(prefix + '.skip', prefix + '_cross_platform.skip')
    return
  end

  if File.exist?(File.join(platform,'cross_platform'))
    puts "-- Translated to cross_platform format"
    GEO.get_GDS(dataset, prefix + '_cross_platform', field, File.join(platform_path(platform), 'translations')) 
  end
end

.process_GSE(series, info) ⇒ Object



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/MARQ/GEO.rb', line 395

def self.process_GSE(series, info)
  return if Dir.glob(File.join(info[:platform], 'GSE', series) + '.*').any?

  gsms = []
  conditions = {}
  info[:arrays].each{|gsm, cond|
    gsms << gsm
    cond.each{|condition, value|
      conditions[condition] ||= []
      conditions[condition] << value
    }
  }
  platform = info[:platform]
  do_log = nil
  do_log = !info[:log2] if info[:log2]
  fields = info[:fields]

  puts "Processing GSE #{ series }. Platform #{ platform }"

  prefix = File.join(platform_path(platform), 'GSE', series.to_s)
  puts "-- Original"
  GEO.get_GSE(gsms, conditions, do_log, prefix, nil, fields, info[:title], info[:description])

  # Was there an error?
  if File.exist?(prefix + '.skip')
    FileUtils.cp(prefix + '.skip', prefix + '_cross_platform.skip')
    return
  end

  if platform =~ /_/
    FileUtils.cp(prefix + '.codes', File.join(platform_path(platform),'codes'))
    codes  = Open.read(File.join(platform_path(platform), 'codes')).collect{|l| l.chomp}
    organism = GEO::GPL_info(platform.match(/(.*?)_/)[1])[:organism]
    translations = ID.translate(organism, codes) 
    Open.write(File.join(platform_path(platform), 'translations'), translations.collect{|v| v || "NO MATCH"}.join("\n"))
    Open.write(File.join(platform_path(platform), 'cross_platform'), translations.compact.sort.uniq.join("\n"))
  else
    # Are the codes of the series equivalent to the ones in the platform?
    if  File.open(File.join(platform_path(platform),'codes')).collect{|l| l.chomp} != File.open(prefix + '.codes').collect{|l| l.chomp}
      fix_GSE_ids(File.join(platform_path(platform), 'codes'),prefix);
    end
  end


  if File.exist?(File.join(platform,'translations'))
    FileUtils.cp(File.join(platform,'translations'), prefix + '.translations')
    if File.exist?(prefix + '.swap')
      orders = Open.read(prefix + '.swap').collect{|l| l.chomp}
      inverse_orders = Array.new(orders.length)
      orders.each_with_index{|pos,i|
        next if pos !~ /\d/
        inverse_orders[pos.to_i] = i
      }
      rearange(inverse_orders, prefix + '.translations', "NO MATCH")
    end
    puts "-- Translated to cross_platform format"
    GEO.get_GSE(gsms, conditions, do_log, prefix + '_cross_platform', prefix + '.translations',fields, info[:title], info[:description])
    fix_GSE_ids(File.join(platform_path(platform), 'cross_platform'),prefix + '_cross_platform');
    FileUtils.rm(prefix + '.translations') if File.exist?(prefix + '.translations')
  end
   FileUtils.rm(prefix + '.swap') if File.exist?(prefix + '.swap')
end

.process_platform(platform) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/MARQ/GEO.rb', line 458

def self.process_platform(platform)
  path = platform_path(platform)
  return if File.exist? path

  if platform =~ /_/
    FileUtils.mkdir(path)
    FileUtils.mkdir(path + '/GSE')
    FileUtils.mkdir(path + '/GDS')
    return
  end

  info = GEO::GPL_info(platform)
  organism = info[:organism]

  field = info[:other_ID_field]
  id = info[:BioMart_ID]
  org = info[:organism]
  field = nil if field == ""
  id = nil if id == ""
 

  puts "Processing Platform #{ platform }"
  [platform,
    File.join(platform_path(platform), 'GDS'),
    File.join(platform_path(platform), 'GSE'),
  ].each{|d|
    FileUtils.mkdir d unless File.exist? d
  }

  get_GPL(platform, platform_path(platform), nil)
  FileUtils.mv platform_path(platform) + '.codes', File.join(platform_path(platform), 'codes')
  

  # AILUN translations
  codes  = Open.read(File.join(platform_path(platform), 'codes')).collect{|l| l.chomp}
  ailun = ID.AILUN_translate(platform, codes)
  Open.write(File.join(platform_path(platform), 'ailun'), ailun.collect{|v| v || "NO MATCH"}.join("\n")) if ailun.compact.length > codes.length.to_f / 10

  # BioMart translations
  biomart = []
  if id || field
    if id
      codes  = Open.read(File.join(platform_path(platform), 'codes')).collect{|l| l.chomp}
    else
      if field
        get_GPL(platform, platform_path(platform), field[0])
        FileUtils.mv platform_path(platform) + '.codes', File.join(platform_path(platform), 'other')
      end

      fix = ID_FIX[(organism + "_" + field[1].downcase).to_sym]
      codes  = Open.read(File.join(platform_path(platform), 'other')).collect{|l| 
        code = l.chomp
        code = fix.call(code) if fix
        code
      }
    end

    biomart = ID.translate(organism, codes) 
    Open.write(File.join(platform_path(platform), 'biomart'), biomart.collect{|v| v || "NO MATCH"}.join("\n")) if biomart.compact.length > codes.length.to_f / 10
  end

  # Select Best and save
  translations = []
  if ailun.compact.uniq.length > biomart.compact.uniq.length
    id_type = ID::DEFAULT_FORMATS[organism] || ID::DEFAULT_FORMAT_ALL || id || field || "Entrez Gene Id"
    if id_type.to_s !~ /Entrez/i
      translations = ID.translate(org,ailun.collect{|gene| gene || "NO MATCH"}) 
    else
      translations = ailun 
    end
  else
    translations = biomart
  end

  if translations.compact.length > codes.length.to_f / 10        
    Open.write(File.join(platform_path(platform), 'translations'), translations.collect{|v| v || "NO MATCH"}.join("\n"))
    Open.write(File.join(platform_path(platform), 'cross_platform'), translations.compact.sort.uniq.join("\n"))
  end

end

.process_platform_datasets(platform, force = false) ⇒ Object



540
541
542
543
544
545
546
547
548
549
550
# File 'lib/MARQ/GEO.rb', line 540

def self.process_platform_datasets(platform, force = false)
  raise "Platform #{ platform } not ready" unless File.exist? platform_path(platform)

  info = YAML::load(File.open(File.join(MARQ.datadir, "GEO/platforms/#{platform}.yaml")))

  datasets = GEO::Eutils::GPL_datasets(platform)
  datasets.each{|dataset|
    next if Dir.glob(File.join(platform_path(platform), 'GDS', dataset) + '.*').any? && ! force
    process_GDS(dataset, platform, nil) 
  }
end

.rObject



86
87
88
89
90
91
92
93
# File 'lib/MARQ/GEO.rb', line 86

def self.r
  if @@r.nil?
    RSRuby.instance.source(MARQ.rootdir + '/R/MA.R')
    RSRuby.instance.source(MARQ.rootdir + '/R/GEO.R')
    @@r = RSRuby.instance
  end
  @@r
end

.rearange(order, file, missing = "NA") ⇒ Object

Rearange the lines of a file with the given order



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/MARQ/GEO.rb', line 351

def self.rearange(order, file, missing = "NA")
  orig_lines = File.open(file).collect
  return if orig_lines.empty?
  columns = orig_lines.first.split(/\t/).length
  
  lines = Array.new(order.length)

  orig_lines.each_with_index{|l,i|
    next if l.nil? || order[i].nil?
    lines[order[i]] = l.chomp
  }

  lines = lines.collect{|l| l || [missing]*columns*"\t"}

  fout = File.open(file, 'w')
  fout.write(lines.join("\n"))
  fout.close
end