Discord.net send DMs to a specific user? - c#

So i've been searching for all day for a method that sends to a specific user a private message. I'm making a ticket system and I need to print all tickets in DM to the person that executed the command.
[Command("createtickets")]
[Description("Creates tickets")]
public async Task createtickets(CommandContext ctx)
{
var interactivity = ctx.Client.GetInteractivity();
string filepath = #"C:\Users\Administrator\Desktop\test.txt";
List<string> lines = new List<string>();
lines = File.ReadAllLines(filepath).ToList();
foreach (string line in lines)
{
Console.WriteLine(line);
}
string[] chars = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "k", "z", "n", "u", "r", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
Random rnd = new Random();
for (int i=0; i< 9; i++)
{
string randomString = "";
for (int j = 0; j<12; j++)
{
randomString = randomString + chars[rnd.Next(0, 21)];
}
lines.Add(randomString);
}
File.WriteAllLines(filepath,lines);
await ctx.Channel.SendMessageAsync("10 Tickets were written");
var user = ctx.User;
As you can see there is a list with all the tickets and that is the thing i need to send via DM. I've tried searching on youtube, Discord.net Docs, DSharpplus Docs, every forum on the internet but they all list functions that were probably removed on the recent versions. Can anyone help me with this? I just need a way to DM the user

Related

Trying to figure out how to dynamically change which array is used for a label based on combo box selection?

So I'm unfortunately a bit of a super noob at the moment haha so please bear with me if possible.
The program that I'm attempting to make is a piano note randomizer for the sake of memorizing notes/scales efficiently. I thought it'd be a fun little program to aid in both my piano journey and programming journey haha.
So with the program, I've put a combo box that has an option for every piano scale which looks like this:
I then went ahead and made an array for each of the scales and the notes for that scale like so:
//Arrays to store each scale
string[] cMajor = { "C", "D", "E", "F", "G", "A", "B", "C", };
string[] dMajor = { "D", "E", "F#", "G", "A", "B", "C#", "D", };
string[] eMajor = { "E", "F#", "G#", "A", "B", "C#", "D#", "E", };
string[] fMajor = { "F", "G", "A", "Bb", "C", "D", "E", "F", };
string[] gMajor = { "G", "A", "B", "C", "D", "E", "F#", "G", };
I then used a random number generator to use as the index for the arrays so that it would randomly select a number between 0 and 7 which would ideally select an array index at random which we could then pass into a label which would be the piano note that is displayed on screen.
//Number Generator to pass into the newNumber variable which will act as an index for the array
Random generator = new Random();
int newNumber = generator.Next(0, 7); // Generating a number that equates to the number of notes in a particular scale
I then entered code to check the combobox for the selected input, which would then create the label based off of what scale is selected:
if (comboBox1.SelectedIndex == 0)
{
OUTPUTLABEL1.Font = new Font("Segoe UI", 100);
OUTPUTLABEL1.Location = new Point(80, 5);
OUTPUTLABEL1.Text = cMajor[newNumber];
}
Everything works as intended for the most part. I can select the scale and the note pops up on screen, the issue is that NONE of this is dynamic lol. The scale selection is hard-coded into the program as you can see. And the scale of cMajor is also hardcoded as well.
The idea is that I can select a scale from the ComboBox and the text will dynamically change based on what scale is selected in the ComboBox. The only way I can think of to acomplish this would create a ridiculous amount of redundant if statements and I feel like there's probably a better way I could accomplish this.
Thank you guys so much and once again I do apologize if this is obvious or any other mistake that I might've made. This is my first post here and I hope that I've provided enough information for you guys! Thank you again!
CODE IN IT'S ENTIRETY (Ignore the timer):
public void RandomizeButton_Click(object sender, EventArgs e)
{
//Arrays to store each scale
string[] cMajor = { "C", "D", "E", "F", "G", "A", "B", "C", };
string[] dMajor = { "D", "E", "F#", "G", "A", "B", "C#", "D", };
string[] eMajor = { "E", "F#", "G#", "A", "B", "C#", "D#", "E", };
string[] fMajor = { "F", "G", "A", "Bb", "C", "D", "E", "F", };
string[] gMajor = { "G", "A", "B", "C", "D", "E", "F#", "G", };
//Number Generator to pass into the newNumber variable which will act as an index for the array
Random generator = new Random();
int newNumber = generator.Next(0, 7); // Generating a number that equates to the number of notes in a particular scale
if (comboBox1.SelectedIndex == 0)
{
OUTPUTLABEL1.Font = new Font("Segoe UI", 100);
OUTPUTLABEL1.Location = new Point(80, 5);
OUTPUTLABEL1.Text = cMajor[newNumber];
}
if (TimerCheckbox.Checked)
{
seconds = int.Parse(timeInterval.Text);
timer1.Start();
}
}
If my way of thinking with arrays and combo boxes is less than ideal then I'm open to an entire new solution if needed! Thank you guys so much!
It looks like you want a linked data structure. The most common method to have a map between one variable and another is to use a Dictionary
//you can use a dictionary
public Dictionary<string, string[]> scalesDict = new Dictionary<string, string[]>() {
{"a", new string[] { "C", "D", "E", "F", "G", "A", "B", "C", } },
{"b", new string[] { "D", "E", "F#", "G", "A", "B", "C#", "D", } },
{"c", new string[] { "E", "F#", "G#", "A", "B", "C#", "D#", "E", } },
{"d", new string[] { "F", "G", "A", "Bb", "C", "D", "E", "F", } },
{"e", new string[] { "G", "A", "B", "C", "D", "E", "F#", "G", } } };
private string[] GetScale(string comboBoxValue) {
return scalesDict[comboBoxValue];
}
the above code would get you your array based on the combobox value (replace a,b,c,d, and e with whatever your combobox uses)
dictionaries are very powerful tools in c#
Your question was a bit vague though, so I'm not sure if I've fully covered your issue, let me know
Your question and code cover a few related areas:
Dynamically change which array (of notes) is used for the label based on combo box selection.
Use the Random class correctly to implement the Randomize function.
Automate the Randomize button with a timer.
Ok, you did say I'm open to an entire new solution if needed! so this answer will explore other ways to go about these three things and give you a couple new skills to try out. In particular, if you learn how to use Data Binding early on (as shown in this sample) it's likely to accelerate everything else you ever do in WinForms.
Binding the Scales to the ComboBox
One approach is to define a class that represents a Scale. This associates the information more tightly than a dictionary lookup would. The ToString method will determine what's shown in the combo box.
enum ScaleForm { Major, Minor }
enum Key { A, B, C, D, E, F, G }
enum Signature { Natural, [Description("\u266F")] Sharp, [Description("\u266D")] Flat }
class Scale
{
public Key Key{ get; set; }
public ScaleForm Form { get; set; }
public Signature Signature { get; set; }
public string[] Notes { get; set; } = new string[0];
// Determines what displays in the ComboBox
public override string ToString()=> $"{Key}{Signature.ToUnicode()} {Form}";
}
static class Extensions
{
public static string ToUnicode(this Signature signature)
{
switch (signature)
{
default: return string.Empty;
case Signature.Sharp: return "\u266F";
case Signature.Flat: return "\u266D";
}
}
}
Next make a list of Scale objects that will be the dynamic source of the combo box:
BindingList<Scale> Scales = new BindingList<Scale>();
Initialize the individual scales and the list of scales in the method that loads the main form.
public partial class MainForm : Form
{
public MainForm() => InitializeComponent();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Scales.Add(new Scale
{
Key = Key.C,
Notes = new[] { "C", "D", "E", "F", "G", "A", "B", "C", },
});
Scales.Add(new Scale
{
Key = Key.D,
Notes = new[]
{
"D", "E",
$"F{Signature.Sharp.ToUnicode()}",
"G", "A", "B",
$"C{Signature.Sharp.ToUnicode()}",
"D",
},
});
Scales.Add(new Scale
{
Key = Key.E,
Notes = new[]
{
"E",
$"F{Signature.Sharp.ToUnicode()}",
$"G{Signature.Sharp.ToUnicode()}",
"A", "B",
$"C{Signature.Sharp.ToUnicode()}",
$"D{Signature.Sharp.ToUnicode()}",
"E",
},
});
Scales.Add(new Scale
{
Key = Key.F,
Notes = new[]
{
"F", "G", "A",
$"B{Signature.Flat.ToUnicode()}",
"C", "D", "E", "F",
},
});
Scales.Add(new Scale
{
Key = Key.G,
Notes = new[]
{
"G", "A", "B", "C", "D", "E",
$"F{Signature.Sharp.ToUnicode()}",
"G",
},
});
Scales.Add(new Scale
{
Key = Key.B, Signature = Signature.Flat,
Notes = new[] {
$"B{Signature.Flat.ToUnicode()}",
"C", "D",
$"E{Signature.Flat.ToUnicode()}",
"F", "G", "A",
$"B{Signature.Flat.ToUnicode()}"},
});
comboBoxScales.TabStop= false;
comboBoxScales.DropDownStyle= ComboBoxStyle.DropDownList;
// Attach the list of scales
comboBoxScales.DataSource= Scales;
// Initialize the value
onScaleSelectionChanged(this, EventArgs.Empty);
// Respond to combo box changes
comboBoxScales.SelectedIndexChanged += onScaleSelectionChanged;
// Respond to click randomize
buttonRandomize.Click += onClickRandomize;
// Respond to automated timer checkbox changes
checkBoxTimer.CheckedChanged += onTimerCheckedChanged;
}
.
.
.
}
Also in the same method, the event handlers are attached. For example, when a new scale is chosen in the combo box, the first thing that happens is that the label is set to the root.
private void onScaleSelectionChanged(object? sender, EventArgs e)
{
labelCurrentNote.Text =
((Scale)comboBoxScales.SelectedItem).Notes[0];
}
Using the Random class
You only need one instance of Random. For testing, you can produce the same pseudorandom sequence of numbers every time by seeding it with an int, e.g. new Random(1). But as shown here, the seed is derived from the system clock for a different sequence every time you run it.
private readonly Random _rando = new Random();
private void onClickRandomize(object? sender, EventArgs e) =>
execNextRandom(sender, e);
One implementation would be to get a number between 0 and 7 inclusive, and use it to dereference an array value from the current selection in the combo box. This also makes a point of getting a new note on every click so the user feels confident when they click.
private void execNextRandom(object? sender, EventArgs e)
{
string preview;
do
{
// Randomize, but do not repeat because it makes
// it seem like the button doesn't work!
preview =
((Scale)comboBoxScales.SelectedItem)
.Notes[_rando.Next(0, 8)]; // Will never return 8!
} while (preview.Equals(labelCurrentNote.Text));
labelCurrentNote.Text = preview;
}
Automated timer
One of the easier ways to get a repeating function is to make an async handler for the timer checkbox. There's no need to start and stop a Timer or handle events this way, but it still offers the advantages of keeping your UI responsive by 'not' blocking the thread except for when the new random note is acquired.
private async void onTimerCheckedChanged(object? sender, EventArgs e)
{
while(checkBoxTimer.Checked)
{
execNextRandom(sender, e);
await Task.Delay(TimeSpan.FromSeconds((double)numericUpDownSeconds.Value));
}
}

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
}

How to call Parallel.ForEach with a multidimensional array

I'm having a little trouble figuring out how to call the Parallel.ForEach with a 2D array of strings:
string[,] board = new string[,]{
{"A", "B", "C", "D", "E" },
{"F", "G", "H", "I", "J"},
{"K", "L", "M", "N", "O"},
{"0", "1", "2", "3", "4"}};
Parallel.ForEach(board, row =>
{
for (int i = 0; i < row.Length; ++i)
{
// find all valid sequences
}
});
If I don't specify the type explicitly I get the following error:
The type arguments for method
'System.Threading.Tasks.Parallel.ForEach(System.Collections.Generic.IEnumerable,
System.Action)' cannot be
inferred from the usage. Try
specifying the type arguments
explicitly.
What's the proper way to specify the type arguments explicitly?
The problem for you is that 2-dimensional arrays do not implement IEnumerable<one-dimensional-array>. (It does implement IEnumerable, but it's an IEnumerable of strings that "flattens" the array.) You can do two things:
Change the string[,] to a jagged array-of-arrays, string[][].
Implement your own extension method that iterates over a two-dimensional array and turns it into an IEnumerable<one-dimensional-array>.
You should still be able to make this work with a multi-dimensional array, just using Parallel.For instead of Parallel.ForEach:
string[,] board = new string[,] {
{"A", "B", "C", "D", "E" },
{"F", "G", "H", "I", "J"},
{"K", "L", "M", "N", "O"},
{"0", "1", "2", "3", "4"}
};
int height = board.GetLength(0);
int width = board.GetLength(1);
Parallel.For(0, height, y =>
{
for (int x = 0; x < width; ++x)
{
string value = board[y, x];
// do whatever you need to do here
}
}
);

How to create Alphabetical list with letters?

Hi all I trying to implement alpha ordered list by columns
as shown on picture
But my algorithm is not clear and maybe someone could help me within
string[] letters = new string[] { "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", "Å", "Ä", "Ö", "0-9" };
int j = 0, s = 0, i = 1;
var fullServices = (from se in EntityBase.db.Services
orderby se.Name
select se).ToList();
int total = fullServices.Count;
var grouped = (from l in letters
select new ServiceInfo
{
Letter = l,
Services = EntityBase.db.Services.Where(se => se.Name.StartsWith(l)).ToList(),
Total = EntityBase.db.Services.Where(se => se.Name.StartsWith(l)).Count()
}).ToList();
Dictionary<int, List<ServiceInfo>> result = new Dictionary<int, List<ServiceInfo>>();
changecell:
List<ServiceInfo> item = new List<ServiceInfo>();
while (j < letters.Count())
{
letterchange:
List<Service> _services = new List<Service>();
while (s < total)
{
if ((s == (5 + (total % 5 > i ? 1 : 0)) * i))
{
item.Add(new ServiceInfo() { Letter = letters[j], Services = _services });
result.Add(i, item);
if (i == 6)
goto exit;
i++;
goto changecell;
}
//start render services
if (fullServices.ElementAt(s).Name.StartsWith(letters[j]))
{
_services.Add(fullServices.ElementAt(s));
s++;//increment service in list
}
else //letter switch
{
item.Add(new ServiceInfo() { Letter = letters[j], Services = _services });
j++;
goto letterchange;
}
}//end render services
}
exit:
return View(result);
In a result of my code I see missed letters X Y Z Å Ä Ö
and it's looks like this
Here comes code that renders dictionary
<% foreach (KeyValuePair<int, List<BL.Models.Infos.ServiceInfo>> col in Model)
{ %>
<ul class="col">
<% foreach (var item in col.Value)
{ %>
<% if (!item.Services.Any())
{%>
<li class="disabled">
<h1>
<%= item.Letter %></h1>
</li>
<%}
else
{ %>
<li>
<h1>
<%= item.Letter %>
</h1>
</li>
<% foreach (var service in item.Services)
{ %>
<li><%= service.Name %></li>
<%}
}
}%>
</ul>
<%} %>
Please help...
Well, you're certainly right that the code's not particularly clear :)
I don't really follow the main loop of your code, but here's a simpler starting point. Note that it won't group 0-9 properly (it only deals with 0 at the moment): I'm not sure the best way to approach that, to be honest. You might want to put that off until you get some entries which don't match any of the normal letters...
using System;
using System.Linq;
class Test
{
static void Main(string[] args)
{
ShowGroups();
}
private static readonly char[] Letters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ0".ToCharArray();
// This is taking the place of EntityBase.db.Services
// for the purposes of the test program
public static string[] Services = { "Blogger", "Delicious",
"Digg", "Ebay", "Facebook", "Feed", "Flickr",
"Friendfeed", "Friendster", "Furl", "Google",
"Gosupermodel", "Lastfm", "Linkedin", "Livejournal",
"Magnolia", "Mixx", "Myspace", "NetOnBuy", "Netvibes",
"Newsvine", "Picasa", "Pownce", "Reddit", "Stumbleupon",
"Technorati", "Twitter", "Vimeo", "Webshots",
"Wordpress" };
public static void ShowGroups()
{
var groupedByLetter =
from letter in Letters
join service in Services on letter equals service[0] into grouped
select new { Letter = letter, Services = grouped };
// Demo of how to access the groups
foreach (var entry in groupedByLetter)
{
Console.WriteLine("=== {0} ===", entry.Letter);
foreach (var service in entry.Services)
{
Console.WriteLine (" {0}", service);
}
Console.WriteLine();
}
}
}
I don't know how you intend to split the results into 5 equal columns though...
This might be way off - I've just done this in Notepad (don't have access to an IDE right now). It looks like you're trying to populate the Dictionary<int, List<ServiceInfo>> result instance and I'm assuming that your View method understands how to layout the results in columns.
Here goes:
string[] letters = new string[] { "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", "Å", "Ä", "Ö", "0-9" };
var result = new Dictionary<int, List<ServiceInfo>>();
foreach (var letter in letters)
{
var services = (from se in EntityBase.db.Services
where se.Name.StartsWith(letter)
orderby se.Name
select select new ServiceInfo
{
Letter = letter,
Services = EntityBase.db.Services.Where(se => se.Name.StartsWith(letter)).ToList(),
Total = EntityBase.db.Services.Where(se => se.Name.StartsWith(letter)).Count()
}).ToList();
result.Add(i, services);
}
If it's correct, it probably won't be the fastest implementation, but it's more readable.

Categories

Resources