public void write(BamWriter writer,
                              int index)
            {
                SerializedBamAlignment alignment = _bamAlignmentList[index];

                byte[] byteArray   = null;
                int    smallOffset = 0;

                _serializedAlignments.GetByteArrayAndOffset(alignment.SerializedOffset, ref byteArray, ref smallOffset);
                writer.Write(byteArray, (uint)alignment.SerializedSize, smallOffset);
            }
        void WriteListsInOrder(List <BamRecordAccessor> bamContainers, BamWriter bamWriter)
        {
            List <int> bamAlignmentListIndexes = new List <int>();

            for (int i = 0; i < bamContainers.Count; ++i)
            {
                if (!bamContainers[i].IsEnd())
                {
                    bamAlignmentListIndexes.Add(i);
                }
            }

            if (bamAlignmentListIndexes.Count == 0)
            {
                return;
            }

            BamRecordAccessorComparer recordAccessorComparer             = new BamRecordAccessorComparer();
            SortedList <BamRecordAccessor, BamRecordAccessor> sortedList = new SortedList <BamRecordAccessor, BamRecordAccessor>(bamContainers.Count, recordAccessorComparer);

            for (int i = 0; i < bamContainers.Count; ++i)
            {
                var bamContainer = bamContainers[i];
                sortedList.Add(bamContainer, bamContainer);
            }

            while (sortedList.Count > 1)
            {
                var bamContainer = sortedList.Values[0];
                sortedList.RemoveAt(0);

                bamContainer.WriteCurrentAlignment(bamWriter);
                bamContainer.MoveToNextRecord();

                if (!bamContainer.IsEnd())
                {
                    sortedList.Add(bamContainer, bamContainer);
                }
            }

            // There is only 1 list left
            var finalBamContainer = sortedList.Values[0];

            while (!finalBamContainer.IsEnd())
            {
                finalBamContainer.WriteCurrentAlignment(bamWriter);
                finalBamContainer.MoveToNextRecord();
            }
        }
        // This method is very specialized. Each list should be the same size, so sorting
        // each on its own thread is very efficient. There is one additional pass
        // to merge the lists, but the writing occurs during this iteration, so we're
        // not waiting to write.
        private void SortMemAndWrite()
        {
            Logger.WriteToLog("Sorting BAM data...");

            // Sort each of the lists
            Parallel.ForEach(_bamAlignmentLists,
                             new ParallelOptions {
                MaxDegreeOfParallelism = NumThreads
            },
                             bamAlignmentList =>
            {
                bamAlignmentList.Sort(new BamAlignmentComparer(bamAlignmentList));
            }
                             );

            Logger.WriteToLog("BAM sort complete.");

            List <BamRecordAccessor> bamListContainer = new List <BamRecordAccessor>();

            foreach (SerializedAlignmentContainer bamAlignmentList in _bamAlignmentLists)
            {
                bamListContainer.Add(new BamListContainer(bamAlignmentList));
            }

            BamWriter bamWriter = _numTempFiles > 0 ? GetNewTemporaryBamWriter() : this;

            WriteListsInOrder(bamListContainer, bamWriter);

            Logger.WriteToLog("Finished writing temporary file.");

            if (_numTempFiles > 0)
            {
                bamWriter.Dispose();

                foreach (var alignmentContainer in _bamAlignmentLists)
                {
                    alignmentContainer.Clear();
                }
            }
        }
 public override void WriteCurrentAlignment(BamWriter writer)
 {
     byte[] serializedAlignment = BamWriter.SerializeAlignment(ref _currentBamAlignment);
     writer.Write(serializedAlignment, (uint)serializedAlignment.Length);
 }
 public override void WriteCurrentAlignment(BamWriter writer)
 {
     _bamList.write(writer, _index);
 }
 public abstract void WriteCurrentAlignment(BamWriter writer);