| 1 |
package fileIO; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
|
| 5 |
import dna.Data; |
| 6 |
import shared.Parse; |
| 7 |
import shared.PreParser; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public class SummaryFile { |
| 16 |
|
| 17 |
public static void main(String[] args){ |
| 18 |
if(args.length==0){ |
| 19 |
System.out.println("Usage: SummaryFile <summary file> <reference fasta>"); |
| 20 |
System.exit(0); |
| 21 |
} |
| 22 |
|
| 23 |
{ |
| 24 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 25 |
args=pp.args; |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
String summary=null, ref=null; |
| 30 |
|
| 31 |
for(int i=0; i<args.length; i++){ |
| 32 |
|
| 33 |
if(args[i].contains("=")){ |
| 34 |
final String arg=args[i]; |
| 35 |
final String[] split=arg.split("="); |
| 36 |
String a=split[0].toLowerCase(); |
| 37 |
String b=split.length>1 ? split[1] : null; |
| 38 |
|
| 39 |
if(a.equals("summary")){ |
| 40 |
summary=b; |
| 41 |
}else if(a.equals("ref") || a.equals("reference")){ |
| 42 |
ref=b; |
| 43 |
}else{ |
| 44 |
throw new RuntimeException("Unknown parameter: "+args[i]); |
| 45 |
} |
| 46 |
|
| 47 |
}else{ |
| 48 |
if(args[i].endsWith("summary.txt")){ |
| 49 |
summary=args[i]; |
| 50 |
}else{ |
| 51 |
ref=args[i]; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
if(summary==null && args.length>0){ |
| 57 |
summary=args[0]; |
| 58 |
} |
| 59 |
|
| 60 |
if(summary==null){ |
| 61 |
System.out.println("Usage: SummaryFile <summary file> <reference fasta>"); |
| 62 |
System.exit(0); |
| 63 |
} |
| 64 |
|
| 65 |
if(ref==null){ |
| 66 |
|
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public boolean compare(final String refName){ |
| 71 |
try { |
| 72 |
File ref=new File(refName); |
| 73 |
if(!ref.exists()){ |
| 74 |
if(refName.startsWith("stdin")){return false;} |
| 75 |
else{ |
| 76 |
assert(false) : "No such file: "+refName; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
if(!refName.equals(source) && !ref.getCanonicalPath().equals(new File(source).getCanonicalPath())){ |
| 86 |
|
| 87 |
|
| 88 |
return false; |
| 89 |
|
| 90 |
} |
| 91 |
if(bytes!=ref.length()){ |
| 92 |
|
| 93 |
return false; |
| 94 |
} |
| 95 |
if(modified!=ref.lastModified()){ |
| 96 |
|
| 97 |
return false; |
| 98 |
} |
| 99 |
} catch (Exception e) { |
| 100 |
|
| 101 |
e.printStackTrace(); |
| 102 |
return false; |
| 103 |
} |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
public static boolean compare(final String summaryName, final String refName){ |
| 108 |
assert(refName!=null) : "Null reference file name."; |
| 109 |
if(!new File(summaryName).exists()){ |
| 110 |
|
| 111 |
return false; |
| 112 |
} |
| 113 |
SummaryFile sf=new SummaryFile(summaryName); |
| 114 |
return sf.compare(refName); |
| 115 |
} |
| 116 |
|
| 117 |
public static String getName(){ |
| 118 |
return getName(Data.GENOME_BUILD); |
| 119 |
} |
| 120 |
|
| 121 |
public static String getName(int build){ |
| 122 |
return Data.ROOT_GENOME+build+"/summary.txt"; |
| 123 |
} |
| 124 |
|
| 125 |
public SummaryFile(String path){ |
| 126 |
summaryFname=path; |
| 127 |
String s; |
| 128 |
TextFile tf=new TextFile(summaryFname, false); |
| 129 |
for(s=tf.nextLine(); s!=null; s=tf.nextLine()){ |
| 130 |
if(s.charAt(0)=='#'){ |
| 131 |
if(s.startsWith("#Version")){ |
| 132 |
String[] split=s.split("\t"); |
| 133 |
version=(split.length>1 ? Integer.parseInt(split[1]) : 0); |
| 134 |
} |
| 135 |
}else{ |
| 136 |
String[] split=s.split("\t"); |
| 137 |
String a=split[0]; |
| 138 |
String b=split[1]; |
| 139 |
if(a.equalsIgnoreCase("chroms")){chroms=(int)Long.parseLong(b);} |
| 140 |
else if(a.equalsIgnoreCase("bases")){bases=Long.parseLong(b);} |
| 141 |
else if(a.equalsIgnoreCase("version")){version=Integer.parseInt(b);} |
| 142 |
else if(a.equalsIgnoreCase("defined")){definedBases=Long.parseLong(b);} |
| 143 |
else if(a.equalsIgnoreCase("contigs")){contigs=Integer.parseInt(b);} |
| 144 |
else if(a.equalsIgnoreCase("scaffolds")){scaffolds=Integer.parseInt(b);} |
| 145 |
else if(a.equalsIgnoreCase("interpad")){interpad=Integer.parseInt(b);} |
| 146 |
else if(a.equalsIgnoreCase("undefined")){undefinedBases=Long.parseLong(b);} |
| 147 |
else if(a.equalsIgnoreCase("name")){name=b;} |
| 148 |
else if(a.equalsIgnoreCase("source")){source=b;} |
| 149 |
else if(a.equalsIgnoreCase("bytes")){bytes=Long.parseLong(b);} |
| 150 |
else if(a.equalsIgnoreCase("last modified")){modified=Long.parseLong(b);} |
| 151 |
else if(a.equalsIgnoreCase("scafprefixes")){scafprefixes=Parse.parseBoolean(b);} |
| 152 |
else{throw new RuntimeException("In file "+tf.name+": Unknown term "+s);} |
| 153 |
} |
| 154 |
} |
| 155 |
tf.close(); |
| 156 |
} |
| 157 |
|
| 158 |
public final String summaryFname; |
| 159 |
|
| 160 |
public int chroms; |
| 161 |
public long contigs; |
| 162 |
public long scaffolds; |
| 163 |
public int interpad; |
| 164 |
public long bases; |
| 165 |
public long definedBases; |
| 166 |
public long undefinedBases; |
| 167 |
public String name; |
| 168 |
public String source; |
| 169 |
public int version; |
| 170 |
public long bytes; |
| 171 |
public long modified; |
| 172 |
public boolean scafprefixes; |
| 173 |
|
| 174 |
} |