public void FastQFormatter() { string filepathOriginal = @"TestUtils\FASTQ\SRR002012_5.fastq"; Assert.IsTrue(File.Exists(filepathOriginal)); FastQParser parser = new FastQParser(); FastQFormatter formatter = new FastQFormatter(); // Read the original file IList <IQualitativeSequence> seqsOriginal = null; parser = new FastQParser(); seqsOriginal = parser.Parse(filepathOriginal); Assert.IsNotNull(seqsOriginal); // Use the formatter to write the original sequences to a temp file string filepathTmp = Path.GetTempFileName(); using (TextWriter writer = new StreamWriter(filepathTmp)) { foreach (IQualitativeSequence s in seqsOriginal) { formatter.Format(s, writer); } } // Read the new file, then compare the sequences IList <IQualitativeSequence> seqsNew = null; parser = new FastQParser(); seqsNew = parser.Parse(filepathTmp); Assert.IsNotNull(seqsNew); // Now compare the sequences. int countOriginal = seqsOriginal.Count(); int countNew = seqsNew.Count(); Assert.AreEqual(countOriginal, countNew); int i; for (i = 0; i < countOriginal; i++) { Assert.AreEqual(seqsOriginal[i].ID, seqsNew[i].ID); string orgSeq = seqsOriginal[i].ToString(); string newSeq = seqsNew[i].ToString(); string orgscores = ASCIIEncoding.ASCII.GetString(seqsOriginal[i].Scores); string newscores = ASCIIEncoding.ASCII.GetString(seqsNew[i].Scores); Assert.AreEqual(orgSeq, newSeq); Assert.AreEqual(orgscores, newscores); } // Passed all the tests, delete the tmp file. If we failed an Assert, // the tmp file will still be there in case we need it for debugging. File.Delete(filepathTmp); ((FastQParser)parser).Dispose(); }
private void InValidateFastQFormatter(FastQFormatParameters param) { // Gets the expected sequence from the Xml string filepath = utilityObj.xmlUtil.GetTextValue( Constants.MultiSeqSangerRnaProNode, Constants.FilePathNode); // Parse a FastQ file. var fastQParser = new FastQParser(); using (fastQParser.Open(filepath)) { FastQFormatter fastQFormatter = null; switch (param) { case FastQFormatParameters.Sequence: try { fastQFormatter = new FastQFormatter(); fastQFormatter.Format(null as ISequence, filepath); Assert.Fail(); } catch (ArgumentNullException) { fastQFormatter.Close(); ApplicationLog.WriteLine( "FastQ Parser P2 : Successfully validated the exception"); } break; default: try { IEnumerable <IQualitativeSequence> sequence = fastQParser.Parse(); fastQFormatter = new FastQFormatter(); fastQFormatter.Format(sequence, null); Assert.Fail(); } catch (ArgumentNullException) { ApplicationLog.WriteLine("FastQ Parser P2 : Successfully validated the exception"); } break; } } }
/// <summary> /// General method to validate FastQ Formatter. /// <param name="nodeName">xml node name.</param> /// <param name="fileExtension">Different temporary file extensions</param> /// </summary> private void ValidateFastQFormatter(string nodeName, FastQFileParameters fileExtension) { // Gets the expected sequence from the Xml string filePath = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode); string expectedQualitativeSequence = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode); string expectedSequenceId = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceIdNode); string tempFileName = Path.GetTempFileName(); // Parse a FastQ file using parseOne method. var fastQParserObj = new FastQParser(); using (fastQParserObj.Open(filePath)) { IQualitativeSequence oneSequence = fastQParserObj.ParseOne(); // Format Parsed Sequence to temp file with different extension. var fastQFormatter = new FastQFormatter(); using (fastQFormatter.Open(tempFileName)) { fastQFormatter.Format(oneSequence); } string parsedValue; string parsedId; var fastQParserObjTemp = new FastQParser(); using (fastQParserObjTemp.Open(tempFileName)) { oneSequence = fastQParserObjTemp.Parse().First(); parsedValue = oneSequence.ConvertToString(); parsedId = oneSequence.ID; } // Validate qualitative parsing temporary file. Assert.AreEqual(expectedQualitativeSequence, parsedValue); Assert.AreEqual(expectedSequenceId, parsedId); ApplicationLog.WriteLine(string.Format("FastQ Formatter BVT: The FASTQ sequence '{0}' validation after Write() and Parse() is found to be as expected.", parsedValue)); ApplicationLog.WriteLine(string.Format("FastQ Formatter BVT: The FASTQ sequence '{0}' validation after Write() and Parse() is found to be as expected.", parsedId)); File.Delete(tempFileName); } }
private void InValidateFastQFormatter(FastQFormatParameters param) { // Gets the expected sequence from the Xml string filepath = utilityObj.xmlUtil.GetTextValue( Constants.MultiSeqSangerRnaProNode, Constants.FilePathNode); // Parse a FastQ file. var fastQParser = new FastQParser(); using (fastQParser.Open(filepath)) { FastQFormatter fastQFormatter = null; switch (param) { case FastQFormatParameters.Sequence: try { fastQFormatter = new FastQFormatter(); fastQFormatter.Format(null as ISequence, filepath); Assert.Fail(); } catch (ArgumentNullException) { fastQFormatter.Close(); ApplicationLog.WriteLine( "FastQ Parser P2 : Successfully validated the exception"); } break; default: try { IEnumerable<IQualitativeSequence> sequence = fastQParser.Parse(); fastQFormatter = new FastQFormatter(); fastQFormatter.Format(sequence, null); Assert.Fail(); } catch (ArgumentNullException) { ApplicationLog.WriteLine("FastQ Parser P2 : Successfully validated the exception"); } break; } } }
/// <summary> /// General method to Invalidate FastQ Parser. /// <param name="nodeName">xml node name.</param> /// <param name="param">FastQ Formatter different parameters</param> /// </summary> void InValidateFastQFormatter(FastQFormatParameters param) { // Gets the expected sequence from the Xml string filepath = _utilityObj._xmlUtil.GetTextValue( Constants.MultiSeqSangerRnaProNode, Constants.FilePathNode); FastQFormatType expectedFormatType = Utility.GetFastQFormatType( _utilityObj._xmlUtil.GetTextValue(Constants.MultiSeqSangerRnaProNode, Constants.FastQFormatType)); // Parse a FastQ file. using (FastQParser fastQParser = new FastQParser()) { fastQParser.AutoDetectFastQFormat = true; fastQParser.FastqType = expectedFormatType; IQualitativeSequence sequence = null; sequence = fastQParser.ParseOne(filepath); FastQFormatter fastQFormatter = new FastQFormatter(); TextWriter txtWriter = null; switch (param) { case FastQFormatParameters.TextWriter: try { fastQFormatter.Format(sequence, null as TextWriter); Assert.Fail(); } catch (Exception) { ApplicationLog.WriteLine( "FastQ Parser P2 : Successfully validated the exception"); Console.WriteLine( "FastQ Parser P2 : Successfully validated the exception"); } break; case FastQFormatParameters.Sequence: try { fastQFormatter.Format(null as ISequence, txtWriter); Assert.Fail(); } catch (Exception) { ApplicationLog.WriteLine( "FastQ Parser P2 : Successfully validated the exception"); Console.WriteLine( "FastQ Parser P2 : Successfully validated the exception"); } break; case FastQFormatParameters.QualitativeSequence: try { fastQFormatter.Format(null as IQualitativeSequence, txtWriter); Assert.Fail(); } catch (Exception) { ApplicationLog.WriteLine( "FastQ Parser P2 : Successfully validated the exception"); Console.WriteLine( "FastQ Parser P2 : Successfully validated the exception"); } break; default: try { fastQFormatter.Format(sequence as QualitativeSequence, null as TextWriter); Assert.Fail(); } catch (Exception) { ApplicationLog.WriteLine( "FastQ Parser P2 : Successfully validated the exception"); Console.WriteLine( "FastQ Parser P2 : Successfully validated the exception"); } break; } } }
/// <summary> /// General method to validate BasicSequence Parser. /// <param name="nodeName">xml node name.</param> /// </summary> private void ValidateBasicSequenceParser(string nodeName) { // Gets the expected sequence from the Xml string filepathOriginal = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode); string expectedQualitativeSequence = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode); string expectedSequenceId = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceIdNode); IAlphabet alphabet = Utility.GetAlphabet(utilityObj.xmlUtil.GetTextValue(nodeName, Constants.AlphabetNameNode)); Assert.IsTrue(File.Exists(filepathOriginal)); string tempPath = Path.GetTempFileName(); try { ISequenceParser fastQParserObj = SequenceParsers.FindParserByFileName("temp.fq"); // Read the original file IEnumerable<ISequence> seqsOriginal = fastQParserObj.Parse(filepathOriginal); Assert.IsNotNull(seqsOriginal); // Use the formatter to write the original sequences to a temp file var formatter = new FastQFormatter(); formatter.Format(seqsOriginal.ElementAt(0), tempPath); // Read the new file, then compare the sequences var fastQParserObjNew = new FastQParser(); IEnumerable<IQualitativeSequence> seqsNew = fastQParserObjNew.Parse(tempPath); Assert.IsNotNull(seqsNew); // Validate qualitative Sequence upon parsing FastQ file. Assert.AreEqual(expectedQualitativeSequence, new string(seqsOriginal.ElementAt(0).Select(a => (char) a).ToArray())); Assert.AreEqual( seqsOriginal.ElementAt(0).ID.ToString(null), expectedSequenceId); Assert.AreEqual( seqsOriginal.ElementAt(0).Alphabet.Name, alphabet.Name); ApplicationLog.WriteLine(string.Format("FastQ Parser P1: The FASTQ sequence '{0}' validation after Parse() is found to be as expected.", seqsOriginal.ElementAt(0))); } finally { File.Delete(tempPath); } }
private void ValidateFastQFormatter(string nodeName, bool writeMultipleSequences) { // Gets the expected sequence from the Xml string filePath = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode); string expectedQualitativeSequence = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode); string expectedSequenceId = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceIdNode); string tempFileName = Path.GetTempFileName(); // Parse a FastQ file. var fastQParserObj = new FastQParser(); using (fastQParserObj.Open(filePath)) { IEnumerable<IQualitativeSequence> qualSequenceList = fastQParserObj.Parse(); var fastQFormatter = new FastQFormatter(); using (fastQFormatter.Open(tempFileName)) { if (writeMultipleSequences) { foreach (IQualitativeSequence newQualSeq in qualSequenceList) { fastQFormatter.Format(newQualSeq); } } else { fastQFormatter.Format(qualSequenceList.First()); } } // temp file is closed. // Read the new file and validate the first Sequence. FastQParser fastQParserObjNew = new FastQParser(); IQualitativeSequence firstSequence = fastQParserObjNew.ParseOne(tempFileName); // Validate qualitative Sequence upon parsing FastQ file. Assert.AreEqual(expectedQualitativeSequence, firstSequence.ConvertToString()); Assert.AreEqual(expectedSequenceId, firstSequence.ID); ApplicationLog.WriteLine(string.Format("FastQ Parser P1: The FASTQ sequence '{0}' validation after Parse() is found to be as expected.", firstSequence)); File.Delete(tempFileName); } }
/// <summary> /// General method to validate FastQ formatting /// Qualitative Sequence by passing TextWriter as a parameter /// <param name="nodeName">xml node name.</param> /// </summary> private void ValidateFastQFormatByFormattingQualSeqeunce(string nodeName) { // Gets the actual sequence and the alphabet from the Xml IAlphabet alphabet = Utility.GetAlphabet(utilityObj.xmlUtil.GetTextValue(nodeName, Constants.AlphabetNameNodeV2)); FastQFormatType expectedFormatType = Utility.GetFastQFormatType(utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FastQFormatType)); string qualSequence = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode); string expectedQualitativeSequence = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode); string qualityScores = ""; int i; for (i = 0; i < qualSequence.Length; i++) qualityScores = qualityScores + "}"; byte[] seq = Encoding.UTF8.GetBytes(qualSequence); byte[] qScore = Encoding.UTF8.GetBytes(qualityScores); string tempFileName = Path.GetTempFileName(); // Create a Qualitative Sequence. var qualSeq = new QualitativeSequence(alphabet, expectedFormatType, seq, qScore); var formatter = new FastQFormatter(); using (formatter.Open(tempFileName)) { formatter.Format(qualSeq); formatter.Close(); var fastQParserObj = new FastQParser(); using (fastQParserObj.Open(tempFileName)) { // Read the new file and validate Sequences. var seqsNew = fastQParserObj.Parse(); var firstSequence = seqsNew.First(); // Validate qualitative Sequence upon parsing FastQ file. Assert.AreEqual(expectedQualitativeSequence, firstSequence.ConvertToString()); Assert.IsTrue(string.IsNullOrEmpty(firstSequence.ID)); ApplicationLog.WriteLine(string.Format("FastQ Parser P1: The FASTQ sequence '{0}' validation after Parse() is found to be as expected.", firstSequence)); } File.Delete(tempFileName); } }
public static void Main(string[] args) { try { PlatformManager.Services.MaxSequenceSize = int.MaxValue; PlatformManager.Services.DefaultBufferSize = 4096; PlatformManager.Services.Is64BitProcessType = true; if (args.Length > 3) { Console.WriteLine("Too many arguments"); DisplayHelp(); } else if (args.Length < 2) { Console.WriteLine("Not enough arguments"); DisplayHelp(); } else if (args [0] == "h" || args [0] == "help" || args [0] == "?" || args [0] == "-h") { DisplayHelp(); } else { string bam_name = args [0]; string threshold = args [1]; string output = args [2]; if (!File.Exists(bam_name)) { Console.WriteLine("Can't find file: " + bam_name); return; } double min_rq; bool converted = Double.TryParse(threshold, out min_rq); if (!converted) { Console.WriteLine("Could not parse minimum threshold from : " + threshold + " expected decimal number in [0,1] interval."); return; } if (min_rq < 0.0 || min_rq > 1.0) { Console.WriteLine("Minimum RQ value: " + min_rq + " was not in [0,1] interval."); return; } if (File.Exists(output)) { Console.WriteLine("The output file already exists, please specify a new name or delete the old one."); return; } var fastq = new FastQFormatter(); fastq.FormatType = FastQFormatType.Sanger; var os = new FileStream(output, FileMode.CreateNew); // Filter and output PacBioCCSBamReader bamreader = new PacBioCCSBamReader(); int numRead = 0; int numFiltered = 0; foreach (var read in bamreader.Parse(bam_name)) { numRead++; var ccs = read as PacBioCCSRead; if (ccs.ReadQuality > min_rq) { //read.ID = read.ID + "/RQ=" + read.ReadQuality; fastq.Format(os, read); } else { numFiltered++; } } os.Close(); Console.WriteLine("Parsed " + numRead + " reads and filtered out " + numFiltered + " for RQ < " + min_rq); } } catch (DllNotFoundException thrown) { Console.WriteLine("Error thrown when attempting to generate the CCS results."); Console.WriteLine("A shared library was not found. To solve this, please add the folder" + " with the downloaded file libMonoPosixHelper" + "to your environmental variables (LD_LIBRARY_PATH and DYLD_LIBRARY_PATH on Mac OS X)."); Console.WriteLine("Error: " + thrown.Message); Console.WriteLine(thrown.StackTrace); } catch (Exception thrown) { Console.WriteLine("Error thrown when attempting to generate the FASTQ File"); Console.WriteLine("Error: " + thrown.Message); Console.WriteLine(thrown.StackTrace); while (thrown.InnerException != null) { Console.WriteLine("Inner Exception: " + thrown.InnerException.Message); thrown = thrown.InnerException; } } }
/// <summary> /// General method to validate FastQ Formatter on a Stream. /// <param name="nodeName">xml node name.</param> /// </summary> private void ValidateFastQFormatterOnAStream(string nodeName) { // Gets the expected sequence from the Xml string filePath = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode); string expectedQualitativeSequence = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode); string expectedSequenceId = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceIdNode); string tempFileName1 = Path.GetTempFileName(); // Parse a FastQ file using parseOne method. var fastQParserObj = new FastQParser(); using (fastQParserObj.Open(filePath)) { var oneSequence = fastQParserObj.ParseOne(); // New Sequence after formatting file. var fastQFormatter = new FastQFormatter(); using (fastQFormatter.Open(tempFileName1)) fastQFormatter.Format(oneSequence); var fastQParserObjTemp = new FastQParser(); string parsedValue, parsedId; using (fastQParserObjTemp.Open(tempFileName1)) { oneSequence = fastQParserObjTemp.Parse().First(); parsedValue = oneSequence.ConvertToString(); parsedId = oneSequence.ID; } // Validate qualitative parsing temporary file. Assert.AreEqual(expectedQualitativeSequence, parsedValue); Assert.AreEqual(expectedSequenceId, parsedId); ApplicationLog.WriteLine(string.Format("FastQ Formatter BVT: The FASTQ sequence '{0}' validation after Write() and Parse() is found to be as expected.", parsedValue)); ApplicationLog.WriteLine(string.Format("FastQ Formatter BVT: The FASTQ sequence '{0}' validation after Write() and Parse() is found to be as expected.", parsedId)); File.Delete(tempFileName1); } }
public static void Main(string[] args) { try { PlatformManager.Services.MaxSequenceSize = int.MaxValue; PlatformManager.Services.DefaultBufferSize = 4096; PlatformManager.Services.Is64BitProcessType = true; if (args.Length > 3) { Console.WriteLine ("Too many arguments"); DisplayHelp (); } else if (args.Length < 2) { Console.WriteLine("Not enough arguments"); DisplayHelp(); } else if (args [0] == "h" || args [0] == "help" || args [0] == "?" || args [0] == "-h") { DisplayHelp (); } else { string bam_name = args [0]; string threshold = args [1]; string output = args [2]; if (!File.Exists(bam_name)) { Console.WriteLine ("Can't find file: " + bam_name); return; } double min_rq; bool converted = Double.TryParse(threshold, out min_rq); if (!converted) { Console.WriteLine ("Could not parse minimum threshold from : " + threshold + " expected decimal number in [0,1] interval."); return; } if (min_rq < 0.0 || min_rq > 1.0) { Console.WriteLine ("Minimum RQ value: " + min_rq + " was not in [0,1] interval."); return; } if (File.Exists (output)) { Console.WriteLine ("The output file already exists, please specify a new name or delete the old one."); return; } var fastq = new FastQFormatter(); fastq.FormatType = FastQFormatType.Sanger; var os = new FileStream(output, FileMode.CreateNew); // Filter and output PacBioCCSBamReader bamreader = new PacBioCCSBamReader (); int numRead = 0; int numFiltered = 0; foreach(var read in bamreader.Parse(bam_name)) { numRead++; var ccs = read as PacBioCCSRead; if (ccs.ReadQuality > min_rq) { //read.ID = read.ID + "/RQ=" + read.ReadQuality; fastq.Format(os,read); } else { numFiltered ++; } } os.Close(); Console.WriteLine("Parsed " + numRead + " reads and filtered out " + numFiltered + " for RQ < " + min_rq); } } catch(DllNotFoundException thrown) { Console.WriteLine ("Error thrown when attempting to generate the CCS results."); Console.WriteLine("A shared library was not found. To solve this, please add the folder" + " with the downloaded file libMonoPosixHelper" + "to your environmental variables (LD_LIBRARY_PATH and DYLD_LIBRARY_PATH on Mac OS X)."); Console.WriteLine ("Error: " + thrown.Message); Console.WriteLine (thrown.StackTrace); } catch(Exception thrown) { Console.WriteLine ("Error thrown when attempting to generate the FASTQ File"); Console.WriteLine ("Error: " + thrown.Message); Console.WriteLine (thrown.StackTrace); while (thrown.InnerException != null) { Console.WriteLine ("Inner Exception: " + thrown.InnerException.Message); thrown = thrown.InnerException; } } }