| 1 |
package tax; |
| 2 |
|
| 3 |
import java.io.Serializable; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Arrays; |
| 6 |
import java.util.HashMap; |
| 7 |
import java.util.Map.Entry; |
| 8 |
|
| 9 |
import fileIO.TextFile; |
| 10 |
import fileIO.TextStreamWriter; |
| 11 |
import shared.Parse; |
| 12 |
import shared.Timer; |
| 13 |
import shared.Tools; |
| 14 |
|
| 15 |
public class ImgRecord implements Serializable { |
| 16 |
|
| 17 |
private static final long serialVersionUID = 6438551103300423985L; |
| 18 |
|
| 19 |
public static void main(String[] args){ |
| 20 |
String in=args[0]; |
| 21 |
String out=args.length>1 ? args[1] : null; |
| 22 |
|
| 23 |
if(!Tools.testInputFiles(false, true, in)){ |
| 24 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 25 |
} |
| 26 |
if(!Tools.testOutputFiles(true, false, false, out)){ |
| 27 |
throw new RuntimeException("\nCan't write to some output files.\n"); |
| 28 |
} |
| 29 |
Timer t=new Timer(); |
| 30 |
HashMap<Long, ImgRecord> map=toMap(in, TaxTree.IMG_HQ); |
| 31 |
t.stop(); |
| 32 |
System.err.println(map.size()+"; "+t); |
| 33 |
|
| 34 |
if(out!=null){writeAsText(map, out);} |
| 35 |
} |
| 36 |
|
| 37 |
private static void writeAsText(HashMap<Long, ImgRecord> map, String out){ |
| 38 |
TextStreamWriter tsw=new TextStreamWriter(out, true, false, false); |
| 39 |
for(Entry<Long, ImgRecord> e : map.entrySet()){ |
| 40 |
tsw.println(e.toString()); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public String toString(){ |
| 46 |
StringBuilder sb=new StringBuilder(); |
| 47 |
sb.append(imgID); |
| 48 |
sb.append('\t').append(taxID); |
| 49 |
sb.append('\t').append(name); |
| 50 |
return sb.toString(); |
| 51 |
} |
| 52 |
|
| 53 |
public static HashMap<Long, ImgRecord> toMap(String fname, boolean highQuality){ |
| 54 |
ImgRecord[] array=toArray(fname, highQuality); |
| 55 |
HashMap<Long, ImgRecord> map=new HashMap<Long, ImgRecord>((3+array.length*4)/3); |
| 56 |
for(ImgRecord ir : array){ |
| 57 |
map.put(ir.imgID, ir); |
| 58 |
} |
| 59 |
return map; |
| 60 |
} |
| 61 |
|
| 62 |
public static ImgRecord[] toArray(String fname, boolean highQuality){ |
| 63 |
TextFile tf=new TextFile(fname, false); |
| 64 |
ArrayList<ImgRecord> list=new ArrayList<ImgRecord>(); |
| 65 |
for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){ |
| 66 |
if(line.length()<1 || !Tools.isDigit(line.charAt(0))){ |
| 67 |
|
| 68 |
}else{ |
| 69 |
ImgRecord record=new ImgRecord(line); |
| 70 |
if(!highQuality || record.highQuality){list.add(record);} |
| 71 |
} |
| 72 |
} |
| 73 |
tf.close(); |
| 74 |
return list.toArray(new ImgRecord[0]); |
| 75 |
} |
| 76 |
|
| 77 |
public ImgRecord(String line){ |
| 78 |
String[] split=line.split("\t"); |
| 79 |
|
| 80 |
imgID=Long.parseLong(split[0]); |
| 81 |
name=(storeName ? split[1] : null); |
| 82 |
try { |
| 83 |
taxID=(split[2]==null || split[2].length()<1 ? -1 : Integer.parseInt(split[2])); |
| 84 |
} catch (NumberFormatException e1) { |
| 85 |
|
| 86 |
e1.printStackTrace(); |
| 87 |
System.err.println(line); |
| 88 |
throw new RuntimeException(); |
| 89 |
} |
| 90 |
isPublic=Parse.parseYesNo(split[3]); |
| 91 |
obsolete=Parse.parseYesNo(split[4]); |
| 92 |
genomeType=find(split[5], typeArray); |
| 93 |
boolean hq=false; |
| 94 |
if(split.length>7){ |
| 95 |
try { |
| 96 |
hq=Parse.parseYesNo(split[7]); |
| 97 |
} catch (Exception e) { |
| 98 |
System.err.println(Arrays.toString(split)); |
| 99 |
assert(false); |
| 100 |
} |
| 101 |
} |
| 102 |
highQuality=hq; |
| 103 |
} |
| 104 |
|
| 105 |
public final long imgID; |
| 106 |
public final int taxID; |
| 107 |
public final int genomeType; |
| 108 |
public final boolean isPublic; |
| 109 |
public final boolean obsolete; |
| 110 |
public final boolean highQuality; |
| 111 |
public final String name; |
| 112 |
public final String path(){return "/global/dna/projectdirs/microbial/img_web_data/taxon.fna/"+imgID+".fna";} |
| 113 |
|
| 114 |
final int ISOLATE=0, SINGLE_CELL=1, METAGENOME=2; |
| 115 |
final String[] typeArray={"isolate", "single_cell", "metagenome"}; |
| 116 |
private static int find(String s, String[] array){ |
| 117 |
for(int i=0; i<array.length; i++){ |
| 118 |
if(array[i].equals(s)){return i;} |
| 119 |
} |
| 120 |
return -1; |
| 121 |
} |
| 122 |
|
| 123 |
public static boolean storeName=true; |
| 124 |
public static HashMap<Long, ImgRecord> imgMap; |
| 125 |
|
| 126 |
public static final String DefaultDumpFile="/global/u1/i/img/adhocDumps/taxonDumpForBrian.txt"; |
| 127 |
|
| 128 |
} |