| 1 |
package driver; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.Collection; |
| 5 |
import java.util.LinkedHashSet; |
| 6 |
|
| 7 |
import fileIO.FileFormat; |
| 8 |
import fileIO.ReadWrite; |
| 9 |
import fileIO.TextFile; |
| 10 |
import fileIO.TextStreamWriter; |
| 11 |
import shared.Parse; |
| 12 |
import shared.Parser; |
| 13 |
import shared.PreParser; |
| 14 |
import shared.Shared; |
| 15 |
import shared.Timer; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class CountSharedLines { |
| 24 |
|
| 25 |
public static void main(String[] args){ |
| 26 |
Timer t=new Timer(); |
| 27 |
CountSharedLines x=new CountSharedLines(args); |
| 28 |
x.process(t); |
| 29 |
|
| 30 |
|
| 31 |
Shared.closeStream(x.outstream); |
| 32 |
} |
| 33 |
|
| 34 |
public CountSharedLines(String[] args){ |
| 35 |
|
| 36 |
{ |
| 37 |
PreParser pp=new PreParser(args, getClass(), false); |
| 38 |
args=pp.args; |
| 39 |
outstream=pp.outstream; |
| 40 |
} |
| 41 |
|
| 42 |
Shared.capBuffers(4); |
| 43 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 44 |
ReadWrite.setZipThreads(Shared.threads()); |
| 45 |
|
| 46 |
Parser parser=new Parser(); |
| 47 |
for(int i=0; i<args.length; i++){ |
| 48 |
String arg=args[i]; |
| 49 |
String[] split=arg.split("="); |
| 50 |
String a=split[0].toLowerCase(); |
| 51 |
String b=split.length>1 ? split[1] : null; |
| 52 |
|
| 53 |
if(parser.parseCommon(arg, a, b)){ |
| 54 |
|
| 55 |
}else if(a.equals("in") || a.equals("in1")){ |
| 56 |
if(b!=null){ |
| 57 |
String[] x=b.split(","); |
| 58 |
for(String s : x){ |
| 59 |
in1.add(s); |
| 60 |
} |
| 61 |
} |
| 62 |
}else if(a.equals("names") || a.equals("in2")){ |
| 63 |
if(b!=null){ |
| 64 |
String[] x=b.split(","); |
| 65 |
for(String s : x){ |
| 66 |
in2.add(s); |
| 67 |
} |
| 68 |
} |
| 69 |
}else if(a.equals("verbose")){ |
| 70 |
verbose=Parse.parseBoolean(b); |
| 71 |
ReadWrite.verbose=verbose; |
| 72 |
}else if(a.equals("lines") || a.equals("maxlines")){ |
| 73 |
maxLines=Parse.parseKMG(b); |
| 74 |
}else if(a.equals("substrings") || a.equals("substring")){ |
| 75 |
if(b==null){b="t";} |
| 76 |
if(b.equals("header")){ |
| 77 |
lineSubstringOfName=true; |
| 78 |
}else if(b.equals("name")){ |
| 79 |
nameSubstringOfLine=true; |
| 80 |
}else{ |
| 81 |
nameSubstringOfLine=lineSubstringOfName=Parse.parseBoolean(b); |
| 82 |
} |
| 83 |
}else if(a.equals("prefix") || a.equals("prefixmode")){ |
| 84 |
prefixMode=Parse.parseBoolean(b); |
| 85 |
}else if(a.equals("replace")){ |
| 86 |
assert(b!=null) : "Bad parameter: "+arg; |
| 87 |
String[] split2=b.split(","); |
| 88 |
assert(split2.length==2); |
| 89 |
replace1=split2[0]; |
| 90 |
replace2=split2[1]; |
| 91 |
}else if(a.equals("casesensitive") || a.equals("case")){ |
| 92 |
ignoreCase=!Parse.parseBoolean(b); |
| 93 |
}else if(a.equals("include") || a.equals("retain")){ |
| 94 |
exclude=!Parse.parseBoolean(b); |
| 95 |
}else if(a.equals("exclude") || a.equals("remove")){ |
| 96 |
exclude=Parse.parseBoolean(b); |
| 97 |
}else{ |
| 98 |
outstream.println("Unknown parameter "+args[i]); |
| 99 |
assert(false) : "Unknown parameter "+args[i]; |
| 100 |
|
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
{ |
| 118 |
overwrite=parser.overwrite; |
| 119 |
append=parser.append; |
| 120 |
} |
| 121 |
|
| 122 |
if(in1==null || in2==null){throw new RuntimeException("Error - at least one input file is required from each set.");} |
| 123 |
} |
| 124 |
|
| 125 |
final static String getOutputName(String fname){ |
| 126 |
fname=fname.replaceAll("\\\\", "/"); |
| 127 |
if(!fname.contains("/")){fname="./"+fname;} |
| 128 |
int idx=fname.lastIndexOf('/'); |
| 129 |
final String out=fname.substring(0, idx+1)+"out_"+fname.substring(idx+1); |
| 130 |
return out; |
| 131 |
} |
| 132 |
|
| 133 |
void process(Timer t){ |
| 134 |
|
| 135 |
for(String fname : in1){ |
| 136 |
processInner(fname, getOutputName(fname), in2); |
| 137 |
} |
| 138 |
for(String fname : in2){ |
| 139 |
processInner(fname, getOutputName(fname), in1); |
| 140 |
} |
| 141 |
|
| 142 |
t.stop(); |
| 143 |
|
| 144 |
outstream.println("\nTime: "+t); |
| 145 |
|
| 146 |
if(errorState){ |
| 147 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
LinkedHashSet<String> getContents(String fname){ |
| 153 |
final FileFormat ff=FileFormat.testInput(fname, FileFormat.TEXT, null, true, true); |
| 154 |
final LinkedHashSet<String> set=new LinkedHashSet<String>(); |
| 155 |
final TextFile tf=new TextFile(ff); |
| 156 |
|
| 157 |
for(String line0=tf.readLine(true); line0!=null; line0=tf.readLine(true)){ |
| 158 |
String line=(ignoreCase ? line0.toLowerCase() : line0); |
| 159 |
if(replace1!=null){line=line.replace(replace1, replace2);} |
| 160 |
if(prefixMode){ |
| 161 |
for(int x=1; x<line.length(); x++){ |
| 162 |
char c=line.charAt(x-1); |
| 163 |
char next=line.charAt(x); |
| 164 |
if(Character.isWhitespace(c)){ |
| 165 |
line=line.substring(0, x).trim(); |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
set.add(line); |
| 171 |
} |
| 172 |
errorState|=tf.close(); |
| 173 |
return set; |
| 174 |
} |
| 175 |
|
| 176 |
void processInner(String fnameIn, String fnameOut, Collection<String> list){ |
| 177 |
|
| 178 |
final LinkedHashSet<String> set1=getContents(fnameIn); |
| 179 |
final FileFormat ffout=FileFormat.testOutput(fnameOut, FileFormat.TEXT, null, true, overwrite, append, false); |
| 180 |
|
| 181 |
final TextStreamWriter tsw; |
| 182 |
if(ffout!=null){ |
| 183 |
tsw=new TextStreamWriter(ffout); |
| 184 |
tsw.start(); |
| 185 |
}else{tsw=null;} |
| 186 |
|
| 187 |
for(String fname2 : list){ |
| 188 |
long shared=0; |
| 189 |
final LinkedHashSet<String> set2=getContents(fname2); |
| 190 |
for(String s : set1){ |
| 191 |
if(set2.contains(s)){ |
| 192 |
shared++; |
| 193 |
} |
| 194 |
} |
| 195 |
if(tsw!=null){ |
| 196 |
tsw.print(ReadWrite.stripToCore(fname2)+"\t"+shared+"\n"); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if(tsw!=null){ |
| 201 |
errorState|=tsw.poisonAndWait(); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
private LinkedHashSet<String> in1=new LinkedHashSet<String>(); |
| 211 |
private LinkedHashSet<String> in2=new LinkedHashSet<String>(); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
private boolean exclude=true; |
| 216 |
private boolean nameSubstringOfLine=false; |
| 217 |
private boolean lineSubstringOfName=false; |
| 218 |
private boolean ignoreCase=true; |
| 219 |
private boolean prefixMode=false; |
| 220 |
private long maxLines=-1; |
| 221 |
|
| 222 |
private String replace1=null; |
| 223 |
private String replace2=null; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
private PrintStream outstream=System.err; |
| 229 |
public static boolean verbose=false; |
| 230 |
public boolean errorState=false; |
| 231 |
private boolean overwrite=true; |
| 232 |
private boolean append=false; |
| 233 |
private boolean useSharedHeader=false; |
| 234 |
|
| 235 |
} |