#!/usr/local/bin/perl
# File: makeBandRanges
# Author: Terry Furey
# Date: 10/26/00
# Project: Human
# Description: Create a file of the subband ranges for each of the bands

# Usage message
if ($#ARGV != 0) {
  print stderr "USAGE: makeBandRanges <cytobands.pct.bed>\n";
  exit(1);
}

# Read parameters
$file = shift(@ARGV);
open(FILE, "<$file") || die("Could not open $file\n");

# Find all major band ranges
$currband = "";
$prevband = "";
$prevend = 0;
$currchr = "chr1";
$index = -1;
while ($line = <FILE>) {
  chomp($line);
  ($chr, $start, $end, $band, @rest) = split(' ',$line);
  if ($chr ne $currchr) {
    $bandend[$index] = $prevband;
    $posend[$index] = $prevend;
    printBands();
    $index = -1;
    while ($#bands >= 0) {
      shift(@bands);
      shift(@bandend);
      shift(@bandstart);
      shift(@posstart);
      shift(@posend);
    }
    $currchr = $chr;
  }

  # Determine major bands
  ($majband,$rest) = split(/\./,$band);
  if ($majband ne $currband) {
    if ($index != -1) {
      $bandend[$index] = $prevband;
      $posend[$index] = $prevend;
    }
    $index++;
    $currband = $majband;
    $bands[$index] = $majband;
    $bandstart[$index] = $band;
    $posstart[$index] = $start;
  }
  $prevband = $band;
  $prevend = $end;
}
$bandend[$index] = $prevband;
$posend[$index] = $prevend;
$currchr = $chr;
printBands();
close(FILE);

# Determine minor bands
open(FILE, "<$file") || die("Could not open $file\n");
$currband = "";
$prevband = "";
$prevend = 0;
$currchr = "chr1";
$inband = 0;
$index = -1;
while ($#bands >= 0) {
  shift(@bands);
  shift(@bandend);
  shift(@bandstart);
  shift(@posstart);
  shift(@posend);
}
while ($line = <FILE>) {
  chomp($line);
  ($chr, $start, $end, $band, @rest) = split(' ',$line);
  if ($chr ne $currchr) {
    if (($inband) && ($index != -1)) {
      $bandend[$index] = $prevband;
      $posend[$index] = $prevend;
    }
    printBands();
    $index = -1;
    while ($#bands >= 0) {
      shift(@bands);
      shift(@bandend);
      shift(@bandstart);
      shift(@posstart);
      shift(@posend);
    }
    $currchr = $chr;
  }

  # Determine minor band
  @chars = split(//,$band);
  if (($chars[$#chars-2] eq '.') || ($chars[$#chars-3] eq '.')) {
    $minband = "";
    for ($m = 0; $m < $#chars; $m++) {
      $minband .= $chars[$m];
    }
    if ($minband ne $currband) {
      if (($index != -1) && ($inband)) {
	$bandend[$index] = $prevband;
	$posend[$index] = $prevend;
      }
      $index++;
      $currband = $minband;
      $bands[$index] = $minband;
      $bandstart[$index] = $band;
      $posstart[$index] = $start;
    }
    $inband = 1;
  } else {
    if (($inband) && ($index != -1)) {
      $bandend[$index] = $prevband;
      $posend[$index] = $prevend;
    }
    $inband = 0;
  }
  $prevband = $band;
  $prevend = $end;  
}
if ($inband) {
  $bandend[$index] = $prevband;
  $posend[$index] = $prevend;
}
$currchr = $chr;
printBands();
close(FILE);

sub printBands() {
  for ($i = 0; $i <= $#bands; $i++) {
    print "$currchr\t$bands[$i]\t$bandstart[$i]\t$bandend[$i]\t$posstart[$i]\t$posend[$i]\n";
  }
}
