]> git.donarmstrong.com Git - bin.git/blob - geo_downloader
add start of geo downloader
[bin.git] / geo_downloader
1 #!/usr/bin/perl
2 # geo_downloader downloads expression files from GEO (NCBI)
3 # and is released under the terms of the GNU GPL version 3, or any
4 # later version, at your option. See the file README and COPYING for
5 # more information.
6 # Copyright 2013 by Don Armstrong <don@donarmstrong.com>.
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 =head1 NAME
16
17 geo_downloader - downloads expression files from GEO (NCBI)
18
19 =head1 SYNOPSIS
20
21 geo_downloader [options] [GSE...]
22
23  Options:
24    --debug, -d debugging level (Default 0)
25    --help, -h display this help
26    --man, -m display manual
27
28 =head1 OPTIONS
29
30 =over
31
32 =item B<--debug, -d>
33
34 Debug verbosity. (Default 0)
35
36 =item B<--help, -h>
37
38 Display brief usage information.
39
40 =item B<--man, -m>
41
42 Display this manual.
43
44 =back
45
46 =head1 EXAMPLES
47
48 geo_downloader GSE20400
49
50 =cut
51
52
53 use vars qw($DEBUG);
54 use Net::FTP;
55
56 my %options = (debug           => 0,
57                help            => 0,
58                man             => 0,
59                host => 'ftp.ncbi.nlm.nih.gov',
60               );
61
62 GetOptions(\%options,
63            'debug|d+','help|h|?','man|m');
64
65 pod2usage() if $options{help};
66 pod2usage({verbose=>2}) if $options{man};
67
68 $DEBUG = $options{debug};
69
70 my @USAGE_ERRORS;
71 if (not @ARGV) {
72     push @USAGE_ERRORS,"You must give at least one GEO accession";
73 }
74
75 if (@ARGV != grep {/^(gpl|gse|gsm|gds)\d+$/i} @ARGV) {
76     push @USAGE_ERRORS,"Invalid GEO accession(s): ".
77         join(',',grep {$_ !~ /^(gpl|gse|gsm)\d+$/} @ARGV);
78 }
79
80
81 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
82
83 my $ftp = Net::FTP->new($options{host},Debug=>$DEBUG,Passive=>1) or
84     die "Unable to create new Net::FTP object";
85 print STDERR "Connected\n" if $DEBUG;
86
87 $ftp->login('anonymous') or
88     die "Unable to login";
89 print STDERR "logged in\n" if $DEBUG;
90
91 $ftp->binary();
92 print STDERR "binary\n" if $DEBUG;
93
94
95
96 for my $geo_acc (@ARGV) {
97     my $geo_directory = geo_directory($geo_acc);
98     $ftp->cwd($geo_directory);
99     print STDERR "changed to $geo_directory\n" if $DEBUG;
100     print STDERR "(really) changed to ".$ftp->pwd()."\n" if $DEBUG;
101     my @files = $ftp->ls();
102     print map {$_."\n"} @files;
103     print STDERR "transferred listing\n" if $DEBUG;
104 }
105
106 sub geo_directory {
107     my $geo_acc = shift;
108     $geo_acc = uc($geo_acc);
109     my $geo_acc_dir = $geo_acc;
110     $geo_acc_dir =~ s/\d{3}$/nnn/;
111     my $geo_type_dir = undef;
112     if ($geo_acc =~ /^GSE/) {
113         $geo_type_dir = 'series';
114     } elsif ($geo_acc =~ /^GDS/) {
115         $geo_type_dir = 'datasets';
116     } elsif ($geo_acc =~ /^GPL/) {
117         $geo_type_dir = 'platforms';
118     } elsif ($geo_acc =~ /^GSM/) {
119         $geo_type_dir = 'samples';
120     }
121     return "/geo/".$geo_type_dir.'/'.$geo_acc_dir.'/'.$geo_acc;
122 }
123
124 __END__