1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
package tax;
import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map.Entry;
import fileIO.ByteFile;
import fileIO.ByteFile1;
import fileIO.ByteFile2;
import fileIO.ByteStreamWriter;
import fileIO.FileFormat;
import fileIO.ReadWrite;
import fileIO.TextFile;
import shared.Parse;
import shared.Parser;
import shared.PreParser;
import shared.Shared;
import shared.Timer;
import shared.Tools;
import stream.ConcurrentGenericReadInputStream;
import stream.FastaReadInputStream;
import structures.ByteBuilder;
import structures.ListNum;
import structures.StringNum;
import template.Accumulator;
import template.ThreadWaiter;
/**
* Counts patterns in Accessions.
* Handles hashing for Accession to TaxID lookups.
* @author Brian Bushnell
* @date May 9, 2018
*
*/
public class AnalyzeAccession implements Accumulator<AnalyzeAccession.ProcessThread> {
public static void main(String[] args){
//Start a timer immediately upon code entrance.
Timer t=new Timer();
//Create an instance of this class
AnalyzeAccession x=new AnalyzeAccession(args);
//Run the object
x.process(t);
//Close the print stream if it was redirected
Shared.closeStream(x.outstream);
}
public AnalyzeAccession(String[] args){
{//Preparse block for help, config files, and outstream
PreParser pp=new PreParser(args, getClass(), false);
args=pp.args;
outstream=pp.outstream;
}
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
ReadWrite.MAX_ZIP_THREADS=Shared.threads();
Parser parser=new Parser();
for(int i=0; i<args.length; i++){
String arg=args[i];
String[] split=arg.split("=");
String a=split[0].toLowerCase();
String b=split.length>1 ? split[1] : null;
if(a.equals("verbose")){
verbose=Parse.parseBoolean(b);
ByteFile1.verbose=verbose;
ByteFile2.verbose=verbose;
stream.FastaReadInputStream.verbose=verbose;
ConcurrentGenericReadInputStream.verbose=verbose;
stream.FastqReadInputStream.verbose=verbose;
ReadWrite.verbose=verbose;
}else if(a.equals("in")){
if(b==null){in.clear();}
else{
String[] split2=b.split(",");
for(String s2 : split2){
in.add(s2);
}
}
}else if(a.equals("perfile")){
perFile=Parse.parseBoolean(b);
}else if(b==null && new File(arg).exists()){
in.add(arg);
}else if(parser.parse(arg, a, b)){
//do nothing
}else{
outstream.println("Unknown parameter "+args[i]);
assert(false) : "Unknown parameter "+args[i];
// throw new RuntimeException("Unknown parameter "+args[i]);
}
}
{//Process parser fields
overwrite=parser.overwrite;
append=parser.append;
out=parser.out1;
}
assert(FastaReadInputStream.settingsOK());
if(in==null){throw new RuntimeException("Error - at least one input file is required.");}
// if(!ByteFile.FORCE_MODE_BF2){
// ByteFile.FORCE_MODE_BF2=false;
// ByteFile.FORCE_MODE_BF1=true;
// }
if(out!=null && out.equalsIgnoreCase("null")){out=null;}
if(!Tools.testOutputFiles(overwrite, append, false, out)){
outstream.println((out==null)+", "+out);
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out+"\n");
}
ffout=FileFormat.testOutput(out, FileFormat.TXT, null, true, overwrite, append, false);
ffina=new FileFormat[in.size()];
for(int i=0; i<in.size(); i++){
ffina[i]=FileFormat.testInput(in.get(i), FileFormat.TXT, null, true, false);
}
}
void process(Timer t){
if(perFile) {
process_perFile();
}else{
for(FileFormat ffin : ffina){
process_inner(ffin);
}
}
if(ffout!=null){
ByteStreamWriter bsw=new ByteStreamWriter(ffout);
bsw.println("#Pattern\tCount\tCombos\tBits");
ArrayList<StringNum> list=new ArrayList<StringNum>();
list.addAll(countMap.values());
Collections.sort(list);
Collections.reverse(list);
for(StringNum sn : list){
double combos=1;
for(int i=0; i<sn.s.length(); i++){
char c=sn.s.charAt(i);
if(c=='D'){combos*=10;}
else if(c=='L'){combos*=26;}
}
bsw.print(sn.toString().getBytes());
bsw.println("\t"+(long)combos+"\t"+String.format(Locale.ROOT, "%.2f", Tools.log2(combos)));
}
bsw.start();
errorState|=bsw.poisonAndWait();
}
t.stop();
outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8));
outstream.println();
outstream.println("Valid Lines: \t"+linesOut);
outstream.println("Invalid Lines: \t"+(linesProcessed-linesOut));
if(errorState){
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
}
}
void process_inner(FileFormat ffin){
ByteFile bf=ByteFile.makeByteFile(ffin);
final int threads=Tools.min(8, Shared.threads());
ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads);
for(int i=0; i<threads; i++){alpt.add(new ProcessThread(bf));}
boolean success=ThreadWaiter.startAndWait(alpt, this);
errorState|=!success;
}
void process_perFile(){
ArrayList<ArrayList<ProcessThread>> perFileList=new ArrayList<ArrayList<ProcessThread>>(ffina.length);
for(FileFormat ffin : ffina) {
ByteFile bf=ByteFile.makeByteFile(ffin);
final int threads=Tools.min(16, Shared.threads());
ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads);
for(int i=0; i<threads; i++){alpt.add(new ProcessThread(bf));}
perFileList.add(alpt);
ThreadWaiter.startThreads(alpt);
}
for(ArrayList<ProcessThread> alpt : perFileList){
boolean success=ThreadWaiter.waitForThreads(alpt, this);
errorState|=!success;
}
}
/*--------------------------------------------------------------*/
static class ProcessThread extends Thread {
ProcessThread(ByteFile bf_){
bf=bf_;
}
@Override
public void run() {
final StringBuilder buffer=new StringBuilder(128);
for(ListNum<byte[]> lines=bf.nextList(); lines!=null; lines=bf.nextList()){
assert(lines.size()>0);
if(lines.id==0){
//This one is not really important; the header could be missing.
assert(Tools.startsWith(lines.get(0), "accession")) : bf.name()+"[0]: "+new String(lines.get(0));
}else{
assert(!Tools.startsWith(lines.get(0), "accession")) : bf.name()+"["+lines.id+"]: "+new String(lines.get(0));
}
for(byte[] line : lines){
if(line.length>0){
linesProcessedT++;
bytesProcessedT+=(line.length+1);
boolean valid=lines.id>0 || !(Tools.startsWith(line, "accession")); //Skips test for most lines
if(valid){
linesOutT++;
increment(line, buffer);
}
}
}
}
}
void increment(byte[] line, StringBuilder buffer){
buffer.setLength(0);
for(int i=0; i<line.length; i++){
final byte b=line[i];
if(b==' ' || b=='\t' || b=='.' || b==':'){break;}
final char b2=(char)remap[b];
assert(b2!='?' || b=='+') : "unprocessed symbol in "+new String(line)+"\n"+"'"+(char)b+"'";
buffer.append(b2);
}
String key=buffer.toString();
StringNum value=countMapT.get(key);
if(value!=null){value.increment();}
else{countMapT.put(key, new StringNum(key, 1));}
}
private HashMap<String, StringNum> countMapT=new HashMap<String, StringNum>();
private final ByteFile bf;
long linesProcessedT=0;
long linesOutT=0;
long bytesProcessedT=0;
}
/*--------------------------------------------------------------*/
@Override
public void accumulate(ProcessThread t) {
linesProcessed+=t.linesProcessedT;
linesOut+=t.linesOutT;
bytesProcessed+=t.bytesProcessedT;
for(Entry<String, StringNum> e : t.countMapT.entrySet()){
StringNum value=e.getValue();
final String key=e.getKey();
StringNum old=countMap.get(key);
if(old==null){countMap.put(key, value);}
else{old.add(value);}
}
}
@Override
public boolean success() {
return !errorState;
}
/*--------------------------------------------------------------*/
public static long combos(String s){
double combos=1;
for(int i=0; i<s.length(); i++){
char c=s.charAt(i);
if(c=='D'){combos*=10;}
else if(c=='L'){combos*=26;}
}
return (combos>=Long.MAX_VALUE ? Long.MAX_VALUE : (long)Math.ceil(combos));
}
public static long combos(byte[] s){
double combos=1;
for(int i=0; i<s.length; i++){
byte c=s[i];
if(c=='D'){combos*=10;}
else if(c=='L'){combos*=26;}
}
return (combos>=Long.MAX_VALUE ? -1 : (long)Math.ceil(combos));
}
/*--------------------------------------------------------------*/
public static HashMap<String, Integer> loadCodeMap(String fname){
assert(codeMap==null);
TextFile tf=new TextFile(fname);
ArrayList<String> list=new ArrayList<String>();
for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){
if(!line.startsWith("#")){
String[] split=line.split("\t");
list.add(split[0]);
}
}
HashMap<String, Integer> map=new HashMap<String, Integer>(list.size()*3);
codeBits=(int)Math.ceil(Tools.log2(list.size()));
final int patternBits=63-codeBits;
final long maxCombos=((1L<<(patternBits-1))-1);
for(int i=0; i<list.size(); i++){
String s=list.get(i);
longestPattern=Tools.max(longestPattern, s.length());
long combos=combos(s);
if(combos<0 || combos>=maxCombos){map.put(s, -1);}
else{map.put(s, i);}
}
codeMap=map;
return map;
}
public static long digitize(String s){
String pattern=remap(s);
Integer code=codeMap.get(pattern);
if(code==null){return -2;}
if(code.intValue()<0){return -1;}
long number=0;
for(int i=0; i<pattern.length(); i++){
char c=s.charAt(i);
char p=pattern.charAt(i);
if(p=='-' || p=='?'){
//do nothing
}else if(p=='D'){
number=(number*10)+(c-'0');
}else if(p=='L'){
number=(number*26)+(Tools.toUpperCase(c)-'A');
}else{
assert(false) : s;
}
}
number=(number<<codeBits)+code;
return number;
}
public static long digitize(byte[] s){
String pattern=remap(s);
Integer code=codeMap.get(pattern);
if(code==null){return -2;}
if(code.intValue()<0){return -1;}
long number=0;
for(int i=0; i<pattern.length(); i++){
byte c=s[i];
char p=pattern.charAt(i);
if(p=='-' || p=='?'){
//do nothing
}else if(p=='D'){
number=(number*10)+(c-'0');
}else if(p=='L'){
number=(number*26)+(Tools.toUpperCase(c)-'A');
}else{
assert(false) : new String(s);
}
}
number=(number<<codeBits)+code;
return number;
}
public static String remap(String s){
if(s==null || s.length()<1){return "";}
ByteBuilder buffer=new ByteBuilder(s.length());
for(int i=0; i<s.length(); i++){
final char b=s.charAt(i);
if(b==' ' || b=='\t' || b=='.' || b==':'){break;}
buffer.append((char)remap[b]);
}
return buffer.toString();
}
public static String remap(byte[] s){
ByteBuilder buffer=new ByteBuilder(s.length);
for(int i=0; i<s.length; i++){
final byte b=s[i];
if(b==' ' || b=='\t' || b=='.' || b==':'){break;}
buffer.append((char)remap[b]);
}
return buffer.toString();
}
/*--------------------------------------------------------------*/
private ArrayList<String> in=new ArrayList<String>();
private String out=null;
private boolean perFile=true;
/*--------------------------------------------------------------*/
private HashMap<String, StringNum> countMap=new HashMap<String, StringNum>();
public static HashMap<String, Integer> codeMap;
private static int codeBits=-1;
private static int longestPattern=-1;
private long linesProcessed=0;
private long linesOut=0;
private long bytesProcessed=0;
private long bytesOut=0;
/*--------------------------------------------------------------*/
private final FileFormat[] ffina;
private final FileFormat ffout;
private static final byte[] remap=makeRemap();
private static byte[] makeRemap(){
byte[] array=new byte[128];
Arrays.fill(array, (byte)'?');
for(int i='A'; i<='Z'; i++){array[i]='L';}
for(int i='a'; i<='z'; i++){array[i]='L';}
for(int i='0'; i<='9'; i++){array[i]='D';}
array['_']=array['-']='-';
return array;
}
/*--------------------------------------------------------------*/
private PrintStream outstream=System.err;
public static boolean verbose=false;
public boolean errorState=false;
private boolean overwrite=false;
private boolean append=false;
}
|