samtools-merge(1) | Bioinformatics tools | samtools-merge(1) |
samtools-merge - merges multiple sorted files into a single file
samtools merge [options] -o out.bam [options] in1.bam ... inN.bam
samtools merge [options] out.bam in1.bam ... inN.bam
Merge multiple sorted alignment files, producing a single sorted output file that contains all the input records and maintains the existing sort order.
The output file can be specified via -o as shown in the first synopsis. Otherwise the first non-option filename argument is taken to be out.bam rather than an input file, as in the second synopsis. There is no default; to write to standard output (or to a pipe), use either “-o -” or the equivalent using “-” as the first filename argument.
If -h is specified the @SQ headers of input files will be merged into the specified header, otherwise they will be merged into a composite header created from the input headers. If in the process of merging @SQ lines for coordinate sorted input files, a conflict arises as to the order (for example input1.bam has @SQ for a,b,c and input2.bam has b,a,c) then the resulting output file will need to be re-sorted back into coordinate order.
Unless the -c or -p flags are specified then when merging @RG and @PG records into the output header then any IDs found to be duplicates of existing IDs in the output header will have a suffix appended to them to differentiate them from similar header records from other files and the read records will be updated to reflect this.
The ordering of the records in the input files must match the usage of the -n, -N and -t command-line options. If they do not, the output order will be undefined. Note this also extends to disallowing mixing of "queryname" files with a combination of natural and lexicographical sort orders. See sort for information about record ordering.
Problems may arise when attempting to merge thousands of files together. The operating system may impose a limit on the maximum number of simultaneously open files. See ulimit -n for more information. Additionally many files being read from simultaneously may cause a certain amount of "disk thrashing". To partially alleviate this the merge command will load 1MB of data at a time from each file, but this in turn adds to the overall merge program memory usage. Please take this into account when setting memory limits.
In extreme cases, it may be necessary to reduce the problem to fewer files by successively merging subsets before a second round of merging.
Without -c, all @RG headers appear in the output file, with random suffixes added to their IDs where necessary to differentiate them.
printf '@RG\tID:ga\tSM:hs\tLB:ga\tPL:ILLUMINA\n@RG\tID:454\tSM:hs\tLB:454\tPL:LS454\n' > rg.txt samtools merge -rh rg.txt merged.bam ga.bam 454.bam
The value in a RG tag is determined by the file name the read is coming from. In this example, in the merged.bam, reads from ga.bam will be attached RG:Z:ga, while reads from 454.bam will be attached RG:Z:454.
samtools merge [options] -X <out.bam> </data_folder/in1.bam> [</data_folder/in2.bam> ... </data_folder/inN.bam>] </index_folder/index1.bai> [</index_folder/index2.bai> ... </index_folder/indexN.bai>]
Written by Heng Li from the Sanger Institute.
samtools(1), samtools-sort(1), sam(5)
Samtools website: <http://www.htslib.org/>
24 January 2024 | samtools-1.19.2 |