/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package data.cerevisiae.region;
import fork.lib.base.file.management.Dirs;
import fork.lib.bio.anno.genomic.BedExporter;
import fork.lib.bio.anno.genomic.BedReader;
import fork.lib.bio.anno.genomic.region.DirectionalGenomicRegion;
import fork.lib.bio.anno.genomic.region.GenomicRegion;
import fork.lib.bio.anno.genomic.region.GenomicRegionsBuilder;
import fork.lib.math.algebra.elementary.set.continuous.RegionException;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
/**
*
* @author forksapien
*/
public class GenerateOverviewSet {
protected GenomicRegionsBuilder gb;
protected GenomicRegionsBuilder gbfup= new GenomicRegionsBuilder(),
gbfdown= new GenomicRegionsBuilder(),
gbtup= new GenomicRegionsBuilder(),
gbtdown= new GenomicRegionsBuilder(),
gbin= new GenomicRegionsBuilder(),
gbout= new GenomicRegionsBuilder();
public int side;
public GenerateOverviewSet(GenomicRegionsBuilder gb, int side) throws RegionException{
this.gb=gb;
this.side=side;
init();
}
private void init() throws RegionException{
Iterator<GenomicRegion> it= gb.iterator();
DirectionalGenomicRegion drp= (DirectionalGenomicRegion)it.next();
addIntra(drp);
DirectionalGenomicRegion dr= null;
while(it.hasNext()){
dr= (DirectionalGenomicRegion)it.next();
addEnds(drp, dr);
addIntra(dr);
addInter(drp,dr);
drp=dr;
}
addIntra(dr);
}
protected void addIntra(DirectionalGenomicRegion gr){
try{
gbin.add(new DirectionalGenomicRegion(gr.chr, gr.strand, (int)(gr.low+side+1), (int)(gr.high-side-1)));
}catch(RegionException re){}
}
protected void addInter(DirectionalGenomicRegion dra, DirectionalGenomicRegion drb){
try{
gbout.add(new GenomicRegion(dra.chr, (int)(dra.high+side+1), (int)(drb.low-side-1)));
}catch(RegionException re){}
}
protected void addEnds(DirectionalGenomicRegion dra, DirectionalGenomicRegion drb){
try{
int a= (int) dra.high, b= (int)drb.low;
int al= (int)(a-side), ah= (int)(a+side+1);
int bl= (int)(b-side), bh= (int)(b+side+1);
if(ah>bl){
int mid= (ah+bl)/2;
ah= mid;
bl= mid+1;
}
if((dra.low+side) > al){
al= (int)( (dra.low+side + al)/2 )+1;
}
if((drb.high-side) < bh){
bh= (int)( (drb.high-side + bh)/2 );
}
DirectionalGenomicRegion drll= new DirectionalGenomicRegion(dra.chr, dra.strand, al, a),
drlh= new DirectionalGenomicRegion(dra.chr, dra.strand, a+1, ah),
drrl = new DirectionalGenomicRegion(drb.chr, drb.strand, bl, b),
drrh = new DirectionalGenomicRegion(drb.chr, drb.strand, b+1, bh);
if(dra.isOnForwardStrand()){
gbtup.add(drll);
gbtdown.add(drlh);
}else{
gbfup.add(drlh);
gbfdown.add(drll);
}
if(drb.isOnForwardStrand()){
gbfup.add(drrl);
gbfdown.add(drrh);
}else{
gbtup.add(drrh);
gbtdown.add(drrl);
}
}catch(RegionException re){
//System.err.println("err addEnds()");
}
}
public void writeToDirectory(File dir, String tag)throws Exception{
dir.mkdirs();
new BedExporter(gbfup).writeToFile(new File(dir+"/"+tag+"_five-up_"+side+".bed"));
new BedExporter(gbfdown).writeToFile(new File(dir+"/"+tag+"_five-down_"+side+".bed"));
new BedExporter(gbtup).writeToFile(new File(dir+"/"+tag+"_three-up_"+side+".bed"));
new BedExporter(gbtdown).writeToFile(new File(dir+"/"+tag+"_three-down_"+side+".bed"));
new BedExporter(gbin).writeToFile(new File(dir+"/"+tag+"_intra_"+side+".bed"));
new BedExporter(gbout).writeToFile(new File(dir+"/"+tag+"_inter_"+side+".bed"));
}
public static void main(String[] args) throws Exception {
File dir= Dirs.getFile("dir");
//File f= new File(dir+"/anno/sgdGene_sacCer1.bed"); String org= "sacCer1";
File f= new File(dir+"/anno/xu_2009_orfs.bed"); String org= "sacCer1-xu";
int[]ss= new int[]{150, 300, 450};
for( int i=0; i<ss.length ; i++ ){
int s= ss[i];
File od= new File(dir+"/anno/bed/"+org+"/set/set_"+s);
GenomicRegionsBuilder gb= new BedReader(f).getGenomicRegionsBuilder();
GenerateOverviewSet gg= new GenerateOverviewSet(gb, s);
gg.writeToDirectory(od, org);
}
}
}