1.	use strict;
2.	use Data::Dumper;
3.	use Carp;
4.	
5.	#
6.	# This is a SAS Component
7.	#
8.	
9.	
10.	=head1 svr_function_of
11.	
12.	Get functions of protein-encoding genes
13.	
14.	------
15.	
16.	Example:
17.	
18.	    svr_all_features 3702.1 peg | svr_function_of
19.	
20.	would produce a 2-column table.  The first column would contain
21.	PEG IDs for genes occurring in genome 3702.1, and the second
22.	would contain the functions of those genes.
23.	
24.	------
25.	
26.	The standard input should be a tab-separated table (i.e., each line 
27.	is a tab-separated set of fields).  Normally, the last field in each
28.	line would contain the PEG for which functions are being requested.
29.	If some other column contains the PEGs, use
30.	
31.	    -c N
32.	
33.	where N is the column (from 1) that contains the PEG in each case.
34.	
35.	This is a pipe command. The input is taken from the standard input, and the
36.	output is to the standard output.
37.	
38.	=head2 Command-Line Options
39.	
40.	=over 4
41.	
42.	=item -c Column
43.	
44.	This is used only if the column containing PEGs is not the last.
45.	
46.	=item -s
47.	
48.	Strip comments from the functions.
49.	
50.	=item -keep
51.	
52.	This is used to keep input lines for which the PEG has no function
53.	
54.	=back
55.	
56.	=head2 Output Format
57.	
58.	The standard output is a tab-delimited file. It consists of the input
59.	file with an extra column added (the function associated with the PEG).
60.	
61.	=cut
62.	
63.	use SeedUtils;
64.	use SAPserver;
65.	use Getopt::Long;
66.	use ScriptThing;
67.	
68.	my $usage = "usage: svr_function_of [-c column] [-keep] [-s]";
69.	
70.	my $column;
71.	my $keep = 1;
72.	my $i = "-";
73.	my $s;
74.	my $url = '';
75.	my $rc  = GetOptions('c=i' => \$column, 
76.			     'keep' => \$keep,
77.			     's' => \$s, 'url=s' => \$url,
78.			     'i=s' => \$i);
79.	if (! $rc) { print STDERR $usage; exit }
80.	my $sapObject = SAPserver->new(url => $url);
81.	open my $ih, "<$i";
82.	while (my @tuples = ScriptThing::GetBatch($ih, undef, $column)) {
83.	    my @ids = map { $_->[0] } @tuples;
84.	    my $functions = $sapObject->ids_to_functions(-ids => \@ids);
85.	    for my $tuple (@tuples) {
86.	        my ($id, $line) = @$tuple;
87.	        my $function = $functions->{$id};
88.	        if (! defined $function) {
89.	#           print STDERR "$id not found.\n";
90.		    if ($keep) { print "$line\t\n" }
91.	        } else {
92.	            if ($s) {
93.	                $function = ($function =~ /(.+?)\s*[#!]/ ? $1 : $function);
94.	            }
95.	            print "$line\t$function\n";
96.	        }
97.	    }
98.	}