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.	my $sapObject = SAPserver->new();
66.	use Getopt::Long;
67.	use ScriptThing;
68.	
69.	my $usage = "usage: svr_function_of [-c column] [-keep] [-s]";
70.	
71.	my $column;
72.	my $keep = 1;
73.	my $i = "-";
74.	my $s;
75.	my $rc  = GetOptions('c=i' => \$column, 
76.			     'keep' => \$keep,
77.			     's' => \$s,
78.			     'i=s' => \$i);
79.	if (! $rc) { print STDERR $usage; exit }
80.	open my $ih, "<$i";
81.	while (my @tuples = ScriptThing::GetBatch($ih, undef, $column)) {
82.	    my @ids = map { $_->[0] } @tuples;
83.	    my $functions = $sapObject->ids_to_functions(-ids => \@ids);
84.	    for my $tuple (@tuples) {
85.	        my ($id, $line) = @$tuple;
86.	        my $function = $functions->{$id};
87.	        if (! defined $function) {
88.	#           print STDERR "$id not found.\n";
89.		    if ($keep) { print "$line\t\n" }
90.	        } else {
91.	            if ($s) {
92.	                $function = ($function =~ /(.+?)\s*[#!]/ ? $1 : $function);
93.	            }
94.	            print "$line\t$function\n";
95.	        }
96.	    }
97.	}