/// <summary> /// Write annotation to the given file/stream in wiggle format. /// </summary> /// <param name="annotation">Annotation to write.</param> public void Write(WiggleAnnotation annotation) { if (annotation == null) { throw new ArgumentNullException("annotation"); } if (this.writer == null) { throw new InvalidOperationException(Properties.Resource.FileNotOpened); } // track line this.writer.Write(WiggleSchema.Track); foreach (var x in annotation.Metadata) { this.writer.Write(" " + x.Key + "=" + (x.Value.Contains(' ') ? "\"" + x.Value + "\"" : x.Value)); } this.writer.WriteLine(); // metadata this.writer.Write(annotation.AnnotationType == WiggleAnnotationType.FixedStep ? WiggleSchema.FixedStep : WiggleSchema.VariableStep); this.writer.Write(string.Format(CultureInfo.InvariantCulture, " {0}={1}", WiggleSchema.Chrom, annotation.Chromosome)); if (annotation.AnnotationType == WiggleAnnotationType.FixedStep) { this.writer.Write(string.Format(CultureInfo.InvariantCulture, " {0}={1}", WiggleSchema.Start, annotation.BasePosition)); this.writer.Write(string.Format(CultureInfo.InvariantCulture, " {0}={1}", WiggleSchema.Step, annotation.Step)); } if (annotation.Span != -1) { this.writer.Write(string.Format(CultureInfo.InvariantCulture, " {0}={1}", WiggleSchema.Span, annotation.Span)); } this.writer.WriteLine(); // write data if (annotation.AnnotationType == WiggleAnnotationType.FixedStep) { foreach (var item in annotation) { this.writer.WriteLine(item.Value); } } else { foreach (var item in annotation) { this.writer.WriteLine(item.Key + " " + item.Value); } } this.writer.Flush(); }
/// <summary> /// Writes a single sequence to the formatter. /// </summary> /// <param name="formatter">Formatter</param> /// <param name="annotation">Wiggle Annotation</param> public static void Format(this WiggleFormatter formatter, WiggleAnnotation annotation) { var fs = ParserFormatterExtensions <WiggleFormatter> .GetOpenStream(formatter, true); if (fs != null) { formatter.Format(fs, annotation); } else { throw new Exception("You must open a formatter before calling Write."); } }
/// <summary> /// Parse a wiggle file. /// </summary> /// <param name="reader">Stream to parse.</param> /// <returns>WiggleAnnotation object.</returns> public WiggleAnnotation Parse(StreamReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } if (reader.EndOfStream) { return(null); } string line; WiggleAnnotation result = ParseHeader(reader); if (result.AnnotationType == WiggleAnnotationType.FixedStep) { List <float> fixedStepValues = new List <float>(); while ((line = reader.ReadLine()) != null) { fixedStepValues.Add(float.Parse(line.Trim(), CultureInfo.InvariantCulture)); } result.SetFixedStepAnnotationData(fixedStepValues.ToArray()); } else { List <KeyValuePair <long, float> > variableStepValues = new List <KeyValuePair <long, float> >(); try { while ((line = reader.ReadLine()) != null) { if (string.IsNullOrWhiteSpace(line)) { continue; } string[] keyValue = line.Split(' ', '\t'); variableStepValues.Add(new KeyValuePair <long, float>(long.Parse(keyValue[0], CultureInfo.InvariantCulture), float.Parse(keyValue[1], CultureInfo.InvariantCulture))); } } catch { throw new FileFormatException(Properties.Resource.WiggleBadInputInFile); } result.SetVariableStepAnnotationData(variableStepValues.ToArray()); } return(result); }
/// <summary> /// Writes a single sequence to the formatter. /// </summary> /// <param name="formatter">Formatter</param> /// <param name="annotation">Wiggle Annotation</param> /// <param name="filename">Filename</param> public static void Format(this WiggleFormatter formatter, WiggleAnnotation annotation, string filename) { using (var fs = File.Create(filename)) formatter.Format(fs, annotation); }
/// <summary> /// Parse wiggle header including track line and metadata. /// </summary> /// <param name="reader">Stream reader to parse.</param> /// <returns>Wiggle annotation object initialized with data from the header.</returns> private static WiggleAnnotation ParseHeader(StreamReader reader) { WiggleAnnotation result = new WiggleAnnotation(); string line = reader.ReadLine(); // read comments while (line != null && (line.StartsWith(WiggleSchema.CommentLineStart, StringComparison.OrdinalIgnoreCase) || string.IsNullOrWhiteSpace(line))) { result.Comments.Add(line); line = reader.ReadLine(); } if (line == null || !line.StartsWith(WiggleSchema.Track + " ", StringComparison.Ordinal)) { throw new FormatException(Properties.Resource.WiggleInvalidHeader); } try { result.Metadata = ExtractMetadata(line.Substring((WiggleSchema.Track + " ").Length)); } catch { throw new FormatException(Properties.Resource.WiggleInvalidHeader); } // step and span details line = reader.ReadLine(); if (line.StartsWith(WiggleSchema.FixedStep + " ", StringComparison.Ordinal)) { result.AnnotationType = WiggleAnnotationType.FixedStep; } else if (line.StartsWith(WiggleSchema.VariableStep + " ", StringComparison.Ordinal)) { result.AnnotationType = WiggleAnnotationType.VariableStep; } else { throw new FormatException(Properties.Resource.WiggleInvalidHeader); } string[] tokens = line.Split(' '); Dictionary <string, string> encodingDetails = new Dictionary <string, string>(); for (int i = 1; i < tokens.Length; i++) { string[] metadataArray = tokens[i].Split('='); encodingDetails.Add(metadataArray[0], metadataArray[1]); } try { result.Chromosome = encodingDetails[WiggleSchema.Chrom]; string spanString; if (encodingDetails.TryGetValue(WiggleSchema.Span, out spanString)) { result.Span = int.Parse(spanString, CultureInfo.InvariantCulture); } if (result.AnnotationType == WiggleAnnotationType.FixedStep) { result.Step = int.Parse(encodingDetails[WiggleSchema.Step], CultureInfo.InvariantCulture); result.BasePosition = long.Parse(encodingDetails[WiggleSchema.Start], CultureInfo.InvariantCulture); } } catch { throw new FormatException(Properties.Resource.WiggleInvalidHeader); } return(result); }