/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package data.cerevisiae.histmod.kirmizis2009;
import fork.lib.base.Print;
import fork.lib.base.file.FileName;
import fork.lib.base.file.io.txt.ReadTable;
import fork.lib.base.file.management.Dirs;
import fork.lib.bio.anno.genomic.BedGraphExporter;
import fork.lib.bio.anno.genomic.BedReader;
import fork.lib.bio.anno.genomic.LandscapeBuilder;
import fork.lib.bio.anno.genomic.region.GenomicRegion;
import fork.lib.bio.anno.genomic.region.GenomicRegionsBuilder;
import fork.lib.bio.data.microarray.ProbeToLocationMapper;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
public class ProbeMapper{
protected File anno, probe;
public ProbeMapper(File anno, File probe)throws Exception{
this.anno=anno;
this.probe=probe;
}
public void writeToFiles(ArrayList<String> ids, ArrayList<Double> vs,
File ofor, File orev, String tag)throws Exception{
write(ids, vs, ofor, "forward", tag);
write(ids, vs, orev, "reverse", tag);
}
protected void write(ArrayList<String> ids, ArrayList<Double> vs, File f,String std, String tag)throws Exception{
ProbeMapperStrand pp= new ProbeMapperStrand(anno, probe, std);
BedGraphExporter be= new BedGraphExporter(pp.toLandscapeBuilder(ids, vs));
be.param().tit= tag+"_"+std;
be.writeToFile(f);
}
class ProbeMapperStrand extends ProbeToLocationMapper{
protected File anno, probe;
protected String std;
protected HashMap<String, GenomicRegion> ptr= new HashMap<>();
public ProbeMapperStrand(File anno, File probe, String std)throws Exception{
this.anno=anno;
this.probe=probe;
this.std=std;
init();
}
protected void init() throws Exception{
GenomicRegionsBuilder gba= new BedReader(anno).getGenomicRegionsBuilder();
String[][] arr= new ReadTable(probe).getTableAsArray();
double pp=-1; String pc="", pid=""; int ind=0;
for(int i=0; i<arr.length ; i++){
String[] r= arr[i];
if(r[2].equalsIgnoreCase(std)){
String id= r[0];
double loc= Double.parseDouble(r[3]);
String chr= r[1].substring(0,r[1].length()-12).toLowerCase();
if(ind!=0){
if(gba.containsChromosome(pc)){
if(pc.equals(chr)){
GenomicRegion gr= new GenomicRegion(pc, (int)pp, (int)loc-1);
ptr.put(pid, gr);
}else{
double cl= gba.getRegionsForChromosome(pc).get(0).high;
GenomicRegion gr= new GenomicRegion(pc, (int)pp, (int)cl-1);
ptr.put(pid, gr);
}
}
}
if(i==arr.length-1){
if(gba.containsChromosome(chr)){
double cl= gba.getRegionsForChromosome(chr).get(0).high;
GenomicRegion gr= new GenomicRegion(chr, (int)loc, (int)cl-1);
ptr.put(id, gr);
}
}
pp= loc;
pc= chr;
pid= id;
ind++;
}
}
}
@Override
public HashMap<String, GenomicRegion> probeToRegion() {
return ptr;
}
}
public static void main(String[] args) throws Exception{ //debug
File dir= Dirs.getFile("dir");
File d= new File(dir+"/other_datasets/kirmizis_2009_hist-mod");
File od= new File(d+"/wig");
File anno= new File(dir+"/anno/sacCer1_chr.bed");
File probe= new File(d+"/anno/GPL5683-4516.txt");
File[] fs= d.listFiles();
for(int i=0; i<fs.length ; i++){
File f= fs[i];
if(FileName.getExt(f).equals("txt")){
SignalReader sr= new SignalReader(f);
ProbeMapper pr= new ProbeMapper(anno, probe);
String tag= FileName.getBaseName(f);
pr.writeToFiles(sr.getIDs(), sr.getVs(),
new File(od+"/"+tag+"_for.wig"),
new File(od+"/"+tag+"_rev.wig"),
tag);
}
}
}
}