private static void MergeAndFinalizeBam(List <string> perChromPaths, ISamtoolsWrapper samtoolsWrapper, string outFolder, string outputBamName)
        {
            var finalBams = perChromPaths;
            var mergedBam = Path.Combine(outFolder, outputBamName);

            Logger.WriteToLog($"Calling samtools cat on {perChromPaths.Count} files to create {mergedBam}.");

            samtoolsWrapper.SamtoolsCat(mergedBam, finalBams);

            Logger.WriteToLog($"Calling samtools index on {mergedBam}.");
            samtoolsWrapper.SamtoolsIndex(mergedBam);
            Logger.WriteToLog("Done finalizing bam.");
        }
Exemple #2
0
        private static string MergeAndFinalizeBam(List <string> finalBams, ISamtoolsWrapper samtoolsWrapper, string outFolder, string outFileName, bool keepUnmerged = false, bool doIndex = false, bool doSort = true)
        {
            var mergedBam         = Path.Combine(outFolder, outFileName);
            var sortedMerged      = mergedBam + ".sorted";
            var sortedMergedFinal = sortedMerged + ".bam";

            Logger.WriteToLog($"Calling cat on {finalBams.Count} with output at {mergedBam}.");
            foreach (var finalBam in finalBams)
            {
                Logger.WriteToLog($"Intermediate bam: {finalBam}\t{new FileInfo(finalBam).Length}B");
            }
            samtoolsWrapper.SamtoolsCat(mergedBam, finalBams);
            if (doSort)
            {
                Logger.WriteToLog($"Calling samtools sort on {mergedBam}.");
                samtoolsWrapper.SamtoolsSort(mergedBam, sortedMerged, 3, 500);
            }

            if (doIndex)
            {
                Logger.WriteToLog($"Calling samtools index on {sortedMergedFinal}.");
                samtoolsWrapper.SamtoolsIndex(sortedMergedFinal);
            }

            Logger.WriteToLog("Done finalizing bam.");

            if (File.Exists(sortedMergedFinal) && !keepUnmerged)
            {
                Logger.WriteToLog("Deleting intermediate bams.");
                foreach (var finalBam in finalBams)
                {
                    File.Delete(finalBam);
                }
                File.Delete(mergedBam);

                Logger.WriteToLog("Finished deleting intermediate bams.");
            }

            return(sortedMergedFinal);
        }