in progress...
[jalview.git] / wiki / RubyExamples.wiki
1 #summary Ruby examples
2
3 = Ruby Examples =
4
5 Various simple Ruby examples.
6
7 Author: [https://sites.google.com/site/cmzmasek/ Christian Zmasek], Sanford-Burnham Medical Research Institute
8
9  
10 Copyright (C) 2011 Christian M Zmasek. All rights reserved.
11
12
13 = Using net/ftp to download all Proteomes from the Ensembl Database =
14
15 This, or something like it, can be used to download from Ensembl all "pep.all.fa.gz" files.
16
17 {{{
18 require 'net/ftp'
19
20 EMAIL           = 'myemail'
21 PUB_RELEASE_DIR = '/pub/release-64/fasta'
22 PEP_DIR         = '/pep'
23
24 ftp = Net::FTP.new('ftp.ensembl.org', 'anonymous', EMAIL)
25 ftp.passive = true # To avoid "No route to host" error.
26 ftp.chdir( PUB_RELEASE_DIR )
27 files = ftp.list('*_*') # To only look at files with an underscore.
28 count = 0
29 files.each do | file |
30   species = file.split().last
31   begin
32     ftp.chdir(species + PEP_DIR)
33     pepfiles = ftp.list()
34     pepfiles.each do | pepfile |
35       pepfile = pepfile.split().last
36       if pepfile =~ /all.fa.gz/ # Only want the "all.fa.gz" files (and not the
37                                 # "abinitio" files).
38         ftp.getbinaryfile(pepfile)
39         puts 'downloaded "' + pepfile + '"'
40         count += 1
41       end
42     end
43   rescue Exception
44     puts 'ignoring "' + species + '"'
45   end
46   ftp.chdir(PUB_RELEASE_DIR) # To go back to the starting directory.
47 end
48 ftp.close
49 puts 'done (downloaded ' + count.to_s + ' files)'
50 }}}