// Archibald Haddock (123456789)
// COMP 202-A, Section 0 (Fall 2007)
// Instructor: Cuthbert Calculus
// Assignment 3, Question 1

import java.util.Random;
import java.util.Scanner;

public class Mutate {
	public static void main(String[] args) {
		// Declare constants for each possible letter in the DNA
		// sequence.
		final String A = "A";
		final String C = "C";
		final String G = "G";
		final String T = "T";
		Scanner keyboard;
		Random randomSource;
		String sequence;
		String prefix;
		String suffix;
		String newLetter;
		int numberMutations;
		int index;

		// Initialize the Scanner object to read from the keyboard and
		// read the relevant information.
		keyboard = new Scanner(System.in);

		System.out.print("Enter the number of mutations: ");
		numberMutations = keyboard.nextInt();
		keyboard.nextLine();
		System.out.print("Enter a DNA sequence: ");
		sequence = keyboard.nextLine();
		System.out.println();

		// Create the pseudo-random number generator.
		randomSource = new Random(3735928559L);

		// Perform numberMutations mutations on the String.
		for (int i = 0; i < numberMutations; i++) {
			// Determine the position of the character to be
			// mutated.
			index = randomSource.nextInt(sequence.length());

			// Extract whatever comes before (the prefix) and after
			// (the suffix) the character to be mutated.
			prefix = sequence.substring(0, index);
			suffix = sequence.substring(index + 1,
				sequence.length());

			// Generate a random DNA sequence letter.
			newLetter = "";
			switch (randomSource.nextInt(4)) {
			case 0:
				newLetter = A;
				break;
			case 1:
				newLetter = C;
				break;
			case 2:
				newLetter = G;
				break;
			case 3:
				newLetter = T;
				break;
			}
			
			// Replace the old sequence by a new sequence formed by
			// concatenating the prefix, the mutated character, and
			// the suffix.
			// Replacing the old sequence is necessary, otherwise
			// only the final result will only contain the last
			// mutation.
			sequence = prefix + newLetter + suffix;
		}

		// Display the result.
		System.out.println("The original sequence mutated " +
			numberMutations	+ " times: " + sequence);
	}
}
