| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.Random; |
| 5 |
|
| 6 |
import dna.AminoAcid; |
| 7 |
import fileIO.ByteStreamWriter; |
| 8 |
import fileIO.FileFormat; |
| 9 |
import fileIO.ReadWrite; |
| 10 |
import shared.Parse; |
| 11 |
import shared.Parser; |
| 12 |
import shared.PreParser; |
| 13 |
import shared.Shared; |
| 14 |
import shared.Timer; |
| 15 |
import shared.Tools; |
| 16 |
import structures.ByteBuilder; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class RandomGenome { |
| 24 |
|
| 25 |
public static void main(String[] args){ |
| 26 |
|
| 27 |
Timer t=new Timer(); |
| 28 |
|
| 29 |
|
| 30 |
RandomGenome x=new RandomGenome(args); |
| 31 |
|
| 32 |
|
| 33 |
x.process(t); |
| 34 |
|
| 35 |
|
| 36 |
Shared.closeStream(x.outstream); |
| 37 |
} |
| 38 |
|
| 39 |
public RandomGenome(String[] args){ |
| 40 |
|
| 41 |
{ |
| 42 |
PreParser pp=new PreParser(args, getClass(), false); |
| 43 |
args=pp.args; |
| 44 |
outstream=pp.outstream; |
| 45 |
} |
| 46 |
|
| 47 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 48 |
ReadWrite.setZipThreads(Shared.threads()); |
| 49 |
|
| 50 |
Parser parser=new Parser(); |
| 51 |
for(int i=0; i<args.length; i++){ |
| 52 |
String arg=args[i]; |
| 53 |
String[] split=arg.split("="); |
| 54 |
String a=split[0].toLowerCase(); |
| 55 |
String b=split.length>1 ? split[1] : null; |
| 56 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 57 |
|
| 58 |
if(a.equals("chroms")){ |
| 59 |
chroms=Parse.parseIntKMG(b); |
| 60 |
}else if(a.equals("len") || a.equals("length") || a.equals("size")){ |
| 61 |
totalLength=Parse.parseKMG(b); |
| 62 |
}else if(a.equals("pad")){ |
| 63 |
pad=Tools.max(0, Parse.parseIntKMG(b)); |
| 64 |
}else if(a.equals("gc")){ |
| 65 |
gc=Float.parseFloat(b); |
| 66 |
}else if(a.equals("verbose")){ |
| 67 |
verbose=Parse.parseBoolean(b); |
| 68 |
ReadWrite.verbose=verbose; |
| 69 |
}else if(a.equals("nohomopolymers") || a.equals("banhomopolymers") || a.equals("nopoly")){ |
| 70 |
noPoly=Parse.parseBoolean(b); |
| 71 |
}else if(a.equals("includestop") || a.equals("stop")){ |
| 72 |
includeStop=Parse.parseBoolean(b); |
| 73 |
}else if(a.equals("seed")){ |
| 74 |
seed=Long.parseLong(b); |
| 75 |
}else if(parser.parse(arg, a, b)){ |
| 76 |
|
| 77 |
}else{ |
| 78 |
outstream.println("Unknown parameter "+args[i]); |
| 79 |
assert(false) : "Unknown parameter "+args[i]; |
| 80 |
|
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
{ |
| 85 |
overwrite=parser.overwrite; |
| 86 |
append=parser.append; |
| 87 |
|
| 88 |
out=parser.out1; |
| 89 |
} |
| 90 |
|
| 91 |
wrap=Shared.FASTA_WRAP; |
| 92 |
assert(wrap>0) : "Wrap is too small."; |
| 93 |
assert(chroms>0) : "Chroms must be greater than 0."; |
| 94 |
assert(totalLength>=chroms) : "Length must be at least chroms."; |
| 95 |
assert(2*pad+totalLength/chroms<Shared.MAX_ARRAY_LEN) : "Length per chrom must be at most "+Shared.MAX_ARRAY_LEN; |
| 96 |
chromLength=(int)(totalLength/chroms); |
| 97 |
|
| 98 |
if(out!=null && out.equalsIgnoreCase("null")){out=null;} |
| 99 |
|
| 100 |
if(!Tools.testOutputFiles(overwrite, append, false, out)){ |
| 101 |
outstream.println((out==null)+", "+out); |
| 102 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out+"\n"); |
| 103 |
} |
| 104 |
|
| 105 |
ffout=FileFormat.testOutput(out, FileFormat.FA, null, true, overwrite, append, false); |
| 106 |
|
| 107 |
randy=Shared.threadLocalRandom(seed); |
| 108 |
} |
| 109 |
|
| 110 |
void process(Timer t){ |
| 111 |
if(Shared.AMINO_IN){ |
| 112 |
processAmino(t); |
| 113 |
}else{ |
| 114 |
processNucleotide(t); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
void processNucleotide(Timer t){ |
| 119 |
|
| 120 |
ByteStreamWriter bsw=new ByteStreamWriter(ffout); |
| 121 |
bsw.start(); |
| 122 |
|
| 123 |
for(int chrom=1; chrom<=chroms; chrom++){ |
| 124 |
bsw.print('>').print("contig").println(chrom); |
| 125 |
ByteBuilder bb=new ByteBuilder(wrap+1); |
| 126 |
byte prev='N'; |
| 127 |
final int max=chromLength+2*pad; |
| 128 |
final int pad2=chromLength+pad; |
| 129 |
if(gc==0.5f){ |
| 130 |
for(int i=0; i<max; ){ |
| 131 |
for(int j=0; j<wrap && i<max; i++, j++){ |
| 132 |
byte b; |
| 133 |
if(i<pad || i>=pad2){b='N';} |
| 134 |
else{ |
| 135 |
b=AminoAcid.numberToBase[randy.nextInt(4)]; |
| 136 |
while(noPoly && b==prev){b=AminoAcid.numberToBase[randy.nextInt(4)];} |
| 137 |
} |
| 138 |
bb.append(b); |
| 139 |
prev=b; |
| 140 |
} |
| 141 |
bb.nl(); |
| 142 |
bsw.print(bb); |
| 143 |
bb.clear(); |
| 144 |
} |
| 145 |
}else{ |
| 146 |
for(int i=0; i<max; ){ |
| 147 |
for(int j=0; j<wrap && i<max; i++, j++){ |
| 148 |
boolean at=randy.nextFloat()>=gc; |
| 149 |
char b; |
| 150 |
if(i<pad || i>=pad2){b='N';} |
| 151 |
else{ |
| 152 |
if(at){ |
| 153 |
b=randy.nextBoolean() ? 'A' : 'T'; |
| 154 |
}else{ |
| 155 |
b=randy.nextBoolean() ? 'C' : 'G'; |
| 156 |
} |
| 157 |
while(noPoly && b==prev){ |
| 158 |
if(at){ |
| 159 |
b=randy.nextBoolean() ? 'A' : 'T'; |
| 160 |
}else{ |
| 161 |
b=randy.nextBoolean() ? 'C' : 'G'; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
bb.append(b); |
| 166 |
prev=(byte)b; |
| 167 |
} |
| 168 |
bb.nl(); |
| 169 |
bsw.print(bb); |
| 170 |
bb.clear(); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
bsw.poisonAndWait(); |
| 175 |
} |
| 176 |
|
| 177 |
void processAmino(Timer t){ |
| 178 |
|
| 179 |
ByteStreamWriter bsw=new ByteStreamWriter(ffout); |
| 180 |
bsw.start(); |
| 181 |
|
| 182 |
final byte[] acids=AminoAcid.numberToAcid; |
| 183 |
final int limit=(includeStop ? acids.length : acids.length-1); |
| 184 |
for(int chrom=1; chrom<=chroms; chrom++){ |
| 185 |
bsw.print('>').print("gene").println(chrom); |
| 186 |
ByteBuilder bb=new ByteBuilder(wrap+1); |
| 187 |
byte prev='X'; |
| 188 |
final int max=chromLength+2*pad; |
| 189 |
final int pad2=chromLength+pad; |
| 190 |
for(int i=0; i<max; ){ |
| 191 |
for(int j=0; j<wrap && i<max; i++, j++){ |
| 192 |
byte b; |
| 193 |
if(i<pad || i>=pad2){b='X';} |
| 194 |
else{ |
| 195 |
b=acids[randy.nextInt(limit)]; |
| 196 |
while(noPoly && b==prev){b=acids[randy.nextInt(limit)];} |
| 197 |
} |
| 198 |
bb.append(b); |
| 199 |
prev=b; |
| 200 |
} |
| 201 |
bb.nl(); |
| 202 |
bsw.print(bb); |
| 203 |
bb.clear(); |
| 204 |
} |
| 205 |
} |
| 206 |
bsw.poisonAndWait(); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
private String out=null; |
| 212 |
|
| 213 |
int chroms=1; |
| 214 |
long totalLength=1000000; |
| 215 |
float gc=0.5f; |
| 216 |
final int chromLength; |
| 217 |
final int wrap; |
| 218 |
int pad=0; |
| 219 |
boolean noPoly=false; |
| 220 |
boolean includeStop=false; |
| 221 |
long seed=-1; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
final Random randy; |
| 226 |
|
| 227 |
private long linesOut=0; |
| 228 |
private long bytesOut=0; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
private final FileFormat ffout; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
private PrintStream outstream=System.err; |
| 237 |
public static boolean verbose=false; |
| 238 |
public boolean errorState=false; |
| 239 |
private boolean overwrite=true; |
| 240 |
private boolean append=false; |
| 241 |
|
| 242 |
} |