title: extract overlapping regions
body: <p>I have a file characterizing genomic regions that looks like this:</p>
  
  <pre><code>chrom   chromStart  chromEnd    PGB
  chr1    12874   28371   2
  chr1    15765   21765   1
  chr1    15795   28371   2
  chr1    18759   24759   1
  chr1    28370   34961   1
  chr3    233278  240325  1
  chr3    239279  440831  2
  chr3    356365  362365  1
  </code></pre>
  
  <p>Basically PGB describes the category of the genomic region characterised by its chromosome number (chrom), start (chromStart) and end (chromEnd) coordinates.</p>
  
  <p>I wish to collapse the overlapping regions such that overlapping regions of PGB = 1 and 2 are in a new category, PGB = 3. Output being:</p>
  
  <pre><code>chrom   chromStart  chromEnd    PGB
  chr1    12874   15764   2
  chr1    15765   24759   3
  chr1    24760   28369   2
  chr1    28370   28371   3
  chr1    28372   34961   1
  chr3    233278  239278  1
  chr3    239279  240325  3
  chr3    240326  356364  2
  chr3    356365  440831  3
  </code></pre>
  
  <p>Basically I wish to obtain an output file which reports unique regions. There are a two criteria.</p>
  
  <p>First, if PGB (column 4) is identical between rows, merge range. eg. </p>
  
  <pre><code>chrom   chromStart  chromEnd    PGB
  chr1    1   10  1
  chr1    5   15  1
  </code></pre>
  
  <p>output</p>
  
  <pre><code>chrom   chromStart  chromEnd    PGB
  chr1    1   15  1
  </code></pre>
  
  <p>Second, if PGB is different between rows, chr (column 1) is identical, and the ranges overlap (col2 and 3), report overlapping range as PGB = 3 as well as the ranges unique to their individual categories.</p>
  
  <p>eg. </p>
  
  <pre><code>chrom   chromStart  chromEnd    PGB
  chr1    30  100 1
  chr1    50  150 2
  </code></pre>
  
  <p>output</p>
  
  <pre><code>chrom   chromStart  chromEnd    PGB
  chr1    30  49  1
  chr1    50  100 3
  chr1    101 150 2
  </code></pre>
  
  <p>I hope that illustrates the problem better.</p>