in progress...
[jalview.git] / forester / perl / pf_cutoff_extract.pl
1 #!/usr/bin/perl -W
2
3 # $Id: pf_cutoff_extract.pl,v 1.4 2009/11/11 02:28:19 cmzmasek Exp $
4
5 # This extracts GA, TC, or NC score cutoff values from
6 # Pfam HMM files (GA1, TC1, NC1)
7 # Copyright (C) 2008-2009 Christian M. Zmasek
8 # All rights reserved
9 # Created 2007-08-01 in Winterthur, Switzerland by CMZ
10
11 # Usage: pf_cutoff_extract.pl <Pfam HMM file> <GA|TC|NC> <outfile>
12
13 use strict;
14
15 if ( scalar( @ARGV ) != 3 ) {
16     print "\npf_cutoff_extract.pl <Pfam HMM file> <GA|TC|NC> <outfile>\n\n";
17     exit( -1 );
18 }
19
20 my $infile      = $ARGV[ 0 ];
21 my $cutoff_type = uc( $ARGV[ 1 ] );
22 my $outfile     = $ARGV[ 2 ];
23
24 my $GA = "GA";
25 my $TC = "TC";
26 my $NC = "NC"; 
27
28 if ( -e $outfile ) {
29     die "\n$0: \"$outfile\" already exists.\n\n";
30 }
31 unless( ( -s $infile ) && ( -f $infile ) && ( -T $infile ) ) {
32     die "\n$0: cannot read from \"$infile\".\n\n";
33 }
34 unless( $cutoff_type eq $GA || $cutoff_type eq $TC || $cutoff_type eq $NC ) {
35     die "\n$0: illegal value \"$cutoff_type\" for cutoff type.\n\n";
36 }
37
38 open( IN, "$infile" ) || die "\n$0: Cannot open file \"$infile\": $!\n";
39 open( OUT, ">$outfile" ) || die "\n$0: Cannot create file \"$outfile\": $!\n";
40
41 my $line        = "";
42 my $name        = "";
43 my $line_number = 0;
44 my $n           = 0;
45
46 while ( $line = <IN> ) {
47     $line_number++;
48     if ( $line =~ /^NAME\s+(.+)/ ) {
49         if ( length( $name ) > 0 ) {
50             die "\n$0: Unexpected line $line at line $line_number: $!\n";
51         }
52         $name = $1;
53     }
54     elsif ( $line =~ /^$cutoff_type\s+(\S+)\s+[^;]+/ ) {
55         if ( length( $name ) < 1 ) {
56             die "\n$0: Unexpected line $line at line $line_number: $!\n";
57         }
58         $n++;
59         print OUT "$name $1\n";
60         $name = "";
61     }
62     elsif ( $line =~ /\/\// ) {
63        $name = ""; 
64     }
65 }
66
67 close( OUT ) || die "\n$0: Cannot close file \"$outfile\": $!\n";;
68
69 print( "\nExtracted $n $cutoff_type" . "1 values to \"$outfile\"\n" );
70 print( "\nOK\n" );
71
72 exit( 0 );
73