jpayne@68: package fileIO; jpayne@68: jpayne@68: import java.io.File; jpayne@68: jpayne@68: import dna.Data; jpayne@68: import shared.Parse; jpayne@68: import shared.PreParser; jpayne@68: jpayne@68: /** jpayne@68: * Tests to see if a summary file matches a reference fasta file, based on date, size, and name jpayne@68: * @author Brian Bushnell jpayne@68: * @date Mar 11, 2013 jpayne@68: * jpayne@68: */ jpayne@68: public class SummaryFile { jpayne@68: jpayne@68: public static void main(String[] args){ jpayne@68: if(args.length==0){ jpayne@68: System.out.println("Usage: SummaryFile "); jpayne@68: System.exit(0); jpayne@68: } jpayne@68: jpayne@68: {//Preparse block for help, config files, and outstream jpayne@68: PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); jpayne@68: args=pp.args; jpayne@68: //outstream=pp.outstream; jpayne@68: } jpayne@68: jpayne@68: String summary=null, ref=null; jpayne@68: jpayne@68: for(int i=0; i1 ? split[1] : null; jpayne@68: jpayne@68: if(a.equals("summary")){ jpayne@68: summary=b; jpayne@68: }else if(a.equals("ref") || a.equals("reference")){ jpayne@68: ref=b; jpayne@68: }else{ jpayne@68: throw new RuntimeException("Unknown parameter: "+args[i]); jpayne@68: } jpayne@68: jpayne@68: }else{ jpayne@68: if(args[i].endsWith("summary.txt")){ jpayne@68: summary=args[i]; jpayne@68: }else{ jpayne@68: ref=args[i]; jpayne@68: } jpayne@68: } jpayne@68: } jpayne@68: jpayne@68: if(summary==null && args.length>0){ jpayne@68: summary=args[0]; jpayne@68: } jpayne@68: jpayne@68: if(summary==null){ jpayne@68: System.out.println("Usage: SummaryFile "); jpayne@68: System.exit(0); jpayne@68: } jpayne@68: jpayne@68: if(ref==null){ jpayne@68: jpayne@68: } jpayne@68: } jpayne@68: jpayne@68: public boolean compare(final String refName){ jpayne@68: try { jpayne@68: File ref=new File(refName); jpayne@68: if(!ref.exists()){ jpayne@68: if(refName.startsWith("stdin")){return false;} jpayne@68: else{ jpayne@68: assert(false) : "No such file: "+refName; jpayne@68: } jpayne@68: } jpayne@68: // if(!refName.equals(source) && !Files.isSameFile(ref.toPath(), new File(source).toPath())){ //This is Java-7 specific. jpayne@68: //// assert(false) : refName+", "+source+": "+(Files.isSameFile(ref.toPath(), new File(source).toPath()))+ jpayne@68: //// "\n"+ref.getCanonicalPath()+", "+new File(source).getCanonicalPath()+": "+(ref.getCanonicalPath().equals(new File(source).getCanonicalPath())); jpayne@68: // return false; jpayne@68: // jpayne@68: // } jpayne@68: if(!refName.equals(source) && !ref.getCanonicalPath().equals(new File(source).getCanonicalPath())){ jpayne@68: // assert(false) : refName+", "+source+": "+(Files.isSameFile(ref.toPath(), new File(source).toPath()))+ jpayne@68: // "\n"+ref.getCanonicalPath()+", "+new File(source).getCanonicalPath()+": "+(ref.getCanonicalPath().equals(new File(source).getCanonicalPath())); jpayne@68: return false; jpayne@68: jpayne@68: } jpayne@68: if(bytes!=ref.length()){ jpayne@68: // assert(false) : bytes+", "+ref.length(); jpayne@68: return false; jpayne@68: } jpayne@68: if(modified!=ref.lastModified()){ jpayne@68: // assert(false) : modified+", "+ref.lastModified(); jpayne@68: return false; jpayne@68: } jpayne@68: } catch (Exception e) { jpayne@68: // TODO Auto-generated catch block jpayne@68: e.printStackTrace(); jpayne@68: return false; jpayne@68: } jpayne@68: return true; jpayne@68: } jpayne@68: jpayne@68: public static boolean compare(final String summaryName, final String refName){ jpayne@68: assert(refName!=null) : "Null reference file name."; jpayne@68: if(!new File(summaryName).exists()){ jpayne@68: // assert(false); jpayne@68: return false; jpayne@68: } jpayne@68: SummaryFile sf=new SummaryFile(summaryName); jpayne@68: return sf.compare(refName); jpayne@68: } jpayne@68: jpayne@68: public static String getName(){ jpayne@68: return getName(Data.GENOME_BUILD); jpayne@68: } jpayne@68: jpayne@68: public static String getName(int build){ jpayne@68: return Data.ROOT_GENOME+build+"/summary.txt"; jpayne@68: } jpayne@68: jpayne@68: public SummaryFile(String path){ jpayne@68: summaryFname=path; jpayne@68: String s; jpayne@68: TextFile tf=new TextFile(summaryFname, false); jpayne@68: for(s=tf.nextLine(); s!=null; s=tf.nextLine()){ jpayne@68: if(s.charAt(0)=='#'){ jpayne@68: if(s.startsWith("#Version")){ jpayne@68: String[] split=s.split("\t"); jpayne@68: version=(split.length>1 ? Integer.parseInt(split[1]) : 0); jpayne@68: } jpayne@68: }else{ jpayne@68: String[] split=s.split("\t"); jpayne@68: String a=split[0]; jpayne@68: String b=split[1]; jpayne@68: if(a.equalsIgnoreCase("chroms")){chroms=(int)Long.parseLong(b);} jpayne@68: else if(a.equalsIgnoreCase("bases")){bases=Long.parseLong(b);} jpayne@68: else if(a.equalsIgnoreCase("version")){version=Integer.parseInt(b);} jpayne@68: else if(a.equalsIgnoreCase("defined")){definedBases=Long.parseLong(b);} jpayne@68: else if(a.equalsIgnoreCase("contigs")){contigs=Integer.parseInt(b);} jpayne@68: else if(a.equalsIgnoreCase("scaffolds")){scaffolds=Integer.parseInt(b);} jpayne@68: else if(a.equalsIgnoreCase("interpad")){interpad=Integer.parseInt(b);} jpayne@68: else if(a.equalsIgnoreCase("undefined")){undefinedBases=Long.parseLong(b);} jpayne@68: else if(a.equalsIgnoreCase("name")){name=b;} jpayne@68: else if(a.equalsIgnoreCase("source")){source=b;} jpayne@68: else if(a.equalsIgnoreCase("bytes")){bytes=Long.parseLong(b);} jpayne@68: else if(a.equalsIgnoreCase("last modified")){modified=Long.parseLong(b);} jpayne@68: else if(a.equalsIgnoreCase("scafprefixes")){scafprefixes=Parse.parseBoolean(b);} jpayne@68: else{throw new RuntimeException("In file "+tf.name+": Unknown term "+s);} jpayne@68: } jpayne@68: } jpayne@68: tf.close(); jpayne@68: } jpayne@68: jpayne@68: public final String summaryFname; jpayne@68: jpayne@68: public int chroms; jpayne@68: public long contigs; jpayne@68: public long scaffolds; jpayne@68: public int interpad; jpayne@68: public long bases; jpayne@68: public long definedBases; jpayne@68: public long undefinedBases; jpayne@68: public String name; jpayne@68: public String source; jpayne@68: public int version; jpayne@68: public long bytes; jpayne@68: public long modified; jpayne@68: public boolean scafprefixes; jpayne@68: jpayne@68: }