66import java .io .FileWriter ;
77import java .io .IOException ;
88import java .util .TreeMap ;
9+ import java .util .TreeSet ;
910
1011import org .jax .peastools .util .Location ;
1112import org .jax .peastools .util .Util ;
@@ -15,17 +16,46 @@ public class FilterRegions {
1516 public static void main (String [] args ){
1617 FilterRegions fb = new FilterRegions ();
1718 try {
18- fb .filter (args [0 ], args [1 ], args [2 ]);
19+ TreeSet <String > chromosomes = new TreeSet <String >();
20+ if (args .length > 3 ) {
21+ chromosomes = fb .getChromosomes (args [3 ]);
22+ }
23+ else {
24+ chromosomes = fb .getDefaultChromosomes ();
25+ }
26+
27+ fb .filter (args [0 ], args [1 ], chromosomes , args [2 ]);
1928 } catch (IOException e ) {
2029 e .printStackTrace ();
2130 }
2231 }
32+
33+ private TreeSet <String > getDefaultChromosomes (){
34+ TreeSet <String > rv = new TreeSet <String >();
35+ for (int i = 1 ; i < 23 ; i ++) {
36+ rv .add ("chr" +Integer .toString (i ));
37+ }
38+ return rv ;
39+ }
40+
41+ private TreeSet <String > getChromosomes (String file ) throws IOException {
42+ BufferedReader br = new BufferedReader (new FileReader (file ));
43+
44+ TreeSet <String > rv = new TreeSet <String >();
45+
46+ while (br .ready ()) {
47+ rv .add (br .readLine ());
48+ }
49+
50+ br .close ();
51+
52+ return rv ;
53+ }
2354
24- public void filter (String peakfile , String filter , String out ) throws IOException {
55+ public void filter (String peakfile , String filter , TreeSet < String > chromosomes , String out ) throws IOException {
2556 Util u = new Util ();
2657 BEDPeak [] allpeaks = readPeaks (peakfile ).values ().toArray (new BEDPeak [0 ]);
2758 Location [] blacklistpeaks = u .readLocationsWithIds (filter );
28-
2959 TreeMap <String , Location []> allsorted = u .getChrStartSorted (allpeaks );
3060 TreeMap <String , Location []> blsorted = u .getChrStartSorted (blacklistpeaks );
3161
@@ -34,15 +64,8 @@ public void filter(String peakfile, String filter, String out) throws IOExceptio
3464 BufferedWriter bw = new BufferedWriter (new FileWriter (out ));
3565 for (int i = 0 ; i < chrs .length ; i ++){
3666 String curchr = chrs [i ];
37- int chridx = 23 ;
38- try {
39- chridx = Integer .parseInt (curchr .replace ("chr" , "" ));
40- }
41- catch (NumberFormatException e ){
42- }
43-
44- //Only chromosomes 1-22
45- if (chridx < 1 || chridx >= 23 ){
67+
68+ if (!chromosomes .contains (curchr )){
4669 continue ;
4770 }
4871
0 commit comments