« blib/script/gbrowse_syn_load_alignment_database.pl
↑ Summary
blib/conf/plugins/LDAPAuthenticate.pm »
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | #$Id: scan_gbrowse.pl,v 1.1 2009-03-05 23:02:59 lstein Exp $ | ||
| 4 | |||
| 5 | # This script scans the gbrowse at the indicated URL and returns track | ||
| 6 | # configuration stanzas for all tracks that are marked "discoverable." | ||
| 7 | # If the --cache <cachetime> argument is present, then results will be | ||
| 8 | # cached for <cachetime> seconds. | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | use Getopt::Long; | ||
| 13 | use File::Spec; | ||
| 14 | use File::Basename 'basename'; | ||
| 15 | use LWP::Simple; | ||
| 16 | |||
| 17 | use constant SECS_PER_DAY => 60*60*24; | ||
| 18 | |||
| 19 | my $cache_time = 0; | ||
| 20 | |||
| 21 | my $USAGE = <<USAGE; | ||
| 22 | Usage: $0 <gbrowse_url> | ||
| 23 | |||
| 24 | Scan the gbrowse2 instance at <gbrowse_url> for discoverable tracks | ||
| 25 | and create appropriate remote track configuration stanzas suitable for | ||
| 26 | incorporation into the local gbrowse2 config file. | ||
| 27 | |||
| 28 | Options: | ||
| 29 | --cache <cachetime> Cache the results in a temporary file | ||
| 30 | for cachetime seconds. | ||
| 31 | USAGE | ||
| 32 | ; | ||
| 33 | |||
| 34 | GetOptions('cache=i' => \$cache_time) or die $USAGE; | ||
| 35 | my $url = shift or die $USAGE; | ||
| 36 | |||
| 37 | my $tmpdir = File::Spec->catfile(File::Spec->tmpdir,'gbrowse_scanner'.'_'.(getpwuid($<))[0]); | ||
| 38 | unless (-e $tmpdir && -d _) { | ||
| 39 | mkdir $tmpdir or die "Couldn't create $tmpdir: $!"; | ||
| 40 | } | ||
| 41 | (my $fname = $url) =~ s!/!_!g; | ||
| 42 | my $cache_file = File::Spec->catfile($tmpdir,"$fname.cache"); | ||
| 43 | my $dest_file = File::Spec->catfile($tmpdir,"$fname.dest"); | ||
| 44 | build_cache($url,$dest_file,$cache_file) | ||
| 45 | unless -e $cache_file && ((-M $cache_file) * SECS_PER_DAY)<$cache_time; | ||
| 46 | |||
| 47 | open my $fh,'<',$cache_file or die "Couldn't open cache file $cache_file: $!"; | ||
| 48 | print while <$fh>; | ||
| 49 | close $fh; | ||
| 50 | exit 0; | ||
| 51 | |||
| 52 | sub build_cache { | ||
| 53 | my ($url,$dest,$cache) = @_; | ||
| 54 | $url =~ s/\?.+//; # get rid of any CGI args there already | ||
| 55 | my $source = basename($url); | ||
| 56 | |||
| 57 | $url .= '/' unless $url =~ /\/$/; | ||
| 58 | |||
| 59 | my $response = mirror("$url?gbgff=scan",$dest); | ||
| 60 | if (is_error($response)) { | ||
| 61 | die "Could not scan $url. Status code was $response"; | ||
| 62 | } | ||
| 63 | open my $fh,'<',$dest or die "Could not open $dest: $!"; | ||
| 64 | open my $out,'>',$cache or die "Could not open $cache for writing: $!"; | ||
| 65 | |||
| 66 | my ($hostname) = $url =~ /^\w+:\/\/([^\/]+)/; | ||
| 67 | (my $colon_escape = $url) =~ s/:/\\:/g; | ||
| 68 | |||
| 69 | # paragraph mode to read stanzas | ||
| 70 | local $/ = ''; | ||
| 71 | while (<$fh>) { | ||
| 72 | my ($label,$rest) = /\[([^\]]+)\]\s+(.+)/s; | ||
| 73 | my $remote = "$url?gbgff=1;s=1;t=$label;segment=\$segment"; | ||
| 74 | chomp($rest); | ||
| 75 | print $out <<END; | ||
| 76 | [${hostname}_${source}_${label}] | ||
| 77 | remote feature = $remote | ||
| 78 | category = Tracks from $colon_escape | ||
| 79 | $rest | ||
| 80 | |||
| 81 | END | ||
| 82 | ; | ||
| 83 | } | ||
| 84 | close $out; | ||
| 85 | close $fh; | ||
| 86 | } | ||
| 87 | |||
| 88 | __END__ |