#!/usr/bin/perl

=head1 NAME

2007-03-15-pod-getopt.pl - learn about command-line option parsing and POD.

=head1 SYNOPSIS

2007-03-15-pod-getopt.pl -f <go-file> -q <query-goid> -q <query-goid>

=head1 DESCRIPTION

This script parses and processes the Gene Ontology. It supports the following command-line options:

=over

=item count

Print  the number of functions in the Gene Ontology.

=item file

The name of the file describing the structure of the Gene
Ontology. The latest version is available at http://www.geneontology.org/ontology/gene_ontology_edit.obo. 


=item help

Print a brief help message.

=item man

Print a detailed manual.


=item parents

Print all GO functions that have a number of parents at least as large
as this argument.


=item query-goid

The GO ID of a function you want to query. You can provide this option
multiple times.



=back

=cut

use strict;
use warnings;

use Data::Dumper;
use Getopt::Long;
use Pod::Usage;

#use GO::Term;
use GO::Ontology;

=pod

Insert documentation here.

=cut

my ($count_num_functions, $distance_to_ancestor, $compute_depths, 
    $go, $go_file, $help, $man, $num_parents, @query_goids);

#$go_file = "gene_ontology.obo";
# passing the file name on the command line is better than hardcoding its location in the script.


GetOptions(
	   "count" => \$count_num_functions,
	   "depths|D" => \$compute_depths,
	   "distance=i" => \$distance_to_ancestor,
	   "help" => \$help,
	   "man" => \$man,
	   # the argument following -p on the command line is an integer.
	   "parents=i" => \$num_parents,
	   # the argument following -q on the command line is a
	   # string. -q can appear multiple times.
	   "query-gene=s"=> \@query_goids, 
	   # the argument following -f on the command line is a string.
	   "file=s" => \$go_file,
	   );
pod2usage(1) if $help;
pod2usage(-verbose => 2) if $man;

#$go_file = $ARGV[0];
print("GO file is $go_file.\n");
$go = GO::Ontology::new();

$go->read($go_file);
my ($goterm, $ancestors);
foreach my $query (@query_goids)
  {
    $goterm = $go->terms($query);
    print("$goterm\n");
    print("GO id $query: ", $goterm->name(), "\n");
    if (defined($distance_to_ancestor))
      {
	$ancestors = $go->get_ancestors($query, $distance_to_ancestor);
	print(Dumper($ancestors));
	$ancestors = $go->get_ancestors_at_distance($query, 
						    $distance_to_ancestor);
	print(Dumper($ancestors));
      }
  }

if ($compute_depths) 
  {
    # don't forget to compute the children of each term.
    $go->compute_children();
    my $depths = $go->compute_depths();
    print(Dumper($depths));
  }
