-- | the number of tag pairs that overlap with the region
-- covered by the contact
readCount :: BI.IdxHandle  -- ^ bam file handler
          -> Int           -- ^ half window width
          -> Contact
          -> IO Int
readCount handle w ((c1, p1), (c2, p2)) = viewBam handle (c1, p1-w, p1+w) $$ CL.fold f 0
  where
    f acc x =
        case mateTargetName x of
            Just chr -> if chr == c2
                           then let mp = fromIntegral . fromJust .  matePosition $ x
                                    l = fromIntegral . fromJust . queryLength $ x
                                in if isOverlapped r2 (mp, mp+l)
                                      then acc + 1
                                      else acc
                           else acc
            _ -> acc
    isOverlapped (lo,hi) (lo',hi') = lo' < hi && hi' > lo
    r2 = (p2-w, p2+w)

readCount :: BI.IdxHandle  -- ^ bam file handler
          -> Int           -- ^ half window width
          -> Contact
          -> IO Int
readCount handle w ((c1, p1), (c2, p2)) = viewBam handle (c1, p1-w, p1+w) $$ CL.fold f 0
  where
    f acc x =
        case mateTargetName x of
            Just chr -> if chr == c2
                           then let mp = fromIntegral . fromJust .  matePosition $ x
                                    l = fromIntegral . fromJust . queryLength $ x
                                in if isOverlapped r2 (mp, mp+l)
                                      then acc + 1
                                      else acc
                           else acc
            _ -> acc
    isOverlapped (lo,hi) (lo',hi') = lo' < hi && hi' > lo
    r2 = (p2-w, p2+w)

readCount handle w ((c1, p1), (c2, p2)) = viewBam handle (c1, p1-w, p1+w) $$ CL.fold f 0
  where
    f acc x =
        case mateTargetName x of
            Just chr -> if chr == c2
                           then let mp = fromIntegral . fromJust .  matePosition $ x
                                    l = fromIntegral . fromJust . queryLength $ x
                                in if isOverlapped r2 (mp, mp+l)
                                      then acc + 1
                                      else acc
                           else acc
            _ -> acc
    isOverlapped (lo,hi) (lo',hi') = lo' < hi && hi' > lo
    r2 = (p2-w, p2+w)
