bio 2.0.5 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/doc/Tutorial.md ADDED
@@ -0,0 +1,1274 @@
1
+ # BioRuby Tutorial
2
+
3
+ * Copyright (C) 2001-2003 KATAYAMA Toshiaki <k .at. bioruby.org>
4
+ * Copyright (C) 2005-2011 Pjotr Prins, Naohisa Goto and others
5
+
6
+ This document was last modified: 2011/10/14 Current editor: Michael O'Keefe <okeefm (at) rpi (dot) edu>
7
+
8
+ The latest version resides in the GIT source code repository: ./doc/[Tutorial.rd](https://github.com/bioruby/bioruby/blob/master/doc/Tutorial.rd).
9
+
10
+ ## Introduction
11
+
12
+ This is a tutorial for using Bioruby. A basic knowledge of Ruby is required. If you want to know more about the programming language, we recommend the latest Ruby book [Programming Ruby](http://www.pragprog.com/titles/ruby)by Dave Thomas and Andy Hunt - the first edition can be read online[here](http://www.ruby-doc.org/docs/ProgrammingRuby/).
13
+
14
+ For BioRuby you need to install Ruby and the BioRuby package on your computer
15
+
16
+ You can check whether Ruby is installed on your computer and what version it has with the
17
+
18
+ ```sh
19
+ % ruby -v
20
+ ```
21
+
22
+ command. You should see something like:
23
+
24
+ ```
25
+ ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]
26
+ ```
27
+
28
+ If you see no such thing you'll have to install Ruby using your installation manager. For more information see the[Ruby](http://www.ruby-lang.org/en/) website.
29
+
30
+ With Ruby download and install Bioruby using the links on the[Bioruby](http://bioruby.org/) website. The recommended installation is via RubyGems:
31
+
32
+ ```
33
+ gem install bio
34
+ ```
35
+
36
+ See also the Bioruby [wiki](http://bioruby.open-bio.org/wiki/Installation).
37
+
38
+ A lot of BioRuby's documentation exists in the source code and unit tests. To really dive in you will need the latest source code tree. The embedded rdoc documentation can be viewed online at[bioruby's rdoc](http://bioruby.org/rdoc/). But first lets start!
39
+
40
+ ## Trying Bioruby
41
+
42
+ Bioruby comes with its own shell. After unpacking the sources run one of the following commands:
43
+
44
+ ```
45
+ bioruby
46
+ ```
47
+
48
+ or, from the source tree
49
+
50
+ ```
51
+ cd bioruby
52
+ ruby -I lib bin/bioruby
53
+ ```
54
+
55
+ and you should see a prompt
56
+
57
+ ```
58
+ bioruby>
59
+ ```
60
+
61
+ Now test the following:
62
+
63
+ ```ruby
64
+ bioruby> require 'bio'
65
+ bioruby> seq = Bio::Sequence::NA.new("atgcatgcaaaa")
66
+ ==> "atgcatgcaaaa"
67
+
68
+ bioruby> seq.complement
69
+ ==> "ttttgcatgcat"
70
+ ```
71
+
72
+ See the the Bioruby shell section below for more tweaking. If you have trouble running examples also check the section below on trouble shooting. You can also post a question to the mailing list. BioRuby developers usually try to help.
73
+
74
+ ## Working with nucleic / amino acid sequences (Bio::Sequence class)
75
+
76
+ The Bio::Sequence class allows the usual sequence transformations and translations. In the example below the DNA sequence "atgcatgcaaaa" is converted into the complemental strand and spliced into a subsequence; next, the nucleic acid composition is calculated and the sequence is translated into the amino acid sequence, the molecular weight calculated, and so on. When translating into amino acid sequences, the frame can be specified and optionally the codon table selected (as defined in codontable.rb).
77
+
78
+ ```ruby
79
+ bioruby> seq = Bio::Sequence::NA.new("atgcatgcaaaa")
80
+ ==> "atgcatgcaaaa"
81
+
82
+ # complemental sequence (Bio::Sequence::NA object)
83
+ bioruby> seq.complement
84
+ ==> "ttttgcatgcat"
85
+
86
+ bioruby> seq.subseq(3,8) # gets subsequence of positions 3 to 8 (starting from 1)
87
+ ==> "gcatgc"
88
+ bioruby> seq.gc_percent
89
+ ==> 33
90
+ bioruby> seq.composition
91
+ ==> {"a"=>6, "c"=>2, "g"=>2, "t"=>2}
92
+ bioruby> seq.translate
93
+ ==> "MHAK"
94
+ bioruby> seq.translate(2) # translate from frame 2
95
+ ==> "CMQ"
96
+ bioruby> seq.translate(1,11) # codon table 11
97
+ ==> "MHAK"
98
+ bioruby> seq.translate.codes
99
+ ==> ["Met", "His", "Ala", "Lys"]
100
+ bioruby> seq.translate.names
101
+ ==> ["methionine", "histidine", "alanine", "lysine"]
102
+ bioruby> seq.translate.composition
103
+ ==> {"K"=>1, "A"=>1, "M"=>1, "H"=>1}
104
+ bioruby> seq.translate.molecular_weight
105
+ ==> 485.605
106
+ bioruby> seq.complement.translate
107
+ ==> "FCMH"
108
+ ```
109
+
110
+ get a random sequence with the same NA count:
111
+
112
+ ```ruby
113
+ bioruby> counts = {'a'=>seq.count('a'),'c'=>seq.count('c'),'g'=>seq.count('g'),'t'=>seq.count('t')}
114
+ ==> {"a"=>6, "c"=>2, "g"=>2, "t"=>2}
115
+ bioruby!> randomseq = Bio::Sequence::NA.randomize(counts)
116
+ ==!> "aaacatgaagtc"
117
+
118
+ bioruby!> print counts
119
+ a6c2g2t2
120
+ bioruby!> p counts
121
+ {"a"=>6, "c"=>2, "g"=>2, "t"=>2}
122
+ ```
123
+
124
+ The p, print and puts methods are standard Ruby ways of outputting to the screen. If you want to know more about standard Ruby commands you can use the 'ri' command on the command line (or the help command in Windows). For example
125
+
126
+ ```sh
127
+ % ri puts
128
+ % ri p
129
+ % ri File.open
130
+ ```
131
+
132
+ Nucleic acid sequence are members of the Bio::Sequence::NA class, and amino acid sequence are members of the Bio::Sequence::AA class. Shared methods are in the parent Bio::Sequence class.
133
+
134
+ As Bio::Sequence inherits Ruby's String class, you can use String class methods. For example, to get a subsequence, you can not only use subseq(from, to) but also String#[].
135
+
136
+ Please take note that the Ruby's string's are base 0 - i.e. the first letter has index 0, for example:
137
+
138
+ ```ruby
139
+ bioruby> s = 'abc'
140
+ ==> "abc"
141
+ bioruby> s[0].chr
142
+ ==> "a"
143
+ bioruby> s[0..1]
144
+ ==> "ab"
145
+ ```
146
+
147
+ So when using String methods, you should subtract 1 from positions conventionally used in biology. (subseq method will throw an exception if you specify positions smaller than or equal to 0 for either one of the "from" or "to".)
148
+
149
+ The window_search(window_size, step_size) method shows a typical Ruby way of writing concise and clear code using 'closures'. Each sliding window creates a subsequence which is supplied to the enclosed block through a variable named +s+.
150
+
151
+ * Show average percentage of GC content for 20 bases (stepping the default one base at a time):
152
+
153
+ ```ruby
154
+ bioruby> seq = Bio::Sequence::NA.new("atgcatgcaattaagctaatcccaattagatcatcccgatcatcaaaaaaaaaa")
155
+ ==> "atgcatgcaattaagctaatcccaattagatcatcccgatcatcaaaaaaaaaa"
156
+
157
+ bioruby> a=[]; seq.window_search(20) { |s| a.push s.gc_percent }
158
+ bioruby> a
159
+ ==> [30, 35, 40, 40, 35, 35, 35, 30, 25, 30, 30, 30, 35, 35, 35, 35, 35, 40, 45, 45, 45, 45, 40, 35, 40, 40, 40, 40, 40, 35, 35, 35, 30, 30, 30]
160
+ ```
161
+
162
+ Since the class of each subsequence is the same as original sequence (Bio::Sequence::NA or Bio::Sequence::AA or Bio::Sequence), you can use all methods on the subsequence. For example,
163
+
164
+ * Shows translation results for 15 bases shifting a codon at a time
165
+
166
+ ```ruby
167
+ bioruby> a = []
168
+ bioruby> seq.window_search(15, 3) { | s | a.push s.translate }
169
+ bioruby> a
170
+ ==> ["MHAIK", "HAIKL", "AIKLI", "IKLIP", "KLIPI", "LIPIR", "IPIRS", "PIRSS", "IRSSR", "RSSRS", "SSRSS", "SRSSK", "RSSKK", "SSKKK"]
171
+ ```
172
+
173
+ Finally, the window_search method returns the last leftover subsequence. This allows for example
174
+
175
+ * Divide a genome sequence into sections of 10000bp and output FASTA formatted sequences (line width 60 chars). The 1000bp at the start and end of each subsequence overlapped. At the 3' end of the sequence the leftover is also added:
176
+
177
+ ```ruby
178
+ i = 1
179
+ textwidth=60
180
+ remainder = seq.window_search(10000, 9000) do |s|
181
+ puts s.to_fasta("segment #{i}", textwidth)
182
+ i += 1
183
+ end
184
+ if remainder
185
+ puts remainder.to_fasta("segment #{i}", textwidth)
186
+ end
187
+ ```
188
+
189
+ If you don't want the overlapping window, set window size and stepping size to equal values.
190
+
191
+ Other examples
192
+
193
+ * Count the codon usage
194
+
195
+ ```ruby
196
+ bioruby> codon_usage = Hash.new(0)
197
+ bioruby> seq.window_search(3, 3) { |s| codon_usage[s] += 1 }
198
+ bioruby> codon_usage
199
+ ==> {"cat"=>1, "aaa"=>3, "cca"=>1, "att"=>2, "aga"=>1, "atc"=>1, "cta"=>1, "gca"=>1, "cga"=>1, "tca"=>3, "aag"=>1, "tcc"=>1, "atg"=>1}
200
+ ```
201
+
202
+ * Calculate molecular weight for each 10-aa peptide (or 10-nt nucleic acid)
203
+
204
+ ```ruby
205
+ bioruby> a = []
206
+ bioruby> seq.window_search(10, 10) { |s| a.push s.molecular_weight }
207
+ bioruby> a
208
+ ==> [3096.2062, 3086.1962, 3056.1762, 3023.1262, 3073.2262]
209
+ ```
210
+
211
+ In most cases, sequences are read from files or retrieved from databases. For example:
212
+
213
+ ```ruby
214
+ require 'bio'
215
+
216
+ input_seq = ARGF.read # reads all files in arguments
217
+
218
+ my_naseq = Bio::Sequence::NA.new(input_seq)
219
+ my_aaseq = my_naseq.translate
220
+
221
+ puts my_aaseq
222
+ ```
223
+
224
+ Save the program above as na2aa.rb. Prepare a nucleic acid sequence described below and save it as my_naseq.txt:
225
+
226
+ ```
227
+ gtggcgatctttccgaaagcgatgactggagcgaagaaccaaagcagtgacatttgtctg
228
+ atgccgcacgtaggcctgataagacgcggacagcgtcgcatcaggcatcttgtgcaaatg
229
+ tcggatgcggcgtga
230
+ ```
231
+
232
+ na2aa.rb translates a nucleic acid sequence to a protein sequence. For example, translates my_naseq.txt:
233
+
234
+ ```sh
235
+ % ruby na2aa.rb my_naseq.txt
236
+ ```
237
+
238
+ or use a pipe!
239
+
240
+ ```sh
241
+ % cat my_naseq.txt|ruby na2aa.rb
242
+ ```
243
+
244
+ Outputs
245
+
246
+ ```
247
+ VAIFPKAMTGAKNQSSDICLMPHVGLIRRGQRRIRHLVQMSDAA*
248
+ ```
249
+
250
+ You can also write this, a bit fancifully, as a one-liner script.
251
+
252
+ ```sh
253
+ % ruby -r bio -e 'p Bio::Sequence::NA.new($<.read).translate' my_naseq.txt
254
+ ```
255
+
256
+ In the next section we will retrieve data from databases instead of using raw sequence files. One generic example of the above can be found in ./sample/na2aa.rb.
257
+
258
+ ## Parsing GenBank data (Bio::GenBank class)
259
+
260
+ We assume that you already have some GenBank data files. (If you don't, download some .seq files from ftp://ftp.ncbi.nih.gov/genbank/)
261
+
262
+ As an example we will fetch the ID, definition and sequence of each entry from the GenBank format and convert it to FASTA. This is also an example script in the BioRuby distribution.
263
+
264
+ A first attempt could be to use the Bio::GenBank class for reading in the data:
265
+
266
+ ```ruby
267
+ #!/usr/bin/env ruby
268
+
269
+ require 'bio'
270
+
271
+ # Read all lines from STDIN split by the GenBank delimiter
272
+ while entry = gets(Bio::GenBank::DELIMITER)
273
+ gb = Bio::GenBank.new(entry) # creates GenBank object
274
+
275
+ print ">#{gb.accession} " # Accession
276
+ puts gb.definition # Definition
277
+ puts gb.naseq # Nucleic acid sequence
278
+ # (Bio::Sequence::NA object)
279
+ end
280
+ ```
281
+
282
+ But that has the disadvantage the code is tied to GenBank input. A more generic method is to use Bio::FlatFile which allows you to use different input formats:
283
+
284
+ ```ruby
285
+ #!/usr/bin/env ruby
286
+
287
+ require 'bio'
288
+
289
+ ff = Bio::FlatFile.new(Bio::GenBank, ARGF)
290
+ ff.each_entry do |gb|
291
+ definition = "#{gb.accession} #{gb.definition}"
292
+ puts gb.naseq.to_fasta(definition, 60)
293
+ end
294
+ ```
295
+
296
+ For example, in turn, reading FASTA format files:
297
+
298
+ ```ruby
299
+ #!/usr/bin/env ruby
300
+
301
+ require 'bio'
302
+
303
+ ff = Bio::FlatFile.new(Bio::FastaFormat, ARGF)
304
+ ff.each_entry do |f|
305
+ puts "definition : " + f.definition
306
+ puts "nalen : " + f.nalen.to_s
307
+ puts "naseq : " + f.naseq
308
+ end
309
+ ```
310
+
311
+ In the above two scripts, the first arguments of Bio::FlatFile.new are database classes of BioRuby. This is expanded on in a later section.
312
+
313
+ Again another option is to use the Bio::DB.open class:
314
+
315
+ ```ruby
316
+ #!/usr/bin/env ruby
317
+
318
+ require 'bio'
319
+
320
+ ff = Bio::GenBank.open("gbvrl1.seq")
321
+ ff.each_entry do |gb|
322
+ definition = "#{gb.accession} #{gb.definition}"
323
+ puts gb.naseq.to_fasta(definition, 60)
324
+ end
325
+ ```
326
+
327
+ Next, we are going to parse the GenBank 'features', which is normally very complicated:
328
+
329
+ ```ruby
330
+ #!/usr/bin/env ruby
331
+
332
+ require 'bio'
333
+
334
+ ff = Bio::FlatFile.new(Bio::GenBank, ARGF)
335
+
336
+ # iterates over each GenBank entry
337
+ ff.each_entry do |gb|
338
+
339
+ # shows accession and organism
340
+ puts "# #{gb.accession} - #{gb.organism}"
341
+
342
+ # iterates over each element in 'features'
343
+ gb.features.each do |feature|
344
+ position = feature.position
345
+ hash = feature.assoc # put into Hash
346
+
347
+ # skips the entry if "/translation=" is not found
348
+ next unless hash['translation']
349
+
350
+ # collects gene name and so on and joins it into a string
351
+ gene_info = [
352
+ hash['gene'], hash['product'], hash['note'], hash['function']
353
+ ].compact.join(', ')
354
+
355
+ # shows nucleic acid sequence
356
+ puts ">NA splicing('#{position}') : #{gene_info}"
357
+ puts gb.naseq.splicing(position)
358
+
359
+ # shows amino acid sequence translated from nucleic acid sequence
360
+ puts ">AA translated by splicing('#{position}').translate"
361
+ puts gb.naseq.splicing(position).translate
362
+
363
+ # shows amino acid sequence in the database entry (/translation=)
364
+ puts ">AA original translation"
365
+ puts hash['translation']
366
+ end
367
+ end
368
+ ```
369
+
370
+ * Note: In this example Feature#assoc method makes a Hash from a feature object. It is useful because you can get data from the hash by using qualifiers as keys. But there is a risk some information is lost when two or more qualifiers are the same. Therefore an Array is returned by Feature#feature.
371
+
372
+ Bio::Sequence#splicing splices subsequences from nucleic acid sequences according to location information used in GenBank, EMBL and DDBJ.
373
+
374
+ When the specified translation table is different from the default (universal), or when the first codon is not "atg" or the protein contains selenocysteine, the two amino acid sequences will differ.
375
+
376
+ The Bio::Sequence#splicing method takes not only DDBJ/EMBL/GenBank feature style location text but also Bio::Locations object. For more information about location format and Bio::Locations class, see bio/location.rb.
377
+
378
+ * Splice according to location string used in a GenBank entry
379
+
380
+ ```ruby
381
+ naseq.splicing('join(2035..2050,complement(1775..1818),13..345')
382
+ ```
383
+
384
+ * Generate Bio::Locations object and pass the splicing method
385
+
386
+ ```ruby
387
+ locs = Bio::Locations.new('join((8298.8300)..10206,1..855)')
388
+ naseq.splicing(locs)
389
+ ```
390
+
391
+ You can also use this splicing method for amino acid sequences (Bio::Sequence::AA objects).
392
+
393
+ * Splicing peptide from a protein (e.g. signal peptide)
394
+
395
+ ```ruby
396
+ aaseq.splicing('21..119')
397
+ ```
398
+
399
+ ### More databases
400
+
401
+ Databases in BioRuby are essentially accessed like that of GenBank with classes like Bio::GenBank, Bio::KEGG::GENES. A full list can be found in the ./lib/bio/db directory of the BioRuby source tree.
402
+
403
+ In many cases the Bio::DatabaseClass acts as a factory pattern and recognises the database type automatically - returning a parsed object. For example using Bio::FlatFile class as described above. The first argument of the Bio::FlatFile.new is database class name in BioRuby (such as Bio::GenBank, Bio::KEGG::GENES and so on).
404
+
405
+ ```ruby
406
+ ff = Bio::FlatFile.new(Bio::DatabaseClass, ARGF)
407
+ ```
408
+
409
+ Isn't it wonderful that Bio::FlatFile automagically recognizes each database class?
410
+
411
+ ```ruby
412
+ #!/usr/bin/env ruby
413
+
414
+ require 'bio'
415
+
416
+ ff = Bio::FlatFile.auto(ARGF)
417
+ ff.each_entry do |entry|
418
+ p entry.entry_id # identifier of the entry
419
+ p entry.definition # definition of the entry
420
+ p entry.seq # sequence data of the entry
421
+ end
422
+ ```
423
+
424
+ An example that can take any input, filter using a regular expression and output to a FASTA file can be found in sample/any2fasta.rb. With this technique it is possible to write a Unix type grep/sort pipe for sequence information. One example using scripts in the BIORUBY sample folder:
425
+
426
+ ```
427
+ fastagrep.rb '/At|Dm/' database.seq | fastasort.rb
428
+ ```
429
+
430
+ greps the database for Arabidopsis and Drosophila entries and sorts the output to FASTA.
431
+
432
+ Other methods to extract specific data from database objects can be different between databases, though some methods are common (see the guidelines for common methods in bio/db.rb).
433
+
434
+ * entry_id --&gt; gets ID of the entry
435
+ * definition --&gt; gets definition of the entry
436
+ * reference --&gt; gets references as Bio::Reference object
437
+ * organism --&gt; gets species
438
+ * seq, naseq, aaseq --&gt; returns sequence as corresponding sequence object
439
+
440
+ Refer to the documents of each database to find the exact naming of the included methods.
441
+
442
+ In general, BioRuby uses the following conventions: when a method name is plural, the method returns some object as an Array. For example, some classes have a "references" method which returns multiple Bio::Reference objects as an Array. And some classes have a "reference" method which returns a single Bio::Reference object.
443
+
444
+ ### Alignments (Bio::Alignment)
445
+
446
+ The Bio::Alignment class in bio/alignment.rb is a container class like Ruby's Hash and Array classes and BioPerl's Bio::SimpleAlign. A very simple example is:
447
+
448
+ ```ruby
449
+ bioruby> seqs = [ 'atgca', 'aagca', 'acgca', 'acgcg' ]
450
+ bioruby> seqs = seqs.collect{ |x| Bio::Sequence::NA.new(x) }
451
+ # creates alignment object
452
+ bioruby> a = Bio::Alignment.new(seqs)
453
+ bioruby> a.consensus
454
+ ==> "a?gc?"
455
+ # shows IUPAC consensus
456
+ p a.consensus_iupac # ==> "ahgcr"
457
+
458
+ # iterates over each seq
459
+ a.each { |x| p x }
460
+ # ==>
461
+ # "atgca"
462
+ # "aagca"
463
+ # "acgca"
464
+ # "acgcg"
465
+ # iterates over each site
466
+ a.each_site { |x| p x }
467
+ # ==>
468
+ # ["a", "a", "a", "a"]
469
+ # ["t", "a", "c", "c"]
470
+ # ["g", "g", "g", "g"]
471
+ # ["c", "c", "c", "c"]
472
+ # ["a", "a", "a", "g"]
473
+
474
+ # doing alignment by using CLUSTAL W.
475
+ # clustalw command must be installed.
476
+ factory = Bio::ClustalW.new
477
+ a2 = a.do_align(factory)
478
+ ```
479
+
480
+ Read a ClustalW or Muscle 'ALN' alignment file:
481
+
482
+ ```ruby
483
+ bioruby> aln = Bio::ClustalW::Report.new(File.read('../test/data/clustalw/example1.aln'))
484
+ bioruby> aln.header
485
+ ==> "CLUSTAL 2.0.9 multiple sequence alignment"
486
+ ```
487
+
488
+ Fetch a sequence:
489
+
490
+ ```ruby
491
+ bioruby> seq = aln.get_sequence(1)
492
+ bioruby> seq.definition
493
+ ==> "gi|115023|sp|P10425|"
494
+ ```
495
+
496
+ Get a partial sequence:
497
+
498
+ ```
499
+ bioruby> seq.to_s[60..120]
500
+ ==> "LGYFNG-EAVPSNGLVLNTSKGLVLVDSSWDNKLTKELIEMVEKKFQKRVTDVIITHAHAD"
501
+ ```
502
+
503
+ Show the full alignment residue match information for the sequences in the set:
504
+
505
+ ```
506
+ bioruby> aln.match_line[60..120]
507
+ ==> " . **. . .. ::*: . * : : . .: .* * *"
508
+ ```
509
+
510
+ Return a Bio::Alignment object:
511
+
512
+ ```ruby
513
+ bioruby> aln.alignment.consensus[60..120]
514
+ ==> "???????????SN?????????????D??????????L??????????????????H?H?D"
515
+ ```
516
+
517
+ ## Restriction Enzymes (Bio::RE)
518
+
519
+ BioRuby has extensive support for restriction enzymes (REs). It contains a full library of commonly used REs (from REBASE) which can be used to cut single stranded RNA or double stranded DNA into fragments. To list all enzymes:
520
+
521
+ ```ruby
522
+ rebase = Bio::RestrictionEnzyme.rebase
523
+ rebase.each do |enzyme_name, info|
524
+ p enzyme_name
525
+ end
526
+ ```
527
+
528
+ and to cut a sequence with an enzyme follow up with:
529
+
530
+ ```ruby
531
+ res = seq.cut_with_enzyme('EcoRII', {:max_permutations => 0},
532
+ {:view_ranges => true})
533
+ if res.kind_of? Symbol #error
534
+ err = Err.find_by_code(res.to_s)
535
+ unless err
536
+ err = Err.new(:code => res.to_s)
537
+ end
538
+ end
539
+ res.each do |frag|
540
+ em = EnzymeMatch.new
541
+
542
+ em.p_left = frag.p_left
543
+ em.p_right = frag.p_right
544
+ em.c_left = frag.c_left
545
+ em.c_right = frag.c_right
546
+
547
+ em.err = nil
548
+ em.enzyme = ar_enz
549
+ em.sequence = ar_seq
550
+ p em
551
+ end
552
+ ```
553
+
554
+ ## Sequence homology search by using the FASTA program (Bio::Fasta)
555
+
556
+ Let's start with a query.pep file which contains a sequence in FASTA format. In this example we are going to execute a homology search from a remote internet site or on your local machine. Note that you can use the ssearch program instead of fasta when you use it in your local machine.
557
+
558
+ ### using FASTA in local machine
559
+
560
+ Install the fasta program on your machine (the command name looks like fasta34. FASTA can be downloaded from ftp://ftp.virginia.edu/pub/fasta/).
561
+
562
+ First, you must prepare your FASTA-formatted database sequence file target.pep and FASTA-formatted query.pep.
563
+
564
+ ```ruby
565
+ #!/usr/bin/env ruby
566
+
567
+ require 'bio'
568
+
569
+ # Creates FASTA factory object ("ssearch" instead of
570
+ # "fasta34" can also work)
571
+ factory = Bio::Fasta.local('fasta34', ARGV.pop)
572
+ (EDITOR's NOTE: not consistent pop command)
573
+
574
+ ff = Bio::FlatFile.new(Bio::FastaFormat, ARGF)
575
+
576
+ # Iterates over each entry. the variable "entry" is a
577
+ # Bio::FastaFormat object:
578
+ ff.each do |entry|
579
+ # shows definition line (begins with '>') to the standard error output
580
+ $stderr.puts "Searching ... " + entry.definition
581
+
582
+ # executes homology search. Returns Bio::Fasta::Report object.
583
+ report = factory.query(entry)
584
+
585
+ # Iterates over each hit
586
+ report.each do |hit|
587
+ # If E-value is smaller than 0.0001
588
+ if hit.evalue < 0.0001
589
+ # shows identifier of query and hit, E-value, start and
590
+ # end positions of homologous region
591
+ print "#{hit.query_id} : evalue #{hit.evalue}\t#{hit.target_id} at "
592
+ p hit.lap_at
593
+ end
594
+ end
595
+ end
596
+ ```
597
+
598
+ We named above script f_search.rb. You can execute it as follows:
599
+
600
+ ```sh
601
+ % ./f_search.rb query.pep target.pep > f_search.out
602
+ ```
603
+
604
+ In above script, the variable "factory" is a factory object for executing FASTA many times easily. Instead of using Fasta#query method, Bio::Sequence#fasta method can be used.
605
+
606
+ ```ruby
607
+ seq = ">test seq\nYQVLEEIGRGSFGSVRKVIHIPTKKLLVRKDIKYGHMNSKE"
608
+ seq.fasta(factory)
609
+ ```
610
+
611
+ When you want to add options to FASTA commands, you can set the third argument of the Bio::Fasta.local method. For example, the following sets ktup to 1 and gets a list of the top 10 hits:
612
+
613
+ ```ruby
614
+ factory = Bio::Fasta.local('fasta34', 'target.pep', '-b 10')
615
+ factory.ktup = 1
616
+ ```
617
+
618
+ Bio::Fasta#query returns a Bio::Fasta::Report object. We can get almost all information described in FASTA report text with the Report object. For example, getting information for hits:
619
+
620
+ ```ruby
621
+ report.each do |hit|
622
+ puts hit.evalue # E-value
623
+ puts hit.sw # Smith-Waterman score (*)
624
+ puts hit.identity # % identity
625
+ puts hit.overlap # length of overlapping region
626
+ puts hit.query_id # identifier of query sequence
627
+ puts hit.query_def # definition(comment line) of query sequence
628
+ puts hit.query_len # length of query sequence
629
+ puts hit.query_seq # sequence of homologous region
630
+ puts hit.target_id # identifier of hit sequence
631
+ puts hit.target_def # definition(comment line) of hit sequence
632
+ puts hit.target_len # length of hit sequence
633
+ puts hit.target_seq # hit of homologous region of hit sequence
634
+ puts hit.query_start # start position of homologous
635
+ # region in query sequence
636
+ puts hit.query_end # end position of homologous region
637
+ # in query sequence
638
+ puts hit.target_start # start posiotion of homologous region
639
+ # in hit(target) sequence
640
+ puts hit.target_end # end position of homologous region
641
+ # in hit(target) sequence
642
+ puts hit.lap_at # array of above four numbers
643
+ end
644
+ ```
645
+
646
+ Most of above methods are common to the Bio::Blast::Report described below. Please refer to the documentation of the Bio::Fasta::Report class for FASTA-specific details.
647
+
648
+ If you need the original output text of FASTA program you can use the "output" method of the factory object after the "query" method.
649
+
650
+ ```ruby
651
+ report = factory.query(entry)
652
+ puts factory.output
653
+ ```
654
+
655
+ ### using FASTA from a remote internet site
656
+
657
+ * Note: Currently, only GenomeNet (fasta.genome.jp) is supported. check the class documentation for updates.
658
+
659
+ For accessing a remote site the Bio::Fasta.remote method is used instead of Bio::Fasta.local. When using a remote method, the databases available may be limited, but, otherwise, you can do the same things as with a local method.
660
+
661
+ Available databases in GenomeNet:
662
+
663
+ * Protein database
664
+ * nr-aa, genes, vgenes.pep, swissprot, swissprot-upd, pir, prf, pdbstr
665
+ * Nucleic acid database
666
+ * nr-nt, genbank-nonst, gbnonst-upd, dbest, dbgss, htgs, dbsts, embl-nonst, embnonst-upd, genes-nt, genome, vgenes.nuc
667
+
668
+ Select the databases you require. Next, give the search program from the type of query sequence and database.
669
+
670
+ * When query is an amino acid sequence
671
+ * When protein database, program is "fasta".
672
+ * When nucleic database, program is "tfasta".
673
+ * When query is a nucleic acid sequence
674
+ * When nucleic database, program is "fasta".
675
+ * (When protein database, the search would fail.)
676
+
677
+ For example, run:
678
+
679
+ ```ruby
680
+ program = 'fasta'
681
+ database = 'genes'
682
+
683
+ factory = Bio::Fasta.remote(program, database)
684
+ ```
685
+
686
+ and try out the same commands as with the local search shown earlier.
687
+
688
+ ## Homology search by using BLAST (Bio::Blast class)
689
+
690
+ The BLAST interface is very similar to that of FASTA and both local and remote execution are supported. Basically replace above examples Bio::Fasta with Bio::Blast!
691
+
692
+ For example the BLAST version of f_search.rb is:
693
+
694
+ ```ruby
695
+ # create BLAST factory object
696
+ factory = Bio::Blast.local('blastp', ARGV.pop)
697
+ ```
698
+
699
+ For remote execution of BLAST in GenomeNet, Bio::Blast.remote is used. The parameter "program" is different from FASTA - as you can expect:
700
+
701
+ * When query is a amino acid sequence
702
+ * When protein database, program is "blastp".
703
+ * When nucleic database, program is "tblastn".
704
+ * When query is a nucleic acid sequence
705
+ * When protein database, program is "blastx"
706
+ * When nucleic database, program is "blastn".
707
+ * ("tblastx" for six-frame search.)
708
+
709
+ Bio::BLAST uses "-m 7" XML output of BLAST by default when either XMLParser or REXML (both of them are XML parser libraries for Ruby - of the two XMLParser is the fastest) is installed on your computer. In Ruby version 1.8.0 or later, REXML is bundled with Ruby's distribution.
710
+
711
+ When no XML parser library is present, Bio::BLAST uses "-m 8" tabular deliminated format. Available information is limited with the "-m 8" format so installing an XML parser is recommended.
712
+
713
+ Again, the methods in Bio::Fasta::Report and Bio::Blast::Report (and Bio::Fasta::Report::Hit and Bio::Blast::Report::Hit) are similar. There are some additional BLAST methods, for example, bit_score and midline.
714
+
715
+ ```ruby
716
+ report.each do |hit|
717
+ puts hit.bit_score
718
+ puts hit.query_seq
719
+ puts hit.midline
720
+ puts hit.target_seq
721
+
722
+ puts hit.evalue
723
+ puts hit.identity
724
+ puts hit.overlap
725
+ puts hit.query_id
726
+ puts hit.query_def
727
+ puts hit.query_len
728
+ puts hit.target_id
729
+ puts hit.target_def
730
+ puts hit.target_len
731
+ puts hit.query_start
732
+ puts hit.query_end
733
+ puts hit.target_start
734
+ puts hit.target_end
735
+ puts hit.lap_at
736
+ end
737
+ ```
738
+
739
+ For simplicity and API compatibility, some information such as score is extracted from the first Hsp (High-scoring Segment Pair).
740
+
741
+ Check the documentation for Bio::Blast::Report to see what can be retrieved. For now suffice to say that Bio::Blast::Report has a hierarchical structure mirroring the general BLAST output stream:
742
+
743
+ * In a Bio::Blast::Report object, @iterations is an array of Bio::Blast::Report::Iteration objects.
744
+ * In a Bio::Blast::Report::Iteration object, @hits is an array of Bio::Blast::Report::Hits objects.
745
+ * In a Bio::Blast::Report::Hits object, @hsps is an array of Bio::Blast::Report::Hsp objects.
746
+
747
+ See bio/appl/blast.rb and bio/appl/blast/*.rb for more information.
748
+
749
+ ### Parsing existing BLAST output files
750
+
751
+ When you already have BLAST output files and you want to parse them, you can directly create Bio::Blast::Report objects without the Bio::Blast factory object. For this purpose use Bio::Blast.reports, which supports the "-m 0" default and "-m 7" XML type output format.
752
+
753
+ * For example:
754
+
755
+ ```ruby
756
+ blast_version = nil; result = []
757
+ Bio::Blast.reports(File.new("../test/data/blast/blastp-multi.m7")) do |report|
758
+ blast_version = report.version
759
+ report.iterations.each do |itr|
760
+ itr.hits.each do |hit|
761
+ result.push hit.target_id
762
+ end
763
+ end
764
+ end
765
+ blast_version
766
+ # ==> "blastp 2.2.18 [Mar-02-2008]"
767
+ result
768
+ # ==> ["BAB38768", "BAB38768", "BAB38769", "BAB37741"]
769
+ ```
770
+
771
+ * another example:
772
+
773
+ ```ruby
774
+ require 'bio'
775
+ Bio::Blast.reports(ARGF) do |report|
776
+ puts "Hits for " + report.query_def + " against " + report.db
777
+ report.each do |hit|
778
+ print hit.target_id, "\t", hit.evalue, "\n" if hit.evalue < 0.001
779
+ end
780
+ end
781
+ ```
782
+
783
+ Save the script as hits_under_0.001.rb and to process BLAST output files *.xml, you can run it with:
784
+
785
+ ```sh
786
+ % ruby hits_under_0.001.rb *.xml
787
+ ```
788
+
789
+ Sometimes BLAST XML output may be wrong and can not be parsed. Check whether blast is version 2.2.5 or later. See also blast --help.
790
+
791
+ Bio::Blast loads the full XML file into memory. If this causes a problem you can split the BLAST XML file into smaller chunks using XML-Twig. An example can be found in [Biotools](http://github.com/pjotrp/biotools/).
792
+
793
+ ### Add remote BLAST search sites
794
+
795
+ ```
796
+ Note: this section is an advanced topic
797
+ ```
798
+
799
+ Here a more advanced application for using BLAST sequence homology search services. BioRuby currently only supports GenomeNet. If you want to add other sites, you must write the following:
800
+
801
+ * the calling CGI (command-line options must be processed for the site).
802
+ * make sure you get BLAST output text as supported format by BioRuby (e.g. "-m 8", "-m 7" or default("-m 0")).
803
+
804
+ In addition, you must write a private class method in Bio::Blast named "exec_MYSITE" to get query sequence and to pass the result to Bio::Blast::Report.new(or Bio::Blast::Default::Report.new):
805
+
806
+ ```ruby
807
+ factory = Bio::Blast.remote(program, db, option, 'MYSITE')
808
+ ```
809
+
810
+ When you write above routines, please send them to the BioRuby project, and they may be included in future releases.
811
+
812
+ ## Generate a reference list using PubMed (Bio::PubMed)
813
+
814
+ Nowadays using NCBI E-Utils is recommended. Use Bio::PubMed.esearch and Bio::PubMed.efetch.
815
+
816
+ ```ruby
817
+ #!/usr/bin/env ruby
818
+
819
+ require 'bio'
820
+
821
+ # NCBI announces that queries without email address will return error
822
+ # after June 2010. When you modify the script, please enter your email
823
+ # address instead of the staff's.
824
+ Bio::NCBI.default_email = 'staff@bioruby.org'
825
+
826
+ keywords = ARGV.join(' ')
827
+
828
+ options = {
829
+ 'maxdate' => '2003/05/31',
830
+ 'retmax' => 1000,
831
+ }
832
+
833
+ entries = Bio::PubMed.esearch(keywords, options)
834
+
835
+ Bio::PubMed.efetch(entries).each do |entry|
836
+ medline = Bio::MEDLINE.new(entry)
837
+ reference = medline.reference
838
+ puts reference.bibtex
839
+ end
840
+ ```
841
+
842
+ The script works same as pmsearch.rb. But, by using NCBI E-Utils, more options are available. For example published dates to search and maximum number of hits to show results can be specified.
843
+
844
+ See the [help page of E-Utils](http://eutils.ncbi.nlm.nih.gov/entrez/query/static/eutils_help.html)for more details.
845
+
846
+ ### More about BibTeX
847
+
848
+ In this section, we explain the simple usage of TeX for the BibTeX format bibliography list collected by above scripts. For example, to save BibTeX format bibliography data to a file named genoinfo.bib.
849
+
850
+ ```sh
851
+ % ./pmfetch.rb 10592173 >> genoinfo.bib
852
+ % ./pmsearch.rb genome bioinformatics >> genoinfo.bib
853
+ ```
854
+
855
+ The BibTeX can be used with Tex or LaTeX to form bibliography information with your journal article. For more information on using BibTex see [BibTex HowTo site](http://www.bibtex.org/Using/). A quick example:
856
+
857
+ Save this to hoge.tex:
858
+
859
+ ```latex
860
+ \documentclass{jarticle}
861
+ \begin{document}
862
+ \bibliographystyle{plain}
863
+ foo bar KEGG database~\cite{PMID:10592173} baz hoge fuga.
864
+ \bibliography{genoinfo}
865
+ \end{document}
866
+ ```
867
+
868
+ Then,
869
+
870
+ ```sh
871
+ % latex hoge
872
+ % bibtex hoge # processes genoinfo.bib
873
+ % latex hoge # creates bibliography list
874
+ % latex hoge # inserts correct bibliography reference
875
+ ```
876
+
877
+ Now, you get hoge.dvi and hoge.ps - the latter of which can be viewed with any Postscript viewer.
878
+
879
+ ### Bio::Reference#bibitem
880
+
881
+ When you don't want to create a bib file, you can use Bio::Reference#bibitem method instead of Bio::Reference#bibtex. In the above pmfetch.rb and pmsearch.rb scripts, change
882
+
883
+ ```
884
+ puts reference.bibtex
885
+ ```
886
+
887
+ to
888
+
889
+ ```
890
+ puts reference.bibitem
891
+ ```
892
+
893
+ Output documents should be bundled in \begin{thebibliography} and \end{thebibliography}. Save the following to hoge.tex
894
+
895
+ ```latex
896
+ \documentclass{jarticle}
897
+ \begin{document}
898
+ foo bar KEGG database~\cite{PMID:10592173} baz hoge fuga.
899
+
900
+ \begin{thebibliography}{00}
901
+
902
+ \bibitem{PMID:10592173}
903
+ Kanehisa, M., Goto, S.
904
+ KEGG: kyoto encyclopedia of genes and genomes.,
905
+ {\em Nucleic Acids Res}, 28(1):27--30, 2000.
906
+
907
+ \end{thebibliography}
908
+ \end{document}
909
+ ```
910
+
911
+ and run
912
+
913
+ ```sh
914
+ % latex hoge # creates bibliography list
915
+ % latex hoge # inserts corrent bibliography reference
916
+ ```
917
+
918
+ # OBDA
919
+
920
+ OBDA (Open Bio Database Access) is a standardized method of sequence database access developed by the Open Bioinformatics Foundation. It was created during the BioHackathon by BioPerl, BioJava, BioPython, BioRuby and other projects' members (2002).
921
+
922
+ * BioRegistry (Directory)
923
+ * Mechanism to specify how and where to retrieve sequence data for each database.
924
+ * BioFlat
925
+ * Flatfile indexing by using binary tree or BDB(Berkeley DB).
926
+ * BioFetch
927
+ * Server-client model for getting entry from database via http.
928
+ * BioSQL
929
+ * Schemas to store sequence data to relational databases such as MySQL and PostgreSQL, and methods to retrieve entries from the database.
930
+
931
+ This tutorial only gives a quick overview of OBDA. Check out[the OBDA site](http://obda.open-bio.org) for more extensive details.
932
+
933
+ ## BioRegistry
934
+
935
+ BioRegistry allows for locating retrieval methods and database locations through configuration files. The priorities are
936
+
937
+ * The file specified with method's parameter
938
+ * ~/.bioinformatics/seqdatabase.ini
939
+ * /etc/bioinformatics/seqdatabase.ini
940
+ * http://www.open-bio.org/registry/seqdatabase.ini
941
+
942
+ Note that the last locaation refers to www.open-bio.org and is only used when all local configulation files are not available.
943
+
944
+ In the current BioRuby implementation all local configulation files are read. For databases with the same name settings encountered first are used. This means that if you don't like some settings of a database in the system's global configuration file (/etc/bioinformatics/seqdatabase.ini), you can easily override them by writing settings to ~/.bioinformatics/seqdatabase.ini.
945
+
946
+ The syntax of the configuration file is called a stanza format. For example
947
+
948
+ ```
949
+ [DatabaseName]
950
+ protocol=ProtocolName
951
+ location=ServerName
952
+ ```
953
+
954
+ You can write a description like the above entry for every database.
955
+
956
+ The database name is a local label for yourself, so you can name it freely and it can differ from the name of the actual databases. In the actual specification of BioRegistry where there are two or more settings for a database of the same name, it is proposed that connection to the database is tried sequentially with the order written in configuration files. However, this has not (yet) been implemented in BioRuby.
957
+
958
+ In addition, for some protocols, you must set additional options other than locations (e.g. user name for MySQL). In the BioRegistory specification, current available protocols are:
959
+
960
+ * index-flat
961
+ * index-berkeleydb
962
+ * biofetch
963
+ * biosql
964
+ * bsane-corba
965
+ * xembl
966
+
967
+ In BioRuby, you can use index-flat, index-berkleydb, biofetch and biosql. Note that the BioRegistry specification sometimes gets updated and BioRuby does not always follow quickly.
968
+
969
+ Here is an example. It creates a Bio::Registry object and reads the configuration files:
970
+
971
+ ```ruby
972
+ reg = Bio::Registry.new
973
+
974
+ # connects to the database "genbank"
975
+ serv = reg.get_database('genbank')
976
+
977
+ # gets entry of the ID
978
+ entry = serv.get_by_id('AA2CG')
979
+ ```
980
+
981
+ The variable "serv" is a server object corresponding to the settings written in the configuration files. The class of the object is one of Bio::SQL, Bio::Fetch, and so on. Note that Bio::Registry#get_database("name") returns nil if no database is found.
982
+
983
+ After that, you can use the get_by_id method and some specific methods. Please refer to the sections below for more information.
984
+
985
+ ## BioFlat
986
+
987
+ BioFlat is a mechanism to create index files of flat files and to retrieve these entries fast. There are two index types. index-flat is a simple index performing binary search without using any external libraries of Ruby. index-berkeleydb uses Berkeley DB for indexing - but requires installing bdb on your computer, as well as the BDB Ruby package. To create the index itself, you can use br_bioflat.rb command bundled with BioRuby.
988
+
989
+ ```sh
990
+ % br_bioflat.rb --makeindex database_name [--format data_format] filename...
991
+ ```
992
+
993
+ The format can be omitted because BioRuby has autodetection. If that doesn't work, you can try specifying the data format as the name of a BioRuby database class.
994
+
995
+ Search and retrieve data from database:
996
+
997
+ ```sh
998
+ % br_bioflat.rb database_name identifier
999
+ ```
1000
+
1001
+ For example, to create an index of GenBank files gbbct*.seq and get the entry from the database:
1002
+
1003
+ ```sh
1004
+ % br_bioflat.rb --makeindex my_bctdb --format GenBank gbbct*.seq
1005
+ % br_bioflat.rb my_bctdb A16STM262
1006
+ ```
1007
+
1008
+ If you have Berkeley DB on your system and installed the bdb extension module of Ruby (see [the BDB project page](http://raa.ruby-lang.org/project/bdb/) ), you can create and search indexes with Berkeley DB - a very fast alternative that uses little computer memory. When creating the index, use the "--makeindex-bdb" option instead of "--makeindex".
1009
+
1010
+ ```sh
1011
+ % br_bioflat.rb --makeindex-bdb database_name [--format data_format] filename...
1012
+ ```
1013
+
1014
+ ## BioFetch
1015
+
1016
+ ```
1017
+ Note: this section is an advanced topic
1018
+ ```
1019
+
1020
+ BioFetch is a database retrieval mechanism via CGI. CGI Parameters, options and error codes are standardized. Client access via http is possible giving the database name, identifiers and format to retrieve entries.
1021
+
1022
+ The BioRuby project has a BioFetch server at bioruby.org. It uses GenomeNet's DBGET system as a backend. The source code of the server is in sample/ directory. Currently, there are only two BioFetch servers in the world: bioruby.org and EBI.
1023
+
1024
+ Here are some methods to retrieve entries from our BioFetch server.
1025
+
1026
+ 1. Using a web browser
1027
+
1028
+ ```
1029
+ http://bioruby.org/cgi-bin/biofetch.rb
1030
+ ```
1031
+
1032
+ 2. Using the br_biofetch.rb command
1033
+
1034
+ ```sh
1035
+ % br_biofetch.rb db_name entry_id
1036
+ ```
1037
+
1038
+ 3. Directly using Bio::Fetch in a script
1039
+
1040
+ ```ruby
1041
+ serv = Bio::Fetch.new(server_url)
1042
+ entry = serv.fetch(db_name, entry_id)
1043
+ ```
1044
+
1045
+ 4. Indirectly using Bio::Fetch via BioRegistry in script
1046
+
1047
+ ```ruby
1048
+ reg = Bio::Registry.new
1049
+ serv = reg.get_database('genbank')
1050
+ entry = serv.get_by_id('AA2CG')
1051
+ ```
1052
+
1053
+ If you want to use (4), you have to include some settings in seqdatabase.ini. For example:
1054
+
1055
+ ```
1056
+ [genbank]
1057
+ protocol=biofetch
1058
+ location=http://bioruby.org/cgi-bin/biofetch.rb
1059
+ biodbname=genbank
1060
+ ```
1061
+
1062
+ ### The combination of BioFetch, Bio::KEGG::GENES and Bio::AAindex1
1063
+
1064
+ Bioinformatics is often about gluing things together. Here is an example that gets the bacteriorhodopsin gene (VNG1467G) of the archaea Halobacterium from KEGG GENES database and gets alpha-helix index data (BURA740101) from the AAindex (Amino acid indices and similarity matrices) database, and shows the helix score for each 15-aa length overlapping window.
1065
+
1066
+ ```ruby
1067
+ #!/usr/bin/env ruby
1068
+
1069
+ require 'bio'
1070
+
1071
+ entry = Bio::Fetch.query('hal', 'VNG1467G')
1072
+ aaseq = Bio::KEGG::GENES.new(entry).aaseq
1073
+
1074
+ entry = Bio::Fetch.query('aax1', 'BURA740101')
1075
+ helix = Bio::AAindex1.new(entry).index
1076
+
1077
+ position = 1
1078
+ win_size = 15
1079
+
1080
+ aaseq.window_search(win_size) do |subseq|
1081
+ score = subseq.total(helix)
1082
+ puts [ position, score ].join("\t")
1083
+ position += 1
1084
+ end
1085
+ ```
1086
+
1087
+ The special method Bio::Fetch.query uses the preset BioFetch server at bioruby.org. (The server internally gets data from GenomeNet. Because the KEGG/GENES database and AAindex database are not available from other BioFetch servers, we used the bioruby.org server with Bio::Fetch.query method.)
1088
+
1089
+ ## BioSQL
1090
+
1091
+ BioSQL is a well known schema to store and retrive biological sequences using a RDBMS like PostgreSQL or MySQL: note that SQLite is not supported. First of all, you must install a database engine or have access to a remote one. Then create the schema and populate with the taxonomy. You can follow the [Official Guide](http://code.open-bio.org/svnweb/index.cgi/biosql/view/biosql-schema/trunk/INSTALL) to accomplish these steps. Next step is to install these gems:
1092
+
1093
+ * ActiveRecord
1094
+ * CompositePrimaryKeys (Rails doesn't handle by default composite primary keys)
1095
+ * The layer to comunicate with you preferred RDBMS (postgresql, mysql, jdbcmysql in case you are running JRuby )
1096
+
1097
+ You can find ActiveRecord's models in /bioruby/lib/bio/io/biosql
1098
+
1099
+ When you have your database up and running, you can connect to it like this:
1100
+
1101
+ ```ruby
1102
+ #!/usr/bin/env ruby
1103
+
1104
+ require 'bio'
1105
+
1106
+ connection = Bio::SQL.establish_connection({'development'=>{'hostname'=>"YourHostname",
1107
+ 'database'=>"CoolBioSeqDB",
1108
+ 'adapter'=>"jdbcmysql",
1109
+ 'username'=>"YourUser",
1110
+ 'password'=>"YouPassword"
1111
+ }
1112
+ },
1113
+ 'development')
1114
+
1115
+ #The first parameter is the hash contaning the description of the configuration; similar to database.yml in Rails applications, you can declare different environment.
1116
+ #The second parameter is the environment to use: 'development', 'test', or 'production'.
1117
+
1118
+ #To store a sequence into the database you simply need a biosequence object.
1119
+ biosql_database = Bio::SQL::Biodatabase.find(:first)
1120
+ ff = Bio::GenBank.open("gbvrl1.seq")
1121
+
1122
+ ff.each_entry do |gb|
1123
+ Bio::SQL::Sequence.new(:biosequence=>gb.to_biosequence, :biodatabase=>biosql_database
1124
+ end
1125
+
1126
+ #You can list all the entries into every database
1127
+ Bio::SQL.list_entries
1128
+
1129
+ #list databases:
1130
+ Bio::SQL.list_databases
1131
+
1132
+ #retriving a generic accession
1133
+ bioseq = Bio::SQL.fetch_accession("YouAccession")
1134
+
1135
+ #If you use biosequence objects, you will find all its method mapped to BioSQL sequences.
1136
+ #But you can also access to the models directly:
1137
+
1138
+ #get the raw sequence associated with your accession
1139
+ bioseq.entry.biosequence
1140
+
1141
+ #get the length of your sequence; this is the explicit form of bioseq.length
1142
+ bioseq.entry.biosequence.length
1143
+
1144
+ #convert the sequence into GenBank format
1145
+ bioseq.to_biosequence.output(:genbank)
1146
+ ```
1147
+
1148
+ BioSQL's [schema](http://www.biosql.org/wiki/Schema_Overview) is not very intuitive for beginners, so spend some time on understanding it. In the end if you know a little bit of Ruby on Rails, everything will go smoothly. You can find information on Annotation [here](http://www.biosql.org/wiki/Annotation_Mapping). ToDo: add exemaples from George. I remember he did some cool post on BioSQL and Rails.
1149
+
1150
+ # PhyloXML
1151
+
1152
+ PhyloXML is an XML language for saving, analyzing and exchanging data of annotated phylogenetic trees. PhyloXML's parser in BioRuby is implemented in Bio::PhyloXML::Parser, and its writer in Bio::PhyloXML::Writer. More information can be found at [www.phyloxml.org](http://www.phyloxml.org).
1153
+
1154
+ Bio::PhyloXML have been split out from BioRuby core and have been released as bio-phyloxml gem. To use Bio::PhyloXML, install the bio-phyloxml gem.
1155
+
1156
+ ```sh
1157
+ % gem install bio-phyloxml
1158
+ ```
1159
+
1160
+ The tutorial of Bio::PhyloXML is bundled in bio-phyloxml.https://github.com/bioruby/bioruby-phyloxml/blob/master/doc/Tutorial.rd
1161
+
1162
+ ## The BioRuby example programs
1163
+
1164
+ Some sample programs are stored in ./samples/ directory. For example, the n2aa.rb program (transforms a nucleic acid sequence into an amino acid sequence) can be run using:
1165
+
1166
+ ```
1167
+ ./sample/na2aa.rb test/data/fasta/example1.txt
1168
+ ```
1169
+
1170
+ ## Unit testing and doctests
1171
+
1172
+ BioRuby comes with an extensive testing framework with over 1300 tests and 2700 assertions. To run the unit tests:
1173
+
1174
+ ```
1175
+ cd test
1176
+ ruby runner.rb
1177
+ ```
1178
+
1179
+ We have also started with doctest for Ruby. We are porting the examples in this tutorial to doctest - more info upcoming.
1180
+
1181
+ ## Further reading
1182
+
1183
+ See the BioRuby in anger Wiki. A lot of BioRuby's documentation exists in the source code and unit tests. To really dive in you will need the latest source code tree. The embedded rdoc documentation for the BioRuby source code can be viewed online athttp://bioruby.org/rdoc/.
1184
+
1185
+ ## BioRuby Shell
1186
+
1187
+ The BioRuby shell implementation is located in ./lib/bio/shell. It is very interesting as it uses IRB (the Ruby intepreter) which is a powerful environment described in[Programming Ruby's IRB chapter](http://ruby-doc.org/docs/ProgrammingRuby/html/irb.html). IRB commands can be typed directly into the shell, e.g.
1188
+
1189
+ ```
1190
+ bioruby!> IRB.conf[:PROMPT_MODE]
1191
+ ==!> :PROMPT_C
1192
+ ```
1193
+
1194
+ Additionally, you also may want to install the optional Ruby readline support - with Debian libreadline-ruby. To edit a previous line you may have to press line down (down arrow) first.
1195
+
1196
+ # Helpful tools
1197
+
1198
+ Apart from rdoc you may also want to use rtags - which allows jumping around source code by clicking on class and method names.
1199
+
1200
+ ```
1201
+ cd bioruby/lib
1202
+ rtags -R --vi
1203
+ ```
1204
+
1205
+ For a tutorial see [here](http://rtags.rubyforge.org/)
1206
+
1207
+ # APPENDIX
1208
+
1209
+ ## Biogem: Additional BioRuby plugins
1210
+
1211
+ Biogem is one of the exciting developments for Ruby in bioinformatics! Biogems add new functionality next to the BioRuby core project (BioRuby is a biogem itself). A biogem is simply installed with
1212
+
1213
+ ```
1214
+ gem install bio # The core BioRuby gem
1215
+ gem install bio-core # BioRuby + stable pure Ruby biogems
1216
+ gem install bio-core-ext # bio-core + stable Ruby extensions
1217
+ ```
1218
+
1219
+ Information on these biogems, and the many others available, see [Biogems.info](http://biogems.info/) or [gems.bioruby.org](http://gems.bioruby.org/).
1220
+
1221
+ ## Ruby Ensembl API
1222
+
1223
+ The Ruby Ensembl API is a Ruby API to the Ensembl database. It is NOT currently included in the BioRuby archives. To install it, see[the Ruby-Ensembl Github](http://wiki.github.com/jandot/ruby-ensembl-api)for more information.
1224
+
1225
+ ### Gene Ontology (GO) through the Ruby Ensembl API
1226
+
1227
+ Gene Ontologies can be fetched through the Ruby Ensembl API package:
1228
+
1229
+ ```ruby
1230
+ require 'ensembl'
1231
+ Ensembl::Core::DBConnection.connect('drosophila_melanogaster')
1232
+ infile = IO.readlines(ARGV.shift) # reading your comma-separated accession mapping file (one line per mapping)
1233
+ infile.each do |line|
1234
+ accs = line.split(",") # Split the comma-sep.entries into an array
1235
+ drosphila_acc = accs.shift # the first entry is the Drosophila acc
1236
+ mosq_acc = accs.shift # the second entry is your Mosq. acc
1237
+ gene = Ensembl::Core::Gene.find_by_stable_id(drosophila_acc)
1238
+ print "#{mosq_acc}"
1239
+ gene.go_terms.each do |go|
1240
+ print ",#{go}"
1241
+ end
1242
+ end
1243
+ ```
1244
+
1245
+ Prints each mosq. accession/uniq identifier and the GO terms from the Drosphila homologues.
1246
+
1247
+ ## Using BioPerl or BioPython from Ruby
1248
+
1249
+ A possible route is to opt for JRuby and Jython on the JAVA virtual machine (JVM).
1250
+
1251
+ At the moment there is no easy way of accessing BioPerl or BioPython directly from Ruby. A possibility is to create a Perl or Python server that gets accessed through XML/RPC or SOAP.
1252
+
1253
+ ## Installing required external libraries
1254
+
1255
+ At this point for using BioRuby no additional libraries are needed.
1256
+
1257
+ This may change, so keep an eye on the Bioruby website. Also when a package is missing BioRuby should show an informative message.
1258
+
1259
+ At this point installing third party Ruby packages can be a bit painful, as the gem standard for packages evolved late and some still force you to copy things by hand. Therefore read the README's carefully that come with each package.
1260
+
1261
+ ## Trouble shooting
1262
+
1263
+ * Error: in &#96;require': no such file to load -- bio (LoadError)
1264
+
1265
+ Ruby is failing to find the BioRuby libraries - add it to the RUBYLIB path, or pass it to the interpeter. For example:
1266
+
1267
+ ```
1268
+ ruby -I$BIORUBYPATH/lib yourprogram.rb
1269
+ ```
1270
+
1271
+ ## Modifying this page
1272
+
1273
+ IMPORTANT NOTICE: This page is maintained in the BioRuby source code repository. Please edit the file there otherwise changes may get lost. See BioRuby Developer Information for repository and mailing list access.
1274
+