/// <summary> Aligns two sequences by Smith-Waterman algorithm</summary> /// <param name="s1">sequene #1 </param> /// <param name="s2">sequene #2 </param> /// <param name="matrix">scoring matrix </param> /// <param name="o">open gap penalty </param> /// <param name="e">extend gap penalty </param> /// <returns> alignment object contains the two aligned sequences, /// the alignment score and alignment statistics</returns> /// <seealso cref="Sequence"/> /// <seealso cref="Matrix"/> public PairwiseAlignment Align(Sequence s1, Sequence s2, ScoringMatrix matrix, float gapOpenPenalty, float gapExtensionPenalty) { if (s1 == null) { Console.WriteLine("Error: S1 is null"); throw new SmithWatermanGotohException("S1 is null"); } if (s2 == null) { Console.WriteLine("Error: S2 is null"); throw new SmithWatermanGotohException("S2 is null"); } int m = s1.Length + 1; int n = s2.Length + 1; var pointers = new Directions[m * n]; // Initializes the boundaries of the traceback matrix to STOP. for (int i = 0, k = 0; i < m; i++, k += n) { pointers[k] = Directions.STOP; } for (int j = 1; j < n; j++) { pointers[j] = Directions.STOP; } var sizesOfVerticalGaps = new short[m * n]; var sizesOfHorizontalGaps = new short[m * n]; for (int i = 0, k = 0; i < m; i++, k += n) { for (int j = 0; j < n; j++) { sizesOfVerticalGaps[k + j] = sizesOfHorizontalGaps[k + j] = 1; } } _scoringMatrix = matrix; _gapOpenPenalty = gapOpenPenalty; _gapExtensionPenalty = gapExtensionPenalty; Cell cell = Construct(s1, s2, pointers, sizesOfVerticalGaps, sizesOfHorizontalGaps); PairwiseAlignment alignment = Traceback(s1.Residues, s2.Residues, pointers, cell, sizesOfVerticalGaps, sizesOfHorizontalGaps); alignment.Name1 = s1.Label; alignment.Name2 = s2.Label; alignment.GapOpenPenalty = _gapOpenPenalty; alignment.GapExtendPenalty = _gapExtensionPenalty; alignment.Matrix = matrix.Name; return(alignment); }
static int ComputeBlock(Block block, PartialMatrix <TDistance> matrix, bool isDiagonal) { int count = 0; SmithWatermanGotoh aligner = new SmithWatermanGotoh(_alignmentType); ScoringMatrix scoringMatrix = ScoringMatrix.Load(_scoringMatrixName); if (isDiagonal) { for (int i = block.RowRange.StartIndex; i <= block.RowRange.EndIndex; i++) { Sequence si = _sequences[i]; for (int j = block.ColumnRange.StartIndex; j < i; j++) { Sequence sj = _sequences[j]; PairwiseAlignment alignment = aligner.Align(si, sj, scoringMatrix, _gapOpen, _gapExtension); if ((_writeAlignments) && (_writeAlignmentsWriter != null)) { WriteAlignment(_writeAlignmentsWriter, alignment); } matrix[i, j] = matrix[j, i] = ComputeDistance(alignment); count++; } } } else { for (int i = block.RowRange.StartIndex; i <= block.RowRange.EndIndex; i++) { Sequence si = _sequences[i]; for (int j = block.ColumnRange.StartIndex; j <= block.ColumnRange.EndIndex; j++) { Sequence sj = _sequences[j]; PairwiseAlignment alignment = aligner.Align(si, sj, scoringMatrix, _gapOpen, _gapExtension); if ((_writeAlignments) && (_writeAlignmentsWriter != null)) { WriteAlignment(_writeAlignmentsWriter, alignment); } matrix[i, j] = ComputeDistance(alignment); count++; } } } return(count); }
public async Task <IActionResult> Align(SequenceViewModel Model, IFormFile FirstFile, IFormFile SecondFile) { if (!string.IsNullOrWhiteSpace(Model.FirstSequence)) { Model.FirstSequence = Model.FirstSequence.Trim().Replace(" ", string.Empty).ToUpper(); } if (!string.IsNullOrWhiteSpace(Model.SecondSequence)) { Model.SecondSequence = Model.SecondSequence.Trim().Replace(" ", string.Empty).ToUpper(); } if (string.IsNullOrWhiteSpace(Model.FirstSequence) && FirstFile != null) { if (FirstFile.ContentType == "text/plain") { string FirstSequence = (await Helper.ConvertFileByteToByteStringAsync(FirstFile)).Trim().Replace(" ", string.Empty).ToUpper(); if (FirstSequence.Length > 10000) { return(RedirectToAction("Grid", "Alignment")); } else if (FirstSequence.Length == 0) { return(View("Error", new ErrorViewModel { Message = "You Can't send a sequence of 0 Length", Solution = "You should send a sequence greater than 0 length" })); } else { Model.FirstSequence = FirstSequence; } } else { return(View("Error", new ErrorViewModel { Message = "You Can't upload a file of any type rather than txt file format", Solution = "You should upload a file of txt file format" })); } } if (string.IsNullOrWhiteSpace(Model.SecondSequence) && SecondFile != null) { if (SecondFile.ContentType == "text/plain") { string SecondSequence = (await Helper.ConvertFileByteToByteStringAsync(SecondFile)).Trim().Replace(" ", string.Empty).ToUpper(); if (SecondSequence.Length > 10000) { return(RedirectToAction("Grid", "Alignment")); } else if (SecondSequence.Length == 0) { return(View("Error", new ErrorViewModel { Message = "You Can't send a sequence of 0 Length", Solution = "You should send a sequence greater than 0 length" })); } else { Model.SecondSequence = SecondSequence; } } else { return(View("Error", new ErrorViewModel { Message = "You Can't upload a file of any type rather than txt file format", Solution = "You should upload a file of txt file format" })); } } if ((Model.FirstSequence == null && FirstFile == null) || (Model.SecondSequence == null && SecondFile == null)) { return(View("Error", new ErrorViewModel { Message = "You Can't enter an empty sequence", Solution = "You have to enter the sequence or either upload a file contains the sequence" })); } if (!Regex.IsMatch(Model.FirstSequence, @"^[a-zA-Z]+$") || !Regex.IsMatch(Model.SecondSequence, @"^[a-zA-Z]+$")) { return(View("Error", new ErrorViewModel { Message = "Your sequence must contains only characters", Solution = "Send sequence contains only characters" })); } AlignmentJob JobFound = Repo.AreExist(Model.FirstSequence, Model.SecondSequence, Model.ScoringMatrix, Model.Gap); if (JobFound == null) { JobFound = new AlignmentJob() { AlignmentID = Guid.NewGuid().ToString(), Algorithm = Model.Algorithm, ScoringMatrix = Model.ScoringMatrix, FirstSequenceHash = Helper.SHA1HashStringForUTF8String(Model.FirstSequence), SecondSequenceHash = Helper.SHA1HashStringForUTF8String(Model.SecondSequence), FirstSequenceName = Model.FirstSequenceName, SecondSequenceName = Model.SecomdSequenceName, GapOpenPenalty = Model.GapOpenPenalty, Gap = Model.Gap, GapExtensionPenalty = Model.GapExtensionPenalty, IsAlignmentCompleted = true, }; SequenceAligner AlgorithmInstance = DynamicInvoke.GetAlgorithm(Model.Algorithm); ScoringMatrix ScoringMatrixInstance = DynamicInvoke.GetScoreMatrix(Model.ScoringMatrix); string AlignmentResult = string.Empty; float AlignmentScore = 0.0f; AlignedSequences Result = AlgorithmInstance.Align(Model.FirstSequence, Model.SecondSequence, ScoringMatrixInstance, Model.Gap); AlignmentResult = Result.StandardFormat(210); AlignmentScore = Result.AlignmentScore(ScoringMatrixInstance); JobFound.ByteText = Helper.GetText(AlignmentResult, AlignmentScore, JobFound.AlignmentID, Model.Algorithm, Model.ScoringMatrix, Model.Gap, Model.GapOpenPenalty, Model.GapExtensionPenalty); JobFound.UserFK = UserManager.GetUserId(User); await Repo.AddAlignmentJobAsync(JobFound); if (Model.DownloadDirectly == 1) { return(File(JobFound.ByteText, "text/plain", $"{JobFound.AlignmentID}_Alignment_Result.txt")); } else { return(RedirectToAction("Display", "Profile", new { AlignmentID = JobFound.AlignmentID })); } } else { if (Model.DownloadDirectly == 1) { return(File(JobFound.ByteText, "text/plain", $"{JobFound.AlignmentID}_Alignment_Result.txt")); } else { return(RedirectToAction("Display", "Profile", new { AlignmentID = JobFound.AlignmentID })); } } }
private void button1_Click(object sender, EventArgs e) { // remove whitespace and prepend a - to contents of seq char[] first = ("-" + Regex.Replace(seq1.Text, @"\s+", "").ToUpper()).ToCharArray(); char[] second = ("-" + Regex.Replace(seq2.Text, @"\s+", "").ToUpper()).ToCharArray(); int[] scoreSystem = new int[3]; if (countGaps.Checked) { scoreSystem[0] = 1; scoreSystem[1] = -1; scoreSystem[2] = -2; } else if (countNoGaps.Checked) { scoreSystem[0] = 1; scoreSystem[1] = -1; scoreSystem[2] = 0; } else { // gap options not selected results.Text = "Please select if this alignment should count gaps or not" + "\r\n"; return; } ScoringMatrix simon = new ScoringMatrix(first, second, scoreSystem); OptimumMatrix jack = new OptimumMatrix(first, second); if (isGlobal.Checked) { for (int i = 1; i < first.Length; i++) { for (int j = 1; j < second.Length; j++) { jack.setScore(i, j, simon.calcCell(i, j)); } } } else if (isLocal.Checked) { for (int i = 1; i < first.Length; i++) { for (int j = 1; j < second.Length; j++) { jack.setScore(i, j, simon.calcCellLocal(i, j)); } } } else { // alignment type options not selected results.Text = "Please select if this alignment should be global or local" + "\r\n"; return; } int[] maxPos = simon.getMaxScorePos(); string[] paul = jack.calcOptPathFrom(maxPos[0], maxPos[1]); results.Text += paul[0] + "\r\n" + paul[1] + "\r\n\r\n"; }