| 1 |
package tax; |
| 2 |
|
| 3 |
import java.io.Serializable; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.HashMap; |
| 6 |
|
| 7 |
import fileIO.ReadWrite; |
| 8 |
import fileIO.TextFile; |
| 9 |
import shared.Timer; |
| 10 |
import shared.Tools; |
| 11 |
|
| 12 |
public class ImgRecord2 implements Serializable { |
| 13 |
|
| 14 |
private static final long serialVersionUID = 8596055205924485293L; |
| 15 |
|
| 16 |
public static void main(String[] args){ |
| 17 |
String in=args[0]; |
| 18 |
String out=args.length>1 ? args[1] : null; |
| 19 |
|
| 20 |
if(!Tools.testInputFiles(false, true, in)){ |
| 21 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 22 |
} |
| 23 |
if(!Tools.testOutputFiles(true, false, false, out)){ |
| 24 |
throw new RuntimeException("\nCan't write to some output files.\n"); |
| 25 |
} |
| 26 |
Timer t=new Timer(); |
| 27 |
HashMap<Long, ImgRecord2> map=toMap(in); |
| 28 |
t.stop(); |
| 29 |
System.err.println(map.size()+"; "+t); |
| 30 |
if(out!=null){ReadWrite.writeObjectInThread(map, out, false);} |
| 31 |
} |
| 32 |
|
| 33 |
public static HashMap<Long, ImgRecord2> toMap(String fname){ |
| 34 |
ImgRecord2[] array=toArray(fname); |
| 35 |
HashMap<Long, ImgRecord2> map=new HashMap<Long, ImgRecord2>((3+array.length*4)/3); |
| 36 |
for(ImgRecord2 ir : array){ |
| 37 |
map.put(ir.imgID, ir); |
| 38 |
} |
| 39 |
return map; |
| 40 |
} |
| 41 |
|
| 42 |
public static ImgRecord2[] toArray(String fname){ |
| 43 |
TextFile tf=new TextFile(fname, false); |
| 44 |
ArrayList<ImgRecord2> list=new ArrayList<ImgRecord2>(); |
| 45 |
for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){ |
| 46 |
if(line.length()<1 || !Tools.isDigit(line.charAt(0))){ |
| 47 |
|
| 48 |
}else{ |
| 49 |
ImgRecord2 record=new ImgRecord2(line); |
| 50 |
list.add(record); |
| 51 |
} |
| 52 |
} |
| 53 |
tf.close(); |
| 54 |
return list.toArray(new ImgRecord2[0]); |
| 55 |
} |
| 56 |
|
| 57 |
public ImgRecord2(String line){ |
| 58 |
String[] split=line.split("\t"); |
| 59 |
|
| 60 |
imgID=Long.parseLong(split[0]); |
| 61 |
taxID=(split[1]==null || split[1].length()<1 ? 0 : Integer.parseInt(split[1])); |
| 62 |
name=(storeName ? split[2] : null); |
| 63 |
} |
| 64 |
|
| 65 |
public static final long parseImgId(String header, boolean doAssertions){ |
| 66 |
final char delimiter=TaxTree.ncbiHeaderDelimiter(header); |
| 67 |
assert(!doAssertions || delimiter!=' ') : header; |
| 68 |
|
| 69 |
if(delimiter==' '){return -1;} |
| 70 |
|
| 71 |
final int idx=header.indexOf("img"+delimiter); |
| 72 |
|
| 73 |
assert(!doAssertions || idx>=0) : "Could not img id number from "+header; |
| 74 |
if(idx<0){return -1;} |
| 75 |
|
| 76 |
long img=0; |
| 77 |
for(int i=idx+4; i<header.length(); i++){ |
| 78 |
final char c=header.charAt(i); |
| 79 |
if(c==delimiter || c==' '){break;} |
| 80 |
assert(Tools.isDigit(c)) : c+", '"+header+"'"; |
| 81 |
img=img*10+(c-'0'); |
| 82 |
} |
| 83 |
|
| 84 |
assert(!doAssertions || img>0) : "Could not img id number from "+header; |
| 85 |
return img>0 ? img : -1; |
| 86 |
} |
| 87 |
|
| 88 |
public static boolean storeName=true; |
| 89 |
public final long imgID; |
| 90 |
public final int taxID; |
| 91 |
public final String name; |
| 92 |
|
| 93 |
} |