| 1 |
package gff; |
| 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.HashMap; |
| 8 |
|
| 9 |
import fileIO.ByteFile; |
| 10 |
import fileIO.ByteStreamWriter; |
| 11 |
import fileIO.FileFormat; |
| 12 |
import fileIO.ReadWrite; |
| 13 |
import prok.PGMTools; |
| 14 |
import shared.Parse; |
| 15 |
import shared.Parser; |
| 16 |
import shared.PreParser; |
| 17 |
import shared.Shared; |
| 18 |
import shared.Timer; |
| 19 |
import shared.Tools; |
| 20 |
import stream.Read; |
| 21 |
import stream.ReadInputStream; |
| 22 |
|
| 23 |
public class CutGff_ST { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public static void main(String[] args){ |
| 34 |
|
| 35 |
Timer t=new Timer(); |
| 36 |
|
| 37 |
|
| 38 |
CutGff_ST x=new CutGff_ST(args); |
| 39 |
|
| 40 |
|
| 41 |
x.process(t); |
| 42 |
|
| 43 |
|
| 44 |
Shared.closeStream(x.outstream); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public CutGff_ST(String[] args){ |
| 52 |
|
| 53 |
{ |
| 54 |
PreParser pp=new PreParser(args, null, false); |
| 55 |
args=pp.args; |
| 56 |
outstream=pp.outstream; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 61 |
ReadWrite.setZipThreads(Shared.threads()); |
| 62 |
|
| 63 |
Shared.TRIM_READ_COMMENTS=Shared.TRIM_RNAME=true; |
| 64 |
GffLine.parseAttributes=true; |
| 65 |
|
| 66 |
{ |
| 67 |
final Parser parser=parse(args); |
| 68 |
overwrite=parser.overwrite; |
| 69 |
append=parser.append; |
| 70 |
|
| 71 |
out=parser.out1; |
| 72 |
} |
| 73 |
|
| 74 |
fixExtensions(); |
| 75 |
checkFileExistence(); |
| 76 |
checkStatics(); |
| 77 |
|
| 78 |
ffout=FileFormat.testOutput(out, FileFormat.PGM, null, true, overwrite, append, false); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private Parser parse(String[] args){ |
| 87 |
|
| 88 |
Parser parser=new Parser(); |
| 89 |
parser.overwrite=overwrite; |
| 90 |
for(int i=0; i<args.length; i++){ |
| 91 |
String arg=args[i]; |
| 92 |
String[] split=arg.split("="); |
| 93 |
String a=split[0].toLowerCase(); |
| 94 |
String b=split.length>1 ? split[1] : null; |
| 95 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 96 |
|
| 97 |
|
| 98 |
if(PGMTools.parseStatic(arg, a, b)){ |
| 99 |
|
| 100 |
}else if(a.equals("in") || a.equals("infna") || a.equals("fnain") || a.equals("fna") || a.equals("ref")){ |
| 101 |
assert(b!=null); |
| 102 |
Tools.addFiles(b, fnaList); |
| 103 |
}else if(a.equals("gff") || a.equals("ingff") || a.equals("gffin")){ |
| 104 |
assert(b!=null); |
| 105 |
Tools.addFiles(b, gffList); |
| 106 |
}else if(a.equals("verbose")){ |
| 107 |
verbose=Parse.parseBoolean(b); |
| 108 |
ReadWrite.verbose=verbose; |
| 109 |
}else if(a.equals("invert")){ |
| 110 |
invert=Parse.parseBoolean(b); |
| 111 |
}else if(a.equals("type") || a.equals("types")){ |
| 112 |
types=b; |
| 113 |
}else if(a.equals("attributes") || a.equals("requiredattributes")){ |
| 114 |
requiredAttributes=b.split(","); |
| 115 |
}else if(a.equals("banattributes") || a.equals("bannedattributes")){ |
| 116 |
bannedAttributes=b.split(","); |
| 117 |
}else if(a.equals("banpartial")){ |
| 118 |
banPartial=Parse.parseBoolean(b); |
| 119 |
} |
| 120 |
|
| 121 |
else if(a.equals("minlen")){ |
| 122 |
minLen=Integer.parseInt(b); |
| 123 |
}else if(a.equals("maxlen")){ |
| 124 |
maxLen=Integer.parseInt(b); |
| 125 |
} |
| 126 |
|
| 127 |
else if(parser.parse(arg, a, b)){ |
| 128 |
|
| 129 |
}else if(arg.indexOf('=')<0 && new File(arg).exists() && FileFormat.isFastaFile(arg)){ |
| 130 |
fnaList.add(arg); |
| 131 |
}else{ |
| 132 |
outstream.println("Unknown parameter "+args[i]); |
| 133 |
assert(false) : "Unknown parameter "+args[i]; |
| 134 |
|
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
ArrayList<String> banned=new ArrayList<String>(); |
| 139 |
if(banPartial){banned.add("partial=true");} |
| 140 |
if(bannedAttributes!=null){ |
| 141 |
for(String s : bannedAttributes){banned.add(s);} |
| 142 |
} |
| 143 |
bannedAttributes=banned.isEmpty() ? null : banned.toArray(new String[0]); |
| 144 |
|
| 145 |
if(gffList.isEmpty()){ |
| 146 |
for(String s : fnaList){ |
| 147 |
String prefix=ReadWrite.stripExtension(s); |
| 148 |
String gff=prefix+".gff"; |
| 149 |
File f=new File(gff); |
| 150 |
if(!f.exists()){ |
| 151 |
String gz=gff+".gz"; |
| 152 |
f=new File(gz); |
| 153 |
assert(f.exists() && f.canRead()) : "Can't read file "+gff; |
| 154 |
gff=gz; |
| 155 |
} |
| 156 |
gffList.add(gff); |
| 157 |
} |
| 158 |
} |
| 159 |
assert(gffList.size()==fnaList.size()) : "Number of fna and gff files do not match: "+fnaList.size()+", "+gffList.size(); |
| 160 |
return parser; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
private void fixExtensions(){ |
| 165 |
fnaList=Tools.fixExtension(fnaList); |
| 166 |
gffList=Tools.fixExtension(gffList); |
| 167 |
if(fnaList.isEmpty()){throw new RuntimeException("Error - at least one input file is required.");} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
private void checkFileExistence(){ |
| 172 |
|
| 173 |
if(!Tools.testOutputFiles(overwrite, append, false, out)){ |
| 174 |
outstream.println((out==null)+", "+out); |
| 175 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n"); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
ArrayList<String> foo=new ArrayList<String>(); |
| 180 |
foo.addAll(fnaList); |
| 181 |
foo.addAll(gffList); |
| 182 |
if(!Tools.testInputFiles(false, true, foo.toArray(new String[0]))){ |
| 183 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
foo.add(out); |
| 188 |
if(!Tools.testForDuplicateFiles(true, foo.toArray(new String[0]))){ |
| 189 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
private static void checkStatics(){ |
| 195 |
|
| 196 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 197 |
ByteFile.FORCE_MODE_BF2=true; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public void process(Timer t){ |
| 206 |
ByteStreamWriter bsw=new ByteStreamWriter(ffout); |
| 207 |
bsw.start(); |
| 208 |
|
| 209 |
for(int i=0; i<fnaList.size(); i++){ |
| 210 |
processFile(fnaList.get(i), gffList.get(i), types, bsw); |
| 211 |
} |
| 212 |
|
| 213 |
bsw.poisonAndWait(); |
| 214 |
} |
| 215 |
|
| 216 |
private void processFile(String fna, String gff, String types, ByteStreamWriter bsw){ |
| 217 |
ArrayList<GffLine> lines=GffLine.loadGffFile(gff, types, false); |
| 218 |
|
| 219 |
ArrayList<Read> list=ReadInputStream.toReads(fna, FileFormat.FA, -1); |
| 220 |
HashMap<String, Read> map=new HashMap<String, Read>(); |
| 221 |
for(Read r : list){map.put(r.id, r);} |
| 222 |
|
| 223 |
processStrand(lines, map, 0, bsw, invert); |
| 224 |
for(Read r : list){r.reverseComplement();} |
| 225 |
processStrand(lines, map, 1, bsw, invert); |
| 226 |
|
| 227 |
if(invert){ |
| 228 |
for(Read r : list){ |
| 229 |
r.reverseComplement(); |
| 230 |
bsw.println(r); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
private boolean hasAttributes(GffLine gline){ |
| 236 |
int len=gline.length(); |
| 237 |
if(len<minLen || len>maxLen){return false;} |
| 238 |
if(hasAttributes(gline, bannedAttributes)){return false;} |
| 239 |
return requiredAttributes==null || hasAttributes(gline, requiredAttributes); |
| 240 |
} |
| 241 |
|
| 242 |
private boolean hasAttributes(GffLine gline, String[] attributes){ |
| 243 |
if(attributes==null){return false;} |
| 244 |
for(String s : attributes){ |
| 245 |
if(gline.attributes.contains(s)){ |
| 246 |
return true; |
| 247 |
} |
| 248 |
} |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
private void processStrand(ArrayList<GffLine> lines, HashMap<String, Read> map, int strand, ByteStreamWriter bsw, boolean invert){ |
| 253 |
for(GffLine gline : lines){ |
| 254 |
if(gline.strand==strand && hasAttributes(gline)){ |
| 255 |
Read scaf=map.get(gline.seqid); |
| 256 |
assert(scaf!=null) : "Can't find "+gline.seqid+" in "+map.keySet(); |
| 257 |
int start, stop; |
| 258 |
if(strand==0){ |
| 259 |
start=gline.start-1; |
| 260 |
stop=gline.stop-1; |
| 261 |
}else{ |
| 262 |
start=scaf.length()-gline.stop-1; |
| 263 |
stop=scaf.length()-gline.start-1; |
| 264 |
} |
| 265 |
if(invert){ |
| 266 |
byte[] bases=scaf.bases; |
| 267 |
for(int i=start; i<stop; i++){ |
| 268 |
if(i>=0 && i<bases.length){ |
| 269 |
bases[i]='N'; |
| 270 |
} |
| 271 |
} |
| 272 |
}else{ |
| 273 |
if(start>=0 && stop<scaf.length()){ |
| 274 |
Read r=new Read(Arrays.copyOfRange(scaf.bases, start, stop), null, gline.attributes, 1); |
| 275 |
bsw.println(r); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
private ArrayList<String> fnaList=new ArrayList<String>(); |
| 287 |
private ArrayList<String> gffList=new ArrayList<String>(); |
| 288 |
private String out=null; |
| 289 |
private String types="CDS"; |
| 290 |
private boolean invert=false; |
| 291 |
private boolean banPartial=true; |
| 292 |
private int minLen=1; |
| 293 |
private int maxLen=Integer.MAX_VALUE; |
| 294 |
|
| 295 |
private String[] requiredAttributes; |
| 296 |
private String[] bannedAttributes; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
private long bytesOut=0; |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
private final FileFormat ffout; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
private PrintStream outstream=System.err; |
| 313 |
public static boolean verbose=false; |
| 314 |
public boolean errorState=false; |
| 315 |
private boolean overwrite=true; |
| 316 |
private boolean append=false; |
| 317 |
|
| 318 |
} |