package gem;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import gem.util.ArrayUtils;
import gem.util.Progress;
import gem.util.TermCounter;
/**
* Counter class for counting cases in the discretized expression data.
*
* @author Ozgun Babur
* Date: Jun 8, 2008
* Time: 7:13:37 PM
*/
public class CaseCounter implements Constants
{
/**
* Counts cases according the given tail ratio.
*
* @param trips
* @param tailRatio highest (1 / tailRatio) is consideres as high, lowest (1 / tailRatio) is
* considered as low
*/
public static void count(Collection<Triplet> trips, double tailRatio)
{
System.out.print("Counting cases ... ");
Progress p = new Progress(trips.size());
Set<Gene> adjusted = new HashSet<Gene>();
for (Triplet t : trips)
{
if (!adjusted.contains(t.M)) { t.M.rankAdjustStatus(tailRatio); adjusted.add(t.M);}
if (!adjusted.contains(t.F)) { t.F.rankAdjustStatus(tailRatio); adjusted.add(t.F);}
if (!adjusted.contains(t.T)) { t.T.rankAdjustStatus(tailRatio); adjusted.add(t.T);}
count(t);
p.tick();
}
}
public static void count(Collection<Triplet> trips, double tailRatio, boolean[] ignore)
{
System.out.print("Counting cases ... ");
int tc = ArrayUtils.countTrue(ignore);
Progress p = new Progress(trips.size());
Set<Gene> adjusted = new HashSet<Gene>();
for (Triplet t : trips)
{
if (!adjusted.contains(t.M)) { t.M.rankAdjustStatus(tailRatio, 1-tailRatio, ignore, tc); adjusted.add(t.M);}
if (!adjusted.contains(t.F)) { t.F.rankAdjustStatus(tailRatio, 1-tailRatio, ignore, tc); adjusted.add(t.F);}
if (!adjusted.contains(t.T)) { t.T.rankAdjustStatus(tailRatio, 1-tailRatio, ignore, tc); adjusted.add(t.T);}
count(t);
p.tick();
}
}
public static void count(Triplet t)
{
adjustStatus(t);
resetCount(t);
int mi;
int fi;
int ti;
for (int i = 0; i < t.M.getExpSize(); i++)
{
if (t.M.status[i] == MARGINAL ||
t.F.status[i] == MARGINAL ||
t.T.status[i] == MARGINAL) continue;
mi = t.M.status[i] == ABSENT ? 0 : 1;
fi = t.F.status[i] == ABSENT ? 0 : 1;
ti = t.T.status[i] == ABSENT ? 0 : 1;
int ind = (ti * 4) + (mi * 2) + fi;
t.cnt[ind]++;
}
}
public static void count(Triplet t, boolean[] ignore)
{
resetCount(t);
int mi;
int fi;
int ti;
for (int i = 0; i < t.M.getExpSize(); i++)
{
if (ignore[i]) continue;
if (t.M.status[i] == MARGINAL ||
t.F.status[i] == MARGINAL ||
t.T.status[i] == MARGINAL) continue;
mi = t.M.status[i] == ABSENT ? 0 : 1;
fi = t.F.status[i] == ABSENT ? 0 : 1;
ti = t.T.status[i] == ABSENT ? 0 : 1;
int ind = (ti * 4) + (mi * 2) + fi;
t.cnt[ind]++;
}
}
/**
* Counts cases for the given triplet exclusively for the given condition of the given gene
* @param t
* @param gene
*/
public static void count(Triplet t, Gene gene, int cond)
{
resetCount(t);
int mi;
int fi;
int ti;
for (int i = 0; i < gene.getExpSize(); i++)
{
if (gene.status[i] != cond) continue;
if (t.M.status[i] == MARGINAL ||
t.F.status[i] == MARGINAL ||
t.T.status[i] == MARGINAL) continue;
mi = t.M.status[i] == ABSENT ? 0 : 1;
fi = t.F.status[i] == ABSENT ? 0 : 1;
ti = t.T.status[i] == ABSENT ? 0 : 1;
int ind = (ti * 4) + (mi * 2) + fi;
t.cnt[ind]++;
}
}
public static void resetCount(Triplet t)
{
for (int i = 0; i < t.cnt.length; i++)
{
t.cnt[i] = 0;
}
}
public static void adjustStatus(Triplet t)
{
adjustStatus(t.M);
adjustStatus(t.F);
adjustStatus(t.T);
}
public static void adjustStatus(Gene gene)
{
if (gene.status == null)
{
gene.rankAdjustStatus(1D/3);
}
}
/**
* Counts cases for the given triplet exclusively for the given condition of the given gene
* @param t
* @param M2
*/
public static int[] countUncorrelated(Triplet t, Gene M2)
{
int[] cnt = new int[8];
int mi;
int fi;
int ti;
for (int i = 0; i < M2.getExpSize(); i++)
{
if (M2.status[i] == t.M.status[i]) continue;
if (t.M.status[i] == MARGINAL ||
M2.status[i] == MARGINAL ||
t.F.status[i] == MARGINAL ||
t.T.status[i] == MARGINAL) continue;
mi = t.M.status[i] == ABSENT ? 0 : 1;
fi = t.F.status[i] == ABSENT ? 0 : 1;
ti = t.T.status[i] == ABSENT ? 0 : 1;
int ind = (ti * 4) + (mi * 2) + fi;
cnt[ind]++;
}
return cnt;
}
//----------------------------------------------------------------------------------------------
// Section: Detecting non-monotonic triplets
//----------------------------------------------------------------------------------------------
/**
* Counts all 27 bins.
*
* @param t
* @return
*/
public static int[][][] countComplete(Triplet t)
{
int[][][] cnt = new int[3][3][3];
int ri;
int fi;
int ti;
for (int i = 0; i < t.M.getExpSize(); i++)
{
ri = t.M.status[i] == ABSENT ? 0 : t.M.status[i] == PRESENT ? 2 : 1;
fi = t.F.status[i] == ABSENT ? 0 : t.F.status[i] == PRESENT ? 2 : 1;
ti = t.T.status[i] == ABSENT ? 0 : t.T.status[i] == PRESENT ? 2 : 1;
cnt[ri][fi][ti]++;
}
return cnt;
}
//----------------------------------------------------------------------------------------------
// Section: Detecting pairwise relation
//----------------------------------------------------------------------------------------------
public static int[][] countTwo(Gene g1, Gene g2)
{
int[][] cnt = new int[2][2];
int i1;
int i2;
for (int i = 0; i < g1.getExpSize(); i++)
{
if (g1.status[i] == MARGINAL || g2.status[i] == MARGINAL) continue;
i1 = g1.status[i] == ABSENT ? 0 : 1;
i2 = g2.status[i] == ABSENT ? 0 : 1;
cnt[i1][i2]++;
}
return cnt;
}
public static int[][] countTwoWhenThird(Gene g1, Gene g2, Gene g3, boolean equals)
{
int[][] cnt = new int[2][2];
int i1;
int i2;
for (int i = 0; i < g1.getExpSize(); i++)
{
if (g1.status[i] == MARGINAL || g2.status[i] == MARGINAL ||
(equals ? g3.status[i] != g1.status[i] : g3.status[i] == g1.status[i])) continue;
i1 = g1.status[i] == ABSENT ? 0 : 1;
i2 = g2.status[i] == ABSENT ? 0 : 1;
cnt[i1][i2]++;
}
return cnt;
}
//----------------------------------------------------------------------------------------------
// Section: Tissue related
//----------------------------------------------------------------------------------------------
/**
* Counts cases using only the given experiment indexes.
*
* @param trips
* @param pos
*/
public static void count(Collection<Triplet> trips, boolean[] pos)
{
// Progress p = new Progress(trips.size());
for (Triplet t : trips)
{
countTissue(t, pos);
// p.tick();
}
}
/**
* Counts cases using only the given experiment indexes.
*
* @param t
* @param pos
*/
public static void countTissue(Triplet t, boolean[] pos)
{
int ri;
int fi;
int ti;
for (int i = 0; i < t.M.getExpSize(); i++)
{
if (!pos[i]) continue;
if (t.F.status[i] == ABSENT) fi = 0;
else if (t.F.status[i] == PRESENT) fi = 1;
else continue;
if (t.T.status[i] == ABSENT) ti = 0;
else if (t.T.status[i] == PRESENT) ti = 1;
else continue;
if (t.M.status[i] == ABSENT) ri = 0;
else if (t.M.status[i] == PRESENT) ri = 1;
else continue;
int ind = (ti * 4) + (ri * 2) + fi;
t.cnt_tiss[ind]++;
}
}
public static void printTissuesInCounts(Triplet t, String[] names)
{
TermCounter[] tc = new TermCounter[8];
for (int i = 0; i < tc.length; i++) tc[i] = new TermCounter();
int mi;
int fi;
int ti;
for (int i = 0; i < t.M.getExpSize(); i++)
{
if (t.M.status[i] == MARGINAL ||
t.F.status[i] == MARGINAL ||
t.T.status[i] == MARGINAL) continue;
mi = t.M.status[i] == ABSENT ? 0 : 1;
fi = t.F.status[i] == ABSENT ? 0 : 1;
ti = t.T.status[i] == ABSENT ? 0 : 1;
int ind = (ti * 4) + (mi * 2) + fi;
tc[ind].addTerm(names[i]);
}
Map<String, Integer>[] map = new Map[8];
for (int i = 0; i < tc.length; i++)
{
map[i] = tc[i].getTermCounts();
}
for (String tiss : TISSUES)
{
System.out.print(tiss);
for (Map<String, Integer> cnt : map)
{
Integer in = cnt.get(tiss);
System.out.print("\t" + (in == null ? "" : in));
}
System.out.println();
}
}
private static final String[] TISSUES = new String[]{"Kidney", "Colon", "Prostate", "Lung", "Rectosigmoid", "Liver", "Rectum", "Urinary Bladder", "Tyroid", "Breast"};
}