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
|
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 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.StringNum;
/**
* Counts patterns in Accessions.
* Handles hashing for Accession to TaxID lookups.
* @author Brian Bushnell
* @date May 9, 2018
*
*/
public class AnalyzeAccession_ST {
public static void main(String[] args){
//Start a timer immediately upon code entrance.
Timer t=new Timer();
//Create an instance of this class
AnalyzeAccession_ST x=new AnalyzeAccession_ST(args);
//Run the object
x.process(t);
//Close the print stream if it was redirected
Shared.closeStream(x.outstream);
}
public AnalyzeAccession_ST(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("lines")){
maxLines=Long.parseLong(b);
if(maxLines<0){maxLines=Long.MAX_VALUE;}
}else 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(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){
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);
byte[] line=bf.nextLine();
StringBuilder buffer=new StringBuilder(32);
for(int lineNum=0; line!=null; lineNum++){
if(line.length>0){
if(maxLines>0 && linesProcessed>=maxLines){break;}
linesProcessed++;
bytesProcessed+=(line.length+1);
assert((lineNum==0)==(Tools.startsWith(line, "accession"))) : "Line "+lineNum+": "+new String(line);
// final boolean valid=(line[0]!='#');
if(true){
linesOut++;
bytesOut+=(line.length+1);
increment(line, buffer);
}
}
line=bf.nextLine();
}
errorState|=bf.close();
}
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=='.'){break;}
buffer.append((char)remap[b]);
}
String key=buffer.toString();
StringNum value=countMap.get(key);
if(value!=null){value.increment();}
else{countMap.put(key, new StringNum(key, 1));}
}
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=='-'){
//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=='-'){
//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 String remap(String s){
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=='.'){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=='.'){break;}
buffer.append((char)remap[b]);
}
return buffer.toString();
}
/*--------------------------------------------------------------*/
private ArrayList<String> in=new ArrayList<String>();
private String out=null;
/*--------------------------------------------------------------*/
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 long maxLines=Long.MAX_VALUE;
/*--------------------------------------------------------------*/
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;
}
|