Module: Annotations::Genes

Defined in:
lib/MARQ/annotations.rb

Defined Under Namespace

Modules: Genecodis, SENT

Class Method Summary collapse

Class Method Details

.get_genes(dataset, options = {}) ⇒ Object



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/MARQ/annotations.rb', line 538

def self.get_genes(dataset, options = {})
  fdr, cut_off, folds, do_folds, nth_genes = {
    :fdr => false,
    :cut_off => 0.05,
    :folds => 2.5,
    :do_folds => true,
    :nth_genes => 0,
  }.merge(options).values_at(:fdr, :cut_off, :folds, :do_folds, :nth_genes)

  if nth_genes > 0
    return get_genes_nth(dataset, nth_genes)
  end


  path = MARQ.dataset_path(dataset)

  experiments = File.open(path + '.experiments').collect{|l| l.chomp.strip}
  genes       = File.open(path + '.codes').collect{|l| l.chomp.strip}


  experiments_ts   = experiments.select{|exp| exp !~ /\[ratio\]/}
  experiments_fold = experiments.select{|exp| exp =~ /\[ratio\]/}

  experiments_fold = [] if ! do_folds

  values_up = {}
  values_down = {}
  experiments.each{|exp| values_up[exp] = []; values_down[exp] = []}

  File.open(path + '.pvalues').each_with_index{|l, i|
    values = l.chomp.split(/\t/)
    experiments_ts.zip(values).each{|p|
      name = p.first
      value = p.last == "NA" ? 1.0 : p.last.to_f
      values_up[name]     << (value > 0 ? value : 1.0)
      values_down[name]   << (value < 0 ? - value : 1.0)
    }
  }

  File.open(path + '.logratios').each_with_index{|l, i|
    values = l.chomp.split(/\t/)
    experiments.zip(values).each{|p|
      name = p.first
      next unless experiments_fold.include? name
      value = p.last == "NA" ? 0 : p.last.to_f
      values_up[name]     << (p.last > 0 ? value : 0)
      values_down[name]   << (p.last < 0 ? - value : 0)
    }
  }

  genes_up = {}
  genes_down = {}

  threshold = cut_off
  values_up.each{|experiment, values|
    genes_up[experiment] = []
    if experiments_ts.include? experiment
      if fdr
        threshold = FDR.step_up(values.sort, cut_off)
        next if threshold == 0.0
      end
      values.each_with_index{|value, i| genes_up[experiment] << genes[i] if value < threshold}
    elsif experiment_fold.include? experiment
      values.each_with_index{|value, i| genes_up[experiment] << genes[i] if value < folds}
    end
  }
  values_down.each{|experiment, values|
    genes_down[experiment] = []
    if experiments_ts.include? experiment
      if fdr
        threshold = FDR.step_up(values.sort, cut_off)
        next if threshold == 0.0
      end
      values.each_with_index{|value, i| genes_down[experiment] << genes[i] if value < threshold}
    elsif experiment_fold.include? experiment
      values.each_with_index{|value, i| genes_down[experiment] << genes[i] if value < folds}
    end
  }


  {:up => genes_up, :down => genes_down}
end

.get_genes_nth(dataset, num_genes) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/MARQ/annotations.rb', line 513

def self.get_genes_nth(dataset, num_genes)
  path = MARQ::Dataset.path(dataset)

  experiments = File.open(path + '.experiments').collect{|l| l.chomp.strip}
  genes       = File.open(path + '.codes').collect{|l| l.chomp.strip}
  total_genes = genes.length

  genes_up = {}
  genes_down = {}
  experiments.each{|exp| genes_up[exp] = []; genes_down[exp] = []}

  File.open(path + '.orders').each_with_index{|l, i|
    values = l.chomp.split(/\t/)
    experiments.zip(values).each{|p|
      name = p.first
      value = p.last
      next if p.last == "NA"
      genes_up[name]   <<  genes[i] if value.to_i < num_genes
      genes_down[name] <<  genes[i] if value.to_i > total_genes - num_genes
    }
  }

  {:up => genes_up, :down => genes_down}
end

.get_genes_old(dataset, cut_off = 0.1, fdr = false) ⇒ Object



621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/MARQ/annotations.rb', line 621

def self.get_genes_old(dataset, cut_off = 0.1, fdr = false)

  path = MARQ::Dataset.path(dataset)

  experiments = File.open(path + '.experiments').collect{|l| l.chomp.strip}.select{|name| !name.match(/\[ratio\]/)}
  genes = File.open(path + '.codes').collect{|l| l.chomp.strip}


  values_up = {}
  values_down = {}
  experiments.each{|exp| values_up[exp] = []; values_down[exp] = []}


  if File.exist?(path + '.pvalues')
    File.open(path + '.pvalues').each_with_index{|l, i|
      values = l.chomp.split(/\t/)
      experiments.zip(values).each{|p|
        value = p.last == "NA" ? 1.0 : p.last.to_f
        values_up[p.first]     << (value > 0 ? value : 1.0)
        values_down[p.first]   << (value < 0 ? - value : 1.0)
      }
    }
  end

  genes_up = {}
  genes_down = {}

  threshold = cut_off
  values_up.each{|experiment, values|
    genes_up[experiment] = []
    if fdr
      threshold = FDR.step_up(values.sort, cut_off)
      next if threshold == 0.0
    end
    values.each_with_index{|value, i| genes_up[experiment] << genes[i] if value < threshold}
  }
  values_down.each{|experiment, values|
    genes_down[experiment] = []
    if fdr
      threshold = FDR.step_up(values.sort, cut_off)
      next if threshold == 0.0
    end
    values.each_with_index{|value, i| genes_down[experiment] << genes[i] if value < threshold}
  }


  {:up => genes_up, :down => genes_down}
end