public class DNAComputer
{
	// This program performs string operations
	// on a random DNA sequence.

	public static void main(String[] args)
	{
		// Variables (to be initialized later)

		String sequence;	// random DNA sequence
		String subsequence;	// shorter DNA sequence

		// Generate a random DNA sequence

		sequence = makeRandomDNA();

		System.out.println("The random DNA sequence is: ");
		System.out.println(sequence + "\n");

		// Compute and display the sequence's "complement"

		System.out.println("The complement of the random DNA sequence is: ");
		System.out.println(complement(sequence) + "\n");

		// Compute and display the number of occurrences of
		// various subsequences in the DNA sequence

		System.out.println(sequence);

		System.out.print("The number of occurrences of AC is: ");
		subsequence = new String("AC");
		System.out.println(count(sequence, subsequence));

		System.out.print("The number of occurrences of TGG is: ");
		subsequence = new String("TGG");
		System.out.println(count(sequence, subsequence));

		System.out.print("The number of occurrences of ACTG is: ");
		subsequence = new String("ACTG");
		System.out.println(count(sequence, subsequence));

		System.out.print("The number of occurrences of CC is: ");
		subsequence = new String("CC");
		System.out.println(count(sequence, subsequence));

		System.out.println();

	}

	public static String makeRandomDNA()
	{
		int numSymbols = (int)(Math.random() * 41) + 40;  // random integer between 40 and 80 inclusive

		// Make a random DNA sequence with numSymbols symbols

		String DNAletters = new String("ACTG");
		String result = new String("");		// start with empty string
		
		for (int i = 1; i <= numSymbols; i++)
		{
			// Pick a random letter from "ACTG" string
			char symbol = DNAletters.charAt((int)(Math.random()*4));

			// concatenate next random symbol on to result
			result = result + symbol;
		}

		return result;
	}

	public static String complement(String sequence)
	{
		// Create and return the complement of the given
		// DNA sequence

		String newSequence = "";
		char newLetter;

		for (int i = 0; i < sequence.length(); i++)
		{
			if (sequence.charAt(i)=='A')
				newLetter = 'T';
			else if (sequence.charAt(i)=='T')
				newLetter = 'A';
			else if (sequence.charAt(i)=='C')
				newLetter = 'G';
			else
				newLetter = 'C';

			newSequence = newSequence + newLetter;
		}

		return newSequence;

	}

	public static int count(String sequence, String subsequence)
	{
		// Count and return the number of occurrences of the 
		// subsequence in the given DNA sequence. You may assume
		// the subsequence is shorter than the sequence.

		int count = 0;
		int seqLength = sequence.length();
		int subseqLength = subsequence.length();

		for (int i = 0; i < seqLength-subseqLength; i++)
		{
			if (sequence.substring(i,i+subseqLength).equals(subsequence))
				count++;
		}

		return count;

	}

}






