Beispiel #1
0
        /// <summary>
        /// Read data into the specified SnpCollection from the SnpCollection
        /// file having the specified filename (which may include a path). 
        /// The specified CancellationToken can be used to abort the read. 
        /// The progress parameter will be updated by this method with a 
        /// value of 0-100 to reflect the percent progress of the read.
        /// </summary>
        /// <param name="snps">The SnpCollection to receive the read data.</param>
        /// <param name="filename">The path and filename of the SnpCollection file.</param>
        /// <param name="cancel">The CancellationToken that can be used to abort the read.</param>
        /// <param name="progress">The progress parameter that will be updated to reflect 
        /// the percent progress of the read.</param>
        public static void Read(SnpCollection snps, string filename, CancellationToken cancel, Progress progress) {
            if (snps == null) throw new ArgumentNullException("The SnpCollection cannot be null.");
            if (filename == null) throw new ArgumentNullException("filename cannot be null.");
            if (String.IsNullOrWhiteSpace(filename)) throw new ArgumentOutOfRangeException("filename cannot be empty.");

            using (StreamReader reader = new StreamReader(filename)) {
                long length = 0;
                if (progress != null) length = reader.BaseStream.Length;
                string[] columns = new string[6];
                string line;
                while ((line = reader.ReadLine()) != null) {
                    cancel.ThrowIfCancellationRequested();
                    line.FastSplit(',', columns);
                    byte chr = Convert.ToByte(columns[2]);
                    if (chr == 0) chr = 23; // handles legacy use of 0 for X 
                    Snp snp;
                    if ((!String.IsNullOrWhiteSpace(columns[3]) && Char.IsDigit(columns[3][0]))) {
                        // new format snp file
                        snp = new Snp(columns[0], chr, Convert.ToInt32(columns[3]), Convert.ToSingle(columns[4]), columns[1], columns[5]);
                    } else {
                        // old SnpMap format snp file
                        snp = new Snp(columns[0], chr, -1, -1, columns[1], columns[3]);
                    }
                    snps.Add(snp);
                    if (progress != null) progress.Set(reader.BaseStream.Position, length);

                }
            }
        }
Beispiel #2
0
 public static void UpdateCentimorgans(string[] args) {
     SnpCollection snps = new SnpCollection(1, 23);
     string filename = args[1];
     SnpFile.ReadRutgers(snps, filename, new System.Threading.CancellationToken(), null);
     for (int i = 1; i <= 23; i++) snps.Add(new Snp("fake" + i, (byte) i, 0, 0, null, null));
     SnpCollection refSnps = new SnpCollection(1, 23);
     SnpFile.Read(refSnps, "RefSnps.csv");
     Snp prevSnp = null;
     foreach(Snp snp in refSnps) {
         Snp rutgerSnp = null;
         if (snps.Contains(snp.RsId)) {
             rutgerSnp = snps[snp.RsId];
             //if ((rutgerSnp.Chromosome != snp.Chromosome) || (Math.Abs(rutgerSnp.Position-snp.Position) > 10)) {
             //    Console.WriteLine("mismatched pos for " + snp.RsId + " - " + snp.Chromosome + ":" + snp.Position + " vs. " + rutgerSnp.Chromosome + ":" + rutgerSnp.Position);
             //    snp.cM = snps.ExtrapolateCentiMorganPosition(snp.Chromosome, snp.Position);
             //} else {
             //    snp.cM = snps[snp.RsId].cM;
             //}
             snp.Chromosome = rutgerSnp.Chromosome;
             snp.Position = rutgerSnp.Position;
         } 
         if ((rutgerSnp != null) && (rutgerSnp.cM > 0)) {
             snp.cM = rutgerSnp.cM;
         } else {
             snp.cM = snps.ExtrapolateCentiMorganPosition(snp.Chromosome, snp.Position);
         }
         if ((prevSnp != null) && (prevSnp.Chromosome == snp.Chromosome)
             && (prevSnp.cM > snp.cM)) {
             Console.WriteLine("cM out of sequence " + prevSnp.RsId + "-" + snp.RsId);
         }
         prevSnp = snp;
     }
     SnpFile.Write(refSnps, "RefSnps2.csv");
 }
Beispiel #3
0
        /// <summary>
        /// Read data into the specified SnpCollection from the Rutgers SNP
        /// map file having the specified filename (which may include a path). 
        /// The specified CancellationToken can be used to abort the read. 
        /// The progress parameter will be updated by this method with a 
        /// value of 0-100 to reflect the percent progress of the read.
        /// </summary>
        /// </summary>
        /// <remarks>See http://compgen.rutgers.edu/RutgersMap/AffymetrixIllumina.aspx </remarks>
        /// <param name="snps">The SnpCollection to receive the read data.</param>
        /// <param name="filename">The path and filename of the Rutgers SNP map file.</param>
        /// <param name="cancel">The CancellationToken that can be used to abort the read.</param>
        /// <param name="progress">The progress parameter that will be updated to reflect 
        /// the percent progress of the read.</param>
        public static void ReadRutgers(SnpCollection snps, string filename, CancellationToken cancel, Progress progress) {
            if (snps == null) throw new ArgumentNullException("The SnpCollection cannot be null.");
            if (filename == null) throw new ArgumentNullException("filename cannot be null.");
            if (String.IsNullOrWhiteSpace(filename)) throw new ArgumentOutOfRangeException("filename cannot be empty.");

            using (StreamReader reader = new StreamReader(filename)) {
                long length = 0;
                if (progress != null) length = reader.BaseStream.Length;
                string[] columns = new string[4];
                string line;
                reader.ReadLine(); // skip header
                while ((line = reader.ReadLine()) != null) {
                    cancel.ThrowIfCancellationRequested();
                    line.FastSplit(',', columns);
                    byte? chr = Snp.ChromosomeToByte(columns[1]);
                    if (chr.HasValue && (chr.Value >= 1) && (chr.Value <= 23)) {
                        float cM;
                        Snp snp;
                        if (float.TryParse(columns[3], out cM)) {
                            snp = new Snp(columns[0], chr.Value, Convert.ToInt32(columns[2]), cM, null, null);
                        } else {
                            snp = new Snp(columns[0], chr.Value, Convert.ToInt32(columns[2]));
                        }
                        snps.Add(snp);
                    }
                    if (progress != null) progress.Set(reader.BaseStream.Position, length);
                }
            }
        }
Beispiel #4
0
 /// <summary>
 /// Gets an SnpCollection of the SNPs contained in this Genome.
 /// </summary>
 /// <returns>An SnpCollection.</returns>
 protected SnpCollection GetSnps() {
     lock (lockObj) {
         if (this.snps == null) {
             var looseSnps = this.genotypes.Keys.ToArray<Snp>();
             //Array.Sort(looseSnps);
             ushort minChr = ushort.MaxValue;
             ushort maxChr = ushort.MinValue;
             foreach (var snp in looseSnps) {
                 if (snp.Chromosome < minChr) minChr = snp.Chromosome;
                 if (snp.Chromosome > maxChr) maxChr = snp.Chromosome;
             }
             snps = new SnpCollection(minChr, maxChr);
             foreach (var snp in looseSnps) snps.Add(snp);
         }
         return this.snps;
     }
 }