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