Module: GEO::Process

Defined in:
lib/MARQ/GEO.rb

Overview

Use R to load and process the datasets

Defined Under Namespace

Modules: R Classes: AdhocPlatformCollisionError, PlatformNotProcessedError

Class Method Summary collapse

Class Method Details

.fix_GSE_ids(platform_codes_file, prefix) ⇒ Object

Fix possible discrepancies in ids between series and platforms



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

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 - 1] ||= nil

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

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

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

Process a dataset. Need to specify the platform. The field parameter can be used to use a different column for the field.

Deprecated in favor of using the original firt column and using a different one only for translation



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/MARQ/GEO.rb', line 445

def self.GDS(dataset, platform, field = nil)
  raise PlatformNotProcessedError if ! MARQ::Platform.exists? platform

  cross_platform = MARQ::Name.is_cross_platform? dataset

  platform_path = GEO.platform_path(platform)
  prefix = File.join(platform_path, 'GDS', dataset)

  FileUtils.rm(prefix + '.skip') if File.exist?(prefix + '.skip')

  if cross_platform
    R.GDS(MARQ::Name.clean(dataset), prefix, field, File.join(platform_path, 'translations'))
  else
    R.GDS(dataset, prefix, field, nil) 
  end
end

.GPL(platform) ⇒ Object

Load GPL data. Translates IDS of the platform probes using AILUN and our system (called biomart for clarity)



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
538
539
540
541
542
543
544
# File 'lib/MARQ/GEO.rb', line 465

def self.GPL(platform)
  path = File.join(DATA_DIR, platform)
  return if File.exist?(path)

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

  info = SOFT.GPL(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(path, 'GDS'),
    File.join(path, 'GSE'),
  ].each{|d|
    FileUtils.mkdir d unless File.exist? d
  }

  R.GPL(platform, path, nil)
  FileUtils.mv path + '.codes', File.join(path, 'codes')


  # AILUN translations
  codes  = Open.read(File.join(path, 'codes')).collect{|l| l.chomp}
  ailun = ID.AILUN_translate(platform, codes)
  Open.write(File.join(path, '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(path, 'codes')).collect{|l| l.chomp}
    else
      if field
        R.GPL(platform, path, field[0])
        FileUtils.mv path + '.codes', File.join(path, 'other')
      end

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

    biomart = translate(organism, codes) 
    Open.write(File.join(path, '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 = 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(path, 'translations'), translations.collect{|v| v || "NO MATCH"}.join("\n"))
    Open.write(File.join(path, 'cross_platform'), translations.compact.sort.uniq.join("\n"))
  end

end

.GSE(series, info) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
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
# File 'lib/MARQ/GEO.rb', line 369

def self.GSE(series, info)
  platform = info[:platform]
  do_log   = info[:log2].nil? ? nil : !info[:log2]
  fields   = info[:fields]
 
  # Determine samples and sample conditions
  gsms = []
  conditions = {}
  info[:arrays].each{|gsm, cond|
    gsms << gsm
    cond.each{|type, value|
      conditions[type] ||= []
      conditions[type] << value
    }
  }
 
  # Adhoc platforms are for series with samples from different platforms.
  # They are created when the series is processed
  adhoc_platform = platform.match(/_/) != nil

  raise PlatformNotProcessedError if ! adhoc_platform && ! MARQ::Platform.exists?(platform)
  raise AdhocPlatformCollisionError if adhoc_platform && MARQ::Platform.exists?(platform) && ! MARQ::Name.is_cross_platform?(series)

  platform_path = GEO.platform_path(platform) || File.join(MARQ.datadir, 'GEO', platform)

  prefix = File.join(platform_path, 'GSE', series)
  
  FileUtils.mkdir_p File.join(platform_path, 'GSE') unless File.exists? File.join(platform_path, 'GSE')

  FileUtils.rm(prefix + '.skip') if File.exist?(prefix + '.skip')

  if ! MARQ::Name.is_cross_platform?(series)
    R.GSE(gsms, conditions, do_log, prefix, nil, fields, info[:title], info[:description])

    # Set up codes and cross_platform for adhoc platforms
    if adhoc_platform 
      codes        = Open.read(prefix + '.codes').collect{|l| l.chomp}
      organism     = GEO.platform_organism(platform.split(/_/)[0])
      translations = translate(organism, codes) 
      FileUtils.cp(prefix + '.codes', File.join(platform_path,'codes'))
      if translations.compact.length > codes.length.to_f / 10
        Open.write(File.join(platform_path, 'translations'), translations.collect{|v| v || "NO MATCH"}.join("\n"))
        Open.write(File.join(platform_path, 'cross_platform'), translations.compact.sort.uniq.join("\n"))
      end
    else
      fix_GSE_ids(File.join(platform_path, 'codes'), prefix);
    end

  else
    FileUtils.cp(File.join(platform,'translations'), prefix + '.translations')

    swap_file = File.join(MARQ::Dataset.path(MARQ::Name.clean series) + '.swap')
    if File.exist?(swap_file)
      orders = Open.read(swap_file).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

    R.GSE(gsms, conditions, do_log, prefix, prefix +  '.translations', fields, info[:title], info[:description])

    fix_GSE_ids(File.join(platform_path, 'cross_platform'),prefix);

    FileUtils.rm(prefix + '.translations') if File.exist?(prefix + '.translations')
  end

end

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

Rearange the lines of a file with the given order. The order specifies, for each position in the original file, where it should en in the final file



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/MARQ/GEO.rb', line 324

def self.rearange(order, file, missing = "NA")
  orig_lines = []
  File.open(file).each_line{|l| orig_lines << l}

  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 order[i].nil?
    lines[order[i]] = l.chomp
  }

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

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

.translate(org, list) ⇒ Object



313
314
315
316
317
318
319
320
# File 'lib/MARQ/GEO.rb', line 313

def self.translate(org, list)
  begin
    ID.translate_DB(org, list)
  rescue
    puts "DB translation failed, resorting to index"
    ID.translate_index(org, list)
  end
end