| 1 | package NCBIx::Geo::Base; | ||||||||||
| 2 | 1 | ▶ | use Class::Std; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use Class::Std::Utils; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use LWP::Simple; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use version; our $VERSION = qv('1.0.0'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use constant GEO_FTP_URL => 'ftp://ftp.ncbi.nih.gov/pub/geo/DATA/supplementary/samples/'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use constant GEO_ACC_URL => 'http://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?view=data&db=GeoDb_blob01&acc='; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | { | ||||||||||
| 17 | 1 | my %debug_of :ATTR( :get<debug> :set<debug> :default<'0'> :init_arg<debug> ); | |||||||||
| 18 | |||||||||||
| 19 | 0 | ▶ | sub START { | ||||||||
|
Subroutine
START: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 20 | 0 | my ($self, $ident, $arg_ref) = @_; | |||||||||
| 21 | |||||||||||
| 22 | 0 | ▶ | if ( defined $arg_ref->{debug} ) { $self->set_debug( $arg_ref->{debug} ); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | |||||||||||
| 24 | 0 | return; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 0 | ▶ | sub debug { my ( $self ) = @_; return $self->get_debug(); } | ||||||||
|
Subroutine
debug: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | sub get_accn_type { | ||||||||
|
Subroutine
get_accn_type: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 30 | 0 | my ( $self, $arg_ref ) = @_; | |||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | my $accn = defined $arg_ref->{accn} ? $arg_ref->{accn} : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | $accn =~ m/^(\w{3})/; | |||||||||
| 34 | 0 | return $1; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 0 | ▶ | sub get_sample_data { | ||||||||
|
Subroutine
get_sample_data: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||
| 38 | 0 | my ( $self, $arg_ref ) = @_; | |||||||||
| 39 | 0 | ▶ | my $sample_accn = defined $arg_ref->{accn} ? $arg_ref->{accn} : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | ▶ | my @file_exts = defined $arg_ref->{exts} ? @{ $arg_ref->{exts} } : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | # Calculate the NCBI GSM directory | ||||||||||
| 43 | 0 | my $gsm_dir = $sample_accn; | |||||||||
| 44 | 0 | $gsm_dir =~ s/.{3}$/nnn/; | |||||||||
| 45 | 0 | my $gsm_url = GEO_FTP_URL . "$gsm_dir/$sample_accn/"; | |||||||||
| 46 | |||||||||||
| 47 | # Get the raw data | ||||||||||
| 48 | 0 | foreach my $ext ( @file_exts ) { | |||||||||
| 49 | 0 | ▶ | if ( $ext eq 'TXT' ) { $ext = 'txt'; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | |||||||||||
| 51 | # Calculate file_name and file_path_name | ||||||||||
| 52 | 0 | my $file = $sample_accn . '.' . $ext . '.gz'; | |||||||||
| 53 | 0 | my $path_name = $self->get_data_dir() . $file; | |||||||||
| 54 | |||||||||||
| 55 | # Download if it doesn't exist | ||||||||||
| 56 | 0 | ▶ | if (! -s $path_name ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | my $url = $gsm_url . $file; | |||||||||
| 58 | 0 | $self->_debug( "DOWNLOAD: $url" ); | |||||||||
| 59 | 0 | my $result = get( $url ); | |||||||||
| 60 | 0 | $self->_debug( "SAVE FILE: $path_name" ); | |||||||||
| 61 | 0 | $self->_set_file_text({ file => $path_name, text => $result }); | |||||||||
| 62 | } else { | ||||||||||
| 63 | 0 | $self->_debug( "FILE EXISTS: $path_name" ); | |||||||||
| 64 | } | ||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | # Calculate call data file_name and file_path_name | ||||||||||
| 68 | 0 | my $file = $sample_accn . '.html'; | |||||||||
| 69 | 0 | my $path_name = $self->get_data_dir() . $file; | |||||||||
| 70 | |||||||||||
| 71 | # Download call data if it doesn't exist | ||||||||||
| 72 | 0 | ▶ | if (! -s $path_name ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | my $acc_url = GEO_ACC_URL . $sample_accn; | |||||||||
| 74 | 0 | $self->_debug( "DOWNLOAD: $acc_url" ); | |||||||||
| 75 | 0 | my $result = get( $acc_url ); | |||||||||
| 76 | 0 | $self->_debug( "SAVE FILE: $path_name" ); | |||||||||
| 77 | 0 | $self->_set_file_text({ file => $path_name, text => $result }); | |||||||||
| 78 | } else { | ||||||||||
| 79 | 0 | $self->_debug( "FILE EXISTS: $path_name" ); | |||||||||
| 80 | } | ||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | sub _set_file_text { | ||||||||
|
Subroutine
_set_file_text: not called CC=4 SCAR=30.0
| |||||||||||
| 84 | 0 | my ( $self, $arg_ref ) = @_; | |||||||||
| 85 | 0 | ▶ | my $file = defined $arg_ref->{file} ? $arg_ref->{file} : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | ▶ | my $text = defined $arg_ref->{text} ? $arg_ref->{text} : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | if ( $file ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | open( OUTFILE, '>', $file ); | |||||||||
| 90 | 0 | print OUTFILE $text; | |||||||||
| 91 | 0 | close( OUTFILE ); | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 0 | return; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | sub _get_file_text { | ||||||||
|
Subroutine
_get_file_text: not called CC=4 SCAR=30.0
| |||||||||||
| 98 | 0 | my ( $self, $arg_ref ) = @_; | |||||||||
| 99 | 0 | ▶ | my $file = defined $arg_ref->{file} ? $arg_ref->{file} : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | $self->_debug( "INFILE: $file" ); | |||||||||
| 101 | 0 | my $lines; | |||||||||
| 102 | |||||||||||
| 103 | 0 | ▶ | if ( $file ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | open( INFILE, '<', $file ); | |||||||||
| 105 | 0 | ▶ | while ( my $line = <INFILE> ) { $lines .= $line; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 0 | close( INFILE ); | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 0 | return $lines; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 0 | ▶ | sub _get_url { | ||||||||
|
Subroutine
_get_url: not called CC=2 SCAR=17.9
| |||||||||||
| 113 | 0 | my ( $self, $url ) = @_; | |||||||||
| 114 | 0 | my $response; | |||||||||
| 115 | 0 | eval { $response = get( $url ); }; | |||||||||
| 116 | 0 | ▶ | if( $@ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | $self->warn("Can't query website: $@"); | |||||||||
| 118 | 0 | return; | |||||||||
| 119 | } | ||||||||||
| 120 | 0 | $self->debug( "resp is $response\n"); | |||||||||
| 121 | 0 | return $response; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 0 | ▶ | sub _debug { | ||||||||
|
Subroutine
_debug: not called CC=2 SCAR=17.9
| |||||||||||
| 125 | 0 | my ( $self, $message ) = @_; | |||||||||
| 126 | 0 | ▶ | if ( $self->debug() ) { print( $message . "\n" ); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 0 | return $self; | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | 0 | ▶ | sub __exception { | ||||||||
|
Subroutine
__exception: not called CC=1 SCAR=6.9
| |||||||||||
| 131 | 0 | my ( $data ) = @_; | |||||||||
| 132 | 0 | print "\n#######################################\n"; | |||||||||
| 133 | 0 | print "# Exception #\n"; | |||||||||
| 134 | 0 | print "#######################################\n"; | |||||||||
| 135 | 0 | print Dumper( $data ); | |||||||||
| 136 | 0 | print "#######################################\n\n"; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 1 | 1; # Magic true value required at end of module | |||||||||
| 142 | __END__ | ||||||||||