/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package data.cerevisiae.tfpwm;
import fork.lib.base.collection.NamedTable;
import fork.lib.base.file.FileName;
import fork.lib.base.file.io.txt.MarkEntry;
import fork.lib.base.file.io.txt.MarkReader;
import fork.lib.base.file.management.Dirs;
import fork.lib.bio.seq.FastaEntry;
import fork.lib.bio.seq.FastaReader;
import fork.lib.bio.seq.align.Motif;
import fork.lib.bio.seq.align.MotifToLandscape;
import fork.lib.bio.seq.align.PWM;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/**
*
* @author forksapien
*/
public class ZhuToLandscape {
protected File f;
protected MarkReader mr;
public ZhuToLandscape(File tf)throws Exception{
this.f=tf;
init();
}
protected void init() throws Exception{
mr= new MarkReader(f,"-primary");
mr.setIfFirstChar(false);
}
public class PWMEntry{
public String tit;
public PWM pwm;
public PWMEntry(String tit, PWM pwm){
this.tit=tit;
this.pwm=pwm;
}
}
public PWMEntry nextEntry() throws Exception{
MarkEntry en= mr.nextEntry();
if(en==null){
return null;
}
String tit= en.getTitle().split("-primary")[0].toUpperCase();
NamedTable<Character,Integer,Double> tab= new NamedTable<>();
ArrayList<String> ch= en.getChunk();
char[] rs= new char[]{'A','C','G','T'};
for(int i=0; i<ch.size() ; i++){
String[] ss= ch.get(i).split("\t");
Character row= rs[i];
tab.appendEmptyRow(row);
for(int j=0; j<ss.length ; j++){
Integer col= j;
Double v= Double.parseDouble(ss[j]);
if(!tab.containsColumn(col)){
tab.appendEmptyColumn(col);
}
tab.setValueAt(v, row, col);
}
}
PWM mot=new PWM(tab);
return new PWMEntry(tit, mot);
}
public static void main(String[] args) throws Exception{ //debug
File dir= Dirs.getFile("dir");
File f= new File(dir+"/data/tf/zhu_2009_yeast-tf.txt");
File od= new File(dir+"/data/tf/"+FileName.getBaseName(f));
ZhuToLandscape zz= new ZhuToLandscape(f);
PWMEntry en;
while( (en=zz.nextEntry())!=null ){
String nm= en.tit;
PWM mot= en.pwm;
FastaReader fr= new FastaReader(new File(dir+"/anno/genomes/sacCer1/sacCer1.fa"));
MotifToLandscape ml= new MotifToLandscape(fr, mot);
System.out.println(nm+" ");
File of= new File(od+"/"+nm+".wig");
ml.writeToFile(of, nm);
fr.close();
/*
String s= "AGATTATTTAATAATGGGAC";
double a= mot.align(s);
mot.print();
System.out.println(a);
*/
//System.exit(1);
}
}
}