#!/usr/bin/perl

# agpToLift - generate a lift file from an AGP file

# $Header: /projects/compbio/cvsroot/kent/src/utils/agpToLift,v 1.2 2007/03/09 19:29:00 angie Exp $

use warnings;
use strict;

my %chromSizes = ();
my @lines = ();
my $i;
my $revStrand = 0;

# check for options

while ($#ARGV >= 0) {
  my $arg = shift(@ARGV);
  if ($arg eq "-revStrand") {
    warn ("\n  using -revStrand; the resulting lift file will contain a \n" .
	  "  strand column which may or may not be processed correctly \n" .
	  "  by liftUp depending on type\n\n");
    $revStrand = 1;
  } else {
    die "$arg is not a valid parameter\n";
  }
}

# read in AGP file, saving chrom lengths
while (<>) {
    $lines[$i++] = $_;
    my ($chrom, $chromStart, $chromEnd, $ix, $type, 
            $frag, $fragStart, $fragEnd, $strand) = split /\s+/;
    $chromSizes{$chrom} = $chromEnd;
}

# process AGP lines, writing lift file, using chrom length info
for ($i=0; $i < @lines; $i++) {
    $_ = $lines[$i];
    my ($chrom, $chromStart, $chromEnd, $ix, $type, 
            $frag, $fragStart, $fragEnd, $strand) = split /\s+/;
    next if ($type eq "N");     # skip gap
    if ($strand eq "-") {
        if (!$revStrand) {
            die ("Input contains reverse strand fragments: use -revStrand\n");
        }
    }
    printf "%s\t%s\t%s\t%s\t%s%s\n", 
            $chromStart - 1, $frag, $fragEnd - $fragStart + 1,
            $chrom, $chromSizes{$chrom},
	    ($revStrand ? "\t$strand" : "");
}


