/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package data.cerevisiae.tfpwm;
import fork.lib.base.Print;
import fork.lib.base.file.io.txt.ReadTable;
import fork.lib.base.file.management.Dirs;
import fork.lib.math.algebra.elementary.set.discrete.DiscreteSet;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
/**
*
* @author forksapien
*/
public class Overlap {
protected File fa, fb;
double[] thras= new double[]{0.005};
double[] thrbs= new double[]{4};
public Overlap(File fa, File fb)throws Exception{
this.fa=fa;
this.fb=fb;
init();
}
protected void init() throws Exception{
Tab ta= tab(fa);
Tab tb= tab(fb);
HashMap<String,Integer> cbi= new HashMap<>();
for(int i=1; i<tb.cns.length ; i++){
cbi.put(tb.cns[i], i);
}
for(int i=1; i<ta.cns.length ; i++){
String cna= ta.cns[i];
if(cbi.containsKey(cna)){
double[] vas= getCol(ta.arr, i);
double[] vbs= getCol(tb.arr, cbi.get(cna));
String[] idas= getID(ta.arr);
String[] idbs= getID(tb.arr);
for(int j=0; j<thras.length ; j++){
double thra= thras[j];
double thrb= thrbs[j];
DiscreteSet<String> ida= new DiscreteSet<>(), idb= new DiscreteSet<>();
for(int k=0; k<vas.length ; k++){
if(vas[k]<thra){ida.add(idas[k]);}
}
for(int k=0; k<vbs.length ; k++){
if(vbs[k]>thrb){idb.add(idbs[k]);}
}
int na= ida.size(), nb= idb.size();
int tna= ta.arr.length-1, tnb= tb.arr.length-1;
DiscreteSet<String> ovl= ida.intersection(idb);
double ra= (double)na/tna, rb= (double) nb/tnb;
double exp= (double) (tna+tnb)/2*ra*rb;
//Print.collection(ida);Print.collection(idb);
System.out.println("tf:"+cna+" thra:"+thra+" thrb:"+thrb);
System.out.println("na:"+na+" nb:"+ nb+" ov:"+ ovl.size()+" ex:"+exp);
System.out.println();
//System.exit(1);
}
}
}
}
private double[] getCol(String[][] arr, int ind){
double[] ret= new double[arr.length-1];
for(int i=1; i<arr.length ; i++){
ret[i-1]= Double.parseDouble(arr[i][ind]);
}
return ret;
}
private String[] getID(String[][] arr){
String[] ret= new String[arr.length-1];
for(int i=1; i<arr.length ; i++){
ret[i-1]= arr[i][0];
}
return ret;
}
private Tab tab(File f)throws Exception{
String[][] arr= new ReadTable(f).getTableAsArray();
String[] cns= arr[0];
for(int i=1; i<cns.length ; i++){
String tf= cns[i];
tf= tf.replace("_YPD", "");
tf= tf.replace("(", "");
tf= tf.replace(")", "");
tf= tf.replace(" ", "_");
tf= tf.toUpperCase();
cns[i]= tf;
}
return new Tab(cns, arr);
}
private String[][] ReadTable(File f) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
class Tab{
public String[] cns;
public String[][] arr;
public Tab(String[] cns, String[][] arr){
this.cns=cns;
this.arr=arr;
}
}
public static void main(String[] args) throws Exception{ //debug
File dir= Dirs.getFile("dir");
File fa= new File(dir+"/other_datasets/harbison_2004/pvalbygene_forpaper_abbr.txt");
File fb= new File(dir+"/data/tf/all_zhu_2009_yeast-tf_sacCer1-xu_gene_five_150.0.txt");
Overlap oo= new Overlap(fa,fb);
}
}