| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
package jalview.analysis; |
| 22 |
|
|
| 23 |
|
import java.util.Locale; |
| 24 |
|
|
| 25 |
|
import java.io.File; |
| 26 |
|
import java.io.FileNotFoundException; |
| 27 |
|
import java.io.PrintStream; |
| 28 |
|
import java.util.Arrays; |
| 29 |
|
import java.util.Random; |
| 30 |
|
|
| 31 |
|
import jalview.datamodel.Alignment; |
| 32 |
|
import jalview.datamodel.AlignmentI; |
| 33 |
|
import jalview.datamodel.Sequence; |
| 34 |
|
import jalview.datamodel.SequenceI; |
| 35 |
|
import jalview.io.FastaFile; |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
@author |
| 46 |
|
|
| |
|
| 73.1% |
Uncovered Elements: 35 (130) |
Complexity: 28 |
Complexity Density: 0.33 |
|
| 47 |
|
public class AlignmentGenerator |
| 48 |
|
{ |
| 49 |
|
private static final char GAP = '-'; |
| 50 |
|
|
| 51 |
|
private static final char ZERO = '0'; |
| 52 |
|
|
| 53 |
|
private static final char[] NUCS = "GTCA".toCharArray(); |
| 54 |
|
|
| 55 |
|
private static final char[] PEPS = "MILVFYWHKRDEQNTCGASNP".toCharArray(); |
| 56 |
|
|
| 57 |
|
private static char[] BASES; |
| 58 |
|
|
| 59 |
|
private Random random; |
| 60 |
|
|
| 61 |
|
private PrintStream ps; |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
@param |
| 78 |
|
@throws |
| 79 |
|
|
| |
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 5 |
Complexity Density: 0.31 |
|
| 80 |
0 |
public static void main(String[] args) throws FileNotFoundException... |
| 81 |
|
{ |
| 82 |
0 |
if (args.length != 6 && args.length != 7) |
| 83 |
|
{ |
| 84 |
0 |
usage(); |
| 85 |
0 |
return; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
0 |
PrintStream ps = System.out; |
| 89 |
0 |
if (args.length == 7) |
| 90 |
|
{ |
| 91 |
0 |
ps = new PrintStream(new File(args[6])); |
| 92 |
|
} |
| 93 |
|
|
| 94 |
0 |
boolean nucleotide = args[0].toLowerCase(Locale.ROOT).startsWith("n"); |
| 95 |
0 |
int width = Integer.parseInt(args[1]); |
| 96 |
0 |
int height = Integer.parseInt(args[2]); |
| 97 |
0 |
long randomSeed = Long.valueOf(args[3]); |
| 98 |
0 |
int gapPercentage = Integer.valueOf(args[4]); |
| 99 |
0 |
int changePercentage = Integer.valueOf(args[5]); |
| 100 |
|
|
| 101 |
0 |
ps.println("; " + height + " sequences of " + width + " bases with " |
| 102 |
|
+ gapPercentage + "% gaps and " + changePercentage |
| 103 |
|
+ "% mutations (random seed = " + randomSeed + ")"); |
| 104 |
|
|
| 105 |
0 |
new AlignmentGenerator(nucleotide, ps).generate(width, height, |
| 106 |
|
randomSeed, gapPercentage, changePercentage); |
| 107 |
|
|
| 108 |
0 |
if (ps != System.out) |
| 109 |
|
{ |
| 110 |
0 |
ps.close(); |
| 111 |
|
} |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| |
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
| 117 |
0 |
private static void usage()... |
| 118 |
|
{ |
| 119 |
0 |
System.out.println("Usage:"); |
| 120 |
0 |
System.out.println("arg0: n (for nucleotide) or p (for peptide)"); |
| 121 |
0 |
System.out.println("arg1: number of (non-gap) bases per sequence"); |
| 122 |
0 |
System.out.println("arg2: number of sequences"); |
| 123 |
0 |
System.out.println( |
| 124 |
|
"arg3: an integer as random seed (same seed = same results)"); |
| 125 |
0 |
System.out.println("arg4: percentage of gaps to (randomly) generate"); |
| 126 |
0 |
System.out.println( |
| 127 |
|
"arg5: percentage of 'mutations' to (randomly) generate"); |
| 128 |
0 |
System.out.println( |
| 129 |
|
"arg6: (optional) path to output file (default is sysout)"); |
| 130 |
0 |
System.out.println("Example: AlignmentGenerator n 12 15 387 10 5"); |
| 131 |
0 |
System.out.println( |
| 132 |
|
"- 15 nucleotide sequences of 12 bases each, approx 10% gaps and 5% mutations, random seed = 387"); |
| 133 |
|
|
| 134 |
|
} |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 140 |
20 |
public AlignmentGenerator(boolean nuc)... |
| 141 |
|
{ |
| 142 |
20 |
this(nuc, System.out); |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 150 |
20 |
public AlignmentGenerator(boolean nucleotide, PrintStream printStream)... |
| 151 |
|
{ |
| 152 |
20 |
BASES = nucleotide ? NUCS : PEPS; |
| 153 |
20 |
ps = printStream; |
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
@param |
| 161 |
|
@param |
| 162 |
|
@param |
| 163 |
|
@param |
| 164 |
|
@param |
| 165 |
|
|
| |
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
| 166 |
36 |
public AlignmentI generate(int width, int height, long randomSeed,... |
| 167 |
|
int gapPercentage, int changePercentage) |
| 168 |
|
{ |
| 169 |
36 |
SequenceI[] seqs = new SequenceI[height]; |
| 170 |
36 |
random = new Random(randomSeed); |
| 171 |
36 |
seqs[0] = generateSequence(1, width, gapPercentage); |
| 172 |
1688 |
for (int seqno = 1; seqno < height; seqno++) |
| 173 |
|
{ |
| 174 |
1652 |
seqs[seqno] = generateAnotherSequence(seqs[0].getSequence(), |
| 175 |
|
seqno + 1, width, changePercentage); |
| 176 |
|
} |
| 177 |
36 |
AlignmentI al = new Alignment(seqs); |
| 178 |
|
|
| 179 |
36 |
if (ps != null) |
| 180 |
|
{ |
| 181 |
36 |
ps.println(new FastaFile().print(al.getSequencesArray(), true)); |
| 182 |
|
} |
| 183 |
|
|
| 184 |
36 |
return al; |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
|
| 190 |
|
@param |
| 191 |
|
@param |
| 192 |
|
@param |
| 193 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
| 194 |
36 |
private SequenceI generateSequence(int seqno, int length,... |
| 195 |
|
int gapPercentage) |
| 196 |
|
{ |
| 197 |
36 |
StringBuilder seq = new StringBuilder(length); |
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
1699 |
for (int count = 0; count < length;) |
| 203 |
|
{ |
| 204 |
1663 |
boolean addGap = random.nextInt(100) < gapPercentage; |
| 205 |
1663 |
char c = addGap ? GAP |
| 206 |
|
: BASES[random.nextInt(Integer.MAX_VALUE) % BASES.length]; |
| 207 |
1663 |
seq.append(c); |
| 208 |
1663 |
if (!addGap) |
| 209 |
|
{ |
| 210 |
1618 |
count++; |
| 211 |
|
} |
| 212 |
|
} |
| 213 |
36 |
final String seqName = "SEQ" + seqno; |
| 214 |
36 |
final String seqString = seq.toString(); |
| 215 |
36 |
SequenceI sq = new Sequence(seqName, seqString); |
| 216 |
36 |
sq.createDatasetSequence(); |
| 217 |
36 |
return sq; |
| 218 |
|
} |
| 219 |
|
|
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
|
@param |
| 224 |
|
@param |
| 225 |
|
@param |
| 226 |
|
|
| 227 |
|
@param |
| 228 |
|
@return |
| 229 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (43) |
Complexity: 9 |
Complexity Density: 0.33 |
|
| 230 |
1652 |
private SequenceI generateAnotherSequence(char[] ds, int seqno, int width,... |
| 231 |
|
int changePercentage) |
| 232 |
|
{ |
| 233 |
1652 |
int length = ds.length; |
| 234 |
1652 |
char[] seq = new char[length]; |
| 235 |
1652 |
Arrays.fill(seq, ZERO); |
| 236 |
1652 |
int gapsWanted = length - width; |
| 237 |
1652 |
int gapsAdded = 0; |
| 238 |
|
|
| 239 |
|
|
| 240 |
|
|
| 241 |
|
|
| 242 |
200600 |
for (int pos = 0; pos < length; pos++) |
| 243 |
|
{ |
| 244 |
198948 |
if (ds[pos] == GAP) |
| 245 |
|
{ |
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| 249 |
7945 |
seq[pos] = randomCharacter(GAP, changePercentage); |
| 250 |
7945 |
if (seq[pos] == GAP) |
| 251 |
|
{ |
| 252 |
7564 |
gapsAdded++; |
| 253 |
|
} |
| 254 |
|
} |
| 255 |
|
} |
| 256 |
|
|
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
2033 |
while (gapsAdded < gapsWanted) |
| 262 |
|
{ |
| 263 |
381 |
boolean added = false; |
| 264 |
772 |
while (!added) |
| 265 |
|
{ |
| 266 |
391 |
int pos = random.nextInt(length); |
| 267 |
391 |
if (seq[pos] != GAP) |
| 268 |
|
{ |
| 269 |
381 |
seq[pos] = GAP; |
| 270 |
381 |
added = true; |
| 271 |
381 |
gapsAdded++; |
| 272 |
|
} |
| 273 |
|
} |
| 274 |
|
} |
| 275 |
|
|
| 276 |
|
|
| 277 |
|
|
| 278 |
|
|
| 279 |
200600 |
for (int pos = 0; pos < length; pos++) |
| 280 |
|
{ |
| 281 |
198948 |
if (seq[pos] == ZERO) |
| 282 |
|
{ |
| 283 |
190632 |
char c = randomCharacter(ds[pos], changePercentage); |
| 284 |
190632 |
seq[pos] = c; |
| 285 |
|
} |
| 286 |
|
} |
| 287 |
1652 |
final String seqName = "SEQ" + seqno; |
| 288 |
1652 |
final String seqString = new String(seq); |
| 289 |
1652 |
SequenceI sq = new Sequence(seqName, seqString); |
| 290 |
1652 |
sq.createDatasetSequence(); |
| 291 |
1652 |
return sq; |
| 292 |
|
} |
| 293 |
|
|
| 294 |
|
|
| 295 |
|
|
| 296 |
|
|
| 297 |
|
|
| 298 |
|
@param |
| 299 |
|
|
| 300 |
|
@param |
| 301 |
|
@return |
| 302 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 303 |
198577 |
private char randomCharacter(char c, int changePercentage)... |
| 304 |
|
{ |
| 305 |
198577 |
final boolean mutation = random.nextInt(100) < changePercentage; |
| 306 |
|
|
| 307 |
198577 |
if (!mutation) |
| 308 |
|
{ |
| 309 |
188969 |
return c; |
| 310 |
|
} |
| 311 |
|
|
| 312 |
9608 |
char newchar = c; |
| 313 |
19733 |
while (newchar == c) |
| 314 |
|
{ |
| 315 |
10125 |
newchar = BASES[random.nextInt(Integer.MAX_VALUE) % BASES.length]; |
| 316 |
|
} |
| 317 |
9608 |
return newchar; |
| 318 |
|
} |
| 319 |
|
} |