What regular expression need for match nested expression? - c#

I'm triying to make a regex in .net to match nested expresion.
I wan't to extract a condition and value1 and value2 like this:
CASE WHEN condition THEN value1 ELSE value2 END
The string that i would like to match it's this:
CASE WHEN 5 + CASE WHEN 6 < 3 THEN 5 ELSE 2 END = 0 THEN 0 ELSE CASE WHEN 6 < 0 THEN 2 ELSE 1 END / (3 + CASE WHEN 5 < 0 THEN 2 ELSE 0 END) END
Formatted string:
CASE WHEN 5 +
CASE WHEN 6 < 3
THEN 5
ELSE 2
END = 0
THEN 0
ELSE
CASE WHEN 6 < 0
THEN 2
ELSE 1
END / (3 + CASE WHEN 5 < 0
THEN 2
ELSE 0
END)
END
I use a below Regex string, but only match without nested expresion:
(CASE WHEN )(?<cond>[\s\S]*?)( THEN )(?<val1>[\s\S]*?)( ELSE )(?<val2>[\s\S]*?)( END)
What regular expression need to extract the first "CASE WHEN" as below?
Condition: 5 + CASE WHEN 6 < 3 THEN 5 ELSE 2 END = 0
Value1: 0
Value2: CASE WHEN 6 < 0 THEN 2 ELSE 1 END / (3 + CASE WHEN 5 < 0 THEN 2 ELSE 0 END)

Related

C# store and display calculated iteration in one line using messagebox.show

I am working on this program. My purpose is to Store the result of calculated input data in one int[] variable and display it in one line using messagebox.show.
int[] data = new int[] { 65, 66, 67, 32, 100, 90 }; // I declare int[] data it contain my data that I want to work with the length change.
int[] array = new int[6]; // i declare a table length of 6
foreach (var b in data) // for every element in my data I want to do this operations and build my array.
{
array[0] = b / 200;
array[1] = b / 79;
array[2] = b / 27;
array[3] = b / 19;
array[4] = b / 21;
array[5] = b / 3;
Console.WriteLine("{0}", string.Join(" ", array)); // this line is for console application
// output of this line is :
/*
0 0 2 3 3 21
0 0 2 3 3 22
0 0 2 3 3 22
0 0 1 1 1 10
0 1 3 5 4 33
0 1 3 4 4 30 */
MessageBox.Show(" "+ string.Join(" ", array)); // this line is for windowsform application
My purpose is in windowsform application to display my variable using messagebox.show. I aim the calculated to store them in one variable and to display them like this one :
0 0 2 3 3 21 0 0 2 3 3 22 0 0 2 3 3 22 0 0 1 1 1 10 0 1 3 5 4 33 0 1 3 4 4 30
I really appreciate any help.
kind regards
You can simply join the string in loop and then display them outside of the loop in your message box. Use StringBuilder class for appending results.
StringBuilder sb = new StringBuilder();
for(...)
{
...
...
sb.AppendFormat("{0} ", string.Join(" ", array).Trim())
}
MessageBox.Show(sb.ToString());

Sequence Alignment Algorithm with a group of characters instead of one character

Summary:
I'm beginning with some details about alignment algorithms, and at the end, I ask my question. If you know about alignment algorithm pass the beginning.
Consider we have two strings like:
ACCGAATCGA
ACCGGTATTAAC
There is some algorithms like: Smith-Waterman Or Needleman–Wunsch, that align this two sequence and create a matrix. take a look at the result in the following section:
Smith-Waterman Matrix
§ § A C C G A A T C G A
§ 0 0 0 0 0 0 0 0 0 0 0
A 0 4 0 0 0 4 4 0 0 0 4
C 0 0 13 9 4 0 4 3 9 4 0
C 0 0 9 22 17 12 7 3 12 7 4
G 0 0 4 17 28 23 18 13 8 18 13
G 0 0 0 12 23 28 23 18 13 14 18
T 0 0 0 7 18 23 28 28 23 18 14
A 0 4 0 2 13 22 27 28 28 23 22
T 0 0 3 0 8 17 22 32 27 26 23
T 0 0 0 2 3 12 17 27 31 26 26
A 0 4 0 0 2 7 16 22 27 31 30
A 0 4 4 0 0 6 11 17 22 27 35
C 0 0 13 13 8 3 6 12 26 22 30
Optimal Alignments
A C C G A - A T C G A
A C C G G A A T T A A
Question:
My question is simple, but maybe the answer is not easy as it looks. I want to use a group of character as a single one like: [A0][C0][A1][B1]. But in these algorithms, we have to use individual characters. How can we achieve that?
P.S. Consider we have this sequence: #read #write #add #write. Then I convert this to something like that: #read to A .... #write to B.... #add to C. Then my sequence become to: ABCB. But I have a lot of different words that start with #. And the ASCII table is not enough to convert all of them. Then I need more characters. the only way is to use something like [A0] ... [Z9] for each word. OR to use numbers.
P.S: some sample code for Smith-Waterman is exist in this link
P.S: there is another post that want something like that, but what I want is different. In this question, we have a group of character that begins with a [ and ends with ]. And no need to use semantic like ee is equal to i.
I adapted this Python implementation (GPL version 3 licensed) of both the Smith-Waterman and the Needleman-Wunsch algorithms to support sequences with multiple character groups:
#This software is a free software. Thus, it is licensed under GNU General Public License.
#Python implementation to Smith-Waterman Algorithm for Homework 1 of Bioinformatics class.
#Forrest Bao, Sept. 26 <http://fsbao.net> <forrest.bao aT gmail.com>
# zeros() was origianlly from NumPy.
# This version is implemented by alevchuk 2011-04-10
def zeros(shape):
retval = []
for x in range(shape[0]):
retval.append([])
for y in range(shape[1]):
retval[-1].append(0)
return retval
match_award = 10
mismatch_penalty = -5
gap_penalty = -5 # both for opening and extanding
gap = '----' # should be as long as your group of characters
space = ' ' # should be as long as your group of characters
def match_score(alpha, beta):
if alpha == beta:
return match_award
elif alpha == gap or beta == gap:
return gap_penalty
else:
return mismatch_penalty
def finalize(align1, align2):
align1 = align1[::-1] #reverse sequence 1
align2 = align2[::-1] #reverse sequence 2
i,j = 0,0
#calcuate identity, score and aligned sequeces
symbol = []
found = 0
score = 0
identity = 0
for i in range(0,len(align1)):
# if two AAs are the same, then output the letter
if align1[i] == align2[i]:
symbol.append(align1[i])
identity = identity + 1
score += match_score(align1[i], align2[i])
# if they are not identical and none of them is gap
elif align1[i] != align2[i] and align1[i] != gap and align2[i] != gap:
score += match_score(align1[i], align2[i])
symbol.append(space)
found = 0
#if one of them is a gap, output a space
elif align1[i] == gap or align2[i] == gap:
symbol.append(space)
score += gap_penalty
identity = float(identity) / len(align1) * 100
print 'Identity =', "%3.3f" % identity, 'percent'
print 'Score =', score
print ''.join(align1)
# print ''.join(symbol)
print ''.join(align2)
def needle(seq1, seq2):
m, n = len(seq1), len(seq2) # length of two sequences
# Generate DP table and traceback path pointer matrix
score = zeros((m+1, n+1)) # the DP table
# Calculate DP table
for i in range(0, m + 1):
score[i][0] = gap_penalty * i
for j in range(0, n + 1):
score[0][j] = gap_penalty * j
for i in range(1, m + 1):
for j in range(1, n + 1):
match = score[i - 1][j - 1] + match_score(seq1[i-1], seq2[j-1])
delete = score[i - 1][j] + gap_penalty
insert = score[i][j - 1] + gap_penalty
score[i][j] = max(match, delete, insert)
# Traceback and compute the alignment
align1, align2 = [], []
i,j = m,n # start from the bottom right cell
while i > 0 and j > 0: # end toching the top or the left edge
score_current = score[i][j]
score_diagonal = score[i-1][j-1]
score_up = score[i][j-1]
score_left = score[i-1][j]
if score_current == score_diagonal + match_score(seq1[i-1], seq2[j-1]):
align1.append(seq1[i-1])
align2.append(seq2[j-1])
i -= 1
j -= 1
elif score_current == score_left + gap_penalty:
align1.append(seq1[i-1])
align2.append(gap)
i -= 1
elif score_current == score_up + gap_penalty:
align1.append(gap)
align2.append(seq2[j-1])
j -= 1
# Finish tracing up to the top left cell
while i > 0:
align1.append(seq1[i-1])
align2.append(gap)
i -= 1
while j > 0:
align1.append(gap)
align2.append(seq2[j-1])
j -= 1
finalize(align1, align2)
def water(seq1, seq2):
m, n = len(seq1), len(seq2) # length of two sequences
# Generate DP table and traceback path pointer matrix
score = zeros((m+1, n+1)) # the DP table
pointer = zeros((m+1, n+1)) # to store the traceback path
max_score = 0 # initial maximum score in DP table
# Calculate DP table and mark pointers
for i in range(1, m + 1):
for j in range(1, n + 1):
score_diagonal = score[i-1][j-1] + match_score(seq1[i-1], seq2[j-1])
score_up = score[i][j-1] + gap_penalty
score_left = score[i-1][j] + gap_penalty
score[i][j] = max(0,score_left, score_up, score_diagonal)
if score[i][j] == 0:
pointer[i][j] = 0 # 0 means end of the path
if score[i][j] == score_left:
pointer[i][j] = 1 # 1 means trace up
if score[i][j] == score_up:
pointer[i][j] = 2 # 2 means trace left
if score[i][j] == score_diagonal:
pointer[i][j] = 3 # 3 means trace diagonal
if score[i][j] >= max_score:
max_i = i
max_j = j
max_score = score[i][j];
align1, align2 = [], [] # initial sequences
i,j = max_i,max_j # indices of path starting point
#traceback, follow pointers
while pointer[i][j] != 0:
if pointer[i][j] == 3:
align1.append(seq1[i-1])
align2.append(seq2[j-1])
i -= 1
j -= 1
elif pointer[i][j] == 2:
align1.append(gap)
align2.append(seq2[j-1])
j -= 1
elif pointer[i][j] == 1:
align1.append(seq1[i-1])
align2.append(gap)
i -= 1
finalize(align1, align2)
If we run this with the following input:
seq1 = ['[A0]', '[C0]', '[A1]', '[B1]']
seq2 = ['[A0]', '[A1]', '[B1]', '[C1]']
print "Needleman-Wunsch"
needle(seq1, seq2)
print
print "Smith-Waterman"
water(seq1, seq2)
We get this output:
Needleman-Wunsch
Identity = 60.000 percent
Score = 20
[A0][C0][A1][B1]----
[A0]----[A1][B1][C1]
Smith-Waterman
Identity = 75.000 percent
Score = 25
[A0][C0][A1][B1]
[A0]----[A1][B1]
For the specific changes I made, see: this GitHub repository.
Imagine we have a log file with alphabetic sequences. Like something you said, I converted sequences to A0A1... . For example, if there was a sequence like #read #write #add #write, it converted to A0A1A2A1. Every time, I read two character and compare them but keep score matrix like before. Here is my code in C# for smith-waterman string alignment.
Notice that Cell is a user defined class.
private void alignment()
{
string strSeq1;
string strSeq2;
string strTemp1;
string strTemp2;
scoreMatrix = new int[Log.Length, Log.Length];
// Lists That Holds Alignments
List<char> SeqAlign1 = new List<char>();
List<char> SeqAlign2 = new List<char>();
for (int i = 0; i<Log.Length; i++ )
{
for (int j=i+1 ; j<Log.Length; j++)
{
strSeq1 = "--" + logFile.Sequence(i);
strSeq2 = "--" + logFile.Sequence(j);
//prepare Matrix for Computing optimal alignment
Cell[,] Matrix = DynamicProgramming.Intialization_Step(strSeq1, strSeq2, intSim, intNonsim, intGap);
// Trace back matrix from end cell that contains max score
DynamicProgramming.Traceback_Step(Matrix, strSeq1, strSeq2, SeqAlign1, SeqAlign2);
this.scoreMatrix[i, j] = DynamicProgramming.intMaxScore;
strTemp1 = Reverse(string.Join("", SeqAlign1));
strTemp2 = Reverse(string.Join("", SeqAlign2));
}
}
}
class DynamicProgramming
{
public static Cell[,] Intialization_Step(string Seq1, string Seq2,int Sim,int NonSimilar,int Gap)
{
int M = Seq1.Length / 2 ;//Length+1//-AAA //Changed: /2
int N = Seq2.Length / 2 ;//Length+1//-AAA
Cell[,] Matrix = new Cell[N, M];
//Intialize the first Row With Gap Penality Equal To Zero
for (int i = 0; i < Matrix.GetLength(1); i++)
{
Matrix[0, i] = new Cell(0, i, 0);
}
//Intialize the first Column With Gap Penality Equal To Zero
for (int i = 0; i < Matrix.GetLength(0); i++)
{
Matrix[i, 0] = new Cell(i, 0, 0);
}
// Fill Matrix with each cell has a value result from method Get_Max
for (int j = 1; j < Matrix.GetLength(0); j++)
{
for (int i = 1; i < Matrix.GetLength(1); i++)
{
Matrix[j, i] = Get_Max(i, j, Seq1, Seq2, Matrix,Sim,NonSimilar,Gap);
}
}
return Matrix;
}
public static Cell Get_Max(int i, int j, string Seq1, string Seq2, Cell[,] Matrix,int Similar,int NonSimilar,int GapPenality)
{
Cell Temp = new Cell();
int intDiagonal_score;
int intUp_Score;
int intLeft_Score;
int Gap = GapPenality;
//string temp1, temp2;
//temp1 = Seq1[i*2].ToString() + Seq1[i*2 + 1]; temp2 = Seq2[j*2] + Seq2[j*2 + 1].ToString();
if ((Seq1[i * 2] + Seq1[i * 2 + 1]) == (Seq2[j * 2] + Seq2[j * 2 + 1])) //Changed: +
{
intDiagonal_score = Matrix[j - 1, i - 1].CellScore + Similar;
}
else
{
intDiagonal_score = Matrix[j - 1, i - 1].CellScore + NonSimilar;
}
//Calculate gap score
intUp_Score = Matrix[j - 1, i].CellScore + GapPenality;
intLeft_Score = Matrix[j, i - 1].CellScore + GapPenality;
if (intDiagonal_score<=0 && intUp_Score<=0 && intLeft_Score <= 0)
{
return Temp = new Cell(j, i, 0);
}
if (intDiagonal_score >= intUp_Score)
{
if (intDiagonal_score>= intLeft_Score)
{
Temp = new Cell(j, i, intDiagonal_score, Matrix[j - 1, i - 1], Cell.PrevcellType.Diagonal);
}
else
{
Temp = new Cell(j, i, intDiagonal_score, Matrix[j , i - 1], Cell.PrevcellType.Left);
}
}
else
{
if (intUp_Score >= intLeft_Score)
{
Temp = new Cell(j, i, intDiagonal_score, Matrix[j - 1, i], Cell.PrevcellType.Above);
}
else
{
Temp = new Cell(j, i, intDiagonal_score, Matrix[j , i - 1], Cell.PrevcellType.Left);
}
}
if (MaxScore.CellScore <= Temp.CellScore)
{
MaxScore = Temp;
}
return Temp;
}
public static void Traceback_Step(Cell[,] Matrix, string Sq1, string Sq2, List<char> Seq1, List<char> Seq2)
{
intMaxScore = MaxScore.CellScore;
while (MaxScore.CellPointer != null)
{
if (MaxScore.Type == Cell.PrevcellType.Diagonal)
{
Seq1.Add(Sq1[MaxScore.CellColumn * 2 + 1]); //Changed: All of the following lines with *2 and +1
Seq1.Add(Sq1[MaxScore.CellColumn * 2]);
Seq2.Add(Sq2[MaxScore.CellRow * 2 + 1]);
Seq2.Add(Sq2[MaxScore.CellRow * 2]);
}
if (MaxScore.Type == Cell.PrevcellType.Left)
{
Seq1.Add(Sq1[MaxScore.CellColumn * 2 + 1]);
Seq1.Add(Sq1[MaxScore.CellColumn * 2]);
Seq2.Add('-');
}
if (MaxScore.Type == Cell.PrevcellType.Above)
{
Seq1.Add('-');
Seq2.Add(Sq2[MaxScore.CellRow * 2 + 1]);
Seq2.Add(Sq2[MaxScore.CellRow * 2]);
}
MaxScore = MaxScore.CellPointer;
}
}
}

Cycle a decimal digit by 1 in c#

I'd like to cycle a decimal digit by 1 using a single operation of possible.
if x is 0 - 8 then x = x + 1
if x is 9 then x = 0
or the opposite
if x is 1 - 9 then x = x - 1
if x is 0 then x = 9
Is there a way to do this with C# in one line of code? If not C# are there any other languages that could?
What if I wanted to cycle it by more than one (2 or 3 or whatever)?
I think what you're looking for is
var y = (x + 1) % 10;
This gives:
x y
------
0 1
1 2
...
8 9
9 0
To decrement
var y = (i + 9) % 10;
Obviously to change the amount, just change 1 or 9 respectively
int Cycle(int x)
{
return x+1 % 10;
}
int result = Cycle(0); // result is 1
int result = Cycle(8); // result is 9
int result = Cycle(9); // result is 0
int Cycle(int x, int by = 1)
{
return (x+by) % 10;
}
now you can call Cycle(9, 3) which should give you 2

How to implement if statement within the specific row and column of text file?

I have text file like
Time amplitude index
1 0,25 25
2 0,68 28
3 -2,5 69
4 2,5 94
5 3 45
6 5 26
What in need to get is this.
Time amplitude index classified
1 0,25 25 1
2 0,68 28 1
3 -2,5 69 2
4 2,5 94 1
5 3 45 1
6 5 26 1
It need to write additional column (classified) with statement 1 (column classified) we classifies that amplitude is increasing during the time, with statement 2 (column classified) we classifies that amplitude is decreasing during the time. I have a big text file and it need to be written in new text file. I was try with code like this:
private void button3_Click(object sender, EventArgs e)
{
StreamReader dat = new StreamReader(textBox1.Text, System.Text.Encoding.Default);
StreamWriter sw = new StreamWriter(textBox2.Text, true);
{
while (!dat.EndOfStream)
{
string mainLine1 = dat.ReadLine();
string[] originalLine1 = mainLine1.Split(new string[] { "\t" }, StringSplitOptions.None);
for (int i = 1; i < mainLine1.Length; i++)
{
if (originalLine1[i] > originalLine1[i-1])
{
sw.WriteLine(originalLine1[0] + "\t" + originalLine1[1] + "\t"+"1");
}
}
}
sw.Close();
}
I know that the code is not right but that is what I figured it.
Solve the problem. If Someone need it.
Example tex file:
1
2
3
2
1
-1
-2
-3
-2
-1
1
2
3
Need to get additional column and write tex file:
1 1
2 1
3 1
2 2
1 2
-1 3
-2 3
-3 3
-2 4
-1 4
1 1
2 1
3 1
Code
string niz;
int j=0;
string[] vrstica, vrstica_prej=null;
while (!dat.EndOfStream)
{
niz = dat.ReadLine();
vrstica = niz.Split('\t');
j++;
if (j < 2)
{
sw.WriteLine(vrstica[0] + "\t" + vrstica[1] + "\t" + "0");
}
else
{
double a = Convert.ToDouble(vrstica[0]);
double a_pre = Convert.ToDouble(vrstica_prej[0]);
if (a > a_pre && a > 0)
{sw.WriteLine(vrstica[0] + "\t" + "1"); }
else if (a < a_pre && a > 0)
{sw.WriteLine(vrstica[0] + "\t" + "2"); }
else if (a < a_pre && a < 0)
{sw.WriteLine(vrstica[0] +"\t" + "3"); }
else if (a > a_pre && a < 0)
{sw.WriteLine(vrstica[0] + "\t" + "4"); }
}
vrstica_prej = vrstica;
}
}
sw.Close();
I hope someone will be glad for it.

Algorithm to represent a sequence of numbers

I have a sequence of numbers to generate, and I want to generate it using some sort of algorithm (iterative or recursive, doesn't matter).
Contextualizing: This numbers are indexes to iterative over a list of lists. I need to do a permutation (combination, i don't know exactly), but I need to generate all combinations of all positions of that list of lists.
The sequence and the output I am trying to get is:
1 1
2 1
3 1
4 1
5 1
1 2
2 1
3 1
4 1
5 1
1 3
2 1
3 1
4 1
5 1
1 4
2 1
3 1
4 1
5 1
1 5
2 1
3 1
4 1
5 1
1 1
2 2
3 1
4 1
5 1
1 2
2 2
3 1
4 1
5 1
1 3
2 2
3 1
4 1
5 1
1 4
2 2
3 1
4 1
5 1
1 5
2 2
3 1
4 1
5 1
1 1
2 3
3 1
4 1
5 1
1 2
2 3
3 1
4 1
5 1
1 3
2 3
3 1
4 1
5 1
1 4
2 3
3 1
4 1
5 1
1 5
2 3
3 1
4 1
5 1
1 1
2 4
3 1
4 1
5 1
and so on... the last state is:
1 5
2 5
3 5
4 5
5 5
Note that at each line break is a step of iteration or recursion. The algorithm must be generic. This code that i wrote can help, but it isn't what I want. :(
List<List<int>> lstDays = new List<List<int>>
{
new List<int>{1,2,3,4,5}, //day 18
new List<int>{1,2,3,4,5}, //day 19
new List<int>{1,2,3,4,5}, //day 22
new List<int>{1,2,3,4,5}, //day 23
new List<int>{1,2,3,4,5}, //day 24
};
for(int i=0;i<lstDays.Count;i++)
{
for(int j=0;j<lstDays[i].Count;j++)
{
for(int k=0;k<lstDays.Count;k++)
{
Console.Write(k+1);
//Console.Write(j+1);
Console.Write('\n');
}
Console.Write('\n');
}
}
I hope that you can help me ! (:
You can do it like this:
int[] second = new[] {0,0,0,0,0};
bool finish = false;
while (true) {
for (int i = 0 ; i != 5 ; i++) {
Console.WriteLine("{0} {1}", i+1, second[i]+1);
}
Console.WriteLine();
int p = 0;
do {
second[p]++;
if (second[p] == 5) {
second[p] = 0;
p++;
} else {
break;
}
} while (p != 5);
if (p == 5) break;
}
The sequence of the second digits is stored in the array "creatively" named second. The do/while loop "increments" this array as if it were a base-5 number stored as five separate digits.
Here is a demo on ideone.
Based on comments below by the venerable Eric Lippert, edits for the OPs original intent:
public void OutputSequence(int length){
Recurse(length-1, Enumerable.Range(1, length).ToArray(), new int[length]);
}
public void Recurse(int position, int[] arr, int[] state){
if (position == -1){
PrintState(state);
return;
}
for (int i = 0; i < arr.Length; i++)
{
state[position] = arr[i];
Recurse(position-1, arr, state);
}
}
public void PrintState(int[] state){
for (int i = 0; i < state.Length; i++)
Console.WriteLine ("{0} {1}",i+1, state[i]);
Console.WriteLine ();
}
OutputSequence(5); will give the output the OP originally asked for.
Old Answer
What you're looking for is called a Cartesian Product. LINQ is your friend:
var pairs = from i in Enumerable.Range(1, 5)
from j in Enumerable.Range(1, 5)
select new {i, j};
foreach(var p in pairs)
Console.WriteLine ("{0} {1}", p.i, p.j);
EDIT: Just for fun, here's a way to do N-Ary cartesian products.
public IEnumerable<IEnumerable<int>> NAryCartesianProduct(int upper, int times){
if (times == 0)
return Enumerable.Empty<IEnumerable<int>>();
var nums = Enumerable.Range(1, upper);
IEnumerable<IEnumerable<int>> products = nums.Select(i => new[]{i});
for (int i = 1; i < times; i++)
{
products = from p in products
from n in nums
select p.Concat(new [] {n});
}
return products;
}
And now you can get what you had before with:
var p = NAryCartesianProduct(5, 2);
foreach(var i in p)
Console.WriteLine (i);
I'm sure there's a more efficient way than creating new arrays all of the time but I just hacked this up quick :)
Here's a much more informative answer on this: Generating all Possible Combinations
EDIT2: Apparently the original link is the origination of the answer from that SO post. I didn't read through to the end until now.

Categories