Package gem

Source Code of gem.Tuple

package gem;

import gem.parser.TabDelimitedFileParser;
import gem.util.Histogram;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
* @author Ozgun Babur
*/
public class Tuple implements Comparable, Constants
{
  Gene U;
  Gene T;

  String u_id;
  String t_id;

  int[] cnt;

  double alphaU;
  double alphaU_pval;

  public Tuple(Gene u, Gene t)
  {
    setGenes(u, t);
  }

  public void setGenes(Gene u, Gene t)
  {
    U = u;
    T = t;

    u_id = U.geneid;
    t_id = T.geneid;

    if (U.status == null) U.rankAdjustStatus(1D/3);
    if (T.status == null) T.rankAdjustStatus(1D/3);

    alphaU = Difference.calcPairwiseCoefficient(U, T);
    alphaU_pval = Difference.calcPairwiseCoefficientPval(U, T);
  }

  public Tuple(String u_id, String t_id)
  {
    this.u_id = u_id;
    this.t_id = t_id;
  }

  public int compareTo(Object o)
  {
    Tuple t = (Tuple) o;

    int c = new Double(alphaU_pval).compareTo(t.alphaU_pval);

    if (c == 0)
    {
      c = new Double(Math.abs(t.alphaU)).compareTo(Math.abs(alphaU));
    }
    return c;
  }

  public static double getThresholdForFDR(List<Tuple> tuples, double targetFDR)
  {
    Collections.sort(tuples);

    int n = 0;
    for (Tuple tuple : tuples)
    {
      n++;

      double fd = tuple.alphaU_pval * tuples.size();
      double fdr = fd / n;

      if (fdr > targetFDR)
      {
        return tuple.alphaU_pval - EPSILON;
      }
    }
    return tuples.get(tuples.size() - 1).alphaU_pval + EPSILON;
  }

  public static List<Tuple> filterToThreshold(List<Tuple> tuples, double thr)
  {
    Iterator<Tuple> iter = tuples.iterator();
    while (iter.hasNext())
    {
      Tuple t = iter.next();
      if (t.alphaU_pval >= thr)
      {
        iter.remove();
      }
    }
    return tuples;
  }

  public static Set<String> getGeneIDs(Collection<Tuple> tuples)
  {
    Set<String> ids = new HashSet<String>();

    for (Tuple t : tuples)
    {
      ids.add(t.u_id);
      ids.add(t.t_id);
    }
    return ids;
  }

  public static List<Tuple> getEffectiveSet(List<Tuple> tuples, double fdr)
  {
    double thr = getThresholdForFDR(tuples, fdr);
    List<Tuple> list = new ArrayList<Tuple>();
    for (Tuple t : tuples)
    {
      if (t.alphaU_pval <= thr) list.add(t);
    }
    return list;
  }

  @Override
  public String toString()
  {
    return U.geneid + " - " + T.geneid;
  }

  public static void main(String[] args) throws IOException
  {
    String facName = "AR";
    TabDelimitedFileParser parser = new TabDelimitedFileParser("resource/factors/"+ facName + "_andr.txt");
    Set<String> tarNames = parser.getColumnSet("Target");
    System.out.println("tarNames.size() = " + tarNames.size());
   
    List<Tuple> tuples = new ArrayList<Tuple>();

    for (String tarName : tarNames)
    {
      tuples.add(new Tuple(Triplet.getSymbolToGeneMap().get(facName),
        Triplet.getSymbolToGeneMap().get(tarName)));
    }
    System.out.println("tuples.size() = " + tuples.size());
    tuples = ExpDataReader.associateTuples(tuples, "resource/expdata/expo", 10, 0);
    System.out.println("tuples.size() = " + tuples.size());

//    Histogram h = new Histogram(0.05);
//    for (Tuple t : tuples)
//    {
//      h.count(t.alphaU);
//    }
//    h.print();
//    if (true) return;

    double thr = getThresholdForFDR(tuples, 0.05);
    System.out.println("thr = " + thr);

    tuples = filterToThreshold(tuples, thr);

    System.out.println("tuples.size() = " + tuples.size());

    BufferedWriter writer = new BufferedWriter(new FileWriter("result/tuples.txt"));
    writer.write("Factor\tTarget\tAlphaF\tpval");
    for (Tuple t : tuples)
    {
      if (Math.abs(t.alphaU) < 0.2) continue;
      writer.write("\n" + t.U.getSymbol() + "\t" + t.T.getSymbol() + "\t" +
        fmt.format(t.alphaU) + "\t" + fmt.format(t.alphaU_pval));
    }
    writer.close();
  }
}
TOP

Related Classes of gem.Tuple

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.