| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # documentation at end of file | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Pod::Usage; | ||
| 7 | use Getopt::Long; | ||
| 8 | use IO::Prompt::Tiny qw(prompt); | ||
| 9 | use Bio::ToolBox::db_helper qw( | ||
| 10 | open_db_connection | ||
| 11 | get_dataset_list | ||
| 12 | ); | ||
| 13 | use Bio::ToolBox::db_helper::config; | ||
| 14 | |||
| 15 | our $VERSION = '2.00'; | ||
| 16 | |||
| 17 | print "\n A script to print all available feature types in a database\n\n"; | ||
| 18 | |||
| 19 | ### Quick help | ||
| 20 | # | ||
| 21 | |||
| 22 | ### Get command line options and initialize values | ||
| 23 | my ( $dbname, $help, $print_version, ); | ||
| 24 | |||
| 25 | # Command line options | ||
| 26 | GetOptions( | ||
| 27 | 'db=s' => \$dbname, # the database name | ||
| 28 | 'help' => \$help, # request help | ||
| 29 | 'version' => \$print_version, # print the version | ||
| 30 | ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; | ||
| 31 | |||
| 32 | # Print help | ||
| 33 | if ($help) { | ||
| 34 | |||
| 35 | # print entire POD | ||
| 36 | pod2usage( | ||
| 37 | { | ||
| 38 | '-verbose' => 2, | ||
| 39 | '-exitval' => 1, | ||
| 40 | } | ||
| 41 | ); | ||
| 42 | } | ||
| 43 | |||
| 44 | # Print version | ||
| 45 | if ($print_version) { | ||
| 46 | print " Biotoolbox script db_types.pl, version $VERSION\n"; | ||
| 47 | eval { | ||
| 48 | require Bio::ToolBox; | ||
| 49 | my $v = Bio::ToolBox->VERSION; | ||
| 50 | print " Biotoolbox package version $v\n"; | ||
| 51 | }; | ||
| 52 | exit; | ||
| 53 | } | ||
| 54 | |||
| 55 | # Check for database | ||
| 56 | unless ($dbname) { | ||
| 57 | if (@ARGV) { | ||
| 58 | $dbname = shift @ARGV; | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | $dbname = request_db_from_user(); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | unless ($dbname) { | ||
| 65 | |||
| 66 | # when no databases are available, print SYNOPSIS | ||
| 67 | pod2usage( | ||
| 68 | { | ||
| 69 | '-verbose' => 0, | ||
| 70 | '-exitval' => 1, | ||
| 71 | } | ||
| 72 | ); | ||
| 73 | exit; | ||
| 74 | } | ||
| 75 | |||
| 76 | # Initialize | ||
| 77 | my $count = 0; | ||
| 78 | my %source2type; | ||
| 79 | |||
| 80 | # Get the features | ||
| 81 | my @types = get_dataset_list($dbname); | ||
| 82 | |||
| 83 | # this returns an array of database types | ||
| 84 | |||
| 85 | foreach my $type (@types) { | ||
| 86 | |||
| 87 | # each type is usually comprised of primary_tag:source_tag | ||
| 88 | # although sometimes it is just the primary_tag | ||
| 89 | |||
| 90 | # get individual tags | ||
| 91 | my ( $primary, $source ); | ||
| 92 | if ( $type =~ /:/ ) { | ||
| 93 | ( $primary, $source ) = split /:/, $type; | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | $primary = $type; | ||
| 97 | $source = 'NONE'; | ||
| 98 | } | ||
| 99 | |||
| 100 | # store the type in an array under the source | ||
| 101 | if ( exists $source2type{$source} ) { | ||
| 102 | push @{ $source2type{$source} }, $primary; | ||
| 103 | } | ||
| 104 | else { | ||
| 105 | $source2type{$source} = [ ($primary) ]; | ||
| 106 | } | ||
| 107 | $count++; | ||
| 108 | } | ||
| 109 | print " Found $count feature types in database '$dbname'\n"; | ||
| 110 | |||
| 111 | # Print the database types by source type | ||
| 112 | foreach my $source ( sort { $a cmp $b } keys %source2type ) { | ||
| 113 | print " There are ", scalar @{ $source2type{$source} }, " feature types ", | ||
| 114 | "with source '$source'\n"; | ||
| 115 | foreach ( sort { $a cmp $b } @{ $source2type{$source} } ) { | ||
| 116 | print " $_\n"; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | print "That's all\n"; | ||
| 121 | |||
| 122 | sub request_db_from_user { | ||
| 123 | my %num2db; | ||
| 124 | my $n = 1; | ||
| 125 | foreach my $key ( $BTB_CONFIG->param() ) { | ||
| 126 | if ( $key =~ /^ (.+) \.dsn $/x ) { | ||
| 127 | next if $1 =~ /example/; | ||
| 128 | $num2db{$n} = $1; | ||
| 129 | $n++; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | return unless $n > 1; | ||
| 133 | |||
| 134 | print " These are the known databases:\n"; | ||
| 135 | foreach ( sort { $a <=> $b } keys %num2db ) { | ||
| 136 | print " $_\t$num2db{$_}\n"; | ||
| 137 | } | ||
| 138 | my $p = " Enter the number of the database to examine "; | ||
| 139 | my $number = prompt($p); | ||
| 140 | return unless exists $num2db{$number}; | ||
| 141 | return $num2db{$number}; | ||
| 142 | } | ||
| 143 | |||
| 144 | __END__ |