#!/usr/local/bin/perl
# File: makeBands.pl
# Author: Terry Furey
# Date: 9/21/00
# Project: Human
# Description: Creates BED file to upload to database for cytogenetic
# band positions

# Usage message
if ($#ARGV != 1) {
  print stderr "USAGE: makeBands <file> <ctg dir>\n";
  exit(1);
}

# Open the file
$file = shift(@ARGV);
if (!-e $file) {
  print stderr "FATAL: $file does not exist\n";
  exit(1);
}
open(FILE, "<$file");
$ctgdir = shift(@ARGV);

# Set up lengths of chromosomes
@chroms = (1..22,X,Y);
foreach $thischr (@chroms) { 
  open(CTG, "<$ctgdir/$thischr/lift/ordered.lft") || die("Could not open $ctgdir/$thischr/lift/ordered.lft");
  $line = <CTG>;
  chomp($line);
  ($start, $name, $ctglen, $chr, $chrlen) = split(' ',$line);
  @length = (@length,$chrlen);
  close(CTG);
}

# Process each line
$currChrom = "chr1";
$total = 0;
$index = -1;
$line = <FILE>;
while ($line = <FILE>) {
  chomp($line);
  ($chr, $arm, $band, $begin, $end, $estbegin, $estend, $stain, $ruler) 
    = split(' ',$line);
  if ($chr ne "") {
  $chr = "chr".$chr;

  # Check if chromosome changed
  if ($chr ne $currChrom) {
    # Re-scale to fit dimensions of chromosome
    $total = 0;
    for ($i = 0; $i <=$#lengths; $i++) {
      $total += $lengths[$i];
    }
    if ($currChrom eq "chrX") {
      $thislength = $length[22];
    } elsif ($currChrom eq "chrY") {
      $thislength = $length[23];
    } else {
      $ind = substr($currChrom, 3) - 1;
      $thislength = $length[$ind];
    }
    $scale = $thislength/$total;
    for ($i = 0; $i <=$#begband; $i++) {
      $begband[$i] = int($begband[$i] * $scale) + 1;
      $endband[$i] = int($endband[$i] * $scale) + 1;
    }
    # Print out to file
    for ($i = 0; $i < $#begband; $i++) {
      $pend = $begband[$i+1] - 1;
      print ("$currChrom\t$begband[$i]\t$pend\t$name[$i]\t$stainband[$i]\n");
    }
    print ("$currChrom\t$begband[$#begband]\t$thislength\t$name[$#begband]\t$stainband[$#begband]\n");
    # Re-initialize variables
    for ($i = 0; $i <= $index; $i++) {
      shift(@name);
      shift(@begband);
      shift(@andband);
      shift(@lengths);
      shift(@stainband);
    }
    $currChrom = $chr;
    $index = -1;
  }
  $index++;
  $name[$index] = $arm.$band;
  $begband[$index] = $begin;
  $endband[$index] = $end;
  $lengths[$index] = $end - $begin;
  $stainband[$index] = $stain;
}
}

# Re-scale to fit dimensions of chromosome
$total = 0;
for ($i = 0; $i <=$#lengths; $i++) {
  $total += $lengths[$i];
}
if ($currChrom eq "chrX") {
  $thislength = $length[22];
} elsif ($currChrom eq "chrY") {
  $thislength = $length[23];
} else {
  $ind = substr($currChrom, 3) - 1;
  $thislength = $length[$ind];
}
$scale = $thislength/$total;
for ($i = 0; $i <=$#begband; $i++) {
  $begband[$i] = int($begband[$i] * $scale);
  $endband[$i] = int($endband[$i] * $scale);
}
# Print out to file
for ($i = 0; $i <$#begband; $i++) {
  $pend = $begband[$i+1] - 1;
  print ("$currChrom\t$begband[$i]\t$pend\t$name[$i]\t$stainband[$i]\n");
}
print ("$currChrom\t$begband[$#begband]\t$thislength\t$name[$#begband]\t$stainband[$#begband]\n");

close(FILE);
