Debian Codegraph @

current/align2/Block.java

raw symbols SCIP
1 package align2;
2
3 import java.io.File;
4 import java.io.Serializable;
5
6 import fileIO.LoadThread;
7 import fileIO.ReadWrite;
8 import shared.KillSwitch;
9
10 /**
11 * @author Brian Bushnell
12 * @date Dec 23, 2012
13 *
14 */
15 public class Block implements Serializable{
16
17 /**
18 *
19 */
20 private static final long serialVersionUID = -1638122096023589384L;
21
22 public Block(int numSites_, int numStarts_){
23 numSites=numSites_;
24 numStarts=numStarts_;
25 sites=new int[numSites];
26 starts=new int[numStarts+1];
27 assert(Integer.bitCount(numStarts)==1 && Integer.bitCount(starts.length)==2) : numStarts;
28 }
29
30 public Block(int[] sites_, int[] starts_){
31 sites=sites_;
32 starts=starts_;
33 numSites=sites.length;
34 numStarts=starts.length-1;
35 assert(Integer.bitCount(numStarts)==1 && Integer.bitCount(starts.length)==2) : numStarts;
36 }
37
38 /** For legacy support */
39 public int[] getHitList(int key){
40 int len=length(key);
41 if(len==0){return null;}
42 int start=starts[key];
43 int[] r=KillSwitch.copyOfRange(sites, start, start+len);
44 return r;
45 }
46
47 /** For legacy support */
48 public int[] getHitList(int start, int stop){
49 int len=length(start, stop);
50 if(len==0){return null;}
51 assert(len>0) : len+", "+start+", "+stop;
52 int[] r=KillSwitch.copyOfRange(sites, start, start+len);
53 return r;
54 }
55
56 /** For legacy support */
57 public int[][] getHitLists(int[] start, int[] stop){
58 int[][] r=new int[start.length][];
59 for(int i=0; i<start.length; i++){r[i]=getHitList(start[i], stop[i]);}
60 return r;
61 }
62
63 public int length(int key){
64 int x=starts[key+1]-starts[key];
65 if(x==0){return 0;}
66 return sites[starts[key]]!=-1 ? x : 0; //Lists can be removed by making the first site -1.
67 }
68
69 public int length(int start, int stop){
70 if(start==stop || sites[start]==-1){return 0;}
71 return stop-start;
72 }
73
74 public boolean write(String fname, boolean overwrite){
75 String fname2=fname+"2.gz";
76 {
77 File f=new File(fname);
78 if(f.exists()){
79 if(!overwrite){
80 assert(false) : "Tried to overwrite file "+f.getAbsolutePath();
81 return false;
82 }
83 }
84 f=new File(fname2);
85 if(f.exists()){
86 if(!overwrite){
87 assert(false) : "Tried to overwrite file "+f.getAbsolutePath();
88 return false;
89 }
90 }
91 }
92 ReadWrite.writeObjectInThread(sites, fname, allowSubprocess);
93 if(!compress){
94 ReadWrite.writeObjectInThread(starts, fname+"2.gz", allowSubprocess);
95 }else{
96 if(copyOnWrite){
97 final int[] x;
98 x=new int[starts.length];
99 for(int i=1; i<x.length; i++){
100 x[i]=starts[i]-starts[i-1];
101 }
102 ReadWrite.writeObjectInThread(starts, fname+"2.gz", allowSubprocess);
103 }else{
104 compress(starts);
105 ReadWrite.writeAsync(starts, fname+"2.gz", allowSubprocess);
106 decompress(starts);
107 }
108 }
109 return true;
110 }
111
112 private static void compress(int[] x){
113 for(int i=x.length-1; i>0; i--){
114 x[i]=x[i]-x[i-1];
115 }
116 }
117
118 private static void decompress(int[] x){
119 int sum=x[0];
120 for(int i=1; i<x.length; i++){
121 sum+=x[i];
122 x[i]=sum;
123 }
124 }
125
126 public static Block read(String fname){
127 String fname2=fname+"2.gz";
128
129 final int[] a, b;
130 {
131 LoadThread<int[]> lta=LoadThread.load(fname, int[].class);
132 b=ReadWrite.read(int[].class, fname2, false);
133 lta.waitForThisToFinish();
134 a=lta.output;
135 }
136 // {
137 // LoadThread<int[]> lta=LoadThread.load(fname, int[].class);
138 // LoadThread<int[]> ltb=LoadThread.load(fname2, int[].class);
139 // lta.waitForThisToFinish();
140 // ltb.waitForThisToFinish();
141 // a=lta.output;
142 // b=ltb.output;
143 // }
144
145 // int[] a=ReadWrite.read(int[].class, fname);
146 // int[] b=ReadWrite.read(int[].class, fname2);
147
148 assert(a!=null && b!=null) : a+", "+b;
149 if(compress){
150 int sum=b[0];
151 for(int i=1; i<b.length; i++){
152 sum+=b[i];
153 b[i]=sum;
154 }
155 }
156 Block r=new Block(a, b);
157 assert(r.sites==a);
158 assert(r.starts==b);
159 return r;
160 }
161
162 public final int numSites;
163 public final int numStarts;
164 public final int[] sites;
165 public final int[] starts;
166
167 private static boolean allowSubprocess=false;
168 private static final boolean compress=true;
169 private static final boolean copyOnWrite=false;
170
171 }