Beispiel #1
0
        private VariantContext decodeLine(string line, bool includeGenotypes)
        {
            // the same line reader is not used for parsing the header and parsing lines, if we see a #, we've seen a header line
            if (line.StartsWith(VCFHeader.HEADER_INDICATOR))
            {
                //TODO: Possibly raise exception?  At least in one scenario, the VCF header has already been parsed before this is called
                //seems like this should always be true based on statement below
                throw new VCFParsingError("While decoding genotype lines came across a commented header line.  Problem is with line:\n " + line);
            }
            // our header cannot be null, we need the genotype sample names and counts
            if (header == null)
            {
                throw new VCFParsingError("VCF Header cannot be null when decoding a record");
            }

            //I think this is not necessary
            int parseSize = Math.Min(header.ColumnCount, NUM_STANDARD_FIELDS + 1);

            //TODO: Original bit of code here could do lazy genotype initalization and so could split off the
            //first 8 columns, leaving any genotype data still lumped together in the 9th column (if present).
            // string[] parts=line.Split(VCFConstants.FIELD_SEPARATOR_CHAR_AS_ARRAY,parseSize,StringSplitOptions.None);
            string[] parts = FastStringUtils.Split(line, VCFConstants.FIELD_SEPARATOR_CHAR, parseSize, StringSplitOptions.None);


            //ND - Modified this heavily, as it is imposssible for header to be null at this stage.
            // if we have don't have a header, or we have a header with no genotyping data check that we have eight columns.
            // Otherwise check that we have nine (normal colummns + genotyping data)
            if ((!header.hasGenotypingData() && parts.Length != NUM_STANDARD_FIELDS) ||
                (header.hasGenotypingData() && parts.Length != (NUM_STANDARD_FIELDS + 1)))
            {
                throw new VCFParsingError("Line " + lineNo + ": there aren't enough columns for line " + line + " (we expected " + (header == null ? NUM_STANDARD_FIELDS : NUM_STANDARD_FIELDS + 1) + " tokens, and saw " + parts.Length + " )");
            }
            return(parseVCFLine(parts, includeGenotypes));
        }
Beispiel #2
0
        void writeHeader(VCFHeader header)
        {
            header = doNotWriteGenotypes ? new VCFHeader(header.MetaDataInSortedOrder) : header;
            try
            {
                // the file format field needs to be written first
                writer.Write(VERSION_LINE + "\n");

                foreach (VCFHeaderLine line in header.MetaDataInSortedOrder)
                {
                    if (VCFHeaderVersion.IsFormatString(line.Key))
                    {
                        continue;
                    }

                    writer.Write(VCFHeader.METADATA_INDICATOR);
                    writer.Write(line.ToString());
                    writer.Write("\n");
                }

                // write out the column line
                writer.Write(VCFHeader.HEADER_INDICATOR);
                bool isFirst = true;
                foreach (string field in VCFHeader.HEADER_FIELDS)
                {
                    if (isFirst)
                    {
                        isFirst = false;                         // don't write out a field separator
                    }
                    else
                    {
                        writer.Write(VCFConstants.FIELD_SEPARATOR);
                    }
                    writer.Write(field.ToString());
                }
                if (header.hasGenotypingData())
                {
                    writer.Write(VCFConstants.FIELD_SEPARATOR);
                    writer.Write("FORMAT");
                    foreach (string sample in header.GenotypeSampleNames)
                    {
                        writer.Write(VCFConstants.FIELD_SEPARATOR);
                        writer.Write(sample);
                    }
                }
                writer.Write("\n");
            }
            catch (IOException e)
            {
                throw new Exception("IOException writing the VCF header.", e);
            }
        }