| 1 |
package stream; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Collection; |
| 5 |
import java.util.HashSet; |
| 6 |
import java.util.LinkedHashMap; |
| 7 |
|
| 8 |
import tax.TaxNode; |
| 9 |
import tax.TaxTree; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class ArrayListSet { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public ArrayListSet(boolean ordered_){ |
| 26 |
this(ordered_, null, TaxTree.stringToLevelExtended("phylum")); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public ArrayListSet(boolean ordered_, TaxTree tree_, int taxLevelE_){ |
| 38 |
ordered=ordered_; |
| 39 |
tree=tree_; |
| 40 |
taxLevelE=taxLevelE_; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public void add(Read r, Iterable<String> names){ |
| 48 |
for(String s : names){add(r, s);} |
| 49 |
} |
| 50 |
|
| 51 |
public void add(Read r, String name){ |
| 52 |
final Pack p=getPack(name, true); |
| 53 |
p.add(r); |
| 54 |
} |
| 55 |
|
| 56 |
public void add(Read r, int id){ |
| 57 |
final Pack p=getPack(id, true); |
| 58 |
p.add(r); |
| 59 |
} |
| 60 |
|
| 61 |
public ArrayList<Read> getAndClear(String name){ |
| 62 |
final Pack p=getPack(name, false); |
| 63 |
return p==null ? null : p.getAndClear(); |
| 64 |
} |
| 65 |
|
| 66 |
public ArrayList<Read> getAndClear(int id){ |
| 67 |
final Pack p=getPack(id, false); |
| 68 |
return p==null ? null : p.getAndClear(); |
| 69 |
} |
| 70 |
|
| 71 |
public Collection<String> getNames(){ |
| 72 |
return nameList; |
| 73 |
} |
| 74 |
|
| 75 |
public int size(){return nameList.size();} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public void addByTaxid(Read r, String name){ |
| 87 |
addByTaxid(r, nameToTaxid(name)); |
| 88 |
} |
| 89 |
|
| 90 |
public void addByTaxid(Read r, int taxid){ |
| 91 |
String key=Integer.toString(taxid); |
| 92 |
final Pack p=getPack(key, true); |
| 93 |
p.add(r); |
| 94 |
} |
| 95 |
|
| 96 |
public void addByTaxid(Read r, ArrayList<String> names){ |
| 97 |
if(names.size()==0){return;} |
| 98 |
else if(names.size()==1){addByTaxid(r, names.get(0));} |
| 99 |
else{addByTaxid(r, (Iterable<String>)names);} |
| 100 |
} |
| 101 |
|
| 102 |
public void addByTaxid(Read r, Iterable<String> names){ |
| 103 |
HashSet<Integer> idset=tls.get(); |
| 104 |
if(idset==null){ |
| 105 |
idset=new HashSet<Integer>(); |
| 106 |
tls.set(idset); |
| 107 |
} |
| 108 |
assert(idset.isEmpty()); |
| 109 |
for(String s : names){ |
| 110 |
idset.add(nameToTaxid(s)); |
| 111 |
} |
| 112 |
for(Integer i : idset){ |
| 113 |
addByTaxid(r, i); |
| 114 |
} |
| 115 |
idset.clear(); |
| 116 |
} |
| 117 |
|
| 118 |
private int nameToTaxid(String name){ |
| 119 |
TaxNode tn=tree.getNode(name, taxLevelE); |
| 120 |
return (tn==null ? -1 :tn.id); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
private Pack getPack(String name, boolean add){ |
| 128 |
Pack p=stringMap.get(name); |
| 129 |
if(p==null && add){p=new Pack(name);} |
| 130 |
return p; |
| 131 |
} |
| 132 |
|
| 133 |
private Pack getPack(int id, boolean add){ |
| 134 |
Pack p=packList.size()>id ? packList.get(id) : null; |
| 135 |
if(p==null && add){p=new Pack(id);} |
| 136 |
return p; |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public String toString(){ |
| 141 |
return nameList.toString(); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
private class Pack { |
| 149 |
|
| 150 |
Pack(String s){ |
| 151 |
assert(s==null || !stringMap.containsKey(s)); |
| 152 |
name=s; |
| 153 |
id=packList.size(); |
| 154 |
nameList.add(s); |
| 155 |
packList.add(this); |
| 156 |
if(s!=null){stringMap.put(s, this);} |
| 157 |
} |
| 158 |
|
| 159 |
Pack(int x){ |
| 160 |
name=null; |
| 161 |
id=x; |
| 162 |
while(packList.size()<=x){packList.add(null);} |
| 163 |
assert(packList.get(x)==null); |
| 164 |
packList.set(x, this); |
| 165 |
} |
| 166 |
|
| 167 |
public void add(Read r){ |
| 168 |
if(list==null){list=new ArrayList<Read>();} |
| 169 |
list.add(r); |
| 170 |
} |
| 171 |
|
| 172 |
public ArrayList<Read> getAndClear(){ |
| 173 |
ArrayList<Read> temp=list; |
| 174 |
list=null; |
| 175 |
return temp; |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public String toString(){ |
| 180 |
return "Pack "+name; |
| 181 |
} |
| 182 |
|
| 183 |
final String name; |
| 184 |
@SuppressWarnings("unused") |
| 185 |
final int id; |
| 186 |
private ArrayList<Read> list; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
private final boolean ordered; |
| 194 |
private final ArrayList<String> nameList=new ArrayList<String>(); |
| 195 |
private final ArrayList<Pack> packList=new ArrayList<Pack>(); |
| 196 |
private final LinkedHashMap<String, Pack> stringMap=new LinkedHashMap<String, Pack>(); |
| 197 |
private final int taxLevelE; |
| 198 |
private final TaxTree tree; |
| 199 |
private final ThreadLocal<HashSet<Integer>> tls=new ThreadLocal<HashSet<Integer>>(); |
| 200 |
|
| 201 |
|
| 202 |
} |