| 1 |
package stream; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
|
| 5 |
import fileIO.ByteFile; |
| 6 |
import fileIO.FileFormat; |
| 7 |
import shared.Shared; |
| 8 |
import shared.Timer; |
| 9 |
import shared.Tools; |
| 10 |
import structures.ByteBuilder; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public class FastaShredInputStream extends ReadInputStream { |
| 18 |
|
| 19 |
public static void main(String[] args){ |
| 20 |
|
| 21 |
int a=20, b=Integer.MAX_VALUE; |
| 22 |
if(args.length>1){a=Integer.parseInt(args[1]);} |
| 23 |
if(args.length>2){b=Integer.parseInt(args[2]);} |
| 24 |
if(args.length>3){MIN_READ_LEN=Integer.parseInt(args[3]);} |
| 25 |
if(args.length>4){TARGET_READ_LEN=Integer.parseInt(args[4]);} |
| 26 |
|
| 27 |
Timer t=new Timer(); |
| 28 |
|
| 29 |
FastaShredInputStream fris=new FastaShredInputStream(args[0], false, false, Shared.bufferData()); |
| 30 |
Read r=fris.next(); |
| 31 |
int i=0; |
| 32 |
|
| 33 |
while(r!=null){ |
| 34 |
if(i<a){System.out.println(r.toText(false));} |
| 35 |
r=fris.next(); |
| 36 |
if(++i>=a){break;} |
| 37 |
} |
| 38 |
while(r!=null && i++<b){r=fris.next();} |
| 39 |
t.stop(); |
| 40 |
System.out.println("Time: \t"+t); |
| 41 |
} |
| 42 |
|
| 43 |
public FastaShredInputStream(String fname, boolean amino_, boolean allowSubprocess_, long maxdata){ |
| 44 |
this(FileFormat.testInput(fname, FileFormat.FASTA, FileFormat.FASTA, 0, allowSubprocess_, false, false), amino_, maxdata); |
| 45 |
} |
| 46 |
|
| 47 |
public FastaShredInputStream(FileFormat ff, boolean amino_, long maxData_){ |
| 48 |
name=ff.name(); |
| 49 |
amino=amino_; |
| 50 |
flag=(amino ? Read.AAMASK : 0); |
| 51 |
|
| 52 |
if(!fileIO.FileFormat.hasFastaExtension(name) && !name.startsWith("stdin")){ |
| 53 |
System.err.println("Warning: Did not find expected fasta file extension for filename "+name); |
| 54 |
} |
| 55 |
|
| 56 |
allowSubprocess=ff.allowSubprocess(); |
| 57 |
minLen=MIN_READ_LEN; |
| 58 |
maxLen=TARGET_READ_LEN; |
| 59 |
maxData=maxData_>0 ? maxData_ : Shared.bufferData(); |
| 60 |
|
| 61 |
bf=open(); |
| 62 |
|
| 63 |
assert(settingsOK()); |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public Read next() { |
| 68 |
throw new RuntimeException("Unsupported"); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public ArrayList<Read> nextList() { |
| 73 |
if(currentList==null){ |
| 74 |
boolean b=fillList(); |
| 75 |
} |
| 76 |
ArrayList<Read> list=currentList; |
| 77 |
currentList=null; |
| 78 |
if(list==null || list.isEmpty()){ |
| 79 |
list=null; |
| 80 |
}else{ |
| 81 |
consumed+=list.size(); |
| 82 |
} |
| 83 |
return list; |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public boolean hasMore() { |
| 88 |
if(currentList==null || currentList.size()==0){ |
| 89 |
if(open){ |
| 90 |
fillList(); |
| 91 |
}else{ |
| 92 |
|
| 93 |
} |
| 94 |
} |
| 95 |
return (currentList!=null && currentList.size()>0); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public void restart() { |
| 100 |
if(bf!=null){close();} |
| 101 |
assert(bf==null); |
| 102 |
|
| 103 |
consumed=0; |
| 104 |
nextReadID=0; |
| 105 |
currentList=null; |
| 106 |
|
| 107 |
if(bf==null){ |
| 108 |
bf=open(); |
| 109 |
}else{ |
| 110 |
assert(false) : "bf should be null"; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public final boolean close(){ |
| 116 |
synchronized(this){ |
| 117 |
if(!open){return false;} |
| 118 |
open=false; |
| 119 |
assert(bf!=null); |
| 120 |
errorState|=bf.close(); |
| 121 |
bf=null; |
| 122 |
} |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public boolean paired() {return false;} |
| 128 |
|
| 129 |
@Override |
| 130 |
public void start() {} |
| 131 |
|
| 132 |
private final boolean fillList(){ |
| 133 |
|
| 134 |
if(!open){ |
| 135 |
currentList=null; |
| 136 |
return false; |
| 137 |
} |
| 138 |
assert(currentList==null); |
| 139 |
currentList=new ArrayList<Read>(BUF_LEN); |
| 140 |
|
| 141 |
long len=0; |
| 142 |
for(int i=0; i<BUF_LEN && len<maxData; i++){ |
| 143 |
Read r=generateRead(); |
| 144 |
if(r==null){ |
| 145 |
close(); |
| 146 |
break; |
| 147 |
} |
| 148 |
currentList.add(r); |
| 149 |
len+=r.length(); |
| 150 |
nextReadID++; |
| 151 |
if(verbose){System.err.println("Generated a read; i="+i+", BUF_LEN="+BUF_LEN);} |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
return currentList.size()>0; |
| 156 |
} |
| 157 |
|
| 158 |
private final Read generateRead(){ |
| 159 |
Read r=null; |
| 160 |
boolean eof=false; |
| 161 |
while(r==null && !eof){ |
| 162 |
while(buffer.length()<maxLen){ |
| 163 |
byte[] line=bf.nextLine(); |
| 164 |
if(line==null){ |
| 165 |
eof=true; |
| 166 |
break; |
| 167 |
} |
| 168 |
if(line.length>0 && line[0]==carrot){break;} |
| 169 |
buffer.append(line); |
| 170 |
} |
| 171 |
if(buffer.length>=minLen){ |
| 172 |
byte[] bases=buffer.expelAndShift(Tools.min(maxLen, buffer.length()), TARGET_READ_OVERLAP); |
| 173 |
r=new Read(bases, null, Long.toString(nextReadID), nextReadID, flag); |
| 174 |
nextReadID++; |
| 175 |
if(verbose){System.err.println("Made read:\t"+(r.length()>1000 ? r.id : r.toString()));} |
| 176 |
if(bases.length<maxLen){buffer.clear();} |
| 177 |
}else{ |
| 178 |
buffer.clear(); |
| 179 |
} |
| 180 |
} |
| 181 |
return r; |
| 182 |
} |
| 183 |
|
| 184 |
private final ByteFile open(){ |
| 185 |
if(open){ |
| 186 |
throw new RuntimeException("Attempt to open already-opened fasta file "+name); |
| 187 |
} |
| 188 |
open=true; |
| 189 |
ByteFile bf=ByteFile.makeByteFile(name, allowSubprocess); |
| 190 |
return bf; |
| 191 |
} |
| 192 |
|
| 193 |
public boolean isOpen(){return open;} |
| 194 |
|
| 195 |
public static final boolean settingsOK(){ |
| 196 |
if(MIN_READ_LEN>=Integer.MAX_VALUE-1){ |
| 197 |
throw new RuntimeException("Minimum FASTA read length is too long: "+MIN_READ_LEN); |
| 198 |
} |
| 199 |
if(MIN_READ_LEN<1){ |
| 200 |
throw new RuntimeException("Minimum FASTA read length is too short: "+MIN_READ_LEN); |
| 201 |
} |
| 202 |
if(TARGET_READ_LEN<1){ |
| 203 |
throw new RuntimeException("Target FASTA read length is too short: "+TARGET_READ_LEN); |
| 204 |
} |
| 205 |
if(MIN_READ_LEN>TARGET_READ_LEN){ |
| 206 |
throw new RuntimeException("Minimum FASTA read length is longer than maximum read length: "+MIN_READ_LEN+">"+TARGET_READ_LEN); |
| 207 |
} |
| 208 |
if(MIN_READ_LEN>=Integer.MAX_VALUE-1 || MIN_READ_LEN<1){return false;} |
| 209 |
if(TARGET_READ_LEN<1 || MIN_READ_LEN>TARGET_READ_LEN){return false;} |
| 210 |
return true; |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public String fname(){return bf.name();} |
| 215 |
|
| 216 |
public final String name; |
| 217 |
|
| 218 |
private ArrayList<Read> currentList=null; |
| 219 |
|
| 220 |
private boolean open=false; |
| 221 |
private ByteBuilder buffer=new ByteBuilder(); |
| 222 |
public ByteFile bf; |
| 223 |
|
| 224 |
private long nextReadID=0; |
| 225 |
private long consumed=0; |
| 226 |
|
| 227 |
public final boolean allowSubprocess; |
| 228 |
public final boolean amino; |
| 229 |
public final int flag; |
| 230 |
private final int BUF_LEN=Shared.bufferLen();; |
| 231 |
private final long maxData; |
| 232 |
private final int maxLen, minLen; |
| 233 |
|
| 234 |
|
| 235 |
public static boolean verbose=false; |
| 236 |
private static final byte carrot='>'; |
| 237 |
|
| 238 |
public static int TARGET_READ_LEN=800; |
| 239 |
public static int TARGET_READ_OVERLAP=31; |
| 240 |
public static int MIN_READ_LEN=31; |
| 241 |
public static boolean FAKE_QUALITY=false; |
| 242 |
public static boolean WARN_IF_NO_SEQUENCE=true; |
| 243 |
public static boolean WARN_FIRST_TIME_ONLY=true; |
| 244 |
public static boolean ABORT_IF_NO_SEQUENCE=false; |
| 245 |
|
| 246 |
} |