Use bam_init1/bam_destroy1 instead of creating/destroying by hand

Prefer explicit NULLing to historically non-portable memset obscureness.
Make samtools merge work with SAM input files

The iterator API doesn't (yet) work with SAM files, as there's no way
to provide the file's headers when initialising or using the iterator.
For SAM files (in the absence of -R REGION), keep the headers around so we
can use sam_read1() rather than sam_itr_queryi(HTS_IDX_REST)/sam_itr_next().

(This will likely be followed up with an option to write SAM output.)
Avoid crashing on unopenable files [minor]

Akin to fclose(), sam_close() requires the file pointer given to it to
be non-NULL; akin to free(), hts_itr_destroy() does not.  Fixes #327.
Parse mpileup -r region against the right file's headers

Restore the code prior to a bug introduced in e7ae7f96c7e78a2dd6eabdaed57037c483951929.
Amongst other things, this ensures that files without the given
reference sequence are reported as failing to parse the region.

Move the setting of data[i]->h lower in the loop to prevent similar
bugs in future.  Ensure that data[i]->iter is always explicitly set.
Large chromosome test case now passes

Fixed by samtools/htslib@ce1a547f0e01d32fbffb62efbcca51c6ddc77d34.
Mark duplicates for name sorted files.

This is an implementation of an alterative method of marking
duplicates using a hash table of positions and orientations.

Signed-off-by: Martin O. Pollard <mp15@sanger.ac.uk>
Mark duplicates for coordinate sorted files

This is a new implementation of mark duplicates for coordinate sorted
files. It uses several hash tables, in a two pass configuration to do
this. A name hash is used to store the information about the first
read, and then a position hash is used to store colliding reads.
Finally a kill hash is used to pass this to the next phase.

Signed-off-by: Martin O. Pollard <mp15@sanger.ac.uk>
Combine coord and name sorted mkdup

Combine the coordinate sorted and name sorted mark duplicates commands.
When the mkdup command is called it will autodetect the sort order from
the @HD.

Signed-off-by: Martin O. Pollard <mp15@sanger.ac.uk>
Implement position buffer idea
Separate out position buffer into separate file and document.
Add static modifiers, comments, rename possig_part to read_vector and separate out; integrate pos_buffer into coordinate sorted mkdup. Add test.
Remove rmdup command

Remove rmdup command from samtools because it has been superceeded.