DNA sequencing.
★★★3-letter sequences encode amino acids in DNA. For example, TTT is phenylalanine and TTA is leucine. This program reads a DNA sequence stored in a file and outputs the number of a particular amino acid in the sequence requested by the user. E.g. in the sequence: ACGTTTGTATTT the sequence TTT appears twice.
Write a program that asks the user to enter three characters and outputs how many times that sequence of characters appears in a file.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
get_amino_acid that:check_sequence that:dna.txt for reading.get_amino_acid to input a valid amino acid.check_sequence to return the number of the amino acids in the file.dna.txt file:
ACAAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTGCTCTCCGGGGCCACGGCCACCGCTGCCCTGC
CCTGGAGGGTGGCCCCACCGGCCGAGACAGCGAGCATATGCAGGAAGCGGCAGGAATAAGGAAAAGCGG
CTCCTGACTTTCCTCGCTTGGTGGTTTGAGTGGACCTCCCAGGCCAGTGCCGGGCCCCTCATAGGAGAG
Enter the amino acid to find:
CCC
There are 4 CCC amino acids in the DNA sequence.
Enter the amino acid to find:
GGT
There are 0 GGT amino acids in the DNA sequence.
Enter the amino acid to find:
GGG
There are 3 GGG amino acids in the DNA sequence.
get_amino_acid
// Input the amino acid
static string get_amino_acid()
{
---
string choice = "";
bool valid = false;
---
// Validation
while (!valid)
{
---
Console.WriteLine("Enter the amino acid to find:");
choice = Console.ReadLine();
---
// Amino acid must be 3 letters
if (choice.Length != 3)
{
---
valid = false;
---
}
---
else
{
---
valid = true;
// Check each letter of the choice
foreach (char letter in choice)
{
---
// Amino acid must contain only the letters ACGT
if (!"ACGT".Contains(letter))
{
---
valid = false;
---
}
---
}
---
}
---
}
---
return choice;
}
check_sequence
// Read the DNA sequence file
static int check_sequence(string amino_acid)
{
---
string file_name = "dna.txt";
// Check file exists
try
{
---
int count = 0;
StreamReader file = new StreamReader(file_name);
string line;
---
// Read each line
while ((line = file.ReadLine()) != null)
{
---
line = line.Trim();
// Consider data letters in threes
for (int index = 0; index <= line.Length - 3; index += 3)
{
---
string sequence = line.Substring(index, 3);
// Add to the count if amino acid found
if (sequence == amino_acid)
{
---
count++;
---
}
---
}
---
}
---
file.Close();
return count;
---
}
---
catch (FileNotFoundException)
{
---
return -1; // File not found
---
}
---
}
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
string amino_acid = get_amino_acid();
int number = check_sequence(amino_acid);
---
// If -1 is returned, the file does not exist
if (number == -1)
{
---
Console.WriteLine("DNA file not found.");
---
}
---
else
{
---
Console.WriteLine($"There are {number} {amino_acid} amino acids in the DNA sequence.");
---
}
---
}