in progress
[jalview.git] / forester / archive / perl / countSpeciesSPTrEMBL.pl
1 #!/usr/bin/perl -W
2
3 # countSpeciesSPTrEMBL.pl
4 # -----------------------
5 #
6 # Copyright (C) 2003 Christian M. Zmasek
7 # All rights reserved
8 #
9 # Created: 02/27/03
10 # Last modified: 02/27/03
11 # Author: Christian M. Zmasek
12 # zmasek@genetics.wustl.edu
13 # http://www.genetics.wustl.edu/eddy/people/zmasek/
14 #
15 # Last modified 05/23/02
16
17 # Purpose. Counts species in SWISS-PROT and TrEMBL.
18 #
19 # Usage.   countSpeciesSPTrEMBL.pl <path/to/trembl.dat> <path/to/sprot.dat> <outfile> 
20 #
21
22
23 use strict;
24
25
26 my $VERSION       = "1.000";
27 my $infile_sp     = "";
28 my $infile_tr     = "";
29 my $outfile       = "";
30
31 my $return_line   = "";
32 my $read          = 0;
33 my $os            = "";
34 my %species_count = (); # full name -> count.
35
36
37 if ( @ARGV != 3 ) {
38     &errorInCommandLine();
39 }
40
41 $infile_tr = $ARGV[ 0 ];
42 $infile_sp = $ARGV[ 1 ];
43 $outfile   = $ARGV[ 2 ];
44
45
46
47 if ( -e $outfile ) {
48     die "\n$0: <<$outfile>> already exists.\n\n";
49 }
50 unless ( ( -s $infile_tr ) && ( -f $infile_tr ) && ( -T $infile_tr ) ) {
51     die "\n$0: <$infile_tr>> does not exist, is empty, or is not a plain textfile.\n\n";
52 }
53 unless ( ( -s $infile_sp ) && ( -f $infile_sp ) && ( -T $infile_sp ) ) {
54     die "\n$0: <<$infile_sp>> does not exist, is empty, or is not a plain textfile.\n\n";
55 }
56
57 open( IN_TR, "$infile_tr" ) || die "\n$0: Cannot open file <<$infile_tr>>: $!\n";
58 open( IN_SP, "$infile_sp" ) || die "\n$0: Cannot open file <<$infile_sp>>: $!\n";
59 open( OUT, ">$outfile" ) || die "\n$0: Cannot create file <<$outfile>>: $!\n";
60
61
62 $read = 0; 
63
64 while ( $return_line = <IN_TR> ) {
65     if ( $return_line =~ /^AC\s+(\S+);/ ) {
66         $read = 1;
67     }
68     elsif ( $return_line =~ /^OS\s+(.+)\.\s*$/ && $read == 1 ) {
69         $os = $1;
70         $os =~ s/\(.+\)//g;
71         $os =~ s/^\s+//;
72         $os =~ s/\s+$//;
73         $os =~ s/\.$//;
74         if ( exists( $species_count{ $os } ) ) {
75             $species_count{ $os } = $species_count{ $os } + 1;   
76         }
77         else {
78             $species_count{ $os } = 1;
79         }
80         print "$os\n";
81     }
82     elsif ( $return_line =~ /^\/\// && $read == 1 ) {
83         $read = 0;
84         $os = "";
85     }
86 }
87
88 close( IN_TR ); 
89
90 $read = 0;
91 $os = "";
92 $return_line =  "";
93
94 while ( $return_line = <IN_SP> ) {
95     if ( $return_line =~ /^ID\s+(\S+)/ ) {
96         $read = 1;
97     }
98     elsif ( $return_line =~ /^OS\s+(.+)\s*$/ && $read == 1 ) {
99         $os = $1;
100         $os =~ s/\(.+//g;
101         $os =~ s/^\s+//;
102         $os =~ s/\s+$//;
103         $os =~ s/\.$//;
104         $read = 0;
105         if ( exists( $species_count{ $os } ) ) {
106             $species_count{ $os } = $species_count{ $os } + 1;   
107         }
108         else {
109             $species_count{ $os } = 1;
110         }
111         print "$os\n";
112     }
113     elsif ( $return_line =~ /^\/\// && $read == 1 ) {
114         $read = 0;
115         $os = "";
116     }
117 }
118
119 close( IN_SP );
120
121
122 foreach my $species ( sort { $species_count{ $b } <=> $species_count{ $a } } keys %species_count ) {
123     print OUT "$species: $species_count{$species}\n";
124 }
125
126
127 print "\n\nDone!\n\n";
128
129 close( OUT );
130
131 exit( 0 );
132
133
134
135
136
137
138 sub errorInCommandLine {
139     print "\n";
140     print " countSpeciesSPTrEMBL.pl $VERSION\n";
141     print " -----------------------\n";
142     print "\n";
143     print " Christian Zmasek (zmasek\@genetics.wustl.edu)\n";
144     print "\n";
145     print " Purpose. Counts species in SWISS-PROT and TrEMBL.\n";
146     print "\n";
147     print " Usage.   countSpeciesSPTrEMBL.pl <path/to/trembl.dat> <path/to/sprot.dat> <outfile>\n";
148     print "\n";
149     exit( -1 );
150 }