From: John Marshall <John.W.Marshall@glasgow.ac.uk>
Date: Tue, 16 Apr 2019 11:48:55 +0100
Subject: Use new htsutil.cpp in test suite instead of samtools
Forwarded: https://github.com/arq5x/bedtools2/pull/818
--- bedtools.orig/.github/workflows/main.yml
+++ bedtools/.github/workflows/main.yml
@@ -9,8 +9,6 @@
 
     steps:
     - uses: actions/checkout@v1
-    - name: Install samtools
-      run: sudo apt-get install -y samtools 
     - name: Compile and test Bedtools
       run: make -j8 && make test
     - name: Compile static binary
--- bedtools.orig/.gitignore
+++ bedtools/.gitignore
@@ -12,12 +12,16 @@
 .cproject
 nbproject
 sandbox
+test/coverage/*.bam
+test/coverage/*.bam.bai
 test/general/o
 test/genomecov/chip.bam
+test/genomecov/merged.bam
 test/genomecov/one_block.bam
 test/genomecov/pair-chip.bam
 test/genomecov/sam-w-del.bam
 test/genomecov/three_blocks.bam
+test/htsutil
 test/multicov/one_block.bam.bai
 test/multicov/two_blocks.bam.bai
 test/genomecov/two_blocks.bam
--- bedtools.orig/Makefile
+++ bedtools/Makefile
@@ -130,7 +130,7 @@
 ALL_LIBS     = $(BT_LIBS) $(LIBS)
 
 
-all: print_banner $(BIN_DIR)/bedtools $(BIN_DIR)/intersectBed
+all: print_banner $(BIN_DIR)/bedtools $(BIN_DIR)/intersectBed test/htsutil
 
 static: print_banner $(BIN_DIR)/bedtools.static
 
@@ -163,6 +163,9 @@
 $(OBJ_DIR)/bedtools.o: $(SRC_DIR)/bedtools.cpp $(OBJ_DIR)/bedtools.d
 	$(CXX_COMPILE)
 
+$(OBJ_DIR)/htsutil.o: $(SRC_DIR)/htsutil.cpp $(OBJ_DIR)/htsutil.d
+	$(CXX_COMPILE)
+
 # HTSlib's htslib.mk provides rules to rebuild $(HTSDIR)/libhts.a.
 HTSDIR = src/utils/htslib
 include $(HTSDIR)/htslib.mk
@@ -181,6 +184,9 @@
 	$(CCPREFIX) $(CC_WRAPPER) $(CXX) -static $(ALL_LDFLAGS) -o $(BIN_DIR)/bedtools.static $(BUILT_OBJECTS) $(HTSDIR)/libhts.a $(ALL_LIBS)
 	@echo "done."
 
+test/htsutil: $(OBJ_DIR)/htsutil.o $(HTSDIR)/libhts.a
+	$(CCPREFIX) $(CC_WRAPPER) $(CXX) $(ALL_LDFLAGS) -o test/htsutil $(OBJ_DIR)/htsutil.o $(HTSDIR)/libhts.a $(ALL_LIBS)
+
 $(BIN_DIR)/intersectBed: | $(BIN_DIR)
 	@echo "- Creating executables for old CLI."
 	@python3 scripts/makeBashScripts.py
@@ -211,7 +217,7 @@
 # not existing), so clean should also remove the generated config.h.
 clean:
 	@echo " * Cleaning up."
-	@rm -f $(VERSION_FILE) $(OBJ_DIR)/* $(BIN_DIR)/*
+	@rm -f $(VERSION_FILE) $(OBJ_DIR)/* $(BIN_DIR)/* test/htsutil
 	@cd src/utils/htslib && make clean > /dev/null
 	@test -e src/utils/htslib/config.mk || rm -f src/utils/htslib/config.h
 .PHONY: clean
--- /dev/null
+++ bedtools/src/htsutil.cpp
@@ -0,0 +1,167 @@
+#include <fstream>
+#include <iostream>
+#include <string>
+
+#include <htslib/bgzf.h>
+#include <htslib/sam.h>
+
+int convert(htsExactFormat outformat, const char *outfname, bool outheaders,
+            htsExactFormat informat, const char *infname,
+            const char *reffname = NULL)
+{
+    htsFile* in = NULL;
+    htsFile* out = NULL;
+    bam_hdr_t* hdr = NULL;
+    bam1_t* b = NULL;
+
+    try {
+        in = hts_open(infname, "r");
+        if (in == NULL)
+            throw std::string("can't open ") + infname;
+
+        if (hts_get_format(in)->format != informat)
+            throw std::string(infname) + ": file is in the wrong format";
+
+        const char *outf = "w";
+        switch (outformat) {
+        case sam:  outf = "w"; break;
+        case bam:  outf = "wb"; break;
+        case cram: outf = "wc"; break;
+        default: throw std::string("invalid outformat argument");
+        }
+
+        out = hts_open(outfname, outf);
+        if (out == NULL)
+            throw std::string("can't write to ") + outfname;
+
+        if (reffname) {
+            hts_set_fai_filename(in, reffname);
+            hts_set_fai_filename(out, reffname);
+        }
+
+        hdr = sam_hdr_read(in);
+        if (hdr == NULL)
+            throw std::string("can't read headers from ") + infname;
+
+        if (outheaders) {
+            if (sam_hdr_write(out, hdr) < 0)
+                throw std::string("can't write headers to ") + outfname;
+        }
+
+        int ret;
+        b = bam_init1();
+        while ((ret = sam_read1(in, hdr, b)) >= 0) {
+            if (sam_write1(out, hdr, b) < 0)
+                throw std::string("can't write record to ") + outfname;
+        }
+        if (ret < -1)
+            throw std::string("truncated input file");
+
+        bam_destroy1(b);
+        bam_hdr_destroy(hdr);
+        hts_close(out);
+        hts_close(in);
+    }
+    catch (const std::string& what) {
+        std::cerr << "htsutil: " << what << '\n';
+        bam_destroy1(b);
+        bam_hdr_destroy(hdr);
+        if (out) hts_close(out);
+        if (in) hts_close(in);
+        return 1;
+    }
+
+    return 0;
+}
+
+int compress(const char *outfname, std::streambuf* in)
+{
+    BGZF* out = NULL;
+
+    try {
+        out = bgzf_open(outfname, "w");
+        if (out == NULL)
+            throw std::string("can't write to ") + outfname;
+
+        char buffer[65536];
+        size_t n;
+        while ((n = in->sgetn(buffer, sizeof buffer)) > 0) {
+            if (bgzf_write(out, buffer, n) < 0)
+                throw std::string("can't write to ") + outfname;
+        }
+
+        bgzf_close(out);
+    }
+    catch (const std::string& what) {
+        std::cerr << "htsutil: " << what << '\n';
+        if (out) bgzf_close(out);
+        return 1;
+    }
+
+    return 0;
+}
+
+int compress(const char *outfname, const char *infname)
+{
+    if (std::string(infname) == "-")
+        return compress(outfname, std::cin.rdbuf());
+    else {
+        std::filebuf in;
+        if (in.open(infname, std::ios::in) == NULL) {
+            std::cerr << "htsutil: can't open " << infname << '\n';
+            return 1;
+        }
+        return compress(outfname, &in);
+    }
+}
+
+int index(const char *fname, bool csi)
+{
+    if (std::string(fname) == "-") {
+        std::cerr << "htsutil: can't index standard output\n";
+        return 1;
+    }
+
+    int ret = sam_index_build(fname, csi? 14 : 0);
+    if (ret < 0) return -ret; // Convert to exit status
+    return 0;
+}
+
+int main(int argc, char **argv)
+{
+    std::string cmd = (argc >= 2)? argv[1] : "--help";
+    const char *src = (argc >= 3)? argv[2] : "-";
+    const char *dest= (argc >= 4)? argv[3] : "-";
+
+    if (cmd == "viewbamrecords")
+        return convert(sam, "-", false, bam, src);
+    else if (cmd == "viewbam")
+        return convert(sam, "-", true, bam, src);
+    else if (cmd == "viewcramrecords")
+        return convert(sam, "-", false, cram, src, (argc >= 4)? argv[3] : NULL);
+    else if (cmd == "bgzfcompress")
+        return compress(dest, src);
+    else if (cmd == "index")
+        return index(src, false);
+    else if (cmd == "indexcsi")
+        return index(src, true);
+    else if (cmd == "samtobam")
+        return convert(bam, dest, true, sam, src);
+    else if (cmd == "samtoindexedbam") {
+        int ret = convert(bam, dest, true, sam, src);
+        if (ret != 0) return ret;
+        return index(dest, false);
+    }
+    else {
+        std::cerr <<
+"Usage: htsutil bgzfcompress [SRCFILE [DESTFILE]]\n"
+"       htsutil index FILE\n"
+"       htsutil indexcsi FILE\n"
+"       htsutil samtobam [SRCFILE [DESTFILE]]\n"
+"       htsutil samtoindexedbam SRCFILE DESTFILE\n"
+"       htsutil viewbam [FILE]\n"
+"       htsutil viewbamrecords [FILE]\n"
+"       htsutil viewcramrecords [FILE [REFFILE]]\n";
+        return 1;
+    }
+}
--- bedtools.orig/test/bamtobed/test-bamtobed.sh
+++ bedtools/test/bamtobed/test-bamtobed.sh
@@ -18,13 +18,13 @@
 #                       BAM files                         #
 ###########################################################
 ###########################################################
-samtools view -Sb one_block.sam > one_block.bam 2>/dev/null
-samtools view -Sb two_blocks.sam > two_blocks.bam 2>/dev/null
-samtools view -Sb three_blocks.sam > three_blocks.bam 2>/dev/null
-#samtools view -Sb sam-w-del.sam > sam-w-del.bam 2>/dev/null
-samtools view -Sb two_blocks_w_D.sam > two_blocks_w_D.bam 2>/dev/null
-samtools view -Sb numeric_tag.sam > numeric_tag.bam 2> /dev/null
-gzip -dfc extra-long-header.sam | samtools view -Sb > extra-long-header.bam 2> /dev/null
+../htsutil samtobam one_block.sam one_block.bam
+../htsutil samtobam two_blocks.sam two_blocks.bam
+../htsutil samtobam three_blocks.sam three_blocks.bam
+#../htsutil samtobam sam-w-del.sam sam-w-del.bam
+../htsutil samtobam two_blocks_w_D.sam two_blocks_w_D.bam
+../htsutil samtobam numeric_tag.sam numeric_tag.bam
+gzip -dfc extra-long-header.sam | ../htsutil samtobam - extra-long-header.bam
 
 
 ##################################################################
--- bedtools.orig/test/bamtofastq/test-bamtofastq.sh
+++ bedtools/test/bamtofastq/test-bamtofastq.sh
@@ -13,7 +13,7 @@
 	fi
 }
 
-samtools view -Sb test2.sam > test2.bam 2> /dev/null
+../htsutil samtobam test2.sam test2.bam
 
 $BT bamtofastq -i test2.bam -fq test2.fq -fq2 test2.fq2 2> /dev/null
 
--- bedtools.orig/test/bedtobam/test-bedtobam.sh
+++ bedtools/test/bedtobam/test-bedtobam.sh
@@ -19,7 +19,7 @@
 echo -e "    bedtobam.t1...\c"
 echo \
 "read_name	0	1	1001	255	1000M	*	0	0	*	*">exp
-echo -e "1\t1000\t2000\tread_name\t255\t+" | $BT bedtobam -i - -g chrsize.tmp| samtools view > obs
+echo -e "1\t1000\t2000\tread_name\t255\t+" | $BT bedtobam -i - -g chrsize.tmp| ../htsutil viewbamrecords > obs
 check obs exp
 rm obs exp
 
--- bedtools.orig/test/coverage/test-coverage.sh
+++ bedtools/test/coverage/test-coverage.sh
@@ -18,11 +18,10 @@
 #                       BAM files                         #
 ###########################################################
 ###########################################################
-samtools view -Sb one_block.sam > one_block.bam 2>/dev/null
-samtools view -Sb two_blocks.sam > two_blocks.bam 2>/dev/null
-samtools view -Sb three_blocks.sam > three_blocks.bam 2>/dev/null
-samtools view -Sb sam-w-del.sam > sam-w-del.bam 2>/dev/null
-
+../htsutil samtobam one_block.sam one_block.bam
+../htsutil samtobam two_blocks.sam two_blocks.bam
+../htsutil samtobam three_blocks.sam three_blocks.bam
+../htsutil samtobam sam-w-del.sam sam-w-del.bam
 
 
 ##################################################################
@@ -731,4 +730,6 @@
 check exp obs
 rm exp obs
 
+rm one_block.bam two_blocks.bam three_blocks.bam sam-w-del.bam
+
 [[ $FAILURES -eq 0 ]] || exit 1;
--- bedtools.orig/test/genomecov/test-genomecov.sh
+++ bedtools/test/genomecov/test-genomecov.sh
@@ -18,13 +18,14 @@
 #                       BAM files                         #
 ###########################################################
 ###########################################################
-samtools view -Sb one_block.sam > one_block.bam 2>/dev/null
-samtools view -Sb two_blocks.sam > two_blocks.bam 2>/dev/null
-samtools view -Sb three_blocks.sam > three_blocks.bam 2>/dev/null
-samtools view -Sb sam-w-del.sam > sam-w-del.bam 2>/dev/null
-samtools view -Sb pair-chip.sam > pair-chip.bam 2>/dev/null
-samtools view -Sb chip.sam > chip.bam 2>/dev/null
+../htsutil samtobam one_block.sam one_block.bam
+../htsutil samtobam two_blocks.sam two_blocks.bam
+../htsutil samtobam three_blocks.sam three_blocks.bam
+../htsutil samtobam sam-w-del.sam sam-w-del.bam
+../htsutil samtobam pair-chip.sam pair-chip.bam
+../htsutil samtobam chip.sam chip.bam
 
+(grep '^@' one_block.sam; sed '/^@/d' *block*.sam) | ../htsutil samtobam - merged.bam
 
 
 ##################################################################
@@ -76,7 +77,7 @@
 chr1	30	40	2
 chr1	40	50	1
 chr1	50	1000	0" > exp
-samtools merge -f /dev/stdout *block*.bam | $BT genomecov -ibam - -bga  > obs
+cat merged.bam | $BT genomecov -ibam - -bga  > obs
 check obs exp
 rm obs exp
 
@@ -93,7 +94,7 @@
 chr1	25	30	3
 chr1	30	50	1
 chr1	50	1000	0" > exp
-samtools merge -f /dev/stdout *block*.bam | $BT genomecov -ibam - -bga -split > obs
+$BT genomecov -ibam merged.bam -bga -split > obs
 check obs exp
 rm obs exp
 
@@ -260,7 +261,7 @@
 check obs exp
 rm obs exp
 
-rm one_block.bam two_blocks.bam three_blocks.bam sam-w-del.bam pair-chip.bam chip.bam
+rm one_block.bam two_blocks.bam three_blocks.bam sam-w-del.bam pair-chip.bam chip.bam merged.bam
 
 ##################################################################
 #  Make sure empty bam doesn't cause failure
--- bedtools.orig/test/intersect/new_test-intersect.sh
+++ bedtools/test/intersect/new_test-intersect.sh
@@ -14,7 +14,7 @@
 
 bam_check() 
 {
-	if diff <(samtools view $1) <(samtools view $2) 
+	if diff <(../htsutil viewbamrecords $1) <(../htsutil viewbamrecords $2)
 	then
 		echo ok
 	else
@@ -740,21 +740,12 @@
 #  Test that an empty query with header, bgzipped, that
 #  runs with -header option will print header
 ############################################################
-
-# I'm not quite sure what this test was trying to do.
-# It may have been attempting to test bzip2 compression,
-# which bedtools does not currently support, or bgzf compression,
-# which is exceedingly rare outside of a BAM file. At any rate,
-# it fails on systems without a "bgzip" command, so I've commented
-# it out for now. NEK 10/02/2017.
 echo -e "    intersect.new.t61...\c"
-echo ok
-#echo "#Random Header" >dummy.txt
-#bgzip dummy.txt
-#echo "#Random Header" >exp
-#$BT intersect -a dummy.txt.gz -b a.bed -header > obs
-#check obs exp
-#rm obs dummy.txt.gz exp
+echo "#Random Header" | ../htsutil bgzfcompress - dummy.txt.gz
+echo "#Random Header" >exp
+$BT intersect -a dummy.txt.gz -b a.bed -header > obs
+check obs exp
+rm obs dummy.txt.gz exp
 
 
 ###########################################################
@@ -910,7 +901,7 @@
 echo \
 "FCC1MK2ACXX:2:2110:4301:28831#	99	chr1	10004	0	100M	=	10047	140	*	*	PG:Z:novoalign	AM:i:2	SM:i:2	MQ:i:0	PQ:i:219	UQ:i:0	AS:i:0	RG:Z:NCH411GBM_CD133low
 FCC1MK2ACXX:2:2110:4301:28831#	147	chr1	10047	0	3S97M	=	10004	-140	*	*	PG:Z:novoalign	AM:i:2	SM:i:70	MQ:i:0	PQ:i:219	UQ:i:194	AS:i:194	RG:Z:NCH411GBM_CD133low" > exp
-$BT intersect -a a.cram -b b.cram | samtools view - > obs
+$BT intersect -a a.cram -b b.cram | ../htsutil viewbamrecords > obs
 check exp obs
 rm exp obs
 [[ $FAILURES -eq 0 ]] || exit 1;
@@ -936,7 +927,7 @@
 echo \
 "FCC1MK2ACXX:2:2110:4301:28831#	99	chr1	10004	0	100M	=	10047	140	CCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCT	CCCFFFFFHHHHHJJIJJJJIGIIIJIGIGJJJJIJJJJIJIIJJDIIIJJIJEHEE@GAHHGE@CEFB;A>AB=??5?B?2?<ABDAD9ABBBC9ABDA	PG:Z:novoalign	AM:i:2	SM:i:2	MQ:i:0	PQ:i:219	UQ:i:0	AS:i:0	MD:Z:100	NM:i:0	RG:Z:NCH411GBM_CD133low
 FCC1MK2ACXX:2:2110:4301:28831#	147	chr1	10047	0	3S97M	=	10004	-140	AACCCTACCCCTACCCCTAACCCTACCCCTACCCCTACCCCTACCCCTACCCCTACCCCTACCCCTACCCTAACCCTAACCCTAACCCTAACCCTAACCC	###?<55-@?250&A?882(@?795&B?8;;/?8('9'A?;8?8C;(<A@FB-/7'F?((@0D:)*9JIIHFCJJJIHFJJJIHEJJHHFFHFFFDDCCB	PG:Z:novoalign	AM:i:2	SM:i:70	MQ:i:0	PQ:i:219	UQ:i:194	AS:i:194	MD:Z:4A5A11A5A5A5A5A5A5A3A34	NM:i:10	RG:Z:NCH411GBM_CD133low">exp
-CRAM_REFERENCE=test_ref.fa $BT intersect -a a.cram -b b.cram | samtools view -T test_ref.fa - > obs
+CRAM_REFERENCE=test_ref.fa $BT intersect -a a.cram -b b.cram | ../htsutil viewcramrecords - test_ref.fa > obs
 check exp obs
 rm exp obs
 [[ $FAILURES -eq 0 ]] || exit 1;
@@ -954,7 +945,7 @@
 GA5:3:49:1480:1116#0	16	dummy_chr	11913868	37	76M	*	0	0	GGGAGGAGGCCAGGACTTCAGGGACCCACAGCCATCACCTCCCTCCCCTGCCCCCTACACACCAACTCTCTGGAAA	#################################44:4=944==;=???>=?>==??=A=A;ABA?A?AABAAABBB	XT:A:U	NM:i:1	X0:i:1	X1:i:0	XM:i:1	XO:i:0	XG:i:0	MD:Z:0T75
 GA5:3:61:213:1812#0	16	dummy_chr	13030396	37	76M	*	0	0	GGTCCGGCGGGGTCGGACTGGACCAGCTGTTGGGCTTTGTTTGCTCTTTTTACGAATTGAAAAACTGAAGCCAGGA	/=81,5948=485=4,),1;;7:87:6=;;@@AB=C8A@@BAB=>5>BBBB>BBAAA9ABA@B4B;BBBBBBBBCB	XT:A:U	NM:i:1	X0:i:1	X1:i:0	XM:i:1	XO:i:0	XG:i:0	MD:Z:0T75
 GA5:3:116:1581:552#0	16	dummy_chr	15055984	37	76M	*	0	0	AGAAAGCCTAAGGTCAGGGTGCCAGCAGGTTTGGTGTCTGGTGAGGTACCCATCTCTGCTTCTAAGGCAGAGCCTT	48887429,3=;98<8<8@;<=?8@??98@@@<=AA>@@?B?A@@BA6BA@=@BABBB???B@BBBBBABBCBB?B	XT:A:U	NM:i:0	X0:i:1	X1:i:0	XM:i:0	XO:i:0	XG:i:0	MD:Z:76" > exp
-$BT intersect -a notexthdr.bam -b notexthdr.bam | samtools view > obs
+$BT intersect -a notexthdr.bam -b notexthdr.bam | ../htsutil viewbamrecords > obs
 check exp obs
 rm exp obs
 [[ $FAILURES -eq 0 ]] || exit 1;
@@ -976,7 +967,7 @@
 ############################################################
 echo -e "    intersect.new.t78...\c"
 echo -e "@HD	VN:1.5	SO:coordinate" > exp
-echo "@HD	VN:1.5	SO:coordinate" | samtools view --no-PG -b  | $BT intersect -a /dev/stdin -b b.bed | samtools view --no-PG -H >obs
+echo "@HD	VN:1.5	SO:coordinate" | ../htsutil samtobam - - | $BT intersect -a /dev/stdin -b b.bed | ../htsutil viewbam >obs
 check exp obs
 rm exp obs
 [[ $FAILURES -eq 0 ]] || exit 1;
--- bedtools.orig/test/intersect/performanceTest.sh
+++ bedtools/test/intersect/performanceTest.sh
@@ -27,8 +27,7 @@
   ../$BT random -l 1000 -n 10000000 -g ../human.hg19.genome | sort -k1,1 -k2,2n > b10M.bed 
   cp a10M.bed a10M_gzipped.bed
   gzip a10M_gzipped.bed
-  cp a10M.bed a10M_bgzipped.bed
-  bgzip a10M_bgzipped.bed
+  ../../htsutil bgzfcompress a10M.bed a10M_bgzipped.bed.gz
   ../$BT bedtobam -i a10M.bed -g ../human.hg19.genome > a10M.bam
   cd ..
 fi
--- bedtools.orig/test/intersect/test-intersect.sh
+++ bedtools/test/intersect/test-intersect.sh
@@ -196,10 +196,10 @@
 #                       -split                            #
 ###########################################################
 ###########################################################
-samtools view -Sb one_block.sam > one_block.bam 2>/dev/null
-samtools view -Sb two_blocks.sam > two_blocks.bam 2>/dev/null
-samtools view -Sb three_blocks.sam > three_blocks.bam 2>/dev/null
-samtools view -Sb split.issue750.sam > split.issue750.bam 2>/dev/null
+../htsutil samtobam one_block.sam one_block.bam
+../htsutil samtobam two_blocks.sam two_blocks.bam
+../htsutil samtobam three_blocks.sam three_blocks.bam
+../htsutil samtobam split.issue750.sam split.issue750.bam
 
 
 ##################################################################
@@ -208,7 +208,7 @@
 echo -e "    intersect.t17...\c"
 echo \
 "three_blocks	16	chr1	1	40	10M10N10M10N10M	*	0	0	GAAGGCCACCGCCGCGGTTATTTTCCTTCA	CCCDDB?=FJIIJIGFJIJHIJJJJJJJJI	MD:Z:50" > exp
-$BT intersect -abam three_blocks.bam -b three_blocks_nomatch.bed | samtools view - > obs
+$BT intersect -abam three_blocks.bam -b three_blocks_nomatch.bed | ../htsutil viewbamrecords > obs
 check obs exp
 rm obs exp
 
@@ -217,7 +217,7 @@
 ##################################################################
 echo -e "    intersect.t18...\c"
 touch exp
-$BT intersect -abam three_blocks.bam -b three_blocks_nomatch.bed -split | samtools view - > obs
+$BT intersect -abam three_blocks.bam -b three_blocks_nomatch.bed -split | ../htsutil viewbamrecords > obs
 check obs exp
 rm obs exp
 
@@ -227,7 +227,7 @@
 echo -e "    intersect.t19...\c"
 echo \
 "three_blocks	16	chr1	1	40	10M10N10M10N10M	*	0	0	GAAGGCCACCGCCGCGGTTATTTTCCTTCA	CCCDDB?=FJIIJIGFJIJHIJJJJJJJJI	MD:Z:50" > exp
-$BT intersect -abam three_blocks.bam -b three_blocks_match.bed -split | samtools view - > obs
+$BT intersect -abam three_blocks.bam -b three_blocks_match.bed -split | ../htsutil viewbamrecords > obs
 check obs exp
 rm obs exp
 
@@ -237,7 +237,7 @@
 ##################################################################
 echo -e "    intersect.t20...\c"
 touch exp
-$BT intersect -abam three_blocks.bam -b three_blocks_match.bed -split -s | samtools view - > obs
+$BT intersect -abam three_blocks.bam -b three_blocks_match.bed -split -s | ../htsutil viewbamrecords > obs
 check obs exp
 rm obs exp
 
@@ -247,7 +247,7 @@
 echo -e "    intersect.t21...\c"
 echo \
 "three_blocks	16	chr1	1	40	10M10N10M10N10M	*	0	0	GAAGGCCACCGCCGCGGTTATTTTCCTTCA	CCCDDB?=FJIIJIGFJIJHIJJJJJJJJI	MD:Z:50" > exp
-$BT intersect -abam three_blocks.bam -b three_blocks_match_1bp.bed -split | samtools view - > obs
+$BT intersect -abam three_blocks.bam -b three_blocks_match_1bp.bed -split | ../htsutil viewbamrecords > obs
 check obs exp
 rm obs exp
 
@@ -256,7 +256,7 @@
 ################################################################################
 echo -e "    intersect.t22...\c"
 touch exp
-$BT intersect -abam three_blocks.bam -b three_blocks_match_1bp.bed -split -f 0.1 | samtools view - > obs
+$BT intersect -abam three_blocks.bam -b three_blocks_match_1bp.bed -split -f 0.1 | ../htsutil viewbamrecords > obs
 check obs exp
 rm obs exp
 
@@ -590,7 +590,7 @@
 echo -e "    intersect.t23...\c"
 echo \
 "mapped	16	chr1	1	40	30M	*	0	0	GAAGGCCACCGCCGCGGTTATTTTCCTTCA	CCCDDB?=FJIIJIGFJIJHIJJJJJJJJI	MD:Z:50" > exp
-samtools view -Sb mapped_and_unmapped.sam 2>/dev/null | $BT intersect -abam - -b a.bed | samtools view - > obs
+../htsutil samtobam mapped_and_unmapped.sam | $BT intersect -abam - -b a.bed | ../htsutil viewbamrecords > obs
 check obs exp
 rm obs exp
 
@@ -600,7 +600,7 @@
 echo -e "    intersect.t24...\c"
 echo \
 "umapped	4	*	1	40	30M	*	0	0	GAAGGCCACCGCCGCGGTTATTTTCCTTCA	CCCDDB?=FJIIJIGFJIJHIJJJJJJJJI	MD:Z:50" > exp
-samtools view -Sb mapped_and_unmapped.sam 2>/dev/null | $BT intersect -abam - -b a.bed -v | samtools view - > obs
+../htsutil samtobam mapped_and_unmapped.sam | $BT intersect -abam - -b a.bed -v | ../htsutil viewbamrecords > obs
 check obs exp
 rm obs exp
 
@@ -977,34 +977,34 @@
 ##################################################################
 echo -e "    intersect.t63...\c"
 echo -n "" > exp
-$BT intersect -a x.bam -b y.bed -f 0.21 -F 0.21 -wa | samtools view - > obs
+$BT intersect -a x.bam -b y.bed -f 0.21 -F 0.21 -wa | ../htsutil viewbamrecords > obs
 check exp obs
 rm exp obs
 
 echo -e "    intersect.t64...\c"
 echo "a1	0	chr1	11	255	10M	*	0	0	*	*
 a2	16	chr2	11	255	10M	*	0	0	*	*" > exp
-$BT intersect -a x.bam -b y.bed -f 0.19 -F 0.21 -wa | samtools view - > obs
+$BT intersect -a x.bam -b y.bed -f 0.19 -F 0.21 -wa | ../htsutil viewbamrecords > obs
 check exp obs
 rm exp obs
 
 echo -e "    intersect.t65...\c"
 echo "a1	0	chr1	11	255	10M	*	0	0	*	*
 a2	16	chr2	11	255	10M	*	0	0	*	*" > exp
-$BT intersect -a x.bam -b y.bed -f 0.19 -F 0.21 -wa | samtools view - > obs
+$BT intersect -a x.bam -b y.bed -f 0.19 -F 0.21 -wa | ../htsutil viewbamrecords > obs
 check exp obs
 rm exp obs
 
 echo -e "    intersect.t66...\c"
 echo "a1	0	chr1	11	255	10M	*	0	0	*	*
 a2	16	chr2	11	255	10M	*	0	0	*	*" > exp
-$BT intersect -a x.bam -b y.bed -f 0.19 -F 0.50 -wa | samtools view - > obs
+$BT intersect -a x.bam -b y.bed -f 0.19 -F 0.50 -wa | ../htsutil viewbamrecords > obs
 check exp obs
 rm exp obs
 
 echo -e "    intersect.t67...\c"
 echo -n "" > exp
-$BT intersect -a x.bam -b y.bed -f 0.19 -F 0.51 -wa | samtools view - > obs
+$BT intersect -a x.bam -b y.bed -f 0.19 -F 0.51 -wa | ../htsutil viewbamrecords > obs
 check exp obs
 rm exp obs
 
--- bedtools.orig/test/multicov/test-multicov.sh
+++ bedtools/test/multicov/test-multicov.sh
@@ -13,14 +13,10 @@
 	fi
 }
 
-samtools view -Sb one_block.sam > one_block.bam 2>/dev/null
-samtools view -Sb two_blocks.sam > two_blocks.bam 2>/dev/null
-samtools view -Sb test-multi.sam > test-multi.bam 2>/dev/null
-samtools view -Sb test-multi.2.sam > test-multi.2.bam 2>/dev/null
-samtools index one_block.bam
-samtools index two_blocks.bam
-samtools index test-multi.bam
-samtools index test-multi.2.bam
+../htsutil samtoindexedbam one_block.sam one_block.bam
+../htsutil samtoindexedbam two_blocks.sam two_blocks.bam
+../htsutil samtoindexedbam test-multi.sam test-multi.bam
+../htsutil samtoindexedbam test-multi.2.sam test-multi.2.bam
 
 ##################################################################
 #  Test one block matches all BEDs
