Ejemplo n.º 1
0
        /// <summary>
        /// For each sequence read by the parser, pass it to the metric handler. Also update GUI summary panels
        /// </summary>
        private void ProcessSequences(string filename, BAMParser parser)
        {
            double incrementOne           = 1;                           // the progress bar will update every [incrementOne] many clusters
            double incrementTwo           = 1;                           // the progress bar will update every [incrementTwo] many clusters
            int    increaseIncrementAfter = 100;                         // after this many clusters, increase the increment at which we update the gui
            double increment;                                            // the current increment at which progress bar will update
            int    updateDisplayForClusterIndex = -1, clusterCount = -1; // whether we have already updated the gui for this cluster

            foreach (SAMAlignedSequence se in parser.ParseSequenceAsEnumerable(filename))
            {
                if (metricHandler.Add(se))
                {
                    clusterCount = metricHandler.ClusterCount;
                    increment    = (clusterCount < increaseIncrementAfter) ? incrementOne : incrementTwo;
                    if ((clusterCount == 1 || (double)clusterCount % increment == 0) && clusterCount != updateDisplayForClusterIndex)
                    {
                        updateDisplayForClusterIndex = clusterCount;

                        Dispatcher.BeginInvoke(
                            System.Windows.Threading.DispatcherPriority.Normal,
                            new StatsDelegate(UpdateStatsPanel), metricHandler.GoodCount, clusterCount,
                            metricHandler.MaxSampleCount, metricHandler.MaxAlignmentQuality, metricHandler.MaxReadQuality,
                            metricHandler.AverageDirt, metricHandler.AverageMapQ, metricHandler.AverageReadQ,
                            metricHandler.AverageDirtGood, metricHandler.AverageMapQGood, metricHandler.AverageReadQGood);
                    }
                }
                else
                {
                    Console.WriteLine(Properties.Resources.HANDLER_FINISHED);
                    break;
                }
            }
            // Tell the handler that there are no more sequences to receive
            metricHandler.SetComplete();
        }
        /// <summary>
        /// Processess a bam file dividing it into Cluster that it then yields back.
        /// </summary>
        public IEnumerable <Cluster> ProcessSequences(string filename)
        {
            //keep track of last cluster and last position
            Cluster curCluster = null;
            //also make sure things are sorted
            int       lastPos = int.MinValue;
            int       lastEnd = int.MinValue;
            BAMParser bp      = new BAMParser();

            foreach (SAMAlignedSequence seq in bp.ParseSequenceAsEnumerable(filename))
            {
                ReadsProcessed++;
                if (seq.Pos < 1 || seq.CIGAR == "*" || seq.Flag.HasFlag(SAMFlags.UnmappedQuery))
                {
                    UnmappedReadsProcessed++;
                    continue;
                }
                //SKIP POSITION AND GET
                if (seq.Pos > 0 && curCluster != null &&
                    seq.RName == curCluster.GenomeLocation.ID && seq.Pos < lastPos)
                {
                    throw new PloidulatorException("BAM file is not in sorted order.  Problem appeared at read: " + seq.ToString());
                }
                if (seq.Pos > lastEnd || seq.RName != curCluster.GenomeLocation.ID)
                {
                    if (curCluster != null)
                    {
                        yield return(curCluster);
                    }
                    curCluster = new Cluster(seq.RName, seq.Pos);
                }
                curCluster.AddRead(seq);
                lastPos = seq.Pos;
                lastEnd = seq.RefEndPos;
                if (ReadsProcessed % updateInterval == 0)
                {
                    if (UpdateHandler != null && curCluster != null)
                    {
                        ClusterProcessorUpdate cu = new ClusterProcessorUpdate()
                        {
                            CurrentCluster = curCluster.GenomeLocation.ID,
                            ReadsProcessed = ReadsProcessed,
                            UnMappedReads  = UnmappedReadsProcessed
                        };
                        UpdateHandler(cu);
                    }
                }
            }
            if (curCluster != null)
            {
                yield return(curCluster);
            }
        }