Debian Codegraph @

current/align2/RefToIndex.java

raw symbols SCIP
1 package align2;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Date;
8
9 import dna.ChromosomeArray;
10 import dna.Data;
11 import dna.FastaToChromArrays2;
12 import fileIO.FileFormat;
13 import fileIO.ReadWrite;
14 import fileIO.SummaryFile;
15 import shared.Shared;
16 import shared.Tools;
17
18 /**
19 * @author Brian Bushnell
20 * @date Sep 25, 2013
21 *
22 */
23 public class RefToIndex {
24
25 public static final void clear(){
26 chromlist=null;
27 }
28
29 public static String summaryLoc(int build){
30 String s=IndexMaker4.fname(1, 1, 13, 1, build);
31 String dir=new File(s).getParent();
32 dir=dir.replace('\\', '/');
33 dir=dir.replace("ref/index/", "ref/genome/");
34 String sf=dir+"/summary.txt";
35 return sf;
36 }
37
38 public static String bloomLoc(int build){
39 return Data.ROOT_INDEX+build+"/bloom.serial";
40 }
41
42 public static void makeIndex(String reference, int build, PrintStream sysout, int keylen){
43 assert(reference!=null);
44 {
45 File f=new File(reference);
46 if(!f.exists() || !f.isFile() || !f.canRead()){
47 if(!reference.startsWith("stdin")){
48 throw new RuntimeException("Cannot read file "+f.getAbsolutePath());
49 }
50 }else{
51 FileFormat ff=FileFormat.testInput(reference, FileFormat.FA, null, false, true, true);
52 if(!ff.fasta()){
53 throw new RuntimeException("Reference file is not in fasta format: "+reference+"\n"+ff);
54 }
55 }
56 }
57
58 String s=IndexMaker4.fname(1, 1, keylen, 1);
59 String dir=new File(s).getParent();
60 dir=dir.replace('\\', '/');
61 final String base=dir.substring(0, dir.length()-7);
62 final String args=(Shared.COMMAND_LINE==null ? "null" : Arrays.toString(Shared.COMMAND_LINE));
63 final String indexlog=base+"build"+build+"_"+
64 (System.nanoTime()&Long.MAX_VALUE)+"."+((args==null ? (reference==null ? "null" : reference) : args).hashCode()&Integer.MAX_VALUE)+".log";
65 dir=dir.replace("ref/index/", "ref/genome/");
66 String sf=dir+"/summary.txt";
67 if(FORCE_READ_ONLY || (!NODISK && new File(sf).exists() && SummaryFile.compare(sf, reference))){
68 //do nothing
69 if(LOG && !NODISK){
70 if(!new File(base).exists()){new File(base).mkdirs();}
71 ReadWrite.writeString(new Date()+"\nFound an already-written genome for build "+build+".\n"+args+"\n", indexlog, true);
72 }
73 sysout.println("NOTE:\tIgnoring reference file because it already appears to have been processed.");
74 sysout.println("NOTE:\tIf you wish to regenerate the index, please manually delete "+dir+"/summary.txt");
75 }else{
76 if(NODISK){}
77 else{//Delete old data if present
78 File f=new File(dir);
79 if(f.exists()){
80 File[] f2=f.listFiles();
81 if(f2!=null && f2.length>0){
82 if(overwrite || f2[0].getAbsolutePath().equals(new File(reference).getAbsolutePath())){
83 sysout.println("NOTE:\tDeleting contents of "+dir+" because reference is specified and overwrite="+overwrite);
84 if(LOG && !NODISK){ReadWrite.writeString(new Date()+"\nDeleting genome for build "+build+".\n"+args+"\n", indexlog, true);}
85 for(File f3 : f2){
86 if(f3.isFile()){
87 String f3n=f3.getName();
88 if((f3n.contains(".chrom") || f3n.endsWith(".txt") || f3n.endsWith(".txt.gz")) && !f3n.endsWith("list.txt")){
89 f3.delete();
90 }
91 }
92 }
93 }else{
94 sysout.println(Arrays.toString(f2));
95 if(LOG && !NODISK){ReadWrite.writeString(new Date()+"\nFailed to overwrite genome for build "+build+".\n"+args+"\n", indexlog, true);}
96 throw new RuntimeException("\nThere is already a reference at location '"+f.getAbsolutePath()+"'. " +
97 "Please delete it (and the associated index), or use a different build ID, " +
98 "or remove the 'reference=' parameter from the command line, or set overwrite=true.");
99 }
100 }
101 }
102
103 dir=dir.replace("ref/genome/", "ref/index/");
104 f=new File(dir);
105 if(f.exists()){
106 File[] f2=f.listFiles();
107 if(f2!=null && f2.length>0){
108 if(overwrite){
109 sysout.println("NOTE:\tDeleting contents of "+dir+" because reference is specified and overwrite="+overwrite);
110 if(LOG && !NODISK){ReadWrite.writeString(new Date()+"\nDeleting index for build "+build+".\n"+args+"\n", indexlog, true);}
111 for(File f3 : f2){
112 if(f3.isFile()){f3.delete();}
113 }
114 }else{
115 if(LOG && !NODISK){ReadWrite.writeString(new Date()+"\nFailed to overwrite index for build "+build+".\n"+args+"\n", indexlog, true);}
116 throw new RuntimeException("\nThere is already an index at location '"+f.getAbsolutePath()+"'. " +
117 "Please delete it, or use a different build ID, or remove the 'reference=' parameter from the command line.");
118 }
119 }
120 }
121 }
122
123 if(!NODISK){
124 sysout.println("Writing reference.");
125 if(LOG && !NODISK){
126 if(!new File(base).exists()){new File(base).mkdirs();}
127 ReadWrite.writeString(new Date()+"\nWriting genome for build "+build+".\n"+args+"\n", indexlog, true);
128 }
129 }
130
131 int oldzl=ReadWrite.ZIPLEVEL;
132 ReadWrite.ZIPLEVEL=Tools.max(4, ReadWrite.ZIPLEVEL);
133
134 //assert(false) : "minScaf="+minScaf+", midPad="+midPad+", maxChromLen="+maxChromLen+
135 // ", startPad="+startPad+", stopPad="+stopPad+", FastaToChromArrays2.END_PADDING="+FastaToChromArrays2.END_PADDING;
136
137 maxChromLen=maxChromLen>0 ? maxChromLen : AUTO_CHROMBITS ? FastaToChromArrays2.MAX_LENGTH : ((1L<<(31-(chrombits<0 ? 2 : chrombits)))-200000);
138 minScaf=minScaf>-1 ? minScaf : FastaToChromArrays2.MIN_SCAFFOLD;
139 midPad=midPad>-1 ? midPad : FastaToChromArrays2.MID_PADDING;
140 startPad=startPad>-1 ? startPad : FastaToChromArrays2.START_PADDING;
141 stopPad=stopPad>-1 ? stopPad : FastaToChromArrays2.END_PADDING;
142
143 String[] ftcaArgs=new String[] {reference, ""+build, "writeinthread=false", "genscaffoldinfo="+genScaffoldInfo, "retain", "waitforwriting=false",
144 "gz="+(Data.CHROMGZ), "maxlen="+maxChromLen,
145 "writechroms="+(!NODISK), "minscaf="+minScaf, "midpad="+midPad, "startpad="+startPad, "stoppad="+stopPad, "nodisk="+NODISK};
146
147 chromlist=FastaToChromArrays2.main2(ftcaArgs);
148
149 ReadWrite.ZIPLEVEL=oldzl;
150 }
151
152 }
153
154 public static boolean AUTO_CHROMBITS=true;
155 public static boolean LOG=false;
156 public static boolean NODISK=false;
157 public static boolean FORCE_READ_ONLY=false;
158 public static boolean overwrite=true;
159 public static boolean append=false;
160 public static boolean genScaffoldInfo=true;
161
162 public static long maxChromLen=-1;
163
164 public static int minScaf=-1, midPad=-1, stopPad=-1, startPad=-1;
165 public static int chrombits=-1;
166 // public static int minScaf=FastaToChromArrays2.MIN_SCAFFOLD;
167 // public static int midPad=FastaToChromArrays2.MID_PADDING;
168 // public static int startPad=FastaToChromArrays2.START_PADDING;
169 // public static int stopPad=FastaToChromArrays2.END_PADDING;
170
171 public static ArrayList<ChromosomeArray> chromlist=null;
172
173 }