/// <summary> /// Check the status of job and if it is in ready status than get results /// </summary> /// <param name="serviceParameters">job id , control id</param> /// <returns>alignment</returns> public ClustalWResult FetchResultsSync(ServiceParameters serviceParameters) { ISequenceAlignment alignment = null; int retrycount = 0; ServiceRequestInformation info; if (null == serviceParameters) { throw new ArgumentNullException("Parameters"); } do { info = GetRequestStatus(serviceParameters); if (info.Status == ServiceRequestStatus.Ready) { break; } retrycount++; Thread.Sleep(Constants.ClusterRetryInterval * retrycount); }while (retrycount < 10); if (info.Status == ServiceRequestStatus.Ready) { IList <ISequenceAlignment> alignments = null; string output = _baseClient.GetOutputAsString(serviceParameters.JobId, serviceParameters.Parameters[CONTROLID].ToString()); using (StringReader reader = new StringReader(output)) { alignments = _ClustalWParser.Parse(reader); } alignment = alignments[0]; } return(new ClustalWResult(alignment)); }
/// <summary> /// Writes specified alignment object to a file. The output is formatted according to the BAM specification. /// Also creates index file in the same location that of the specified filename depending on the CreateIndexFile property. /// If the specified filename is sample.bam then the index file name will be sample.bam.bai. /// </summary> /// <param name="sequenceAlignment">SequenceAlignmentMap object.</param> /// <param name="filename">BAM file name to write BAM data.</param> public void Format(ISequenceAlignment sequenceAlignment, string filename) { if (sequenceAlignment == null) { throw new ArgumentNullException("sequenceAlignment"); } if (string.IsNullOrWhiteSpace(filename)) { throw new ArgumentNullException("filename"); } using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite)) { if (CreateIndexFile) { using (BAMIndexFile bamIndexFile = new BAMIndexFile(filename + Resource.BAM_INDEXFILEEXTENSION, FileMode.Create, FileAccess.Write)) { WriteSequenceAlignment(sequenceAlignment, fs, bamIndexFile); } } else { WriteSequenceAlignment(sequenceAlignment, fs, null); } } }
/// <summary> /// Writes an ISequenceAlignment to the location specified by the stream. /// </summary> /// <param name="stream">The Stream used to write the formatted sequence alignment text.</param> /// <param name="sequenceAlignment">The sequence alignment to format.</param> public void Format(Stream stream, ISequenceAlignment sequenceAlignment) { if (sequenceAlignment == null) { throw new ArgumentNullException(Properties.Resource.ParameterNameSequenceAlignment); } if (stream == null) { throw new ArgumentNullException("stream"); } using (var writer = stream.OpenWrite()) { SAMAlignmentHeader header = sequenceAlignment.Metadata[Helper.SAMAlignmentHeaderKey] as SAMAlignmentHeader; if (header != null) { WriteHeader(writer, header); } foreach (IAlignedSequence alignedSequence in sequenceAlignment.AlignedSequences) { WriteSAMAlignedSequence(writer, alignedSequence); } } }
/// <summary> /// Writes specified alignment object to a file. /// The output is formatted according to the BAM specification. /// </summary> /// <param name="sequenceAlignment">SequenceAlignmentMap object.</param> /// <param name="bamFilename">BAM filename to write BAM data.</param> /// <param name="indexFilename">BAM index filename to write index data.</param> public void Format(ISequenceAlignment sequenceAlignment, string bamFilename, string indexFilename) { if (sequenceAlignment == null) { throw new ArgumentNullException("sequenceAlignment"); } if (string.IsNullOrWhiteSpace(bamFilename)) { throw new ArgumentNullException("bamFilename"); } if (string.IsNullOrWhiteSpace(indexFilename)) { throw new ArgumentNullException("indexFilename"); } if (bamFilename.Equals(indexFilename)) { throw new ArgumentException(Resource.BAM_BAMFileNIndexFileContbeSame); } using (FileStream fs = new FileStream(bamFilename, FileMode.Create, FileAccess.ReadWrite)) { using (BAMIndexFile bamIndexFile = new BAMIndexFile(indexFilename, FileMode.Create, FileAccess.Write)) { WriteSequenceAlignment(sequenceAlignment, fs, bamIndexFile); } } }
/// <summary> /// Writes sequence alignment to specified stream. /// </summary> /// <param name="sequenceAlignment">Sequence alignment object</param> /// <param name="writer">Stream to write.</param> /// <param name="indexFile">BAMIndex file.</param> private void WriteSequenceAlignment(ISequenceAlignment sequenceAlignment, Stream writer, BAMIndexFile indexFile) { // validate sequenceAlignment. SequenceAlignmentMap sequenceAlignmentMap = ValidateAlignment(sequenceAlignment); string tempFilename = Path.GetTempFileName(); // write bam struct to temp file. using (FileStream fstemp = new FileStream(tempFilename, FileMode.Create, FileAccess.ReadWrite)) { WriteUncompressed(sequenceAlignmentMap, fstemp); fstemp.Seek(0, SeekOrigin.Begin); // Compress and write to the specified stream. CompressBAMFile(fstemp, writer); // if index file need to be created. if (indexFile != null) { writer.Seek(0, SeekOrigin.Begin); CreateIndexFile(writer, indexFile); } } // delete the temp file. File.Delete(tempFilename); }
/// <summary> /// Write out the given SequenceAlignmentMap to the file /// </summary> /// <param name="formatter">BAMFormatter</param> /// <param name="sam">SequenceAlignmentMap</param> /// <param name="filename">File to write to</param> /// <param name="indexFilename">BAM index file</param> public static void Format(this BAMFormatter formatter, ISequenceAlignment sam, string filename, string indexFilename) { if (formatter == null) { throw new ArgumentNullException("formatter"); } if (sam == null) { throw new ArgumentNullException("sam"); } if (string.IsNullOrWhiteSpace(filename)) { throw new ArgumentNullException("filename"); } if (string.IsNullOrWhiteSpace(indexFilename)) { throw new ArgumentNullException("indexFilename"); } if (filename == indexFilename) { throw new ArgumentException("Use different filenames for index and alignment.", "indexFilename"); } using (var fs = File.Create(filename)) using (var bamIndexFile = new BAMIndexStorage(File.Create(indexFilename))) { formatter.Format(fs, bamIndexFile, sam); } }
/// <summary> /// Writes an ISequenceAlignment to the location specified by the writer. /// </summary> /// <param name="sequenceAlignment">The sequence alignment to format.</param> /// <param name="writer">The TextWriter used to write the formatted sequence alignment text.</param> public void Format(ISequenceAlignment sequenceAlignment, TextWriter writer) { if (sequenceAlignment == null) { throw new ArgumentNullException(Resource.ParameterNameSequenceAlignment); } if (writer == null) { throw new ArgumentNullException(Resource.ParameterNameWriter); } #region Write alignment header SAMAlignmentHeader header = sequenceAlignment.Metadata[Helper.SAMAlignmentHeaderKey] as SAMAlignmentHeader; if (header != null) { WriteHeader(header, writer); } #endregion #region Write aligned sequences foreach (IAlignedSequence alignedSequence in sequenceAlignment.AlignedSequences) { WriteSAMAlignedSequence(alignedSequence, writer); } #endregion writer.Flush(); }
/// <summary> /// Initializes a new instance of the SequenceAlignment class /// Internal constructor to create SequenceAlignemnt from ISequenceAlignment. /// </summary> /// <param name="seqAlignment">Sequence Alignment</param> internal SequenceAlignment(ISequenceAlignment seqAlignment) { Metadata = seqAlignment.Metadata; AlignedSequences = new List <IAlignedSequence>(seqAlignment.AlignedSequences); Documentation = seqAlignment.Documentation; Sequences = new List <ISequence>(seqAlignment.Sequences); }
/// <summary> /// Writes a single sequence to the formatter. /// </summary> /// <param name="formatter">Formatter</param> /// <param name="sequence">Sequence</param> public static void Format(this ISequenceAlignmentFormatter formatter, ISequenceAlignment sequence) { var fs = ParserFormatterExtensions<ISequenceAlignmentFormatter>.GetOpenStream(formatter, true); if (fs != null) formatter.Format(fs, sequence); else throw new Exception("You must open a formatter before calling Write."); }
/// <summary> /// Parses a list of sequence alignment texts from a file. /// </summary> /// <param name="fileName">The name of a sequence alignment file.</param> /// <returns>The list of parsed ISequenceAlignment objects.</returns> IList <ISequenceAlignment> ISequenceAlignmentParser.Parse(string fileName) { ISequenceAlignment alignment = Parse(fileName); return(new List <ISequenceAlignment>() { alignment }); }
/// <summary> /// Parses a list of sequence alignment texts from a file. /// </summary> /// <param name="fileName">The name of a sequence alignment file.</param> /// <param name="isReadOnly"> /// Flag to indicate whether the resulting sequences in the sequence alignment should be in /// readonly mode or not. If this flag is set to true then the resulting sequences's /// isReadOnly property will be set to true, otherwise it will be set to false. /// </param> /// <returns>The list of parsed ISequenceAlignment objects.</returns> IList <ISequenceAlignment> ISequenceAlignmentParser.Parse(string fileName, bool isReadOnly) { ISequenceAlignment alignment = Parse(fileName, isReadOnly); return(new List <ISequenceAlignment>() { alignment }); }
/// <summary> /// Validate parser parse one method overloads with filePath\textreader /// </summary> /// <param name="nodeName">xml node name</param> /// <param name="parseTypes">enum type to execute different overload</param> void ValidateSAMParserWithParseOne(string nodeName, ParseOrFormatTypes parseTypes) { // Gets the expected sequence from the Xml string filePath = Utility._xmlUtil.GetTextValue( nodeName, Constants.FilePathNode); string expectedSequenceFile = Utility._xmlUtil.GetTextValue( nodeName, Constants.ExpectedSequence); ISequenceAlignmentParser parser = new SAMParser(); ISequenceAlignment alignment = null; // Parse SAM File switch (parseTypes) { case ParseOrFormatTypes.ParseOrFormatText: using (TextReader reader = new StreamReader(filePath)) { alignment = parser.ParseOne(reader); } break; case ParseOrFormatTypes.ParseOrFormatTextWithFlag: using (TextReader reader = new StreamReader(filePath)) { alignment = parser.ParseOne(reader, true); } break; case ParseOrFormatTypes.ParseOrFormatFileName: alignment = parser.ParseOne(filePath); break; case ParseOrFormatTypes.ParseOrFormatFileNameWithFlag: alignment = parser.ParseOne(filePath, true); break; } // Get expected sequences FastaParser parserObj = new FastaParser(); IList <ISequence> expectedSequences = parserObj.Parse(expectedSequenceFile); // Validate parsed output with expected output int count = 0; for (int ialigned = 0; ialigned < alignment.AlignedSequences.Count; ialigned++) { for (int iseq = 0; iseq < alignment.AlignedSequences[ialigned].Sequences.Count; iseq++) { Assert.AreEqual(expectedSequences[count].ToString(), alignment.AlignedSequences[ialigned].Sequences[iseq].ToString()); count++; } } }
/// <summary> /// Validate parser parse one method overloads with filePath\textreader /// </summary> /// <param name="nodeName">xml node name</param> /// <param name="parseTypes">enum type to execute different overload</param> void ValidateSAMParserWithParseOne(string nodeName, ParseOrFormatTypes parseTypes) { // Gets the expected sequence from the Xml string filePath = utilityObj.xmlUtil.GetTextValue( nodeName, Constants.FilePathNode); string expectedSequenceFile = utilityObj.xmlUtil.GetTextValue( nodeName, Constants.ExpectedSequence); ISequenceAlignmentParser parser = new SAMParser(); try { ISequenceAlignment alignment = null; // Parse SAM File switch (parseTypes) { case ParseOrFormatTypes.ParseOrFormatText: using (TextReader reader = new StreamReader(filePath)) { alignment = parser.ParseOne(reader); } break; case ParseOrFormatTypes.ParseOrFormatFileName: alignment = parser.ParseOne(filePath); break; } // Get expected sequences using (FastAParser parserObj = new FastAParser(expectedSequenceFile)) { IEnumerable <ISequence> expectedSequences = parserObj.Parse(); IList <ISequence> expectedSequencesList = expectedSequences.ToList(); // Validate parsed output with expected output int count = 0; for (int ialigned = 0; ialigned < alignment.AlignedSequences.Count; ialigned++) { for (int iseq = 0; iseq < alignment.AlignedSequences[ialigned].Sequences.Count; iseq++) { Assert.AreEqual(new string(expectedSequencesList[count].Select(a => (char)a).ToArray()), new string(alignment.AlignedSequences[ialigned].Sequences[iseq].Select(a => (char)a).ToArray())); count++; } } } } finally { (parser as SAMParser).Dispose(); } }
// Validates the alignment. private SequenceAlignmentMap ValidateAlignment(ISequenceAlignment sequenceAlignment) { SequenceAlignmentMap seqAlignmentMap = sequenceAlignment as SequenceAlignmentMap; if (seqAlignmentMap != null) { ValidateAlignmentHeader(seqAlignmentMap.Header); if (CreateSortedBAMFile && SortType == BAMSortByFields.ChromosomeNameAndCoordinates) { this.refSequences = SortSequenceRanges(seqAlignmentMap.Header.GetReferenceSequenceRanges()); } else { this.refSequences = seqAlignmentMap.Header.GetReferenceSequenceRanges(); } return(seqAlignmentMap); } SAMAlignmentHeader header = sequenceAlignment.Metadata[Helper.SAMAlignmentHeaderKey] as SAMAlignmentHeader; if (header == null) { throw new ArgumentException(Properties.Resource.SAMAlignmentHeaderNotFound); } ValidateAlignmentHeader(header); seqAlignmentMap = new SequenceAlignmentMap(header); if (CreateSortedBAMFile && SortType == BAMSortByFields.ChromosomeNameAndCoordinates) { this.refSequences = SortSequenceRanges(seqAlignmentMap.Header.GetReferenceSequenceRanges()); } else { this.refSequences = seqAlignmentMap.Header.GetReferenceSequenceRanges(); } foreach (IAlignedSequence alignedSeq in sequenceAlignment.AlignedSequences) { SAMAlignedSequenceHeader alignedHeader = alignedSeq.Metadata[Helper.SAMAlignedSequenceHeaderKey] as SAMAlignedSequenceHeader; if (alignedHeader == null) { throw new ArgumentException(Properties.Resource.SAMAlignedSequenceHeaderNotFound); } SAMAlignedSequence samAlignedSeq = new SAMAlignedSequence(alignedHeader); samAlignedSeq.QuerySequence = alignedSeq.Sequences[0]; seqAlignmentMap.QuerySequences.Add(samAlignedSeq); } return(seqAlignmentMap); }
// Validates the alignment. private SequenceAlignmentMap ValidateAlignment(ISequenceAlignment sequenceAlignment) { SequenceAlignmentMap seqAlignmentMap = sequenceAlignment as SequenceAlignmentMap; if (seqAlignmentMap != null) { ValidateAlignmentHeader(seqAlignmentMap.Header); _refSequences = SortSequenceRanges(seqAlignmentMap.Header.GetReferenceSequenceRanges()); foreach (SAMAlignedSequence alignedSequence in seqAlignmentMap.QuerySequences) { string message = alignedSequence.IsValidHeader(); if (!string.IsNullOrEmpty(message)) { throw new ArgumentException(message); } ValidateSQHeader(alignedSequence.RName); } return(seqAlignmentMap); } SAMAlignmentHeader header = sequenceAlignment.Metadata[Helper.SAMAlignmentHeaderKey] as SAMAlignmentHeader; if (header == null) { throw new ArgumentException(Resource.SAMAlignmentHeaderNotFound); } ValidateAlignmentHeader(header); seqAlignmentMap = new SequenceAlignmentMap(header); _refSequences = SortSequenceRanges(seqAlignmentMap.Header.GetReferenceSequenceRanges()); foreach (IAlignedSequence alignedSeq in sequenceAlignment.AlignedSequences) { SAMAlignedSequenceHeader alignedHeader = alignedSeq.Metadata[Helper.SAMAlignedSequenceHeaderKey] as SAMAlignedSequenceHeader; if (alignedHeader == null) { throw new ArgumentException(Resource.SAMAlignedSequenceHeaderNotFound); } ValidateAlignedSequenceHeader(alignedHeader); ValidateSQHeader(alignedHeader.RName); SAMAlignedSequence samAlignedSeq = new SAMAlignedSequence(alignedHeader); samAlignedSeq.QuerySequence = alignedSeq.Sequences[0]; } return(seqAlignmentMap); }
/// <summary> /// Writes a single sequence to the formatter. /// </summary> /// <param name="formatter">Formatter</param> /// <param name="sequence">Sequence</param> public static void Format(this ISequenceAlignmentFormatter formatter, ISequenceAlignment sequence) { var fs = ParserFormatterExtensions <ISequenceAlignmentFormatter> .GetOpenStream(formatter, true); if (fs != null) { formatter.Format(fs, sequence); } else { throw new Exception("You must open a formatter before calling Write."); } }
/// <summary> /// Converts an ISequenceAlignment to a formatted string. /// </summary> /// <param name="sequenceAlignment">The sequence alignment to format.</param> /// <returns>A string of the formatted text.</returns> public string FormatString(ISequenceAlignment sequenceAlignment) { if (sequenceAlignment == null) { throw new ArgumentNullException("sequenceAlignment"); } using (TextWriter writer = new StringWriter()) { Format(sequenceAlignment, writer); return(writer.ToString()); } }
/// <summary> /// Writes specified alignment object to a stream. /// The output is formatted according to the BAM specification. /// Note that this method does not create index file. /// </summary> /// <param name="sequenceAlignment">SequenceAlignmentMap object.</param> /// <param name="writer">Stream to write BAM data.</param> public void Format(ISequenceAlignment sequenceAlignment, Stream writer) { if (sequenceAlignment == null) { throw new ArgumentNullException("sequenceAlignment"); } if (writer == null) { throw new ArgumentNullException("writer"); } WriteSequenceAlignment(sequenceAlignment, writer, null); }
/// <summary> /// Initializes a new instance of the PairwiseSequenceAlignment class. /// Internal constructor to create new instance of PairwiseSequenceAlignment /// from ISequenceAlignment. /// </summary> /// <param name="seqAlignment">ISequenceAlignment instance.</param> internal PairwiseSequenceAlignment(ISequenceAlignment seqAlignment) { _seqAlignment = new SequenceAlignment(seqAlignment); _alignedSequences = new List <PairwiseAlignedSequence>(); foreach (AlignedSequence alignedSeq in seqAlignment.AlignedSequences) { _alignedSequences.Add(new PairwiseAlignedSequence(alignedSeq)); } // Clear the AlignedSequences in the _seqAlignment as this no longer needed. if (!_seqAlignment.AlignedSequences.IsReadOnly) { _seqAlignment.AlignedSequences.Clear(); } }
public void ClustalWParseOne() { string filepath = @"testdata\ClustalW\AlignmentData.aln"; Assert.IsTrue(File.Exists(filepath)); IList <Dictionary <string, string> > expectedOutput = new List <Dictionary <string, string> >(); Dictionary <string, string> expectedAlignment = new Dictionary <string, string>(); expectedAlignment["CYS1_DICDI"] = "-----MKVILLFVLAVFTVFVSS---------------RGIPPEEQ------------SQ" + "FLEFQDKFNKKY-SHEEYLERFEIFKSNLGKIEELNLIAINHKADTKFGVNKFADLSSDE" + "FKNYYLNNKEAIFTDDLPVADYLDDEFINSIPTAFDWRTRG-AVTPVKNQGQCGSCWSFS" + "TTGNVEGQHFISQNKLVSLSEQNLVDCDHECMEYEGEEACDEGCNGGLQPNAYNYIIKNG" + "GIQTESSYPYTAETGTQCNFNSANIGAKISNFTMIP-KNETVMAGYIVSTGPLAIAADAV" + "E-WQFYIGGVF-DIPCN--PNSLDHGILIVGYSAKNTIFRKNMPYWIVKNSWGADWGEQG" + "YIYLRRGKNTCGVSNFVSTSII--"; expectedAlignment["ALEU_HORVU"] = "MAHARVLLLALAVLATAAVAVASSSSFADSNPIRPVTDRAASTLESAVLGALGRTRHALR" + "FARFAVRYGKSYESAAEVRRRFRIFSESLEEVRSTN----RKGLPYRLGINRFSDMSWEE" + "FQATRL-GAAQTCSATLAGNHLMRDA--AALPETKDWREDG-IVSPVKNQAHCGSCWTFS" + "TTGALEAAYTQATGKNISLSEQQLVDCAGGFNNF--------GCNGGLPSQAFEYIKYNG" + "GIDTEESYPYKGVNGV-CHYKAENAAVQVLDSVNITLNAEDELKNAVGLVRPVSVAFQVI" + "DGFRQYKSGVYTSDHCGTTPDDVNHAVLAVGYGVENGV-----PYWLIKNSWGADWGDNG" + "YFKMEMGKNMCAIATCASYPVVAA"; expectedAlignment["CATH_HUMAN"] = "------MWATLPLLCAGAWLLGV--------PVCGAAELSVNSLEK------------FH" + "FKSWMSKHRKTY-STEEYHHRLQTFASNWRKINAHN----NGNHTFKMALNQFSDMSFAE" + "IKHKYLWSEPQNCSAT--KSNYLRGT--GPYPPSVDWRKKGNFVSPVKNQGACGSCWTFS" + "TTGALESAIAIATGKMLSLAEQQLVDCAQDFNNY--------GCQGGLPSQAFEYILYNK" + "GIMGEDTYPYQGKDGY-CKFQPGKAIGFVKDVANITIYDEEAMVEAVALYNPVSFAFEVT" + "QDFMMYRTGIYSSTSCHKTPDKVNHAVLAVGYGEKNGI-----PYWIVKNSWGPQWGMNG" + "YFLIERGKNMCGLAACASYPIPLV"; expectedOutput.Add(expectedAlignment); List <ISequenceAlignment> actualOutput = new List <ISequenceAlignment>(); ISequenceAlignment actualAlignment = null; ISequenceAlignmentParser parser = new ClustalWParser(); using (StreamReader reader = File.OpenText(filepath)) { actualAlignment = parser.ParseOne(reader); } actualOutput.Add(actualAlignment); CompareOutput(actualOutput, expectedOutput); }
/// <summary> /// Writes an ISequenceAlignment to the specified file. /// </summary> /// <param name="sequenceAlignment">The sequence alignment to format.</param> /// <param name="filename">The name of the file to write the formatted sequence alignment text.</param> public void Format(ISequenceAlignment sequenceAlignment, string filename) { if (sequenceAlignment == null) { throw new ArgumentNullException("sequenceAlignment"); } if (string.IsNullOrEmpty(filename)) { throw new ArgumentNullException("filename"); } using (TextWriter writer = new StreamWriter(filename)) { Format(sequenceAlignment, writer); } }
/// <summary> /// Validate parser parse one method overloads with filePath\textreader /// </summary> /// <param name="nodeName">xml node name</param> /// <param name="parseTypes">enum type to execute different overload</param> void ValidateSAMParserWithParseOne(string nodeName, ParseOrFormatTypes parseTypes) { // Gets the expected sequence from the Xml string filePath = utilityObj.xmlUtil.GetTextValue( nodeName, Constants.FilePathNode).TestDir(); string expectedSequenceFile = utilityObj.xmlUtil.GetTextValue( nodeName, Constants.ExpectedSequence).TestDir(); ISequenceAlignmentParser parser = new SAMParser(); ISequenceAlignment alignment = null; // Parse SAM File switch (parseTypes) { case ParseOrFormatTypes.ParseOrFormatText: using (var reader = File.OpenRead(filePath)) { alignment = parser.ParseOne(reader); } break; case ParseOrFormatTypes.ParseOrFormatFileName: alignment = parser.ParseOne(filePath); break; } // Get expected sequences FastAParser parserObj = new FastAParser(); { IEnumerable <ISequence> expectedSequences = parserObj.Parse(expectedSequenceFile); IList <ISequence> expectedSequencesList = expectedSequences.ToList(); // Validate parsed output with expected output int count = 0; foreach (IAlignedSequence alignedSequence in alignment.AlignedSequences) { foreach (ISequence sequence in alignedSequence.Sequences) { Assert.AreEqual(expectedSequencesList[count].ConvertToString(), sequence.ConvertToString()); count++; } } } }
/// <summary> /// Writes a sequence to the formatter. /// </summary> /// <param name="formatter">Formatter</param> /// <param name="sequence">Sequence to write.</param> /// <param name="fileName">Filename to write to</param> public static void Format(this ISequenceAlignmentFormatter formatter, ISequenceAlignment sequence, string fileName) { // In case this extensions method is in scope, we will forward // to the BAMFormatter extension to properly handle the index // file. if (formatter is BAMFormatter && sequence is SequenceAlignmentMap) { BAMFormatterExtensions.Format((BAMFormatter)formatter, (SequenceAlignmentMap)sequence, fileName); } else { using (FileStream fs = File.Create(fileName)) { formatter.Format(fs, sequence); } } }
/// <summary> /// Writes specified alignment object to a stream. /// The output is formatted according to the BAM specification. /// </summary> /// <param name="writer">Stream to write BAM data.</param> /// <param name="indexWriter">BAMIndexFile to write index data.</param> /// <param name="sequenceAlignment">SequenceAlignmentMap object.</param> public void Format(Stream writer, BAMIndexStorage indexWriter, ISequenceAlignment sequenceAlignment) { if (sequenceAlignment == null) { throw new ArgumentNullException("sequenceAlignment"); } if (writer == null) { throw new ArgumentNullException("writer"); } if (indexWriter == null) { throw new ArgumentNullException("indexWriter"); } WriteSequenceAlignment(sequenceAlignment, writer, indexWriter); }
// Validates the alignment. private SequenceAlignmentMap ValidateAlignment(ISequenceAlignment sequenceAlignment) { SequenceAlignmentMap seqAlignmentMap = sequenceAlignment as SequenceAlignmentMap; if (seqAlignmentMap != null) { ValidateAlignmentHeader(seqAlignmentMap.Header); _refSequences = SortSequenceRanges(seqAlignmentMap.Header.GetReferenceSequenceRanges()); return(seqAlignmentMap); } SAMAlignmentHeader header = sequenceAlignment.Metadata[Helper.SAMAlignmentHeaderKey] as SAMAlignmentHeader; if (header == null) { throw new ArgumentException(Resource.SAMAlignmentHeaderNotFound); } ValidateAlignmentHeader(header); seqAlignmentMap = new SequenceAlignmentMap(header); _refSequences = SortSequenceRanges(seqAlignmentMap.Header.GetReferenceSequenceRanges()); foreach (IAlignedSequence alignedSeq in sequenceAlignment.AlignedSequences) { SAMAlignedSequenceHeader alignedHeader = alignedSeq.Metadata[Helper.SAMAlignedSequenceHeaderKey] as SAMAlignedSequenceHeader; if (alignedHeader == null) { throw new ArgumentException(Resource.SAMAlignedSequenceHeaderNotFound); } SAMAlignedSequence samAlignedSeq = new SAMAlignedSequence(alignedHeader); samAlignedSeq.QuerySequence = alignedSeq.Sequences[0]; seqAlignmentMap.QuerySequences.Add(samAlignedSeq); } return(seqAlignmentMap); }
/// <summary> /// Validate sequence alignment instance using different aligners /// </summary> /// <param name="nodeName">xml node name</param> /// <param name="aligner">sw/nw/pw aligners</param> private void ValidateSequenceAlignment(string nodeName, ISequenceAligner aligner) { IAlphabet alphabet = Utility.GetAlphabet(this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.AlphabetNameNode)); string origSequence1 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceNode1); string origSequence2 = this.utilityObj.xmlUtil.GetTextValue(nodeName, Constants.SequenceNode2); // Create input sequences var inputSequences = new List <ISequence>(); inputSequences.Add(new Sequence(alphabet, origSequence1)); inputSequences.Add(new Sequence(alphabet, origSequence2)); // Get aligned sequences IList <ISequenceAlignment> alignments = aligner.Align(inputSequences); ISequenceAlignment alignment = alignments[0]; Assert.AreEqual(alignments[0].AlignedSequences.Count, alignment.AlignedSequences.Count); Assert.AreEqual(alignments[0].Metadata, alignment.Metadata); Assert.AreEqual(inputSequences[0].ToString(), alignment.Sequences[0].ToString()); ApplicationLog.WriteLine(@"Alignment BVT : Validation of sequence alignment completed successfully"); }
/// <summary> /// Writes sequence alignment to specified stream. /// </summary> /// <param name="sequenceAlignment">Sequence alignment object</param> /// <param name="writer">Stream to write.</param> /// <param name="indexStorage">BAMIndex file.</param> private void WriteSequenceAlignment(ISequenceAlignment sequenceAlignment, Stream writer, BAMIndexStorage indexStorage) { // validate sequenceAlignment. SequenceAlignmentMap sequenceAlignmentMap = ValidateAlignment(sequenceAlignment); // write bam struct to temp file. using (var fstemp = PlatformManager.Services.CreateTempStream()) { WriteUncompressed(sequenceAlignmentMap, fstemp, CreateSortedBAMFile); fstemp.Seek(0, SeekOrigin.Begin); // Compress and write to the specified stream. CompressBAMFile(fstemp, writer); } // if index file need to be created. if (indexStorage != null) { writer.Seek(0, SeekOrigin.Begin); CreateBAMIndexFile(writer, indexStorage); } }
/// <summary> /// Parsers the Phylip file for different test cases based /// on Additional parameter /// </summary> /// <param name="nodeName">Xml Node name</param> /// <param name="addParam">Additional parameter</param> static void ParserGeneralTestCases(string nodeName, ParserTestAttributes addParam) { // Gets the Filename string filePath = Utility._xmlUtil.GetTextValue( nodeName, Constants.FilePathNode); Assert.IsNotEmpty(filePath); ApplicationLog.WriteLine(string.Format( "Phylip Parser BVT: Reading the File from location '{0}'", filePath)); Console.WriteLine(string.Format( "Phylip Parser BVT: Reading the File from location '{0}'", filePath)); // Get the rangelist after parsing. PhylipParser parserObj = new PhylipParser(); IList <ISequenceAlignment> sequenceAlignmentList = null; ISequenceAlignment sequenceAlignment = null; // Gets the SequenceAlignment list based on the parameters. switch (addParam) { case ParserTestAttributes.Parse: sequenceAlignmentList = parserObj.Parse(filePath); break; case ParserTestAttributes.ParseOne: sequenceAlignment = parserObj.ParseOne(filePath); break; case ParserTestAttributes.ParseTextReader: sequenceAlignmentList = parserObj.Parse( new StreamReader(filePath)); break; case ParserTestAttributes.ParseOneTextReader: sequenceAlignment = parserObj.ParseOne( new StreamReader(filePath)); break; case ParserTestAttributes.ParseOneTextReaderReadOnly: sequenceAlignment = parserObj.ParseOne( new StreamReader(filePath), false); break; case ParserTestAttributes.ParseTextReaderReadOnly: sequenceAlignmentList = parserObj.Parse( new StreamReader(filePath), false); break; case ParserTestAttributes.ParseReadOnly: sequenceAlignmentList = parserObj.Parse(filePath, false); break; case ParserTestAttributes.ParseOneReadOnly: sequenceAlignment = parserObj.ParseOne(filePath, false); break; case ParserTestAttributes.ParseEncoding: PhylipParser parser = new PhylipParser(Encodings.Ncbi4NA); sequenceAlignmentList = parser.Parse( new StreamReader(filePath), false); break; default: break; } // Gets all the expected values from xml. IList <Dictionary <string, string> > expectedAlignmentList = new List <Dictionary <string, string> >(); Dictionary <string, string> expectedAlignmentObj = new Dictionary <string, string>(); XmlNode expectedAlignmentNodes = Utility._xmlUtil.GetNode( nodeName, Constants.ExpectedAlignmentNode); XmlNodeList alignNodes = expectedAlignmentNodes.ChildNodes; // Create a ISequenceAlignment List switch (addParam) { case ParserTestAttributes.ParseOne: case ParserTestAttributes.ParseOneTextReader: case ParserTestAttributes.ParseOneTextReaderReadOnly: case ParserTestAttributes.ParseOneReadOnly: sequenceAlignmentList = new List <ISequenceAlignment>(); sequenceAlignmentList.Add(sequenceAlignment); break; default: break; } foreach (XmlNode expectedAlignment in alignNodes) { expectedAlignmentObj[expectedAlignment.Name] = expectedAlignment.InnerText; } expectedAlignmentList.Add(expectedAlignmentObj); Assert.IsTrue(CompareOutput(sequenceAlignmentList, expectedAlignmentList)); ApplicationLog.WriteLine( "Phylip Parser BVT: Successfully validated all the Alignment Sequences"); Console.WriteLine( "Phylip Parser BVT: Successfully validated all the Alignment Sequences"); }
// Validates the alignment. private SequenceAlignmentMap ValidateAlignment(ISequenceAlignment sequenceAlignment) { SequenceAlignmentMap seqAlignmentMap = sequenceAlignment as SequenceAlignmentMap; if (seqAlignmentMap != null) { ValidateAlignmentHeader(seqAlignmentMap.Header); if (CreateSortedBAMFile && SortType == BAMSortByFields.ChromosomeNameAndCoordinates) { this.refSequences = SortSequenceRanges(seqAlignmentMap.Header.GetReferenceSequenceRanges()); } else { this.refSequences = seqAlignmentMap.Header.GetReferenceSequenceRanges(); } return seqAlignmentMap; } SAMAlignmentHeader header = sequenceAlignment.Metadata[Helper.SAMAlignmentHeaderKey] as SAMAlignmentHeader; if (header == null) { throw new ArgumentException(Properties.Resource.SAMAlignmentHeaderNotFound); } ValidateAlignmentHeader(header); seqAlignmentMap = new SequenceAlignmentMap(header); if (CreateSortedBAMFile && SortType == BAMSortByFields.ChromosomeNameAndCoordinates) { this.refSequences = SortSequenceRanges(seqAlignmentMap.Header.GetReferenceSequenceRanges()); } else { this.refSequences = seqAlignmentMap.Header.GetReferenceSequenceRanges(); } foreach (IAlignedSequence alignedSeq in sequenceAlignment.AlignedSequences) { SAMAlignedSequenceHeader alignedHeader = alignedSeq.Metadata[Helper.SAMAlignedSequenceHeaderKey] as SAMAlignedSequenceHeader; if (alignedHeader == null) { throw new ArgumentException(Properties.Resource.SAMAlignedSequenceHeaderNotFound); } SAMAlignedSequence samAlignedSeq = new SAMAlignedSequence(alignedHeader); samAlignedSeq.QuerySequence = alignedSeq.Sequences[0]; seqAlignmentMap.QuerySequences.Add(samAlignedSeq); } return seqAlignmentMap; }
/// <summary> /// Always throws NotSupportedException as BAM formatter does not support writing to textfile. /// </summary> /// <param name="sequenceAlignment">SequenceAlignmentMap object.</param> /// <param name="writer">Text writer.</param> public void Format(ISequenceAlignment sequenceAlignment, TextWriter writer) { throw new NotSupportedException(Resource.BAM_TextWriterNotSupported); }
/// <summary> /// Always throws NotSupportedException as BAM format is binary format. /// </summary> /// <param name="sequenceAlignment">SequenceAlignmentMap object.</param> public string FormatString(ISequenceAlignment sequenceAlignment) { throw new NotSupportedException(Resource.BAM_FormatStringNotSupported); }
/// <summary> /// Writes specified alignment object to a stream. /// The output is formatted according to the BAM specification. /// Note that this method does not create index file. /// </summary> /// <param name="sequenceAlignment">SequenceAlignmentMap object.</param> /// <param name="writer">Stream to write BAM data.</param> public void Format(Stream writer, ISequenceAlignment sequenceAlignment) { if (sequenceAlignment == null) { throw new ArgumentNullException("sequenceAlignment"); } if (writer == null) { throw new ArgumentNullException("writer"); } WriteSequenceAlignment(sequenceAlignment, writer, null); }
/// <summary> /// Constructor to set the SequenceAlignment result /// </summary> /// <param name="sequenceAlignment">Sequence alignment object</param> public ClustalWResult(ISequenceAlignment sequenceAlignment) { _sequenceAlignment = sequenceAlignment; }