StackOverflow with Random Name Generator in C# [duplicate] - c#

This question already has answers here:
Random name generator in c#
(8 answers)
Closed 1 year ago.
using System;
namespace npcnames
{
class Program
{
static string RandomVowel()
{
Random rand = new Random();
string[] Vowels = new string[5] { "a", "e", "i", "o", "u" };
int index = rand.Next(Vowels.Length);
string Vowel = Vowels[index];
return Vowel;
}
static string RandomConsonant()
{
Random rand = new Random();
string[] Consonants = new string[21] { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" };
int index = rand.Next(Consonants.Length);
string Consonant = Consonants[index];
return Consonant;
}
static string MaleName()
{
string malename = RandomConsonant() + RandomVowel() + RandomConsonant() + RandomVowel() + RandomConsonant();
return MaleName();
}
static string FemaleName()
{
string femalename = RandomVowel() + RandomConsonant() + RandomVowel() + RandomConsonant() + RandomVowel();
return FemaleName();
}
static void generateFemaleName(int from, int to, int step)
{
for (int a = from; a <= to; a = a + step)
{
Console.WriteLine("Female:");
Console.WriteLine(a + FemaleName());
}
}
static void generateMaleName(int from, int to, int step)
{
for (int b = from; b <= to; b = b + step)
{
Console.WriteLine("Male:");
Console.WriteLine(b + MaleName());
}
}
static void Main(string[] args)
{
generateFemaleName(1,10,1);
Console.WriteLine();
generateMaleName(1,10,1);
}
}
Hi guys I'm new to coding and everything, if anyone can help me how to resolve this issue I would really appreciate it. Problem is that in my code I keep getting stack overflow and I don't know how to prevent it to execute my program normally. The purpose of the program is to generate male and female names of the random selected vowels and consonants in the list of 10 for each.

The issue is here:
static string FemaleName()
{
string femalename = RandomVowel() + RandomConsonant() + RandomVowel() + RandomConsonant() + RandomVowel();
return FemaleName(); // <== bam!
}
Change:
return FemaleName();
to:
return femalename;
The short story is the method is continually calling itself. It's exactly the same for MaleName().
Also, while we are fixing things, Random initialization should be outside the method:
private static Random rand = new Random();
static string RandomConsonant()
{
string[] Consonants = new string[21] { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" };
int index = rand.Next(Consonants.Length);
string Consonant = Consonants[index];
return Consonant;
}

It will be fixed if you change
return MaleName();
to
return malename;
and the same for
return FemaleName();
to
return femalename;
It is the string that you want to return.

Related

Use method of a different class in my form

Good day,
So, I have a class that converts arabic numerals to roman numerals. I have a loop that executes, if the user selected "Arabisch"(arabic) in the dropdownlist.
If the user selected "römisch"(roman) I want to do the same loop but convert the numerals to roman. In following you can see the code of the both classes. Whats the best way to initialize the method of the "ArabicToRoman" class in my loop to convert the numbers to roman?
namespace Friedhof_Generator
{
class ArabicToRoman
{
private string[] Tausender =
{ "", "M", "MM", "MMM" };
private string[] Hunderter =
{ "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
private string[] Zehner =
{ "", "X", "XX", "XXX", "XL", "L", "LX" , "LXX", "LXXX", "XC" };
private string[] Einer =
{ "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
private string ArabicRoman(int arabic)
{
if (arabic >= 4000)
{
int tausend = arabic / 1000;
arabic %= 1000;
return "(" + ArabicRoman(tausend) + ")" +
ArabicRoman(arabic);
}
string result = "";
int num;
num = arabic / 1000;
result += Tausender[num];
arabic %= 1000;
num = arabic / 100;
result += Hunderter[num];
arabic %= 100;
num = arabic / 10;
result += Zehner[num];
arabic %= 10;
result += Einer[arabic];
return result;
}
}
}
Above the ArabicToRoman class. Below the class with the loops.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Friedhof_Generator
{
public partial class frm_Grabnummer : Form
{
public frm_Grabnummer()
{
InitializeComponent();
ddl_Feld.Items.Add("Arabisch");
ddl_Feld.Items.Add("Römisch");
ddl_Feld.Items.Add("Alphabetisch");
ddl_Reihe.Items.Add("Arabisch");
ddl_Reihe.Items.Add("Römisch");
ddl_Reihe.Items.Add("Alphabetisch");
ddl_Nr.Items.Add("Arabisch");
ddl_Nr.Items.Add("Römisch");
ddl_Nr.Items.Add("Alphabetisch");
}
private void ddl_Feld_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void btn_Start_Click(object sender, EventArgs e)
{
int FeldVon, FeldBis, ReiheVon, ReiheBis, NrVon, NrBis;
string Friedhof;
FeldVon = int.Parse(tb_Von_Feld.Text);
FeldBis = int.Parse(tb_Bis_Feld.Text);
ReiheVon = int.Parse(tb_Von_Reihe.Text);
ReiheBis = int.Parse(tb_Bis_Reihe.Text);
NrVon = int.Parse(tb_Von_Nr.Text);
NrBis = int.Parse(tb_Bis_Nr.Text);
Friedhof = tb_Friedhof.Text;
if (ddl_Feld.Text == "Arabisch")
{
for (int iFeld = FeldVon; iFeld <= FeldBis; iFeld++)
{
for (int iReihe = ReiheVon; iReihe <= ReiheBis; iReihe++)
{
for (int iNr = NrVon; iNr <= NrBis; iNr++)
{
lb_Ausgabe.Items.Add(string.Format("{0} - {1} - {2} - {3}", Friedhof, iFeld, iReihe, iNr));
}
}
}
}
else if(ddl_Feld.Text == "Römisch")
{
}
}
}
}
The ArabicToRoman class seems like a typical helper class, in days gone by you would just easily make it public and static
public static class ArabicToRoman
{
private static string[] Tausender = { "", "M", "MM", "MMM" };
private static string[] Hunderter = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
private static string[] Zehner = { "", "X", "XX", "XXX", "XL", "L", "LX" , "LXX", "LXXX", "XC" };
private static string[] Einer = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
public static string Convert(int arabic)
{
...
}
}
Usage
You can call this from anywhere that access to the assembly
ArabicToRoman.Convert(234);
You could also just make your classes and methods public
public class ArabicToRoman
{
private string[] Tausender = { "", "M", "MM", "MMM" };
private string[] Hunderter = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
private string[] Zehner = { "", "X", "XX", "XXX", "XL", "L", "LX" , "LXX", "LXXX", "XC" };
private string[] Einer = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
public string Convert(int arabic)
{
...
}
}
Usage
Once again you call this anywhere that can access the assembly it lives in
var converter = new ArabicToRoman();
converter.Convert(345);
However these days, there is a big push for Dependency Injection DI and Service architecture, You might make it a singleton Service and inject your converters. However this is debatable as well.
For your case and your level of understanding static classes or an instantiable class seem to be a good fit
Further Reading
static (C# Reference)
Use the static modifier to declare a static member, which belongs to
the type itself rather than to a specific object. The static modifier
can be used with classes, fields, methods, properties, operators,
events, and constructors, but it cannot be used with indexers,
finalizers, or types other than classes.

Moving specific elements after others but not to top or bottom

Assume I have an array of
{ "w", "w", "z", "a", "c", "r", "f", "d", "e", "c", "g", "f", "m", "z" }
and I have a rule that all "c" should be moved before "f".
The goal to to keep as closest order to the original as possible while following the rule(s).
A real world example is a an app which knows that some plugin "A" should be higher than some plugin "B" in a load list.
So the expected result would be:
Moved c before first f:
{ "w", "w", "z", "a", "c", "r", "c"<<, "f", "d", "e", "g", "f", "m", "z" }
or
Moved f after last c:
{ "w", "w", "z", "a", "c", "r", "d", "e", "c", >>"f", "g", "f", "m", "z" }
Is it possible to use LINQ or anything ready for such purpose?
I tried OrderBy(x => x == "c" ? 0 : x == "f" ? 1 : (int?)null) but it moves elements to the end instead. I want to sort only specific elements with each other but keep their position relatively to other elements when possible.
UPDATE
The solution should work for any inputs:
public static char GetLetter(Random rand)
{
string chars = "abcdefghijklmnopqrstuvwxyz";
int num = rand.Next(0, chars.Length - 1);
return chars[num];
}
public static IEnumerable<string> Sort(IEnumerable<string> enumerable, string first, string second)
{
???
}
static void Main(string[] args)
{
Random rand = new Random();
string[] input = Enumerable.Range(1, 10).Select(x => GetLetter(rand) + "").ToArray();
var result = Sort(Sort(input, "c", "f"), "m", "e");
}
Supporting multiple elements ("m", "c", "f") and multiple rules is desirable if it doesn't complicate things too much.
This method does what you want:
public static IEnumerable<string> Sort(this IEnumerable<string> enumerable, string first, string second)
{
return enumerable.TakeWhile(s => s != second)
.Concat(enumerable.SkipWhile(s => s != second).OrderBy(a => a != first));
}
The idea is that first the part of the list is taken until the first f (in this case). Then the remaining part is appended to it, sorted by the c characters first.
A drawback is that the list is enumerated twice (partly). If this is performance-critical you should go for an alternative that enumerates only once.
This is a solution:
string[] ch = {"a", "c", "f", "d", "e", "c", "g", "f", "m"};
var res = ch.Select((item, index) => new { item, index })
.OrderBy(x => x.item == "c" ? 0 : Convert.ToInt32(x.index))
.Select(c => c.item).ToArray();
The output:
If In understood your question correctly I think you need to have a array rules in which you will defined the rules like which character should come first. Here I am adding a test considering your seniors. You can modify the rules and use it.
[Test]
public void CustomOrderByTests()
{
char[] ch = { 'a', 'c', 'f', 'd', 'e', 'c', 'g', 'f', 'm' };
char[] rules = { 'a', 'f', 'c' };
var result = rules.Intersect(ch).Union(ch);
char[] expected = { 'a', 'f', 'c', 'd', 'e', 'g', 'm' };
CollectionAssert.AreEqual(result, expected);
}
If you want duplicate element at same position before use Array.Sort() overload it will short the result without removing the duplicates
Array.Sort(rules, ch);
now your ch array will have sorted array.

How to change string revision format in C#?

As for my requirement i want to change string revision format newly. Based on below format.
E.g:
______________
|Test-A |
| ... +
|Test-Z | ==>IF Test-Z then Next time i need to create Revision "AA"
| ... +
|Test-AA |
| ... +
|Test-AZ |==>IF Test-AZ then Next time i need to create Revision "BA"
| ... +
|Test-BA |
| ... +
|Test-BZ |==>IF Test-BZ then Next time i need to create Revision "CA"
| ... +
|Test-CA |
| ... +
|Test-CZ |==>IF Test-CZ then Next time i need to create Revision "DA"
+_____________+
E.g2 :
A, B ....Z & AA, BB....AZ then I need to change BA...BB, BC....BZ & CA, CB....CZ.
Please help me to solve this task.
I would think that you need two methods to do so:
1.: A String to Integer method which returns you a number where you can add 1.
2.: A method which converts back the number into a string.
I'm currently on my tablet, so here just some untested code:
private int StringToInt(string version)
{
int returnValue =0;
// Iterate through the given string char by char and convert it to an integer
// (A=65 in the ascii table, therefore we nned to substract it)
for(int i=0; i<version.Length;i++)
{
// The alphabet has 26 chars so we just think of your version
// as a base26 number and convert it to a base10 number.
retunValue+=Math.Pow(26,i)*((int)version[version.Length-1-i]-65);
}
return returnValue;
}
and
private string IntToSting(int value)
{
String returnValue=String.Empty;
while(value /26>0)
{
// % is the modulo operator. eg if value was 27, value%26 == 1
returnValue=((char)(value%26+65)).ToString()+returnValue;
value-=value%26;
}
returnValue=((char)(value%26+65)).ToString() +returnValue;
}
Note that these methods only work for capital letters and that they do not handle special cases as for example IntToString(0);
You can now simply use it by calling the methods and adding 1 to the result of StringToInteger before calling IntToString.
version = IntToString(StringToInteger(version)+1);
Sounds like homework:
public class RevisionNumber
{
private static readonly string[] _Representations = new[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
private int _Value;
public void Increment()
{
_Value++;
}
public override string ToString()
{
var result = String.Empty;
var value = _Value;
while (value >= _Representations.Length)
{
result = _Representations[(int)value % _Representations.Length] + result;
value /= _Representations.Length;
value--;
}
return _Representations[(int)value] + result;
}
}
Usage:
using System;
namespace ConsoleApplication
{
internal class Program
{
private static void Main(string[] args)
{
var revision = new RevisionNumber();
for (int i = 0; i < 100; i++)
{
Console.WriteLine(revision);
revision.Increment();
}
Console.ReadKey();
}
}
}
The corresponding fiddle is here.

Find strings in array using C#.Net [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I built an app of Numerology based on Name.
I'm beginner using C#.net and I have trouble of functions when I input name.
It's only 1 character that the program was detected.
For Example : Jane is not contained in Arrays but only J is contained in Arrays.
I really appreciate if someone gives another simple algorithms.
string[] Array0 = { " " };
string[] Array1 = { "A", "J", "S" };
string[] Array2 = { "B", "K", "T" };
string[] Array3 = { "C", "L", "U" };
string[] Array4 = { "D", "M", "V" };
string[] Array5 = { "E", "N", "W" };
string[] Array6 = { "F", "O", "X" };
string[] Array7 = { "G", "P", "Y" };
string[] Array8 = { "H", "Q", "Z" };
string[] Array9 = { "I", "R" };
string tempName = Name.ToUpper();
foreach (string x in Array1) {
if (x.Contains(tempName))
{
Response.Write("Your name is contained in Array");
}
else {
Response.Write("Your name is Not Contained in Array");
}
}
Try this:
List<string[]> _array = new List<string[]>() { new string[] {"A", "J", "S"}, new string[] { "B", "K", "T" },new string [] {"C", "L", "U"},
new string[] {"D", "M", "V"}, new string[] { "E", "N", "W" }, new string[] { "F", "O", "X" },
new string[] { "G", "P", "Y" },new string[] { "H", "Q", "Z" }, new string[] { "I", "R" }};
bool _result = _array.Select(a => a.Any(s => Name.ToUpper().Contains(s))).FirstOrDefault();
if(_result)
Response.Write("Your name is contained in Array");
else
Response.Write("Your name is Not Contained in Array");
This code will check for any occurance of Name in array list.

How to complete getting substrings of a genome encoding a given amino acid sequence

The Peptide Encoding Problem consists on Finding substrings of a genome encoding a given amino acid sequence.
The genetic code describes the translation of an RNA 3-mer (codon) into one of 20 different amino acids. The first three circles, moving from the inside out, represent the 1st, 2nd, and 3rd nucleotides of a given codon. The 4th, 5th, and 6th circles define the translated amino acid in three ways: the amino acid’s full name, its 3-letter abbreviation, and its single-letter abbreviation. Three of the 64 total RNA codons are stop codons, which halt translation and implicitly add a 21st stop symbol to the amino acid alphabet.
The problem consists on having
Input: A DNA string Text and an amino acid string Peptide.
and
Output: All substrings of Text encoding Peptide (if any such substrings exist).
Sample Input:
ATGGCCATGGCCCCCAGAACTGAGATCAATAGTACCCGTATTAACGGGTGA
MA
Sample Output:
ATGGCC
GGCCAT
ATGGCC
the result is gotten by reading triplets so, ATGGCC encodes MA, GGCCAT inverse would be CCGGTA written backwards so it is in fact ATGGCC (CCGGTA.reverse)
the complement of genetic code is C-G, G-C, T-A and A-T
So to solve this problem I first use already defined arrays
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
using System.Collections;
class Program
{
private static string[] CODONS = {
"TTT", "TTC", "TTA", "TTG", "TCT",
"TCC", "TCA", "TCG", "TAT", "TAC", "TGT", "TGC", "TGG", "CTT",
"CTC", "CTA", "CTG", "CCT", "CCC", "CCA", "CCG", "CAT", "CAC",
"CAA", "CAG", "CGT", "CGC", "CGA", "CGG", "ATT", "ATC", "ATA",
"ATG", "ACT", "ACC", "ACA", "ACG", "AAT", "AAC", "AAA", "AAG",
"AGT", "AGC", "AGA", "AGG", "GTT", "GTC", "GTA", "GTG", "GCT",
"GCC", "GCA", "GCG", "GAT", "GAC", "GAA", "GAG", "GGT", "GGC",
"GGA", "GGG", };
private static string[] AMINOS_PER_CODON = {
"F", "F", "L", "L", "S", "S",
"S", "S", "Y", "Y", "C", "C", "W", "L", "L", "L", "L", "P", "P",
"P", "P", "H", "H", "Q", "Q", "R", "R", "R", "R", "I", "I", "I",
"M", "T", "T", "T", "T", "N", "N", "K", "K", "S", "S", "R", "R",
"V", "V", "V", "V", "A", "A", "A", "A", "D", "D", "E", "E", "G",
"G", "G", "G", };
private static string[] AMINO_ABBREVIATIONS = {
"F", "L", "I", "M", "V",
"S", "P", "T", "A", "Y", "H", "Q", "N", "K", "D", "E", "C", "W",
"R", "G" };
private static string[] FULL_NAMES = {
"phenylalanine", "leucine",
"isoleucine", "methionine", "valine", "serine", "proline",
"threonine", "alanine", "tyrosine", "histidine", "glutamine",
"asparagine", "lysine", "aspartic acid", "glutamic acid",
"cysteine", "tryptophan", "arginine", "glycine" };
static void Main()
{
//then input is DNA
string dna = "TTCGAATCCGTATTGCTTTGGTTGGTTGAGAGAGCGACCTACATTGCTAGTCAGAATAGGTGATTCACACAAAGCTTAGCACCTGGGCAGCACCCCGTGATGTAAACCTATGGGAACTAAGGGAGTCCTGCGGTTTTAGCCAGCAAGCGAGCCGGCAGGAACACTCATACCATCGGACGCGTTTGACGCCTCCCCGGAAAGGAAGTATTTGAGCCTCATTATTACGTATTGCCCGTTAGTCGACAAATCAAGCCCTCGTACGCAGCTTATTCGTACGACGTGGAGGCGTTCCCACGGGCCTAACACGATTGGAACACCACCATAGTAGTGTGGTTCAAATACCTCCTTTGGAGATCTAGAGCTTCACTCTGATTCTAGAGGCAACTTTACAATCGCTCTACGAAATTGTATGGACATCATCAACCGGATATTCTGGGGCGGTAGAATTTCTTTTGTTCGAATCGCTCTAGGCCAGGATCAAATTAATTGAATTGCGGACTCAAGGATCGCGATAGCCGACACATCGGACGCTGTAGAAAGCCAGTCTCTGGATTTAATCCACCCTCTATGTTTGACAAAGCACTAAAACGGGATAGTTTCGGGTGGTATAAGTTTCCCAAGACGATTGCATCGCAATTCATCAACAACCATGAACTTACTGTTTTAGTACTTCCACACACCTTGTTAAATTACGCCTTTACTTCATGTTGCGGTGTGTGTTAGATAGTGTGCAGCTACAAGTCTACCGCCATCGCAGCTCGGGATACCGGCAGATGAGATGGTCCTGAGCTCGTACCGGACTCAAACTTTTTCCTTTACTACCTAGGAATCGCCCATGCGAATTTGTCGGACACACACCATTACATTAACGTCACAACAGCTACTGTTAGAATTTTGCTCTTGCAAATCCTGGAAAGAGTTAAAAAAACTCTTCCGCGCGCCAATAGGGTAAATAATAGATAGCCAGACGGCTGTAAGAGGTGATGACATTTGCAACAATCATGCTGTCGCATCTTCCGCAAGTTCATGTCGCGCCTAGGCAATGGATCTGCGAATGGGGGCCACGGGGTATGAACTACGGAATTCTAAGAAAGTTGCCATCCAGAGTTAAGGGTTTGAGGCTAGTTGCATCGCTGGTAACGAACTACCTCATTACTTGGACGCGCAGTGTGACTTCACTCCTGTATAGCGATGATGCCAAGCAGGAATTAGCAAATCTGAAGAGCGTTTCCAAACTGGCCACTTGGACTGACACCTATCGCGGGGGATTTCAGCGCGTGTCGCTCTCACATGAGAGCTGCCGTCAGGAGCGGTAGAGTTTAGAGAGGAATGCGACAAACTCCCTATTCACCTCTCTGGTGATGTAAGGATATTTACGCTTAGTTCTATGCCAGGCTTAGGGCCTCTCGGAACTTTGGTGAGTCCTTATTAATTGATGCTACCTCTCCCTTACCTTCGCCCCAAGTCACGTAGAAGTACTCAATCCTGCTACATGATAATCAAATATTTCCAACGTTGGGAAATCGGTGACATCACATACTAGTTAAGAAACCACTGTCAGTGAACTTATATCCGGGGGAGAAAATCTACTAACTTACATACGCTGTGCGAGCAGTTTTCATTATAAGAAAATATACTCCCGAGGTACCGCATCAAGCACGACATTCCCGGAGAGCATAACATTTCGGTGCACCTGCTTTTGTGCGCTTGCTTGCGGTTATTTATAAACTACGCACAAGGCGCAAACCGCAGTGCGCATGTTTTCTCCGCCTGGCTAGAACTCGACATTCTCGTCAACGCCAATCTATGTGAGAGGATTTAGACCTCTGTGAAAACGAGTCCCTCTATAGAATAAATACCCAGATGCCAATGGGGGTTCTATCCGATGGCAGTGCATGGAGTGGTGGCTCCAGATTAAGGATGAGGAGAGGTAAAGATAACAGTTCGGTCGCCACGACGCGTTGCCAATCGAAATATCAGTACTAAAAGGCCCACCGCTCCGCTTTAGTCCGACTTTACATCCTGTGGAAATTGTCGAACGGAGGCTACATCGGGCTATATGAGTGTGAAAACCTATACTTCTCGCGTCGTTACTCAGTGCCGGTCTCCTGTTTCCCCCAGTCTTACGTACCCTTATTGATATTTGCTTCACGTTGAAACGTCCTAACGCAGCGTAAAGAGGTGTTTGAACCTCATTACTATAAAATCGCGATCGAAGGTAGACTACATACGCAAACGCCGAAACCCTCAGTTGGCCTTGTTGCAAGTATGGAACGTTGTAAAATTTTTCCTAGACGTTGAGCTATCGGTACAAGGTCGTTAGCGTCCTTACCCTTCACTTATATGCCCGACAAAACGCGGGTCCTAGTGCAGTGGTGGGAGCTTGGAATCCCGCAATACAAGGACAACCTGTATCTCGTTCGGCGTTCCGCGATCACTCGATCCCGAACCACTCCAAGCCTGGTTGATCAGCAAAAGCGGAAGGATGGATAAAGGGCTACTGGTTAATGGATGTAAACTTCCAATGATGAAATCCTGGAAACGAGGGATCGGGTTACGGTGGCGAACGGGGTACGGCAACGTGGCTATCTAGAGCCCGACGTTACGACTCATGTACATGCTGCTACGTGGTTGAAGCTGACGTTCAGATGAAGCAGTACTGAGTCCTAGGGCTTACTACTACTCCAATAGGTCTGGCCGGCCAGATACAAAAGTTCGTGGCGGCTCACCCCCTTTCTGGCGGGTGTAGCTTGCTGACCGGTTTGCTCGATAACACAGGCTAGCGAATAGTAATGAGGTTCGAAAACCTCTTTCCAACGACTGAAAGGGTCTACACGAACTATCTACATTTCCCCGCCCATGTCCTTCCGTCTGGTTGCTTCTGGAGATCCTTTCGCATTATACCGCAGCGTAGTGGCTCTGGCATATATGAAAAAATCCTTCTGTGGGTATTTGTGCCATCACTTATTGTTCGTACCGATATGGGATTACAAGTGCGATGTGATAATAAGCGAAGAAGCCAACATGTTACACTGTTCATGCGCTCCGGGTAATGTGCGGGCACCATGCTCAGTTCCCGCTCGCAGTTGTCACTGTCCCTGTTTCGGCACCATAATCAACATTTCCACGGCCACGCTGGTGAATAACCGAGGATACCGAAGTACAGCAAGAATGAGAGCGGGACTCCTCCATCTGCTTGTAATACGCCTTCAAGATAGTCCATAAAACGGTCGGGGTCTTGTTTCGGACTAGCCGCTTTGAAACGGTGCATAGTTGTGTCAAGTGTGGACATTGGCTTTCTATCCTCGTCAGCGATCCTCGGAAAGACTCGGGCAGTCGCCCCGAATCGTAATTAGGTAGTAGTGCGGCTCAAAAACTTCCTTCGACCTAACCGCTATAATGTTCGTAGATATAAATTTCGTTTCAGTATTAACAGGCGCACCGTATATATACGGAATGGTGTCGCCCCATTAGCTGCTCGCCAATATTTATCTAAGACCGCGCGCGTCTAGCGCCTTTAGTAGTTGCACCCGAGTATAGTAATGGGGTTCGAAGACTTCCTTCGCAAGGCTGCCATACTGTATCACAAGTACTGACGGAGCCCCGGAGGAGTGCAGGATACGGCAAAGGAGACCATTACCGGGGCATGAGTCCAAGTTAGCCCGTTAGGTGAAGGACGCTGATACAATAGTGAATCCGTTACTGAAAGGTTTAGAAGACCGGGGGGCTCGCACTAGGTCCAAATATTATGAACCCTACTCCTGCAACTGAATTGGCCGTCCAGGCGATATTTAAAAGGGGTTACTAGCAGGTTCATCGGAGCCCGTACTCCTTCCGGGCATAGTCGTTCGACGGGTAGAAATTCATCCAGTCGTGCCGGATACCCCGAGAATACCCCTATTTTTTGATCCTTCACCATCATCGTCCGCGGACTCATCTAAGTACCTCAGACCGAAACTGTTATCGTAGCGAAGAGCGAACTCGAATGACATCGCTTGTCCAACAGGGAAAATATGTAAAGTATATGCAGATTATTATAGGAAGATCACAAACTCCATCGCGCCTAGGCCAAAGACTTGCCAGAACAACATCTCTTCCAGAGCAAGGAAGTGTTTGAACCTCACTATTATCGAGAGAAGTCCCATGAATTTATAATAGTGAGGCTCAAAAACTTCCTTCATCGTCGGGCGCTGGGGCGAGCTAGGCTTCCCTAGCCGTCATTACTGTACCCACGCCAAATATTATCGAGTATGACTACGAAGCCTTCACAAGGGGCTGCAGTAACAGACTAACTCACGCACACCGCAACTACTATCCTAAATTGAGTAAGAAAACCCTCGACTATAGCCCTTGAATTAAATTCGCTATTTAAGGAAGACCGCGCTTCCGCTCGCCCGCGTATAGTTTACCAGTCCCAAGAAACATGTGTGGCCAGCCTACCTGAAGAGCTGTGTAAAGATAGATTCTGACATCCTCAAAAAGAAGTTTTCGAGCCGCACTACTACGCACGGAGCTCCGTTATTCAAGGCATGTCGAAGTACAGCGTGGTGGCCTCTCCTGTTCTCCACCCCAGCTAAACCCACCTTGTTCGAATTCGCGCAACTGTATATGACATGAACACTTACAGGGGTTAGAAGTTACTGCAACCAAGATAGAGCTCGTCGAAGTAATAGTGCGGTTCAAAAACTTCCTTCAATTGGTCTCATCACTTAAATTTAAGAGCTATTGTGAGTACAGGTACGGATGCGGCTTCAGTGGATCTTCAGCATTCATTCCTTGTAGTAATGGGGTTCGAAGACTTCCTTGCCAGGGTACCAAACAAGTCTTGCGCATCCTCCTCCCTAAGGAGGTATTTGAACCCCACTATTACCCACGATAGAACATGCAGGGTTTGATAGTGGAACACCTTTTAGAATCTGGGGATAAATTCCCAGGACTAATGTATGGCTGTAGTAATGAGGTTCAAAAACCTCCTTTTCAGGTGGATCGCAGGCCGTGCTGCCTCACAAGCTGGGACGCCGTCCACGGTATAGCCGGCGTCGGCAGTTACTGTGAAATAGCGGAAACTCGATCCCAATATATCATCTTACGTTTGGCGCCCAATAGTCGCCCAGTACCCGTTGACAGTTCTTTAACTCGGCTTAGAACTACTAGACAGGTTCAACCGAACCTTGCCCTAGTTCCCACTCCCGTAATTCATTTGGGTTTGCATCTAGAATACTGGAGGGTGCATAGACGAAACGTGTACGTCGGAGAAAACGTAAGAAAATGACCTAGACTCATAGTAGTGAGGTTCGAAGACTTCCTTTCAGTGAAATCGATCCACCACTCGCCGCGAAGAGATAATAGCATAGAGCACAAGTGCGCGAGTAGAGAAAAAGGCTATCCCAACCGGGCACGTCCTTCGTGTTTGGCGTTTACATACGGCACCCCGTTTCTGCACGTTAACCGTCTAGTATCCAACGGTGGATGGCGGACGCTAGACTATAGATATGAGATATCGAGACCTGGAGCTGGGTGTGGCTGCAGCCCGGGTCATTGCGGGCTGTGAATTCAAGGGCATGTAAACAAGCGTATATCGAACAGTGGATGGGCACCTGCAATACTCACGGTAGAGTTAGCTCACAGGATTCACGTTGAGGACTATGAGTCCCTCTTCGCTAGCAGTCTGGGGGGATATGGAGTTTAATAGCTTGACGTAGTAATGGGGCTCAAACACCTCTTTGTGTGAGCACAGCTACTTGCATTAAGAGATTCTAAACAGCGATCATCTCGGCTATTTCGGGCCAGCCTTTTCGGCAGGATGTTATGTAGCATTTCTGGAAGCTTCCCCCTCGAATCTACTAGTGGTGAGAAGATGCCCCACCGATATTACTCTTTAATCTTGAGAAACCTAAAACCGATCTGACCTCAGACGGGCGGCTCCCACCCGAGGATAAACTCGTCAATAATAATGTGGCTCGAACACTTCTTTTCTCACTAGGCTTTTACGACACGCCACATGTATTTAAGCATCTACCTAACTTGTGTCTGCTGATATACAGCGCATTCTACGCCCAACCTACCAATTACTTCAACGTAGTGCGTGGCTAAAATTCAGGGGAGCTTCATCTCTGTCTTAATTTGAAGGTTCTTCCGGGGCGTTTGGGAATCTTCGTGCCTTTTGCGAGGTTAAGGTATCAAAGAAGTTTTCGAACCACATTATTACCGCCTTAAGCACCGGCGCATCCTGCTCGTGACAACTCTACCCTGCCCTGATAAAGGCACTGAACGTTCCAGAGAGTGCATCATTGACACGCGAGCAGGCCACAGTAGCCACAAGACGTATGGGTGATTATAGAATTGGTGGAGGTGTTGTTAACGATCAGGAGGACATTAGTGGGAGTTAGGAAAGACCCTATGTTCTCTCTATCGCGGACTTGTAACTTGACAAGCAAAAGGGTAAGAGAGCTGCACACCGAAGCAGGCCCTTCCTATACCTGTTTTTCCTACGCGTAGAGAGGAATCCAGAAAGGTGATAATTGGCATTCGATGAAAAAACAGTGTGCCACTGACTTAGTTCTATATGTGAAGAGCCTGTTAGCACGTGACGGCGGCCTTGGTATAGAGCCCTTAATGGTCTCCATCGCGTAGTAATGGGGCTCGAAAACCTCCTTACTTGGGATTGCGTGGCCTCCTTGTGAGTCATACACAAGGCTTAGGGCTATGGGGCGATACACTCCTTTTCGCGGCGCATGGGGCGGTGATGCCTACATAGTAGTAGTGACTGCCTTTCTGGGGGGCTATTTGTGGATGACCAACACCTGACCAGCGATGCAATCGCTAGGGGAGGTACACCTCTCATATGTTACAACAATCACCGAATTGTGTTTCGAATTCGAATCAAGTTTGCGGTGTCGACCAGATCTGGTCTTGCTGCCATACCGGGTTCGCCGCCTCCGGTGGATAGAACTGCATCTTAAGACATCTGGACCCAGCGGTAAGTAGCGGGAAGAGTTTAGAGTCATTCGTACAACTACAGGCTAAGGGCTTACTGGGGAGTTGTTGTAGGGCATAAAGATCGCCCCATGACTTTTCGTACTTTCCCCGATAGTTCACTCGCAGCGAGCTGCGGCTGGGCTTCGCCACACGAGTACGGGCAACATTTATCTCCTCTAATCACTGGGCACCGCGCGAGGAAATAGAAAACCCTAATCAGTGCTCATGGGCGCATCTATTGGTCTCCGCATGCACGATGCCGCGGAGTGCTTAGTTGTCCCTGCATAATCTTCGTAGATGTATAAGAGATTACCTATTTATTCGGTTTCGGTTCTAGACGTACCTTGCCGCATGAGTATAGGCTAATGAACTGAGTTGGCGCCAGAGGGAAAGGCATAATAATGCGGCTCGAATACTTCCTTAAGGAAGTATTCGAACCACATTACTAT";
//The given peptide
string givenPeptide = "KEVFEPHYY";
char[] givenPeptideArray = givenPeptide.ToCharArray() ;
string codon="";
string auxCodon="";
string convertedCodon = "";
string clean = "";
int pivot = 0;
int foundFlag = 0;
string cleanReverse = "";
int foundFlagReverse = 0;
string convertedCodonReverse = "";
//so the idea I was working was to loop dna string until getting to 3*givenPeptide.Length
//because dna is red 3 by 3...
//then get givenPeptide.Length and compare each char of given peptide with encoded
//letter of scanned triplet
//so for example first givenpeptide is 'M' so I would search for 'ATG'
//then, if found increment a counter and then search for next 'GCC' etc...
//however When testing with longer string I do not get complete result
//I have
for (int pos = 0; pos < dna.Length - (3*givenPeptide.Length) ; pos++) {
pivot = pos;
for (int codonPos = 0; codonPos < givenPeptide.Length; codonPos++)
{
codon = dna.Substring(pivot, 3);
auxCodon = givenPeptideArray[codonPos].ToString();
convertedCodon = codonToAminoAcid(codon);
convertedCodonReverse = codonToAminoAcid(DNAComplement(codon));
if (auxCodon.Equals(convertedCodon) )
{
foundFlag++;
clean += codon;
}
else
{
foundFlag = 0;
clean = "";
}
if (foundFlag == givenPeptide.Length)
{
Console.WriteLine(clean);
foundFlag = 0;
clean = "";
}
//reverse
if (auxCodon.Equals(convertedCodonReverse))
{
foundFlagReverse++;
cleanReverse += codon;
}
else
{
foundFlagReverse = 0;
cleanReverse = "";
}
if (foundFlagReverse == givenPeptide.Length)
{
Console.WriteLine(cleanReverse);
foundFlagReverse = 0;
cleanReverse = "";
}
pivot+=3;
}
}
}//end main
public static string DNAComplement(string dna)
{
char[] array = dna.ToCharArray();
for (int i = 0; i < array.Length; i++)
{
char let = array[i];
if (let == 'A') array[i] = 'T';
else if (let == 'T') array[i] = 'A';
else if (let == 'C') array[i] = 'G';
else if (let == 'G') array[i] = 'C';
}
Array.Reverse(array);
return new string(array);
}
public static string codonToAminoAcid(String codon)
{
for (int k = 0; k < CODONS.Length ; k++)
{
if (CODONS[k].Equals (codon))
{
return AMINOS_PER_CODON[k];
}
}
// never reach here with valid codon
return "X";
}
}//end class
I should get
AAGGAAGTATTTGAGCCTCATTATTAC
AAAGAGGTGTTTGAACCTCATTACTAT
AAGGAGGTATTTGAACCCCACTATTAC
AAAGAAGTTTTCGAACCACATTATTAC
AAGGAAGTGTTTGAACCTCACTATTAT
AAAGAAGTTTTCGAGCCGCACTACTAC
AAGGAAGTATTCGAACCACATTACTAT
ATAATAATGCGGCTCGAATACTTCCTT
GTAGTAATGGGGCTCGAAAACCTCCTT
GTAGTAATGAGGTTCAAAAACCTCCTT
GTAGTAATGGGGTTCGAAGACTTCCTT
ATAATAGTGAGGCTCAAAAACTTCCTT
ATAGTAATGGGGTTCGAAGACTTCCTT
GTAGTAGTGCGGCTCAAAAACTTCCTT
ATAGTAATGAGGTTCGAAAACCTCTTT
ATAATAATGTGGCTCGAACACTTCTTT
GTAGTAATGGGGCTCAAACACCTCTTT
ATAGTAGTGAGGTTCGAAGACTTCCTT
GTAATAGTGCGGTTCAAAAACTTCCTT
ATAGTAGTGTGGTTCAAATACCTCCTT
However I am only getting:
AAGGAAGTATTTGAGCCTCATTATTAC
AAAGAGGTGTTTGAACCTCATTACTAT
AAGGAAGTGTTTGAACCTCACTATTAT
AAAGAAGTTTTCGAGCCGCACTACTAC
AAGGAGGTATTTGAACCCCACTATTAC
AAAGAAGTTTTCGAACCACATTATTAC
I was thinking on problem and It seems the problem is I am not looping all string each time...
It looks like the problem is two-fold. Your outer for loop doesn't go far enough to catch the very end of the dna string. Secondly, it appears that reversing individual codons doesn't work and that you have to take the entire (givenPeptideLength * 3) character string before getting the complement. The following changes output the strings you put in your example so I'm hopeful that it does what you want. It has the benefit of being much more concise as well.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
using System.Collections;
namespace DNA
{
class Program
{
private static string[] CODONS = {
"TTT", "TTC", "TTA", "TTG", "TCT",
"TCC", "TCA", "TCG", "TAT", "TAC", "TGT", "TGC", "TGG", "CTT",
"CTC", "CTA", "CTG", "CCT", "CCC", "CCA", "CCG", "CAT", "CAC",
"CAA", "CAG", "CGT", "CGC", "CGA", "CGG", "ATT", "ATC", "ATA",
"ATG", "ACT", "ACC", "ACA", "ACG", "AAT", "AAC", "AAA", "AAG",
"AGT", "AGC", "AGA", "AGG", "GTT", "GTC", "GTA", "GTG", "GCT",
"GCC", "GCA", "GCG", "GAT", "GAC", "GAA", "GAG", "GGT", "GGC",
"GGA", "GGG", };
private static string[] AMINOS_PER_CODON = {
"F", "F", "L", "L", "S", "S",
"S", "S", "Y", "Y", "C", "C", "W", "L", "L", "L", "L", "P", "P",
"P", "P", "H", "H", "Q", "Q", "R", "R", "R", "R", "I", "I", "I",
"M", "T", "T", "T", "T", "N", "N", "K", "K", "S", "S", "R", "R",
"V", "V", "V", "V", "A", "A", "A", "A", "D", "D", "E", "E", "G",
"G", "G", "G", };
private static string[] AMINO_ABBREVIATIONS = {
"F", "L", "I", "M", "V",
"S", "P", "T", "A", "Y", "H", "Q", "N", "K", "D", "E", "C", "W",
"R", "G" };
private static string[] FULL_NAMES = {
"phenylalanine", "leucine",
"isoleucine", "methionine", "valine", "serine", "proline",
"threonine", "alanine", "tyrosine", "histidine", "glutamine",
"asparagine", "lysine", "aspartic acid", "glutamic acid",
"cysteine", "tryptophan", "arginine", "glycine" };
public static string DNAComplement(string dna)
{
char[] array = dna.ToCharArray();
for (int i = 0; i < array.Length; i++)
{
char let = array[i];
if (let == 'A') array[i] = 'T';
else if (let == 'T') array[i] = 'A';
else if (let == 'C') array[i] = 'G';
else if (let == 'G') array[i] = 'C';
}
Array.Reverse(array);
return new string(array);
}
public static string StringToAminoAcid(String input)
{
string result = "";
for (int i = 0; i < input.Length; i += 3)
{
result += codonToAminoAcid(input.Substring(i, 3));
}
return result;
}
public static string codonToAminoAcid(String codon)
{
for (int k = 0; k < CODONS.Length; k++)
{
if (CODONS[k].Equals(codon))
{
return AMINOS_PER_CODON[k];
}
}
// never reach here with valid codon
return "X";
}
static void Main()
{
//then input is DNA
string dna = "TTCGAATCCGTATTGCTTTGGTTGGTTGAGAGAGCGACCTACATTGCTAGTCAGAATAGGTGATTCACACAAAGCTTAGCACCTGGGCAGCACCCCGTGATGTAAACCTATGGGAACTAAGGGAGTCCTGCGGTTTTAGCCAGCAAGCGAGCCGGCAGGAACACTCATACCATCGGACGCGTTTGACGCCTCCCCGGAAAGGAAGTATTTGAGCCTCATTATTACGTATTGCCCGTTAGTCGACAAATCAAGCCCTCGTACGCAGCTTATTCGTACGACGTGGAGGCGTTCCCACGGGCCTAACACGATTGGAACACCACCATAGTAGTGTGGTTCAAATACCTCCTTTGGAGATCTAGAGCTTCACTCTGATTCTAGAGGCAACTTTACAATCGCTCTACGAAATTGTATGGACATCATCAACCGGATATTCTGGGGCGGTAGAATTTCTTTTGTTCGAATCGCTCTAGGCCAGGATCAAATTAATTGAATTGCGGACTCAAGGATCGCGATAGCCGACACATCGGACGCTGTAGAAAGCCAGTCTCTGGATTTAATCCACCCTCTATGTTTGACAAAGCACTAAAACGGGATAGTTTCGGGTGGTATAAGTTTCCCAAGACGATTGCATCGCAATTCATCAACAACCATGAACTTACTGTTTTAGTACTTCCACACACCTTGTTAAATTACGCCTTTACTTCATGTTGCGGTGTGTGTTAGATAGTGTGCAGCTACAAGTCTACCGCCATCGCAGCTCGGGATACCGGCAGATGAGATGGTCCTGAGCTCGTACCGGACTCAAACTTTTTCCTTTACTACCTAGGAATCGCCCATGCGAATTTGTCGGACACACACCATTACATTAACGTCACAACAGCTACTGTTAGAATTTTGCTCTTGCAAATCCTGGAAAGAGTTAAAAAAACTCTTCCGCGCGCCAATAGGGTAAATAATAGATAGCCAGACGGCTGTAAGAGGTGATGACATTTGCAACAATCATGCTGTCGCATCTTCCGCAAGTTCATGTCGCGCCTAGGCAATGGATCTGCGAATGGGGGCCACGGGGTATGAACTACGGAATTCTAAGAAAGTTGCCATCCAGAGTTAAGGGTTTGAGGCTAGTTGCATCGCTGGTAACGAACTACCTCATTACTTGGACGCGCAGTGTGACTTCACTCCTGTATAGCGATGATGCCAAGCAGGAATTAGCAAATCTGAAGAGCGTTTCCAAACTGGCCACTTGGACTGACACCTATCGCGGGGGATTTCAGCGCGTGTCGCTCTCACATGAGAGCTGCCGTCAGGAGCGGTAGAGTTTAGAGAGGAATGCGACAAACTCCCTATTCACCTCTCTGGTGATGTAAGGATATTTACGCTTAGTTCTATGCCAGGCTTAGGGCCTCTCGGAACTTTGGTGAGTCCTTATTAATTGATGCTACCTCTCCCTTACCTTCGCCCCAAGTCACGTAGAAGTACTCAATCCTGCTACATGATAATCAAATATTTCCAACGTTGGGAAATCGGTGACATCACATACTAGTTAAGAAACCACTGTCAGTGAACTTATATCCGGGGGAGAAAATCTACTAACTTACATACGCTGTGCGAGCAGTTTTCATTATAAGAAAATATACTCCCGAGGTACCGCATCAAGCACGACATTCCCGGAGAGCATAACATTTCGGTGCACCTGCTTTTGTGCGCTTGCTTGCGGTTATTTATAAACTACGCACAAGGCGCAAACCGCAGTGCGCATGTTTTCTCCGCCTGGCTAGAACTCGACATTCTCGTCAACGCCAATCTATGTGAGAGGATTTAGACCTCTGTGAAAACGAGTCCCTCTATAGAATAAATACCCAGATGCCAATGGGGGTTCTATCCGATGGCAGTGCATGGAGTGGTGGCTCCAGATTAAGGATGAGGAGAGGTAAAGATAACAGTTCGGTCGCCACGACGCGTTGCCAATCGAAATATCAGTACTAAAAGGCCCACCGCTCCGCTTTAGTCCGACTTTACATCCTGTGGAAATTGTCGAACGGAGGCTACATCGGGCTATATGAGTGTGAAAACCTATACTTCTCGCGTCGTTACTCAGTGCCGGTCTCCTGTTTCCCCCAGTCTTACGTACCCTTATTGATATTTGCTTCACGTTGAAACGTCCTAACGCAGCGTAAAGAGGTGTTTGAACCTCATTACTATAAAATCGCGATCGAAGGTAGACTACATACGCAAACGCCGAAACCCTCAGTTGGCCTTGTTGCAAGTATGGAACGTTGTAAAATTTTTCCTAGACGTTGAGCTATCGGTACAAGGTCGTTAGCGTCCTTACCCTTCACTTATATGCCCGACAAAACGCGGGTCCTAGTGCAGTGGTGGGAGCTTGGAATCCCGCAATACAAGGACAACCTGTATCTCGTTCGGCGTTCCGCGATCACTCGATCCCGAACCACTCCAAGCCTGGTTGATCAGCAAAAGCGGAAGGATGGATAAAGGGCTACTGGTTAATGGATGTAAACTTCCAATGATGAAATCCTGGAAACGAGGGATCGGGTTACGGTGGCGAACGGGGTACGGCAACGTGGCTATCTAGAGCCCGACGTTACGACTCATGTACATGCTGCTACGTGGTTGAAGCTGACGTTCAGATGAAGCAGTACTGAGTCCTAGGGCTTACTACTACTCCAATAGGTCTGGCCGGCCAGATACAAAAGTTCGTGGCGGCTCACCCCCTTTCTGGCGGGTGTAGCTTGCTGACCGGTTTGCTCGATAACACAGGCTAGCGAATAGTAATGAGGTTCGAAAACCTCTTTCCAACGACTGAAAGGGTCTACACGAACTATCTACATTTCCCCGCCCATGTCCTTCCGTCTGGTTGCTTCTGGAGATCCTTTCGCATTATACCGCAGCGTAGTGGCTCTGGCATATATGAAAAAATCCTTCTGTGGGTATTTGTGCCATCACTTATTGTTCGTACCGATATGGGATTACAAGTGCGATGTGATAATAAGCGAAGAAGCCAACATGTTACACTGTTCATGCGCTCCGGGTAATGTGCGGGCACCATGCTCAGTTCCCGCTCGCAGTTGTCACTGTCCCTGTTTCGGCACCATAATCAACATTTCCACGGCCACGCTGGTGAATAACCGAGGATACCGAAGTACAGCAAGAATGAGAGCGGGACTCCTCCATCTGCTTGTAATACGCCTTCAAGATAGTCCATAAAACGGTCGGGGTCTTGTTTCGGACTAGCCGCTTTGAAACGGTGCATAGTTGTGTCAAGTGTGGACATTGGCTTTCTATCCTCGTCAGCGATCCTCGGAAAGACTCGGGCAGTCGCCCCGAATCGTAATTAGGTAGTAGTGCGGCTCAAAAACTTCCTTCGACCTAACCGCTATAATGTTCGTAGATATAAATTTCGTTTCAGTATTAACAGGCGCACCGTATATATACGGAATGGTGTCGCCCCATTAGCTGCTCGCCAATATTTATCTAAGACCGCGCGCGTCTAGCGCCTTTAGTAGTTGCACCCGAGTATAGTAATGGGGTTCGAAGACTTCCTTCGCAAGGCTGCCATACTGTATCACAAGTACTGACGGAGCCCCGGAGGAGTGCAGGATACGGCAAAGGAGACCATTACCGGGGCATGAGTCCAAGTTAGCCCGTTAGGTGAAGGACGCTGATACAATAGTGAATCCGTTACTGAAAGGTTTAGAAGACCGGGGGGCTCGCACTAGGTCCAAATATTATGAACCCTACTCCTGCAACTGAATTGGCCGTCCAGGCGATATTTAAAAGGGGTTACTAGCAGGTTCATCGGAGCCCGTACTCCTTCCGGGCATAGTCGTTCGACGGGTAGAAATTCATCCAGTCGTGCCGGATACCCCGAGAATACCCCTATTTTTTGATCCTTCACCATCATCGTCCGCGGACTCATCTAAGTACCTCAGACCGAAACTGTTATCGTAGCGAAGAGCGAACTCGAATGACATCGCTTGTCCAACAGGGAAAATATGTAAAGTATATGCAGATTATTATAGGAAGATCACAAACTCCATCGCGCCTAGGCCAAAGACTTGCCAGAACAACATCTCTTCCAGAGCAAGGAAGTGTTTGAACCTCACTATTATCGAGAGAAGTCCCATGAATTTATAATAGTGAGGCTCAAAAACTTCCTTCATCGTCGGGCGCTGGGGCGAGCTAGGCTTCCCTAGCCGTCATTACTGTACCCACGCCAAATATTATCGAGTATGACTACGAAGCCTTCACAAGGGGCTGCAGTAACAGACTAACTCACGCACACCGCAACTACTATCCTAAATTGAGTAAGAAAACCCTCGACTATAGCCCTTGAATTAAATTCGCTATTTAAGGAAGACCGCGCTTCCGCTCGCCCGCGTATAGTTTACCAGTCCCAAGAAACATGTGTGGCCAGCCTACCTGAAGAGCTGTGTAAAGATAGATTCTGACATCCTCAAAAAGAAGTTTTCGAGCCGCACTACTACGCACGGAGCTCCGTTATTCAAGGCATGTCGAAGTACAGCGTGGTGGCCTCTCCTGTTCTCCACCCCAGCTAAACCCACCTTGTTCGAATTCGCGCAACTGTATATGACATGAACACTTACAGGGGTTAGAAGTTACTGCAACCAAGATAGAGCTCGTCGAAGTAATAGTGCGGTTCAAAAACTTCCTTCAATTGGTCTCATCACTTAAATTTAAGAGCTATTGTGAGTACAGGTACGGATGCGGCTTCAGTGGATCTTCAGCATTCATTCCTTGTAGTAATGGGGTTCGAAGACTTCCTTGCCAGGGTACCAAACAAGTCTTGCGCATCCTCCTCCCTAAGGAGGTATTTGAACCCCACTATTACCCACGATAGAACATGCAGGGTTTGATAGTGGAACACCTTTTAGAATCTGGGGATAAATTCCCAGGACTAATGTATGGCTGTAGTAATGAGGTTCAAAAACCTCCTTTTCAGGTGGATCGCAGGCCGTGCTGCCTCACAAGCTGGGACGCCGTCCACGGTATAGCCGGCGTCGGCAGTTACTGTGAAATAGCGGAAACTCGATCCCAATATATCATCTTACGTTTGGCGCCCAATAGTCGCCCAGTACCCGTTGACAGTTCTTTAACTCGGCTTAGAACTACTAGACAGGTTCAACCGAACCTTGCCCTAGTTCCCACTCCCGTAATTCATTTGGGTTTGCATCTAGAATACTGGAGGGTGCATAGACGAAACGTGTACGTCGGAGAAAACGTAAGAAAATGACCTAGACTCATAGTAGTGAGGTTCGAAGACTTCCTTTCAGTGAAATCGATCCACCACTCGCCGCGAAGAGATAATAGCATAGAGCACAAGTGCGCGAGTAGAGAAAAAGGCTATCCCAACCGGGCACGTCCTTCGTGTTTGGCGTTTACATACGGCACCCCGTTTCTGCACGTTAACCGTCTAGTATCCAACGGTGGATGGCGGACGCTAGACTATAGATATGAGATATCGAGACCTGGAGCTGGGTGTGGCTGCAGCCCGGGTCATTGCGGGCTGTGAATTCAAGGGCATGTAAACAAGCGTATATCGAACAGTGGATGGGCACCTGCAATACTCACGGTAGAGTTAGCTCACAGGATTCACGTTGAGGACTATGAGTCCCTCTTCGCTAGCAGTCTGGGGGGATATGGAGTTTAATAGCTTGACGTAGTAATGGGGCTCAAACACCTCTTTGTGTGAGCACAGCTACTTGCATTAAGAGATTCTAAACAGCGATCATCTCGGCTATTTCGGGCCAGCCTTTTCGGCAGGATGTTATGTAGCATTTCTGGAAGCTTCCCCCTCGAATCTACTAGTGGTGAGAAGATGCCCCACCGATATTACTCTTTAATCTTGAGAAACCTAAAACCGATCTGACCTCAGACGGGCGGCTCCCACCCGAGGATAAACTCGTCAATAATAATGTGGCTCGAACACTTCTTTTCTCACTAGGCTTTTACGACACGCCACATGTATTTAAGCATCTACCTAACTTGTGTCTGCTGATATACAGCGCATTCTACGCCCAACCTACCAATTACTTCAACGTAGTGCGTGGCTAAAATTCAGGGGAGCTTCATCTCTGTCTTAATTTGAAGGTTCTTCCGGGGCGTTTGGGAATCTTCGTGCCTTTTGCGAGGTTAAGGTATCAAAGAAGTTTTCGAACCACATTATTACCGCCTTAAGCACCGGCGCATCCTGCTCGTGACAACTCTACCCTGCCCTGATAAAGGCACTGAACGTTCCAGAGAGTGCATCATTGACACGCGAGCAGGCCACAGTAGCCACAAGACGTATGGGTGATTATAGAATTGGTGGAGGTGTTGTTAACGATCAGGAGGACATTAGTGGGAGTTAGGAAAGACCCTATGTTCTCTCTATCGCGGACTTGTAACTTGACAAGCAAAAGGGTAAGAGAGCTGCACACCGAAGCAGGCCCTTCCTATACCTGTTTTTCCTACGCGTAGAGAGGAATCCAGAAAGGTGATAATTGGCATTCGATGAAAAAACAGTGTGCCACTGACTTAGTTCTATATGTGAAGAGCCTGTTAGCACGTGACGGCGGCCTTGGTATAGAGCCCTTAATGGTCTCCATCGCGTAGTAATGGGGCTCGAAAACCTCCTTACTTGGGATTGCGTGGCCTCCTTGTGAGTCATACACAAGGCTTAGGGCTATGGGGCGATACACTCCTTTTCGCGGCGCATGGGGCGGTGATGCCTACATAGTAGTAGTGACTGCCTTTCTGGGGGGCTATTTGTGGATGACCAACACCTGACCAGCGATGCAATCGCTAGGGGAGGTACACCTCTCATATGTTACAACAATCACCGAATTGTGTTTCGAATTCGAATCAAGTTTGCGGTGTCGACCAGATCTGGTCTTGCTGCCATACCGGGTTCGCCGCCTCCGGTGGATAGAACTGCATCTTAAGACATCTGGACCCAGCGGTAAGTAGCGGGAAGAGTTTAGAGTCATTCGTACAACTACAGGCTAAGGGCTTACTGGGGAGTTGTTGTAGGGCATAAAGATCGCCCCATGACTTTTCGTACTTTCCCCGATAGTTCACTCGCAGCGAGCTGCGGCTGGGCTTCGCCACACGAGTACGGGCAACATTTATCTCCTCTAATCACTGGGCACCGCGCGAGGAAATAGAAAACCCTAATCAGTGCTCATGGGCGCATCTATTGGTCTCCGCATGCACGATGCCGCGGAGTGCTTAGTTGTCCCTGCATAATCTTCGTAGATGTATAAGAGATTACCTATTTATTCGGTTTCGGTTCTAGACGTACCTTGCCGCATGAGTATAGGCTAATGAACTGAGTTGGCGCCAGAGGGAAAGGCATAATAATGCGGCTCGAATACTTCCTTAAGGAAGTATTCGAACCACATTACTAT";
//The given peptide
string givenPeptide = "KEVFEPHYY";
int resultCount = 0;
int candidateLength = givenPeptide.Length * 3;
string forward = "";
string backward = "";
for (int pos = 0; pos < dna.Length - candidateLength + 1; pos++) // Added the "+ 1"
{
forward = dna.Substring(pos, candidateLength);
backward = DNAComplement(forward);
// Unremark to get a glimpse into what is happening...
//Console.WriteLine("pos: {0}, forward: {1}, backward: {2}, forwardTranslation: {3}, backwardTranslation: {4}",
// pos, forward, backward, StringToAminoAcid(forward), StringToAminoAcid(backward));
if (
(StringToAminoAcid(forward) == givenPeptide)
||
(StringToAminoAcid(backward) == givenPeptide)
)
{
resultCount++;
Console.WriteLine(String.Format("Result {0,3} at position {1,6}... {2}", resultCount, pos, forward));
}
}
Console.WriteLine("Done");
Console.ReadLine();
}//end main
}//end class
}

Categories

Resources