| 1 |
package align2; |
| 2 |
|
| 3 |
import java.util.HashSet; |
| 4 |
|
| 5 |
import fileIO.TextFile; |
| 6 |
import stream.Read; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
public class Blacklist { |
| 14 |
|
| 15 |
public static boolean inWhitelist(Read r){ |
| 16 |
return r==null ? false : (inWhitelist2(r) || inWhitelist2(r.mate)); |
| 17 |
} |
| 18 |
|
| 19 |
private static boolean inWhitelist2(Read r){ |
| 20 |
if(r==null || !r.mapped() || whitelist==null || whitelist.isEmpty()){return false;} |
| 21 |
byte[] name=r.getScaffoldName(false); |
| 22 |
return (name!=null && whitelist.contains(new String(name))); |
| 23 |
} |
| 24 |
|
| 25 |
public static boolean inBlacklist(Read r){ |
| 26 |
if(r==null){return false;} |
| 27 |
boolean a=inBlacklist2(r); |
| 28 |
boolean b=inBlacklist2(r.mate); |
| 29 |
if(!a && !b){return false;} |
| 30 |
if(a){ |
| 31 |
return b || r.mate==null || !r.mate.mapped(); |
| 32 |
} |
| 33 |
return b && !r.mapped(); |
| 34 |
} |
| 35 |
|
| 36 |
private static boolean inBlacklist2(Read r){ |
| 37 |
if(r==null || !r.mapped() || blacklist==null || blacklist.isEmpty()){return false;} |
| 38 |
byte[] name=r.getScaffoldName(false); |
| 39 |
return (name!=null && blacklist.contains(new String(name))); |
| 40 |
} |
| 41 |
|
| 42 |
public static void addToBlacklist(String fname){ |
| 43 |
addToSet(fname, true); |
| 44 |
} |
| 45 |
|
| 46 |
public static void addToWhitelist(String fname){ |
| 47 |
addToSet(fname, false); |
| 48 |
} |
| 49 |
|
| 50 |
public static synchronized int addToSet(String fname, boolean black){ |
| 51 |
final HashSet<String> set; |
| 52 |
int added=0, overwritten=0; |
| 53 |
if(black){ |
| 54 |
if(blacklist==null){blacklist=new HashSet<String>(4001);} |
| 55 |
set=blacklist; |
| 56 |
}else{ |
| 57 |
if(whitelist==null){whitelist=new HashSet<String>(4001);} |
| 58 |
set=whitelist; |
| 59 |
} |
| 60 |
TextFile tf=new TextFile(fname, false); |
| 61 |
String line=tf.nextLine(); |
| 62 |
if(line==null){return 0;} |
| 63 |
final boolean fasta=(line.charAt(0)=='>'); |
| 64 |
System.err.println("Detected "+(black ? "black" : "white")+"list file "+fname+" as "+(fasta ? "" : "non-")+"fasta-formatted."); |
| 65 |
while(line!=null){ |
| 66 |
String key=null; |
| 67 |
if(fasta){ |
| 68 |
if(line.charAt(0)=='>'){key=new String(line.substring(1));} |
| 69 |
}else{ |
| 70 |
key=line; |
| 71 |
} |
| 72 |
if(key!=null){ |
| 73 |
boolean b=set.add(key); |
| 74 |
added++; |
| 75 |
if(!b){ |
| 76 |
if(overwritten==0){ |
| 77 |
System.err.println("Duplicate "+(black ? "black" : "white")+"list key "+key); |
| 78 |
System.err.println("Subsequent duplicates from this file will not be mentioned."); |
| 79 |
} |
| 80 |
overwritten++; |
| 81 |
} |
| 82 |
} |
| 83 |
line=tf.nextLine(); |
| 84 |
} |
| 85 |
if(overwritten>0){ |
| 86 |
System.err.println("Added "+overwritten+" duplicate keys."); |
| 87 |
} |
| 88 |
return added-overwritten; |
| 89 |
} |
| 90 |
|
| 91 |
public static boolean hasBlacklist(){return blacklist!=null && !blacklist.isEmpty();} |
| 92 |
public static boolean hasWhitelist(){return whitelist!=null && !whitelist.isEmpty();} |
| 93 |
|
| 94 |
public static void clearBlacklist(){blacklist=null;} |
| 95 |
public static void clearWhitelist(){whitelist=null;} |
| 96 |
|
| 97 |
private static HashSet<String> blacklist=null; |
| 98 |
private static HashSet<String> whitelist=null; |
| 99 |
|
| 100 |
} |