#!/usr/local/bin/perl
#*************************************************************************
#
#   Program:    rewritedomainfile
#   File:       rewritedomainfile.perl
#   
#   Version:    V1.0
#   Date:       08.06.01
#   Function:   Rewrite a CATH domain file in an easy-to-read form
#   
#   Copyright:  (c) Reading University / Dr. Andrew C. R. Martin 2001
#   Author:     Dr. Andrew C. R. Martin
#   EMail:      a.c.r.martin@reading.ac.uk
#               andrew@bioinf.org.uk
#               
#*************************************************************************
#
#   This program is not in the public domain, but it may be copied
#   freely providing this notice is retained.
#
#   The code may be modified as required, but any modifications must be
#   documented so that the person responsible can be identified. If 
#   someone else breaks this code, I don't want to be blamed for code 
#   that does not work! 
#
#   The code may not be sold commercially or included as part of a 
#   commercial product.
#
#*************************************************************************
#
#   Description:
#   ============
#   Reads the CATH domain definition file and spews out an easy-to-read
#   version. Non-domain associated fragment information is ignored.
#
#*************************************************************************
#
#   Usage:
#   ======
#   rewritedomainfile.perl domlist > domains.out
#
#*************************************************************************
#
#   Revision History:
#   =================
#   V1.1  11.10.99 Deletes the output file if the input PDB file didn't
#                  exist (i.e. was obsoleted)
#
#*************************************************************************

$| = 1;                                   # Turn on flushing
while(<>)                                 # Loop over the domains file
{
    chomp;
    @fields = split;

    # Obtain the basic identifying data
    $pdb        = substr($fields[0],0,4);
    $chain      = substr($fields[0],4,1);
    $ndomains   = substr($fields[1],1) * 1;
    $nfragments = substr($fields[2],1) * 1;

    # Start looking at the domains
    $base       = 3;
    # For each domain
    for($i=0; $i<$ndomains; $i++)
    {
        $nseg = $fields[$base++];
        # For each segment of this domain, print the identifier and
        # residue range
        for($j=0; $j<$nseg; $j++)
        {
            $domnum = $i+1;
            $domid  = $pdb . $chain . $domnum;
            printf "%s Segment %d ", $domid, $j+1;
            printf "Start: %s %5d %s ", 
                   $fields[$base++], $fields[$base++], $fields[$base++];
            printf "End: %s %5d %s",
                   $fields[$base++], $fields[$base++], $fields[$base++];
            print "\n";
        }
    }
}

