/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package data.cerevisiae.at;
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.region.GenomicRegion;
import fork.lib.bio.anno.genomic.region.GenomicRegionsBuilder;
import fork.lib.bio.seq.FastaSequenceExtractor;
import fork.lib.bio.seq.NucleotideSequence;
import fork.lib.bio.seq.NucleotideSequenceParser;
import fork.lib.bio.seq.SequenceExtractorEntry;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import data.cerevisiae.region.wt4krlen.GenerateLen;
/**
*
* @author forksapien
*/
public class ATContentBp {
protected GenomicRegionsBuilder gb, gbout;
protected int len;
protected File gen;
public ATContentBp(GenomicRegionsBuilder gb, int len, File gen) throws Exception{
this.gb= gb;
this.len=len;
this.gen=gen;
init();
}
protected void init() throws Exception{
gbout= new GenomicRegionsBuilder();
Iterator<GenomicRegion> it= gb.iterator();
while(it.hasNext()){
GenomicRegion gr= it.next();
int low = (int)gr.low;
int high= (int)gr.high;
int ind= low;
while( (ind+len)<=high ){
gbout.add(new GenomicRegion(gr.chr, ind, ind+len-1));
ind+= len;
}
}
}
public void writeToFile(File out, String tit) throws Exception{
out.getParentFile().mkdirs();
BufferedWriter bw= new BufferedWriter(new FileWriter(out));
BedGraphExporter.writeTitle(bw, tit);
FastaSequenceExtractor se= new FastaSequenceExtractor(gen, gbout);
SequenceExtractorEntry en;
while( (en= se.nextEntry())!=null ){
GenomicRegion gr= en.getGenomicRegion();
NucleotideSequence seq= NucleotideSequenceParser.parseSequence(en.getSequence());
double at= seq.atContent();
bw.write( gr.toBedFormat()+"\t"+ ((double)Math.round(at*100)/100)+"\n" );
}
bw.close();
}
public static void main(String[] args) throws Exception {
File dir= Dirs.getFile("dir");
int len= 150;
ATContentBp gg= new ATContentBp(
new BedReader(new File(dir+"/anno/sacCer1_chr.bed")).getGenomicRegionsBuilder(),
len,
new File(dir+"/genomes/sacCer1/sacCer1.fa") );
gg.writeToFile(new File(dir+"/anno/AT-content_"+len+"_sacCer1.wig"), "AT_content_"+len);
}
}