/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package data.cerevisiae.test.lineup;
import fork.lib.base.file.FileName;
import fork.lib.base.file.management.Dirs;
import fork.lib.bio.anno.genomic.BedGraphReader;
import fork.lib.bio.anno.genomic.BedReader;
import fork.lib.bio.anno.genomic.LandscapeBuffer;
import fork.lib.bio.anno.genomic.LandscapeBuilder;
import fork.lib.bio.anno.genomic.LandscapeScoring;
import fork.lib.bio.anno.genomic.LandscapeScoringResultEntry;
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.bio.anno.genomic.region.GenomicRegionsModifier;
import fork.lib.math.algebra.advanced.linearalgebra.Vector;
import fork.lib.math.algebra.elementary.set.continuous.Region;
import fork.lib.math.algebra.elementary.set.continuous.RegionException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
/**
*
* @author muxin gu
*/
public class LineupSide {
protected GenomicRegionsBuilder gb;
protected LandscapeBuffer lb;
protected Vector vec;
public LineupSide(){}
public LineupSide(GenomicRegionsBuilder gb, LandscapeBuffer lb) throws Exception{
this.gb=gb;
this.lb=lb;
init();
}
protected void init() throws Exception{
LandscapeScoring ls = getScoring();
ArrayList<LandscapeScoringResultEntry> res= ls.getResultEntries();
vec= new Vector(new double[res.get(0).scores.length]);
for( int i=0; i<res.size(); i++ ){
LandscapeScoringResultEntry en= res.get(i);
/*
bw.write( ((BedAttribute)en.gr.attribute()).getFieldA() );
double[] vs= en.scores;
for( int j=0; j<vs.length; j++ ){
bw.write("\t"+ (double)Math.round(vs[j]*100)/100);
}
bw.write("\n");
*/
DirectionalGenomicRegion dr= (DirectionalGenomicRegion)en.gr;
boolean ifflip= dr.strand=='-';
Vector vadd= new Vector(en.scores);
if(ifflip){
vadd= vadd.invert();
}
if(vadd.size()==vec.size()){
vec= vec.add(vadd);
}
}
vec= vec.multiplyScalar((double)1/res.size());
}
protected LandscapeScoring getScoring() throws Exception{
return new LandscapeScoring(lb, gb){
protected double[] score(ArrayList<Region> bufs, GenomicRegion gr) throws RegionException{
double[] ret= new double[(int)gr.getRange()+1];
int ind=0;
for( int i=(int)gr.low; i<=(int)gr.high; i++ ){
int ir= i-(int)gr.low;
if(ind>=bufs.size()){
ret[ir]=0;
continue;
}
while(bufs.get(ind).lowerThan(i)){
ind++;
if(ind>=bufs.size()){
break;
}
}
if(ind>=bufs.size()){
ret[ir]=0;
continue;
}
if(bufs.get(ind).contains(i)){
ret[ir]= (double)bufs.get(ind).attribute();
}else{
ret[ir]=0;
}
}
return ret;
}
};
}
public Vector getVector(){
return vec;
}
public void writeToFile(File out) throws Exception{
out.getAbsoluteFile().getParentFile().mkdirs();
BufferedWriter bw= new BufferedWriter(new FileWriter(out));
double[] vs= vec.getDoubles();
for( int i=0; i<vs.length; i++ ){
bw.write(i+"\t"+ vs[i] + "\n");
}
bw.close();
}
public static void main(String[] args) throws Exception { //debug
File dir= Dirs.getFile("dir");
File[] fs= new File[]{
//new File(dir+"/other_datasets/ranjan2013_swr1-chip/swr1-chip_wt.wig"),
//new File(dir+"/other_datasets/ranjan2013_swr1-chip/swr1-chip_htz1d.wig"),
};
File an= new File(dir+"/anno/xu_2009_orfs.bed");
GenomicRegionsBuilder gb= new BedReader(an).getGenomicRegionsBuilder();
gb= new GenomicRegionsModifier(gb).getFivePrimeEnds(300, 300);
for(int i=0; i<fs.length ; i++){
File f= fs[i];
LandscapeBuilder lb= new BedGraphReader(f).getLandscapeBuilder();
File out= new File("out/line5_"+FileName.getBaseName(f)+".txt");
LineupSide ll= new LineupSide(gb, lb);
ll.writeToFile(out);
}
}
}