public String addError( String str, double insertionRate, double deletionRate, double substitutionRate) { if (insertionRate < 0.0 || deletionRate < 0.0 || substitutionRate < 0.0) throw new MhapRuntimeException("Error rate cannot be negative."); if (insertionRate + deletionRate + substitutionRate > 1.00001) throw new MhapRuntimeException("Error rate must be less than or equal to 1.0."); double errorRate = insertionRate + deletionRate + substitutionRate; // use a linked list for insertions LinkedList<Character> modifiedSequence = new LinkedList<>(); for (char a : str.toCharArray()) modifiedSequence.add(a); // now mutate ListIterator<Character> iter = modifiedSequence.listIterator(); while (iter.hasNext()) { char i = iter.next(); if (randGenerator.nextDouble() < errorRate) { double errorType = randGenerator.nextDouble(); if (errorType < substitutionRate) { // mismatch // switch base iter.set(getRandomBase(i)); i++; } else if (errorType < insertionRate + substitutionRate) { // insert iter.previous(); iter.add(getRandomBase(null)); } else { // delete iter.remove(); } } else { // i++; } } StringBuilder returnedString = new StringBuilder(modifiedSequence.size()); for (char c : modifiedSequence) returnedString.append(c); return returnedString.toString(); }
/** * Generate a random value from this distribution. * * @param rng random number generator * @return random value from this distribution */ public double generate(MersenneTwisterFast rng) { try { return gammaDist.inverseCumulativeProbability(rng.nextDouble()); } catch (MathException e) { System.out.println(e.getMessage()); e.printStackTrace(); System.exit(-1); } return -1d; }