# each test will be expanded into 6 tests, to check stream, file, and generator
# versions of input and output BedTools


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# intersect
-   method: intersect
    kwargs:
        a: a.bed
        b: b.bed
        s: True
    expected: |
        chr1	155	200	feature3	0	-
        chr1	900	901	feature4	0	+

-   method: intersect
    kwargs:
        a: a.bed
        b: b.bed
        s: False
    expected: |
        chr1	155	200	feature2	0	+
        chr1	155	200	feature3	0	-
        chr1	900	901	feature4	0	+

-   method: intersect
    kwargs:
        a: a.bed
        b: b.bed
        u: True
    expected: |
        chr1	100	200	feature2	0	+
        chr1	150	500	feature3	0	-
        chr1	900	950	feature4	0	+

-   method: intersect
    kwargs:
        a: a.bed
        b: b.bed
        u: True
        s: True
    expected: |
        chr1	150	500	feature3	0	-
        chr1	900	950	feature4	0	+

-   method: intersect
    kwargs:
        a: a.bed
        b: b.bed
        v: True
    expected: |
        chr1 1 100 feature1 0 +

-   method: intersect
    kwargs:
        a: a.bed
        b: b.bed
        v: True
        s: True
    expected: |
        chr1 1   100 feature1 0 +
        chr1 100 200 feature2 0 +

-   method: intersect
    kwargs:
        abam: gdc.bam
        b: gdc.gff
        bed: True
        v: True
    expected: |
        chr2L	10	15	None	255	+	10	15	0,0,0	1	5,	0,


-   method: intersect
    kwargs:
        abam: gdc.bam
        b: gdc.gff
        bed: True
        u: True
    expected: |
        chr2L	70	75	None	255	-	70	75	0,0,0	1	5,	0,
        chr2L	140	145	None	255	-	140	145	0,0,0	1	5,	0,
        chr2L	150	155	None	255	-	150	155	0,0,0	1	5,	0,
        chr2L	210	215	None	255	+	210	215	0,0,0	1	5,	0,
        chr2L	70	75	None	255	+	70	75	0,0,0	1	5,	0,
        chr2L	140	145	None	255	+	140	145	0,0,0	1	5,	0,
        chr2L	160	165	None	255	+	160	165	0,0,0	1	5,	0,

# same thing, but no explicit -abam, just -a
-   method: intersect
    kwargs:
        a: gdc.bam
        b: gdc.gff
        bed: True
        u: True
    expected: |
        chr2L	70	75	None	255	-	70	75	0,0,0	1	5,	0,
        chr2L	140	145	None	255	-	140	145	0,0,0	1	5,	0,
        chr2L	150	155	None	255	-	150	155	0,0,0	1	5,	0,
        chr2L	210	215	None	255	+	210	215	0,0,0	1	5,	0,
        chr2L	70	75	None	255	+	70	75	0,0,0	1	5,	0,
        chr2L	140	145	None	255	+	140	145	0,0,0	1	5,	0,
        chr2L	160	165	None	255	+	160	165	0,0,0	1	5,	0,

#88888888888888888888888888888888888888888888888888888888888888888888888888888
# subtract

-   method: subtract
    kwargs:
        a: a.bed
        b: b.bed
    expected: |
        chr1	1	100	feature1	0	+
        chr1	100	155	feature2	0	+
        chr1	150	155	feature3	0	-
        chr1	200	500	feature3	0	-
        chr1	901	950	feature4	0	+

-   method: subtract
    kwargs:
        a: a.bed
        b: b.bed
        s: True
    expected: |
        chr1	1	100	feature1	0	+
        chr1	100	200	feature2	0	+
        chr1	150	155	feature3	0	-
        chr1	200	500	feature3	0	-
        chr1	901	950	feature4	0	+

# the difference in f=0.2 and f=0.1 is in feature5 of b.  Since feature2
# and feature3 of a overlap, it's seeing the 'a' feature as a 399-bp
# feature (chr1:100-500), and feature5 of 'b' overlaps this by 44 bp.
#
# So the threshold fraction should be
#
#   44/399 = 0.1103 
#
# f > 0.1103 should return no subtractions, because nothing in b overlaps by that much.
# However, 0.12 doesn't work; need to go to 0.13 . . .
-   method: subtract
    kwargs:
        a: a.bed
        b: b.bed
        s: True
        f: 0.13
    expected: |
        chr1	1	100	feature1	0	+
        chr1	100	200	feature2	0	+
        chr1	150	500	feature3	0	-
        chr1	900	950	feature4	0	+

# f < 0.1103, so should get a subtraction
-   method: subtract
    kwargs:
        a: a.bed
        b: b.bed
        s: True
        f: 0.1
    expected: |
        chr1	1	100	feature1	0	+
        chr1	100	200	feature2	0	+
        chr1	150	155	feature3	0	-
        chr1	200	500	feature3	0	-
        chr1	900	950	feature4	0	+

#88888888888888888888888888888888888888888888888888888888888888888888888888888
# slop
#
-   method: slop
    kwargs:
        i: a.bed
        b: 100
        genome: 'hg19'
    expected: |
        chr1	0	200	feature1	0	+
        chr1	0	300	feature2	0	+
        chr1	50	600	feature3	0	-
        chr1	800	1050	feature4	0	+


-   method: slop
    kwargs:
        i: a.bed
        l: 100
        r: 1
        genome: 'hg19'
    expected: |
        chr1	0	101	feature1	0	+
        chr1	0	201	feature2	0	+
        chr1	50	501	feature3	0	-
        chr1	800	951	feature4	0	+


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# merge
#
-   method: merge
    kwargs:
        i: a.bed
    expected: |
        chr1	1	500
        chr1	900	950

-   method: merge
    kwargs:
        i: a.bed
        s: True
    expected: |
        chr1	1	200	+
        chr1	150	500	-
        chr1	900	950	+

-   method: merge
    kwargs:
        i: a.bed
        s: True
        c: [4, 5, 6]
        o: 'distinct'
    expected: |
        chr1	1	200	+	feature1,feature2	0	+
        chr1	150	500	-	feature3	0	-
        chr1	900	950	+	feature4	0	+

-   method: merge
    kwargs:
        i: b.bed
        d: 700
    expected: |
        chr1 155 901


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# to_bed
#
-   method: bam_to_bed
    kwargs:
        i: small.bam
    expected: |
        chr2L	9329	9365	HWUSI-NAME:2:69:512:1017#0	3	-
        chr2L	10212	10248	HWUSI-NAME:2:91:1201:1113#0	255	-
        chr2L	10255	10291	HWUSI-NAME:2:57:765:914#0	255	+
        chr2L	10427	10463	HWUSI-NAME:2:53:900:1397#0	255	-
        chr2L	10474	10510	HWUSI-NAME:2:64:398:565#0	255	-
        chr2L	10516	10552	HWUSI-NAME:2:69:139:283#0	255	+
        chr2L	10521	10557	HWUSI-NAME:2:49:1095:1884#0	255	+
        chr2L	10628	10664	HWUSI-NAME:2:27:973:1468#0	255	-
        chr2L	10782	10818	HWUSI-NAME:2:21:67:543#0	255	+
        chr2L	10818	10854	HWUSI-NAME:2:27:1324:2012#0	255	-


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# bed6
#
-   method: bed6
    kwargs:
        i: mm9.bed12
    expected: |
        chr1	4797973	4798063	Lypla1,uc007afh.1	1	+
        chr1	4798535	4798567	Lypla1,uc007afh.1	1	+
        chr1	4818664	4818730	Lypla1,uc007afh.1	1	+
        chr1	4820348	4820396	Lypla1,uc007afh.1	1	+
        chr1	4822391	4822462	Lypla1,uc007afh.1	1	+
        chr1	4827081	4827155	Lypla1,uc007afh.1	1	+
        chr1	4829467	4829569	Lypla1,uc007afh.1	1	+
        chr1	4831036	4831213	Lypla1,uc007afh.1	1	+
        chr1	4835043	4836816	Lypla1,uc007afh.1	1	+
        chr1	4797973	4798063	Lypla1,uc007afg.1	1	+
        chr1	4798535	4798567	Lypla1,uc007afg.1	1	+
        chr1	4818664	4818730	Lypla1,uc007afg.1	1	+
        chr1	4820348	4820396	Lypla1,uc007afg.1	1	+
        chr1	4822391	4822462	Lypla1,uc007afg.1	1	+
        chr1	4827081	4827155	Lypla1,uc007afg.1	1	+
        chr1	4829467	4829569	Lypla1,uc007afg.1	1	+
        chr1	4831036	4832908	Lypla1,uc007afg.1	1	+
        chr1	4847894	4848057	Tcea1,uc007afi.1	1	+
        chr1	4857550	4857613	Tcea1,uc007afi.1	1	+
        chr1	4868107	4868213	Tcea1,uc007afi.1	1	+
        chr1	4876824	4876912	Tcea1,uc007afi.1	1	+
        chr1	4879537	4879683	Tcea1,uc007afi.1	1	+
        chr1	4880820	4880877	Tcea1,uc007afi.1	1	+
        chr1	4881995	4882150	Tcea1,uc007afi.1	1	+
        chr1	4883497	4883644	Tcea1,uc007afi.1	1	+
        chr1	4885014	4885086	Tcea1,uc007afi.1	1	+
        chr1	4886436	4887985	Tcea1,uc007afi.1	1	+
        chr1	4848118	4848584	Tcea1,uc007afj.1	1	+
        chr1	4857550	4857613	Tcea1,uc007afj.1	1	+
        chr1	4868107	4868213	Tcea1,uc007afj.1	1	+
        chr1	4876824	4876912	Tcea1,uc007afj.1	1	+
        chr1	4879537	4879683	Tcea1,uc007afj.1	1	+
        chr1	4880820	4880877	Tcea1,uc007afj.1	1	+
        chr1	5073253	5073359	Atp6v1h,uc007afn.1	1	+
        chr1	5074497	5074644	Atp6v1h,uc007afn.1	1	+
        chr1	5079089	5079192	Atp6v1h,uc007afn.1	1	+
        chr1	5083443	5083533	Atp6v1h,uc007afn.1	1	+
        chr1	5085695	5085809	Atp6v1h,uc007afn.1	1	+
        chr1	5088109	5088214	Atp6v1h,uc007afn.1	1	+
        chr1	5091150	5091204	Atp6v1h,uc007afn.1	1	+
        chr1	5107470	5107568	Atp6v1h,uc007afn.1	1	+
        chr1	5114357	5114550	Atp6v1h,uc007afn.1	1	+
        chr1	5123164	5123343	Atp6v1h,uc007afn.1	1	+
        chr1	5125892	5126018	Atp6v1h,uc007afn.1	1	+
        chr1	5133830	5133932	Atp6v1h,uc007afn.1	1	+
        chr1	5140028	5140142	Atp6v1h,uc007afn.1	1	+
        chr1	5152185	5152630	Atp6v1h,uc007afn.1	1	+


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# shuffle
#
#
-   method: shuffle
    kwargs:
        i: a.bed
        seed: 1
        genome: hg19
        chrom: True
    expected: |
        chr1	59535036	59535135	feature1	0	+
        chr1	99179023	99179123	feature2	0	+
        chr1	186189051	186189401	feature3	0	-
        chr1	219133189	219133239	feature4	0	+

#88888888888888888888888888888888888888888888888888888888888888888888888888888
# annotate
#
-   method: annotate
    kwargs:
        i: a.bed
        files: [b.bed, a.bed]
    expected: |
        chr1	1	100	feature1	0	+	0.000000	1.000000
        chr1	100	200	feature2	0	+	0.450000	1.000000
        chr1	150	500	feature3	0	-	0.128571	1.000000
        chr1	900	950	feature4	0	+	0.020000	1.000000

#88888888888888888888888888888888888888888888888888888888888888888888888888888
# flank
#
-   method: flank
    kwargs:
        i: a.bed
        b: 100
        genome: hg19
    expected: |
        chr1	0	1	feature1	0	+
        chr1	100	200	feature1	0	+
        chr1	0	100	feature2	0	+
        chr1	200	300	feature2	0	+
        chr1	50	150	feature3	0	-
        chr1	500	600	feature3	0	-
        chr1	800	900	feature4	0	+
        chr1	950	1050	feature4	0	+

#88888888888888888888888888888888888888888888888888888888888888888888888888888
# genomeCoverage
#
-   method: genome_coverage
    kwargs:
        ibam: gdc.bam
        #genome: dm3
        bg: True
    expected: |
        chr2L	10	15	1
        chr2L	70	75	2
        chr2L	140	145	2
        chr2L	150	155	1
        chr2L	160	165	1
        chr2L	210	215	1

# same thing, but auto-detect BAM
-   method: genome_coverage
    kwargs:
        i: gdc.bam
        #genome: dm3
        bg: True
    expected: |
        chr2L	10	15	1
        chr2L	70	75	2
        chr2L	140	145	2
        chr2L	150	155	1
        chr2L	160	165	1
        chr2L	210	215	1
#
# now auto-detect BAM and allow genome
-   method: genome_coverage
    kwargs:
        i: gdc.bam
        genome: dm3
        bg: True
    expected: |
        chr2L	10	15	1
        chr2L	70	75	2
        chr2L	140	145	2
        chr2L	150	155	1
        chr2L	160	165	1
        chr2L	210	215	1


# force  BAM and allow genome
-   method: genome_coverage
    kwargs:
        ibam: gdc.bam
        genome: dm3
        bg: True
    expected: |
        chr2L	10	15	1
        chr2L	70	75	2
        chr2L	140	145	2
        chr2L	150	155	1
        chr2L	160	165	1
        chr2L	210	215	1



-   method: genome_coverage
    kwargs:
        i: gdc.bed
        genome: dm3
        bg: True
    expected: |
        chr2L	10	15	1
        chr2L	70	75	2
        chr2L	140	145	2
        chr2L	150	155	1
        chr2L	160	165	1
        chr2L	210	215	1

#88888888888888888888888888888888888888888888888888888888888888888888888888888
# coverage
#
-   method: coverage
    kwargs:
        a: b.bed
        b: a.bed
    expected: |
        chr1	155	200	feature5	0	-	2	45	45	1.0000000
        chr1	800	901	feature6	0	+	1	1	101	0.0099010


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# coverage
#
-   method: closest
    kwargs:
        a: a.bed
        b: b.bed
    expected: |
        chr1	1	100	feature1	0	+	chr1	155	200	feature5	0	-
        chr1	100	200	feature2	0	+	chr1	155	200	feature5	0	-
        chr1	150	500	feature3	0	-	chr1	155	200	feature5	0	-
        chr1	900	950	feature4	0	+	chr1	800	901	feature6	0	+

#88888888888888888888888888888888888888888888888888888888888888888888888888888
# tagBam
#
-   method: tag_bam
    kwargs:
        i: gdc.bam
        files: ['tag_test1.bed', 'tag_test2.bed']
        labels: ['1', '2']
    expected: |
        None	0	chr2L	11	255	5M	*	0	0	CGACA	IIIII	NM:i:0	NH:i:1	YB:Z:1
        None	16	chr2L	71	255	5M	*	0	0	TTCTC	IIIII	NM:i:0	NH:i:1	YB:Z:1;2
        None	16	chr2L	141	255	5M	*	0	0	CACCA	IIIII	NM:i:0	NH:i:1	YB:Z:1;2
        None	16	chr2L	151	255	5M	*	0	0	GTTCA	IIIII	NM:i:0	NH:i:1	YB:Z:2
        None	0	chr2L	211	255	5M	*	0	0	AAATA	IIIII	NM:i:0	NH:i:1
        None	0	chr2L	71	255	5M	*	0	0	GAGAA	IIIII	NM:i:0	NH:i:1	YB:Z:1;2
        None	0	chr2L	141	255	5M	*	0	0	TGGTG	IIIII	NM:i:0	NH:i:1	YB:Z:1;2
        None	0	chr2L	161	255	5M	*	0	0	GATAA	IIIII	NM:i:0	NH:i:1	YB:Z:2

-   method: tag_bam
    kwargs:
        i: gdc.bam
        files: ['tag_test1.bed', 'tag_test2.bed']
        labels: ['1', '2']
        s: True
    expected: |
        None	0	chr2L	11	255	5M	*	0	0	CGACA	IIIII	NM:i:0	NH:i:1	YB:Z:1
        None	16	chr2L	71	255	5M	*	0	0	TTCTC	IIIII	NM:i:0	NH:i:1	YB:Z:2
        None	16	chr2L	141	255	5M	*	0	0	CACCA	IIIII	NM:i:0	NH:i:1	YB:Z:2
        None	16	chr2L	151	255	5M	*	0	0	GTTCA	IIIII	NM:i:0	NH:i:1	YB:Z:2
        None	0	chr2L	211	255	5M	*	0	0	AAATA	IIIII	NM:i:0	NH:i:1
        None	0	chr2L	71	255	5M	*	0	0	GAGAA	IIIII	NM:i:0	NH:i:1	YB:Z:1
        None	0	chr2L	141	255	5M	*	0	0	TGGTG	IIIII	NM:i:0	NH:i:1	YB:Z:1
        None	0	chr2L	161	255	5M	*	0	0	GATAA	IIIII	NM:i:0	NH:i:1


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# nucBed
#
-   method: nucleotide_content
    kwargs:
        bed: a.bed
        fi: test.fa
    expected: |
        chr1	1	100	feature1	0	+	0.626263	0.373737	29	21	16	33	0	0	99
        chr1	100	200	feature2	0	+	0.710000	0.290000	36	15	14	35	0	0	100
        chr1	150	500	feature3	0	-	0.651429	0.348571	136	67	55	92	0	0	350
        chr1	900	950	feature4	0	+	0.620000	0.380000	14	6	13	17	0	0	50


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# multiBamCoverage
#
-   method: multi_bam_coverage
    kwargs:
        bed: multibamcov_test.bed
        bams: ['gdc.1.100.bam', 'gdc.50.200.bam']
    expected: |
        chr2L	1	100	+	3	2
        chr2L	50	200	-	2	6

#88888888888888888888888888888888888888888888888888888888888888888888888888888
# windowBed
#

-   method: window
    kwargs:
        a: a.bed
        b: b.bed
    expected: |
        chr1	1	100	feature1	0	+	chr1	155	200	feature5	0	-
        chr1	1	100	feature1	0	+	chr1	800	901	feature6	0	+
        chr1	100	200	feature2	0	+	chr1	155	200	feature5	0	-
        chr1	100	200	feature2	0	+	chr1	800	901	feature6	0	+
        chr1	150	500	feature3	0	-	chr1	155	200	feature5	0	-
        chr1	150	500	feature3	0	-	chr1	800	901	feature6	0	+
        chr1	900	950	feature4	0	+	chr1	155	200	feature5	0	-
        chr1	900	950	feature4	0	+	chr1	800	901	feature6	0	+

-   method: window
    kwargs:
        a: a.bed
        b: b.bed
        Sm: True
    expected: |
        chr1	1	100	feature1	0	+	chr1	155	200	feature5	0	-
        chr1	100	200	feature2	0	+	chr1	155	200	feature5	0	-
        chr1	150	500	feature3	0	-	chr1	800	901	feature6	0	+
        chr1	900	950	feature4	0	+	chr1	155	200	feature5	0	-

-   method: window
    kwargs:
        a: a.bed
        b: b.bed
        sm: True
    expected: |
        chr1	1	100	feature1	0	+	chr1	800	901	feature6	0	+
        chr1	100	200	feature2	0	+	chr1	800	901	feature6	0	+
        chr1	150	500	feature3	0	-	chr1	155	200	feature5	0	-
        chr1	900	950	feature4	0	+	chr1	800	901	feature6	0	+

#88888888888888888888888888888888888888888888888888888888888888888888888888888
# mapBed

-   method: map
    kwargs:
        a: a.bed
        b: d.gff
    expected: |
        chr1	1	100	feature1	0	+	750
        chr1	100	200	feature2	0	+	1025
        chr1	150	500	feature3	0	-	875
        chr1	900	950	feature4	0	+	.

-   method: map
    kwargs:
        a: a.bed
        b: d.gff
        o: collapse
    expected: |
        chr1	1	100	feature1	0	+	300,300,150
        chr1	100	200	feature2	0	+	300,300,150,275
        chr1	150	500	feature3	0	-	300,300,275
        chr1	900	950	feature4	0	+	.

-   method: map
    kwargs:
        a: a.bed
        b: d.gff
        o: collapse
        c: 3
    expected: |
        chr1	1	100	feature1	0	+	gene,mRNA,CDS
        chr1	100	200	feature2	0	+	gene,mRNA,CDS,CDS
        chr1	150	500	feature3	0	-	gene,mRNA,CDS
        chr1	900	950	feature4	0	+	.


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# clusterBed
-   method: cluster
    kwargs:
            i: a.bed
    expected: |
        chr1	1	100	feature1	0	+	1
        chr1	100	200	feature2	0	+	1
        chr1	150	500	feature3	0	-	1
        chr1	900	950	feature4	0	+	2

-   method: cluster
    kwargs:
            i: a.bed
            d: -10
    expected: |
        chr1	1	100	feature1	0	+	1
        chr1	100	200	feature2	0	+	2
        chr1	150	500	feature3	0	-	2
        chr1	900	950	feature4	0	+	3


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# bedpeToBam
-   method: bedpe_to_bam
    kwargs:
            i: bedpe.bed
            genome: hg19
    expected: |
        bedpe_example1	97	chr1	101	255	100M	chr5	5001	0	*	*
        bedpe_example1	145	chr5	5001	255	100M	chr1	101	0	*	*
        bedpe_example2	113	chr9	1001	255	4000M	=	3001	2000	*	*
        bedpe_example2	177	chr9	3001	255	800M	=	1001	2000	*	*


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# pairToPair
-   method: pair_to_pair
    kwargs:
            a: bedpe.bed
            b: bedpe2.bed
            type: either
    expected: |
        chr1	100	200	chr5	5000	5100	bedpe_example1	30	+	-	chr5	5000	5100	chr1	1000	2000	bedpe_example1		-	+
        chr9	1000	5000	chr9	3000	3800	bedpe_example2	100	-	-	chr9	3000	3800	chr9	100	500	bedpe_example2		-	-
        chr9	1000	5000	chr9	3000	3800	bedpe_example2	100	-	-	chr9	3000	3800	chr9	100	500	bedpe_example2		-	-


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# pairToBed
-   method: pair_to_bed
    kwargs:
            a: bedpe.bed
            b: b.bed
    expected: |
        chr1	100	200	chr5	5000	5100	bedpe_example1	30	+	-	chr1	155	200	feature5	0	-


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# expandCols
#
-   method: expand
    kwargs:
            i: expand_test.bed
            c: 5
    expected: |
        chr1	10	20	1,2,3	10
        chr1	10	20	1,2,3	20
        chr1	10	20	1,2,3	30
        chr1	40	50	4,5,6	40
        chr1	40	50	4,5,6	50
        chr1	40	50	4,5,6	60


#88888888888888888888888888888888888888888888888888888888888888888888888888888
# sample
#
-   method: sample
    kwargs:
            i: gdc.bed
            seed: 1
            n: 2
    expected: |
        chr2L	140	145	None	255	-
        chr2L	70	75	None	255	-
