package data.cerevisiae.inhouse.jointable;
import fork.lib.base.Print;
import fork.lib.base.collection.NamedTable;
import fork.lib.base.collection.Table;
import fork.lib.base.file.FileName;
import fork.lib.base.file.io.txt.ReadTable;
import fork.lib.base.file.management.Dirs;
import fork.lib.base.format.StringOp;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
/**
*
* @author forksapien
*/
public class JoinTable {
protected NamedTable<String,String, String> tab = new NamedTable<>();
public JoinTable(){
}
public void initEmptyRow(String[] rns)throws Exception {
for(int j=0; j<rns.length ; j++){
String rn= rns[j];
if(!tab.containsRow(rn)){
tab.appendEmptyRow(rn);
}
}
}
public void initRows(File f) throws IOException{
ReadTable rt= new ReadTable(f);
String[] rns= rt.getColumn(3);
for(int j=0; j<rns.length ; j++){
String rn= rns[j];
if(!tab.containsRow(rn)){
tab.appendEmptyRow(rn);
}
}
String[][] arr= rt.getTableAsArray();
tab.appendEmptyColumn("pos");
tab.appendEmptyColumn("chr");
tab.appendEmptyColumn("low");
tab.appendEmptyColumn("high");
tab.appendEmptyColumn("std");
for(int i=0; i<arr.length ; i++){
String[] r= arr[i];
String n = r[3];
String pos= r[0]+":"+r[1]+"-"+r[2];
tab.setValueAt(pos, n, "pos");
tab.setValueAt(r[0], n, "chr");
tab.setValueAt(r[1], n, "low");
tab.setValueAt(r[2], n, "high");
tab.setValueAt(r[5], n, "std");
}
}
public void addBedFile(File f) throws Exception{
String cn= FileName.getBaseName(f);
ReadTable rt= new ReadTable(f);
this.addHashMapIDToVal(rt.getHashMap(3, 4), cn);
}
public void addNameBedFile(File f) throws Exception{
addBedFile(f);
String cn= FileName.getBaseName(f);
for(int i=0; i<tab.getRowNames().size() ; i++){
String rn= tab.getRowNames().get(i);
String v= tab.getValueAt(rn, cn);
if(v==null){
tab.setValueAt(rn, rn, cn);
}
}
}
public void writeToFile(File out) throws Exception{
out.getParentFile().mkdirs();
tab.setNullValues("NAN");
tab.replaceValues("", "NAN");
tab.writeToFile(out);
}
/**
* col 0 to 1 of txt file
* @param f
* @throws Exception
*/
public void addTextFile(File f)throws Exception {
String cn= FileName.getBaseName(f);
ReadTable rt= new ReadTable(f);
this.addHashMapIDToVal(rt.getHashMap(0, 1), cn);
}
public void addHashMapIDToVal(HashMap<String,String> hm, String cn)throws Exception {
tab.appendEmptyColumn(cn);
String[] rns= new String[hm.keySet().size()];
hm.keySet().toArray(rns);
for(int j=0; j<rns.length ; j++){
String rn= rns[j];
String v= hm.get(rn);
if(tab.containsRow(rn)){
tab.setValueAt(v, rn, cn);
}
}
}
public void addTextFileChunk(File f)throws Exception {
ReadTable rt= new ReadTable(f);
ArrayList<String> r0= rt.getTable().getRow(0);
rt= new ReadTable(f);
rt.param().setSkipRows(0);
for( int i=1; i<r0.size(); i++ ){
String cn= r0.get(i).replace(" ", "");
this.addHashMapIDToVal(rt.getHashMap(0, i), cn);
}
}
public void addElementsAnyway(ArrayList al, String cn){
tab.appendEmptyColumn(cn);
for( int i=0; i<al.size(); i++ ){
tab.setValueAt(al.get(i).toString(), i, tab.getColumnNumber()-1);
}
}
public static void main(String[] args) throws Exception{ //debug
File dir= Dirs.getFile("dir");
File d= new File(dir+"/other_datasets/pelechano_2009_polii");
File f= new File(d+"/pelechano_2009.txt");
File nf= new File(dir+"/anno/sgd_name.bed");
File ref= new File(dir+"/anno/xu_2009_orfs.bed");
//File ref= new File(dir+"/anno/sgdGene_sacCer1.bed");
JoinTable jj= new JoinTable();
jj.initRows(ref);
jj.addNameBedFile(nf);
jj.addBedFile(new File(dir+"/anno/bed/sacCer1-xu/orient/orient3.bed"));
jj.addBedFile(new File(dir+"/anno/bed/sacCer1-xu/prop/sacCer1-xu_length.bed"));
jj.addBedFile(new File(dir+"/anno/bed/sacCer1-xu/prop/sacCer1-xu_subtelo_lab.bed"));
jj.addBedFile(new File(dir+"/other_datasets/kirmizis_2009_hist-mod/wig/score/h3r2me1_for/h3r2me1_for_sacCer1-xu_gene_five_0.150.bed"));
jj.addBedFile(new File(dir+"/other_datasets/kirmizis_2009_hist-mod/wig/score/h3r2me1_for/h3r2me1_for_sacCer1-xu_gene_three_150.0.bed"));
jj.addBedFile(new File(dir+"/other_datasets/kirmizis_2009_hist-mod/wig/score/h3r2me2a_for/h3r2me2a_for_sacCer1-xu_gene_five_0.150.bed"));
jj.addBedFile(new File(dir+"/other_datasets/kirmizis_2009_hist-mod/wig/score/h3r2me2a_for/h3r2me2a_for_sacCer1-xu_gene_three_150.0.bed"));
jj.addBedFile(new File(dir+"/other_datasets/guillemette_2011_h4k3me3-ac/score/h3k4ac/h3k4ac_guillemette2011_sacCer1-xu_gene_five_0.150.bed"));
jj.addBedFile(new File(dir+"/other_datasets/guillemette_2011_h4k3me3-ac/score/h3k4ac/h3k4ac_guillemette2011_sacCer1-xu_gene_three_150.0.bed"));
jj.addBedFile(new File(dir+"/other_datasets/guillemette_2011_h4k3me3-ac/score/h3k4me3/h3k4me3_guillemette2011_sacCer1-xu_gene_five_0.150.bed"));
jj.addBedFile(new File(dir+"/other_datasets/guillemette_2011_h4k3me3-ac/score/h3k4me3/h3k4me3_guillemette2011_sacCer1-xu_gene_three_150.0.bed"));
jj.addBedFile(new File(dir+"/other_datasets/xue_2012_h4k18_h3k16/score/h3k18ac/h3k18ac_xue2012_sacCer1-xu_gene_five_0.150.bed"));
jj.addBedFile(new File(dir+"/other_datasets/xue_2012_h4k18_h3k16/score/h3k18ac/h3k18ac_xue2012_sacCer1-xu_gene_three_150.0.bed"));
jj.addBedFile(new File(dir+"/other_datasets/xue_2012_h4k18_h3k16/score/h4k16ac/h4k16ac_xue2012_sacCer1-xu_gene_five_0.150.bed"));
jj.addBedFile(new File(dir+"/other_datasets/xue_2012_h4k18_h3k16/score/h4k16ac/h4k16ac_xue2012_sacCer1-xu_gene_three_150.0.bed"));
jj.addBedFile(new File(dir+"/other_datasets/cai_2011_h3k9ac/score/h3k9ac/h3k9ac_cai2011_sacCer1-xu_gene_five_0.150.bed"));
jj.addBedFile(new File(dir+"/other_datasets/cai_2011_h3k9ac/score/h3k9ac/h3k9ac_cai2011_sacCer1-xu_gene_three_150.0.bed"));
jj.addBedFile(new File(dir+"/other_datasets/kirmizis_2009_hist-mod/wig/score/h3k36me3_for/h3k36me3_for_sacCer1-xu_gene_cds_full.bed"));
jj.addBedFile(new File(dir+"/other_datasets/kirmizis_2009_hist-mod/wig/score/h3k79me3_for/h3k79me3_for_sacCer1-xu_gene_cds_full.bed"));
jj.addBedFile(new File(dir+"/data/chip/norm/score/sacCer1-xu/k8ac/k8ac_sacCer1-xu_gene_five_0.150.bed"));
jj.addBedFile(new File(dir+"/data/chip/norm/score/sacCer1-xu/k8ac/k8ac_sacCer1-xu_gene_three_150.0.bed"));
File out= new File("plot/combine.txt");
jj.writeToFile(out);
}
}