/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package prop.clas;
import fork.lib.base.file.FileName;
import fork.lib.bio.anno.genomic.BedAttribute;
import fork.lib.bio.anno.genomic.BedReader;
import fork.lib.bio.anno.genomic.region.GenomicRegion;
import fork.lib.bio.anno.genomic.region.GenomicRegionsBuilder;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
/**
*
* @author forksapien
*/
public class ClassTelomere {
protected File chr, file;
public HashSet<GenomicRegion> hm, hmnot;
public int thr;
public ClassTelomere(File chr, File file, int thr) throws Exception{
this.chr=chr;
this.file=file;
this.thr=thr;
init();
}
protected void init() throws Exception{
hm= new HashSet();
hmnot= new HashSet();
GenomicRegionsBuilder gb= new BedReader(file).getGenomicRegionsBuilder();
GenomicRegionsBuilder gbc= new BedReader(chr).getGenomicRegionsBuilder();
HashMap<String, GenomicRegion> cr= new HashMap<>();
Iterator<GenomicRegion> it= gbc.iterator();
while(it.hasNext()){
GenomicRegion gr= it.next();
cr.put(gr.chr, gr);
}
it= gb.iterator();
while(it.hasNext()){
GenomicRegion gr= it.next();
GenomicRegion chrr= cr.get(gr.chr);
if(chrr!=null){
double dl= Math.abs(gr.low - chrr.low);
double dh= Math.abs(gr.high - chrr.high);
double d= Math.min(dl, dh);
if(d<thr){
hm.add(gr);
}else{
hmnot.add(gr);
}
}
}
}
public void writeToFile(File out) throws Exception{
out.getParentFile().mkdirs();
BufferedWriter bw= new BufferedWriter(new FileWriter(out));
Iterator<GenomicRegion> it= hm.iterator();
bw.write(">subtelo\n");
while(it.hasNext()){
bw.write(it.next().getID()+"\n");
}
it= hmnot.iterator();
bw.write(">not_sub\n");
while(it.hasNext()){
bw.write( it.next().getID()+"\n");
}
bw.close();
}
public static void main(String[] args) throws Exception {
File dir= new File("/home/forksapien/mystudy/phd/files");
//File dir= new File("G:/mystudy/phd/files");
//File f= new File(dir+"/anno/sgdGene_sacCer1.bed");
File f= new File(dir+"/anno/xu_2009_orfs.bed");
File chr= new File(dir+"/anno/bed/sacCer1/prop/sacCer1_chr.bed");
int[] ts= new int[]{
10000, 20000, 30000, 50000, 100000
};
for( int i=0; i<ts.length ; i++ ){
int thr= ts[i];
ClassTelomere cl= new ClassTelomere(chr, f, thr);
cl.writeToFile(new File(dir+"/anno/clas/clas-telo_"+FileName.getBaseName(f) +"_"+thr/1000+"k.txt"));
}
}
}