/// <summary> /// Validates Sequence Alignment test cases for the parameters passed. /// </summary> /// <param name="nodeName">Node Name in the xml.</param> /// <param name="offset">Offset</param> /// <param name="caseType">Case type</param> /// <param name="type">Sequence type</param> static void ValidateGeneralSequenceAlignment(string nodeName, OffsetValidation offset, SequenceCaseType caseType, SequenceType type) { // Read the xml file for getting both the files for aligning. string firstInputSequence = string.Empty; string secondInputSequence = string.Empty; IAlphabet alphabet = Utility.GetAlphabet(Utility._xmlUtil.GetTextValue(nodeName, Constants.AlphabetNameNode)); GetInputSequencesWithSequenceType(nodeName, type, out firstInputSequence, out secondInputSequence); ApplicationLog.WriteLine(string.Format(null, "SequenceAlignment P2 : First sequence used is '{0}'.", firstInputSequence)); ApplicationLog.WriteLine(string.Format(null, "SequenceAlignment P2 : Second sequence used is '{0}'.", secondInputSequence)); Console.WriteLine(string.Format(null, "SequenceAlignment P2 : First sequence used is '{0}'.", firstInputSequence)); Console.WriteLine(string.Format(null, "SequenceAlignment P2 : Second sequence used is '{0}'.", secondInputSequence)); // Create two sequences ISequence aInput = null; ISequence bInput = null; Exception actualException = null; switch (caseType) { case SequenceCaseType.LowerCase: aInput = new Sequence(alphabet, firstInputSequence.ToLower(CultureInfo.CurrentCulture)); bInput = new Sequence(alphabet, secondInputSequence.ToLower(CultureInfo.CurrentCulture)); break; case SequenceCaseType.UpperCase: aInput = new Sequence(alphabet, firstInputSequence.ToUpper(CultureInfo.CurrentCulture)); bInput = new Sequence(alphabet, secondInputSequence.ToUpper(CultureInfo.CurrentCulture)); break; case SequenceCaseType.Default: try { aInput = new Sequence(alphabet, firstInputSequence); bInput = new Sequence(alphabet, secondInputSequence); } catch (Exception ex) { actualException = ex; } break; } // Add the sequences to the Sequence alignment object using AddSequence() method. IList <IPairwiseSequenceAlignment> sequenceAlignmentObj = new List <IPairwiseSequenceAlignment>(); try { PairwiseAlignedSequence alignSeq = new PairwiseAlignedSequence(); alignSeq.FirstSequence = aInput; IPairwiseSequenceAlignment seqAlignObj = new PairwiseSequenceAlignment(); seqAlignObj.Add(alignSeq); sequenceAlignmentObj.Add(seqAlignObj); } catch (Exception ex) { actualException = ex; } if (actualException == null) { if (offset == OffsetValidation.Default) { sequenceAlignmentObj[0].PairwiseAlignedSequences[0].SecondSequence = bInput; // Read the output back and validate the same. IList <PairwiseAlignedSequence> newAlignedSequences = sequenceAlignmentObj[0].PairwiseAlignedSequences; ApplicationLog.WriteLine(string.Format(null, "SequenceAlignment P2 : First sequence read is '{0}'.", firstInputSequence)); ApplicationLog.WriteLine(string.Format(null, "SequenceAlignment P2 : Second sequence read is '{0}'.", secondInputSequence)); Console.WriteLine(string.Format(null, "SequenceAlignment P2 : First sequence read is '{0}'.", firstInputSequence)); Console.WriteLine(string.Format(null, "SequenceAlignment P2 : Second sequence read is '{0}'.", secondInputSequence)); Assert.AreEqual(newAlignedSequences[0].FirstSequence.ToString(), firstInputSequence); Assert.AreEqual(newAlignedSequences[0].SecondSequence.ToString(), secondInputSequence); } } else { // Validate that expected exception is thrown using error message. string expectedErrorMessage = Utility._xmlUtil.GetTextValue(nodeName, Constants.ExpectedErrorMessage); Assert.AreEqual(expectedErrorMessage, actualException.Message); ApplicationLog.WriteLine(string.Format(null, "SequenceAlignment P2 : Expected Error message is thrown ", expectedErrorMessage)); Console.WriteLine(string.Format(null, "SequenceAlignment P2 : Expected Error message is thrown ", expectedErrorMessage)); } }
/// <summary> /// Creates the sequence object with sequences in different cases /// </summary> /// <param name="firstSequenceString">First sequence string.</param> /// <param name="secondSequenceString">Second sequence string.</param> /// <param name="alphabet">alphabet type.</param> /// <param name="caseType">Sequence case type</param> /// <param name="firstInputSequence">First input sequence object.</param> /// <param name="secondInputSequence">Second input sequence object.</param> private static void GetSequenceWithCaseType(string firstSequenceString, string secondSequenceString, IAlphabet alphabet, SequenceCaseType caseType, out Sequence firstInputSequence, out Sequence secondInputSequence) { switch (caseType) { case SequenceCaseType.LowerCase: firstInputSequence = new Sequence(alphabet, firstSequenceString.ToString(null) .ToLower(CultureInfo.CurrentCulture)); secondInputSequence = new Sequence(alphabet, secondSequenceString.ToString(null) .ToLower(CultureInfo.CurrentCulture)); break; case SequenceCaseType.UpperCase: firstInputSequence = new Sequence(alphabet, firstSequenceString.ToString(null) .ToUpper(CultureInfo.CurrentCulture)); secondInputSequence = new Sequence(alphabet, secondSequenceString.ToString(null) .ToUpper(CultureInfo.CurrentCulture)); break; case SequenceCaseType.LowerUpperCase: firstInputSequence = new Sequence(alphabet, firstSequenceString.ToString(null) .ToLower(CultureInfo.CurrentCulture)); secondInputSequence = new Sequence(alphabet, secondSequenceString.ToString(null) .ToUpper(CultureInfo.CurrentCulture)); break; case SequenceCaseType.Default: default: firstInputSequence = new Sequence(alphabet, firstSequenceString.ToString(null)); secondInputSequence = new Sequence(alphabet, secondSequenceString.ToString(null)); break; } }
private void ValidateSmithWatermanAlignment(string nodeName, bool isTextFile, SequenceCaseType caseType, AlignParameters additionalParameter, AlignmentType alignType, SimilarityMatrixParameters similarityMatrixParam) { Sequence aInput, bInput; IAlphabet alphabet = Utility.GetAlphabet(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.AlphabetNameNode)); if (isTextFile) { // Read the xml file for getting both the files for aligning. string filePath1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode1); string filePath2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode2); // Parse the files and get the sequence. ISequence originalSequence1 = null; ISequence originalSequence2 = null; var parseObjectForFile1 = new FastAParser { Alphabet = alphabet }; originalSequence1 = parseObjectForFile1.Parse(filePath1).ElementAt(0); originalSequence2 = parseObjectForFile1.Parse(filePath2).ElementAt(0); // Create input sequence for sequence string in different cases. GetSequenceWithCaseType(originalSequence1.ConvertToString(), originalSequence2.ConvertToString(), alphabet, caseType, out aInput, out bInput); } else { string originalSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceNode1); string originalSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceNode2); // Create input sequence for sequence string in different cases. GetSequenceWithCaseType( originalSequence1, originalSequence2, alphabet, caseType, out aInput, out bInput); } ApplicationLog.WriteLine(string.Format("SmithWatermanAligner P2 : First sequence used is '{0}'.", aInput.ConvertToString())); ApplicationLog.WriteLine(string.Format("SmithWatermanAligner P2 : Second sequence used is '{0}'.", bInput.ConvertToString())); // Create similarity matrix object for a given file. string blosumFilePath = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.BlosumFilePathNode); SimilarityMatrix sm; switch (similarityMatrixParam) { case SimilarityMatrixParameters.TextReader: using (TextReader reader = new StreamReader(blosumFilePath)) sm = new SimilarityMatrix(reader); break; case SimilarityMatrixParameters.DiagonalMatrix: string matchValue = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.MatchScoreNode); string misMatchValue = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.MisMatchScoreNode); sm = new DiagonalSimilarityMatrix(int.Parse(matchValue, null), int.Parse(misMatchValue, null)); break; default: sm = new SimilarityMatrix(new StreamReader(blosumFilePath)); break; } int gapOpenCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.GapOpenCostNode), null); int gapExtensionCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.GapExtensionCostNode), null); // Create SmithWatermanAligner instance and set its values. var smithWatermanObj = new SmithWatermanAligner(); if (additionalParameter != AlignParameters.AllParam) { smithWatermanObj.SimilarityMatrix = sm; smithWatermanObj.GapOpenCost = gapOpenCost; smithWatermanObj.GapExtensionCost = gapExtensionCost; } IList<IPairwiseSequenceAlignment> result = null; // Align the input sequences. switch (additionalParameter) { case AlignParameters.AlignList: switch (alignType) { case AlignmentType.Align: result = smithWatermanObj.Align(new List<ISequence> {aInput, bInput}); break; default: result = smithWatermanObj.AlignSimple(new List<ISequence> {aInput, bInput}); break; } break; case AlignParameters.AlignTwo: switch (alignType) { case AlignmentType.Align: result = smithWatermanObj.Align(aInput, bInput); break; default: result = smithWatermanObj.AlignSimple(aInput, bInput); break; } break; case AlignParameters.AllParam: switch (alignType) { case AlignmentType.Align: result = smithWatermanObj.Align(sm, gapOpenCost, gapExtensionCost, aInput, bInput); break; default: result = smithWatermanObj.AlignSimple(sm, gapOpenCost, aInput, bInput); break; } break; default: break; } // Get the expected sequence and scorde from xml config. string expectedSequence1, expectedSequence2, expectedScore; switch (alignType) { case AlignmentType.Align: expectedScore = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedGapExtensionScoreNode); switch (caseType) { case SequenceCaseType.LowerCase: expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants .ExpectedGapExtensionSequence1InLower); expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants .ExpectedGapExtensionSequence2InLower); break; default: expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants .ExpectedGapExtensionSequence1Node); expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants .ExpectedGapExtensionSequence2Node); break; } break; default: expectedScore = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedScoreNode); switch (caseType) { case SequenceCaseType.LowerCase: expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequence1inLowerNode); expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequence2inLowerNode); break; case SequenceCaseType.LowerUpperCase: expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequence1inLowerNode); expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode2); break; default: expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode1); expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode2); break; } break; } // Match the alignment result with expected result. IList<IPairwiseSequenceAlignment> expectedOutput = new List<IPairwiseSequenceAlignment>(); IPairwiseSequenceAlignment align = new PairwiseSequenceAlignment(); var alignedSeq = new PairwiseAlignedSequence { FirstSequence = new Sequence(alphabet, expectedSequence1), SecondSequence = new Sequence(alphabet, expectedSequence2), Score = Convert.ToInt32(expectedScore, null), FirstOffset = Int32.MinValue, SecondOffset = Int32.MinValue, }; align.PairwiseAlignedSequences.Add(alignedSeq); expectedOutput.Add(align); ApplicationLog.WriteLine(string.Format(null, "SmithWatermanAligner P2 : Final Score '{0}'.", expectedScore)); ApplicationLog.WriteLine(string.Format(null, "SmithWatermanAligner P2 : Aligned First Sequence is '{0}'.", expectedSequence1)); ApplicationLog.WriteLine(string.Format(null, "SmithWatermanAligner P2 : Aligned Second Sequence is '{0}'.", expectedSequence2)); Assert.IsTrue(CompareAlignment(result, expectedOutput)); }
/// <summary> /// Validates SmithWatermanAlignment algorithm for the parameters passed. /// </summary> /// <param name="nodeName">Xml node name</param> /// <param name="isTextFile">Is text file an input.</param> /// <param name="caseType">Case Type</param> /// <param name="additionalParameter">parameter based on which certain validations are done.</param> /// <param name="alignType">Is the Align type Simple or Align with Gap Extension cost?</param> private void ValidateSmithWatermanAlignment(string nodeName, bool isTextFile, SequenceCaseType caseType, AlignParameters additionalParameter, AlignmentType alignType) { this.ValidateSmithWatermanAlignment(nodeName, isTextFile, caseType, additionalParameter, alignType, SimilarityMatrixParameters.Default); }
/// <summary> /// Validates SmithWatermanAlignment algorithm for the parameters passed. /// </summary> /// <param name="nodeName">Xml node name</param> /// <param name="isTextFile">Is text file an input.</param> /// <param name="caseType">Case Type</param> /// <param name="additionalParameter">parameter based on which certain validations are done.</param> private void ValidateSmithWatermanAlignment(string nodeName, bool isTextFile, SequenceCaseType caseType, AlignParameters additionalParameter) { this.ValidateSmithWatermanAlignment(nodeName, isTextFile, caseType, additionalParameter, AlignmentType.SimpleAlign); }
/// <summary> /// Validates PairwiseOverlapAlignment algorithm for the parameters passed. /// </summary> /// <param name="nodeName">Xml node name</param> /// <param name="isTextFile">Is text file an input.</param> /// <param name="caseType">Case Type</param> /// <param name="additionalParameter">parameter based on which certain validations are done.</param> /// <param name="alignType">Is the Align type Simple or Align with Gap Extension cost?</param> /// <param name="similarityMatrixParam">Similarity Matrix</param> private void ValidatePairwiseOverlapAlignment(string nodeName, bool isTextFile, SequenceCaseType caseType, AlignParameters additionalParameter, AlignmentType alignType, SimilarityMatrixParameters similarityMatrixParam) { Sequence aInput = null; Sequence bInput = null; IAlphabet alphabet = Utility.GetAlphabet(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.AlphabetNameNode)); if (isTextFile) { // Read the xml file for getting both the files for aligning. string filePath1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode1); string filePath2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode2); var parser1 = new FastAParser(); ISequence originalSequence1 = parser1.Parse(filePath1).ElementAt(0); ISequence originalSequence2 = parser1.Parse(filePath2).ElementAt(0); // Create input sequence for sequence string in different cases. GetSequenceWithCaseType(new string(originalSequence1.Select(a => (char) a).ToArray()), new string(originalSequence2.Select(a => (char) a).ToArray()), alphabet, caseType, out aInput, out bInput); } else { string originalSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceNode1); string originalSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceNode2); // Create input sequence for sequence string in different cases. GetSequenceWithCaseType( originalSequence1, originalSequence2, alphabet, caseType, out aInput, out bInput); } var aInputString = new string(aInput.Select(a => (char) a).ToArray()); var bInputString = new string(bInput.Select(a => (char) a).ToArray()); ApplicationLog.WriteLine(string.Format(null, "PairwiseOverlapAligner P2 : First sequence used is '{0}'.", aInputString)); ApplicationLog.WriteLine(string.Format(null, "PairwiseOverlapAligner P2 : Second sequence used is '{0}'.", bInputString)); // Create similarity matrix object for a given file. string blosumFilePath = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.BlosumFilePathNode); SimilarityMatrix sm = null; switch (similarityMatrixParam) { case SimilarityMatrixParameters.TextReader: using (TextReader reader = new StreamReader(blosumFilePath)) sm = new SimilarityMatrix(reader); break; case SimilarityMatrixParameters.DiagonalMatrix: string matchValue = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.MatchScoreNode); string misMatchValue = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.MisMatchScoreNode); sm = new DiagonalSimilarityMatrix(int.Parse(matchValue, null), int.Parse(misMatchValue, null)); break; default: sm = new SimilarityMatrix(new StreamReader(blosumFilePath)); break; } int gapOpenCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.GapOpenCostNode), null); int gapExtensionCost = int.Parse(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.GapExtensionCostNode), null); // Create PairwiseOverlapAligner instance and set its values. var pairwiseOverlapObj = new PairwiseOverlapAligner(); if (additionalParameter != AlignParameters.AllParam) { pairwiseOverlapObj.SimilarityMatrix = sm; pairwiseOverlapObj.GapOpenCost = gapOpenCost; pairwiseOverlapObj.GapExtensionCost = gapExtensionCost; } IList<IPairwiseSequenceAlignment> result = null; // Align the input sequences. switch (additionalParameter) { case AlignParameters.AlignList: var sequences = new List<ISequence>(); sequences.Add(aInput); sequences.Add(bInput); switch (alignType) { case AlignmentType.Align: result = pairwiseOverlapObj.Align(sequences); break; default: result = pairwiseOverlapObj.AlignSimple(sequences); break; } break; case AlignParameters.AlignTwo: switch (alignType) { case AlignmentType.Align: result = pairwiseOverlapObj.Align(aInput, bInput); break; default: result = pairwiseOverlapObj.AlignSimple(aInput, bInput); break; } break; case AlignParameters.AllParam: switch (alignType) { case AlignmentType.Align: result = pairwiseOverlapObj.Align(sm, gapOpenCost, gapExtensionCost, aInput, bInput); break; default: result = pairwiseOverlapObj.AlignSimple(sm, gapOpenCost, aInput, bInput); break; } break; default: break; } aInput = null; bInput = null; sm = null; // Get the expected sequence and scorde from xml config. string expectedSequence1 = string.Empty; string expectedSequence2 = string.Empty; string expectedScore = string.Empty; switch (alignType) { case AlignmentType.Align: expectedScore = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedGapExtensionScoreNode); expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedGapExtensionSequence1Node); expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedGapExtensionSequence2Node); break; default: expectedScore = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedScoreNode); expectedSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode1); expectedSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode2); break; } IList<IPairwiseSequenceAlignment> expectedOutput = new List<IPairwiseSequenceAlignment>(); string[] expectedSequences1, expectedSequences2; var seperators = new char[1] {';'}; expectedSequences1 = expectedSequence1.Split(seperators); expectedSequences2 = expectedSequence2.Split(seperators); IPairwiseSequenceAlignment align = new PairwiseSequenceAlignment(); PairwiseAlignedSequence alignedSeq; for (int i = 0; i < expectedSequences1.Length; i++) { alignedSeq = new PairwiseAlignedSequence { FirstSequence = new Sequence(alphabet, expectedSequences1[i]), SecondSequence = new Sequence(alphabet, expectedSequences2[i]), Score = Convert.ToInt32(expectedScore, null), FirstOffset = Int32.MinValue, SecondOffset = Int32.MinValue, }; align.PairwiseAlignedSequences.Add(alignedSeq); } expectedOutput.Add(align); Assert.IsTrue(AlignmentHelpers.CompareAlignment(result, expectedOutput,true)); ApplicationLog.WriteLine(string.Format(null, "PairwiseOverlapAligner P2 : Final Score '{0}'.", expectedScore)); ApplicationLog.WriteLine(string.Format(null, "PairwiseOverlapAligner P2 : Aligned First Sequence is '{0}'.", expectedSequence1)); ApplicationLog.WriteLine(string.Format(null, "PairwiseOverlapAligner P2 : Aligned Second Sequence is '{0}'.", expectedSequence2)); }