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