wwood-bioruby 1.2.11
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +205 -0
- data/README_DEV.rdoc +285 -0
- data/VERSION.yml +4 -0
- data/bin/bioruby +44 -0
- data/bin/br_biofetch.rb +47 -0
- data/bin/br_bioflat.rb +293 -0
- data/bin/br_biogetseq.rb +45 -0
- data/bin/br_pmfetch.rb +421 -0
- data/lib/bio.rb +306 -0
- data/lib/bio/alignment.rb +2518 -0
- data/lib/bio/appl/bl2seq/report.rb +334 -0
- data/lib/bio/appl/blast.rb +505 -0
- data/lib/bio/appl/blast/ddbj.rb +142 -0
- data/lib/bio/appl/blast/format0.rb +1438 -0
- data/lib/bio/appl/blast/format8.rb +83 -0
- data/lib/bio/appl/blast/genomenet.rb +263 -0
- data/lib/bio/appl/blast/ncbioptions.rb +220 -0
- data/lib/bio/appl/blast/remote.rb +105 -0
- data/lib/bio/appl/blast/report.rb +767 -0
- data/lib/bio/appl/blast/rexml.rb +144 -0
- data/lib/bio/appl/blast/rpsblast.rb +277 -0
- data/lib/bio/appl/blast/wublast.rb +635 -0
- data/lib/bio/appl/blast/xmlparser.rb +236 -0
- data/lib/bio/appl/blat/report.rb +530 -0
- data/lib/bio/appl/clustalw.rb +219 -0
- data/lib/bio/appl/clustalw/report.rb +152 -0
- data/lib/bio/appl/emboss.rb +203 -0
- data/lib/bio/appl/fasta.rb +235 -0
- data/lib/bio/appl/fasta/format10.rb +325 -0
- data/lib/bio/appl/gcg/msf.rb +212 -0
- data/lib/bio/appl/gcg/seq.rb +195 -0
- data/lib/bio/appl/genscan/report.rb +552 -0
- data/lib/bio/appl/hmmer.rb +126 -0
- data/lib/bio/appl/hmmer/report.rb +683 -0
- data/lib/bio/appl/iprscan/report.rb +374 -0
- data/lib/bio/appl/mafft.rb +259 -0
- data/lib/bio/appl/mafft/report.rb +226 -0
- data/lib/bio/appl/muscle.rb +52 -0
- data/lib/bio/appl/paml/baseml.rb +95 -0
- data/lib/bio/appl/paml/baseml/report.rb +32 -0
- data/lib/bio/appl/paml/codeml.rb +242 -0
- data/lib/bio/appl/paml/codeml/rates.rb +67 -0
- data/lib/bio/appl/paml/codeml/report.rb +67 -0
- data/lib/bio/appl/paml/common.rb +348 -0
- data/lib/bio/appl/paml/common_report.rb +38 -0
- data/lib/bio/appl/paml/yn00.rb +103 -0
- data/lib/bio/appl/paml/yn00/report.rb +32 -0
- data/lib/bio/appl/phylip/alignment.rb +133 -0
- data/lib/bio/appl/phylip/distance_matrix.rb +96 -0
- data/lib/bio/appl/probcons.rb +41 -0
- data/lib/bio/appl/psort.rb +548 -0
- data/lib/bio/appl/psort/report.rb +542 -0
- data/lib/bio/appl/pts1.rb +263 -0
- data/lib/bio/appl/sim4.rb +124 -0
- data/lib/bio/appl/sim4/report.rb +485 -0
- data/lib/bio/appl/sosui/report.rb +151 -0
- data/lib/bio/appl/spidey/report.rb +593 -0
- data/lib/bio/appl/targetp/report.rb +267 -0
- data/lib/bio/appl/tcoffee.rb +55 -0
- data/lib/bio/appl/tmhmm/report.rb +231 -0
- data/lib/bio/command.rb +593 -0
- data/lib/bio/compat/features.rb +157 -0
- data/lib/bio/compat/references.rb +128 -0
- data/lib/bio/data/aa.rb +353 -0
- data/lib/bio/data/codontable.rb +722 -0
- data/lib/bio/data/na.rb +223 -0
- data/lib/bio/db.rb +329 -0
- data/lib/bio/db/aaindex.rb +357 -0
- data/lib/bio/db/biosql/biosql_to_biosequence.rb +67 -0
- data/lib/bio/db/biosql/sequence.rb +508 -0
- data/lib/bio/db/embl/common.rb +352 -0
- data/lib/bio/db/embl/embl.rb +500 -0
- data/lib/bio/db/embl/embl_to_biosequence.rb +85 -0
- data/lib/bio/db/embl/format_embl.rb +190 -0
- data/lib/bio/db/embl/sptr.rb +1283 -0
- data/lib/bio/db/embl/swissprot.rb +42 -0
- data/lib/bio/db/embl/trembl.rb +41 -0
- data/lib/bio/db/embl/uniprot.rb +42 -0
- data/lib/bio/db/fantom.rb +597 -0
- data/lib/bio/db/fasta.rb +410 -0
- data/lib/bio/db/fasta/defline.rb +532 -0
- data/lib/bio/db/fasta/fasta_to_biosequence.rb +63 -0
- data/lib/bio/db/fasta/format_fasta.rb +97 -0
- data/lib/bio/db/genbank/common.rb +307 -0
- data/lib/bio/db/genbank/ddbj.rb +22 -0
- data/lib/bio/db/genbank/format_genbank.rb +187 -0
- data/lib/bio/db/genbank/genbank.rb +250 -0
- data/lib/bio/db/genbank/genbank_to_biosequence.rb +86 -0
- data/lib/bio/db/genbank/genpept.rb +60 -0
- data/lib/bio/db/genbank/refseq.rb +18 -0
- data/lib/bio/db/gff.rb +1846 -0
- data/lib/bio/db/go.rb +481 -0
- data/lib/bio/db/kegg/brite.rb +41 -0
- data/lib/bio/db/kegg/compound.rb +131 -0
- data/lib/bio/db/kegg/drug.rb +98 -0
- data/lib/bio/db/kegg/enzyme.rb +148 -0
- data/lib/bio/db/kegg/expression.rb +155 -0
- data/lib/bio/db/kegg/genes.rb +263 -0
- data/lib/bio/db/kegg/genome.rb +241 -0
- data/lib/bio/db/kegg/glycan.rb +166 -0
- data/lib/bio/db/kegg/keggtab.rb +357 -0
- data/lib/bio/db/kegg/kgml.rb +256 -0
- data/lib/bio/db/kegg/orthology.rb +136 -0
- data/lib/bio/db/kegg/reaction.rb +82 -0
- data/lib/bio/db/kegg/taxonomy.rb +331 -0
- data/lib/bio/db/lasergene.rb +209 -0
- data/lib/bio/db/litdb.rb +107 -0
- data/lib/bio/db/medline.rb +326 -0
- data/lib/bio/db/nbrf.rb +191 -0
- data/lib/bio/db/newick.rb +658 -0
- data/lib/bio/db/nexus.rb +1854 -0
- data/lib/bio/db/pdb.rb +29 -0
- data/lib/bio/db/pdb/atom.rb +77 -0
- data/lib/bio/db/pdb/chain.rb +210 -0
- data/lib/bio/db/pdb/chemicalcomponent.rb +224 -0
- data/lib/bio/db/pdb/model.rb +148 -0
- data/lib/bio/db/pdb/pdb.rb +1911 -0
- data/lib/bio/db/pdb/residue.rb +176 -0
- data/lib/bio/db/pdb/utils.rb +399 -0
- data/lib/bio/db/prosite.rb +597 -0
- data/lib/bio/db/rebase.rb +456 -0
- data/lib/bio/db/soft.rb +404 -0
- data/lib/bio/db/transfac.rb +375 -0
- data/lib/bio/db/url.rb +42 -0
- data/lib/bio/feature.rb +139 -0
- data/lib/bio/io/biosql/biodatabase.rb +64 -0
- data/lib/bio/io/biosql/bioentry.rb +29 -0
- data/lib/bio/io/biosql/bioentry_dbxref.rb +11 -0
- data/lib/bio/io/biosql/bioentry_path.rb +12 -0
- data/lib/bio/io/biosql/bioentry_qualifier_value.rb +10 -0
- data/lib/bio/io/biosql/bioentry_reference.rb +10 -0
- data/lib/bio/io/biosql/bioentry_relationship.rb +10 -0
- data/lib/bio/io/biosql/biosequence.rb +11 -0
- data/lib/bio/io/biosql/comment.rb +7 -0
- data/lib/bio/io/biosql/config/database.yml +20 -0
- data/lib/bio/io/biosql/dbxref.rb +13 -0
- data/lib/bio/io/biosql/dbxref_qualifier_value.rb +12 -0
- data/lib/bio/io/biosql/location.rb +32 -0
- data/lib/bio/io/biosql/location_qualifier_value.rb +11 -0
- data/lib/bio/io/biosql/ontology.rb +10 -0
- data/lib/bio/io/biosql/reference.rb +9 -0
- data/lib/bio/io/biosql/seqfeature.rb +32 -0
- data/lib/bio/io/biosql/seqfeature_dbxref.rb +11 -0
- data/lib/bio/io/biosql/seqfeature_path.rb +11 -0
- data/lib/bio/io/biosql/seqfeature_qualifier_value.rb +20 -0
- data/lib/bio/io/biosql/seqfeature_relationship.rb +11 -0
- data/lib/bio/io/biosql/taxon.rb +12 -0
- data/lib/bio/io/biosql/taxon_name.rb +9 -0
- data/lib/bio/io/biosql/term.rb +27 -0
- data/lib/bio/io/biosql/term_dbxref.rb +11 -0
- data/lib/bio/io/biosql/term_path.rb +12 -0
- data/lib/bio/io/biosql/term_relationship.rb +13 -0
- data/lib/bio/io/biosql/term_relationship_term.rb +11 -0
- data/lib/bio/io/biosql/term_synonym.rb +10 -0
- data/lib/bio/io/das.rb +461 -0
- data/lib/bio/io/dbget.rb +194 -0
- data/lib/bio/io/ddbjxml.rb +638 -0
- data/lib/bio/io/ebisoap.rb +158 -0
- data/lib/bio/io/ensembl.rb +229 -0
- data/lib/bio/io/fastacmd.rb +163 -0
- data/lib/bio/io/fetch.rb +195 -0
- data/lib/bio/io/flatfile.rb +482 -0
- data/lib/bio/io/flatfile/autodetection.rb +545 -0
- data/lib/bio/io/flatfile/bdb.rb +253 -0
- data/lib/bio/io/flatfile/buffer.rb +237 -0
- data/lib/bio/io/flatfile/index.rb +1381 -0
- data/lib/bio/io/flatfile/indexer.rb +805 -0
- data/lib/bio/io/flatfile/splitter.rb +297 -0
- data/lib/bio/io/higet.rb +73 -0
- data/lib/bio/io/hinv.rb +442 -0
- data/lib/bio/io/keggapi.rb +805 -0
- data/lib/bio/io/ncbirest.rb +733 -0
- data/lib/bio/io/ncbisoap.rb +155 -0
- data/lib/bio/io/pubmed.rb +307 -0
- data/lib/bio/io/registry.rb +292 -0
- data/lib/bio/io/soapwsdl.rb +119 -0
- data/lib/bio/io/sql.rb +186 -0
- data/lib/bio/location.rb +867 -0
- data/lib/bio/map.rb +410 -0
- data/lib/bio/pathway.rb +960 -0
- data/lib/bio/reference.rb +602 -0
- data/lib/bio/sequence.rb +456 -0
- data/lib/bio/sequence/aa.rb +152 -0
- data/lib/bio/sequence/adapter.rb +108 -0
- data/lib/bio/sequence/common.rb +310 -0
- data/lib/bio/sequence/compat.rb +123 -0
- data/lib/bio/sequence/dblink.rb +54 -0
- data/lib/bio/sequence/format.rb +358 -0
- data/lib/bio/sequence/format_raw.rb +23 -0
- data/lib/bio/sequence/generic.rb +24 -0
- data/lib/bio/sequence/na.rb +491 -0
- data/lib/bio/shell.rb +44 -0
- data/lib/bio/shell/core.rb +578 -0
- data/lib/bio/shell/demo.rb +146 -0
- data/lib/bio/shell/interface.rb +218 -0
- data/lib/bio/shell/irb.rb +95 -0
- data/lib/bio/shell/object.rb +71 -0
- data/lib/bio/shell/plugin/blast.rb +42 -0
- data/lib/bio/shell/plugin/codon.rb +218 -0
- data/lib/bio/shell/plugin/das.rb +58 -0
- data/lib/bio/shell/plugin/emboss.rb +23 -0
- data/lib/bio/shell/plugin/entry.rb +105 -0
- data/lib/bio/shell/plugin/flatfile.rb +101 -0
- data/lib/bio/shell/plugin/keggapi.rb +181 -0
- data/lib/bio/shell/plugin/midi.rb +430 -0
- data/lib/bio/shell/plugin/obda.rb +45 -0
- data/lib/bio/shell/plugin/psort.rb +56 -0
- data/lib/bio/shell/plugin/seq.rb +247 -0
- data/lib/bio/shell/plugin/soap.rb +87 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/bioruby_generator.rb +29 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_classes.rhtml +4 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_log.rhtml +27 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_methods.rhtml +11 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_modules.rhtml +4 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_variables.rhtml +7 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby-bg.gif +0 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby-gem.png +0 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby-link.gif +0 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby.css +368 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby.rhtml +47 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby_controller.rb +144 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby_helper.rb +47 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/commands.rhtml +8 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/history.rhtml +10 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/index.rhtml +26 -0
- data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/spinner.gif +0 -0
- data/lib/bio/shell/script.rb +25 -0
- data/lib/bio/shell/setup.rb +109 -0
- data/lib/bio/shell/web.rb +102 -0
- data/lib/bio/tree.rb +852 -0
- data/lib/bio/util/color_scheme.rb +191 -0
- data/lib/bio/util/color_scheme/buried.rb +59 -0
- data/lib/bio/util/color_scheme/helix.rb +59 -0
- data/lib/bio/util/color_scheme/hydropathy.rb +64 -0
- data/lib/bio/util/color_scheme/nucleotide.rb +31 -0
- data/lib/bio/util/color_scheme/strand.rb +59 -0
- data/lib/bio/util/color_scheme/taylor.rb +50 -0
- data/lib/bio/util/color_scheme/turn.rb +59 -0
- data/lib/bio/util/color_scheme/zappo.rb +50 -0
- data/lib/bio/util/contingency_table.rb +370 -0
- data/lib/bio/util/restriction_enzyme.rb +228 -0
- data/lib/bio/util/restriction_enzyme/analysis.rb +249 -0
- data/lib/bio/util/restriction_enzyme/analysis_basic.rb +217 -0
- data/lib/bio/util/restriction_enzyme/cut_symbol.rb +107 -0
- data/lib/bio/util/restriction_enzyme/double_stranded.rb +321 -0
- data/lib/bio/util/restriction_enzyme/double_stranded/aligned_strands.rb +130 -0
- data/lib/bio/util/restriction_enzyme/double_stranded/cut_location_pair.rb +103 -0
- data/lib/bio/util/restriction_enzyme/double_stranded/cut_location_pair_in_enzyme_notation.rb +38 -0
- data/lib/bio/util/restriction_enzyme/double_stranded/cut_locations.rb +76 -0
- data/lib/bio/util/restriction_enzyme/double_stranded/cut_locations_in_enzyme_notation.rb +107 -0
- data/lib/bio/util/restriction_enzyme/enzymes.yaml +7061 -0
- data/lib/bio/util/restriction_enzyme/range/cut_range.rb +24 -0
- data/lib/bio/util/restriction_enzyme/range/cut_ranges.rb +47 -0
- data/lib/bio/util/restriction_enzyme/range/horizontal_cut_range.rb +67 -0
- data/lib/bio/util/restriction_enzyme/range/sequence_range.rb +257 -0
- data/lib/bio/util/restriction_enzyme/range/sequence_range/calculated_cuts.rb +242 -0
- data/lib/bio/util/restriction_enzyme/range/sequence_range/fragment.rb +51 -0
- data/lib/bio/util/restriction_enzyme/range/sequence_range/fragments.rb +41 -0
- data/lib/bio/util/restriction_enzyme/range/vertical_cut_range.rb +77 -0
- data/lib/bio/util/restriction_enzyme/single_strand.rb +200 -0
- data/lib/bio/util/restriction_enzyme/single_strand/cut_locations_in_enzyme_notation.rb +135 -0
- data/lib/bio/util/restriction_enzyme/single_strand_complement.rb +23 -0
- data/lib/bio/util/restriction_enzyme/string_formatting.rb +111 -0
- data/lib/bio/util/sirna.rb +288 -0
- data/test/data/HMMER/hmmpfam.out +64 -0
- data/test/data/HMMER/hmmsearch.out +88 -0
- data/test/data/SOSUI/sample.report +11 -0
- data/test/data/TMHMM/sample.report +21 -0
- data/test/data/aaindex/DAYM780301 +30 -0
- data/test/data/aaindex/PRAM900102 +20 -0
- data/test/data/bl2seq/cd8a_cd8b_blastp.bl2seq +53 -0
- data/test/data/bl2seq/cd8a_p53_e-5blastp.bl2seq +37 -0
- data/test/data/blast/2.2.15.blastp.m7 +876 -0
- data/test/data/blast/b0002.faa +15 -0
- data/test/data/blast/b0002.faa.m0 +128 -0
- data/test/data/blast/b0002.faa.m7 +65 -0
- data/test/data/blast/b0002.faa.m8 +1 -0
- data/test/data/blast/blastp-multi.m7 +188 -0
- data/test/data/command/echoarg2.bat +1 -0
- data/test/data/embl/AB090716.embl +65 -0
- data/test/data/embl/AB090716.embl.rel89 +63 -0
- data/test/data/fasta/example1.txt +75 -0
- data/test/data/fasta/example2.txt +21 -0
- data/test/data/genscan/sample.report +63 -0
- data/test/data/iprscan/merged.raw +32 -0
- data/test/data/iprscan/merged.txt +74 -0
- data/test/data/paml/codeml/control_file.txt +30 -0
- data/test/data/paml/codeml/output.txt +78 -0
- data/test/data/paml/codeml/rates +217 -0
- data/test/data/prosite/prosite.dat +2233 -0
- data/test/data/refseq/nm_126355.entret +64 -0
- data/test/data/rpsblast/misc.rpsblast +193 -0
- data/test/data/soft/GDS100_partial.soft +92 -0
- data/test/data/soft/GSE3457_family_partial.soft +874 -0
- data/test/data/uniprot/p53_human.uniprot +1456 -0
- data/test/functional/bio/appl/test_pts1.rb +115 -0
- data/test/functional/bio/io/test_ensembl.rb +229 -0
- data/test/functional/bio/io/test_soapwsdl.rb +52 -0
- data/test/functional/bio/sequence/test_output_embl.rb +51 -0
- data/test/functional/bio/test_command.rb +301 -0
- data/test/runner.rb +14 -0
- data/test/unit/bio/appl/bl2seq/test_report.rb +134 -0
- data/test/unit/bio/appl/blast/test_ncbioptions.rb +112 -0
- data/test/unit/bio/appl/blast/test_report.rb +1135 -0
- data/test/unit/bio/appl/blast/test_rpsblast.rb +398 -0
- data/test/unit/bio/appl/genscan/test_report.rb +182 -0
- data/test/unit/bio/appl/hmmer/test_report.rb +342 -0
- data/test/unit/bio/appl/iprscan/test_report.rb +338 -0
- data/test/unit/bio/appl/mafft/test_report.rb +63 -0
- data/test/unit/bio/appl/paml/codeml/test_rates.rb +45 -0
- data/test/unit/bio/appl/paml/codeml/test_report.rb +45 -0
- data/test/unit/bio/appl/paml/test_codeml.rb +174 -0
- data/test/unit/bio/appl/sosui/test_report.rb +81 -0
- data/test/unit/bio/appl/targetp/test_report.rb +146 -0
- data/test/unit/bio/appl/test_blast.rb +277 -0
- data/test/unit/bio/appl/test_fasta.rb +130 -0
- data/test/unit/bio/appl/test_psort.rb +57 -0
- data/test/unit/bio/appl/test_pts1.rb +77 -0
- data/test/unit/bio/appl/tmhmm/test_report.rb +126 -0
- data/test/unit/bio/data/test_aa.rb +90 -0
- data/test/unit/bio/data/test_codontable.rb +107 -0
- data/test/unit/bio/data/test_na.rb +80 -0
- data/test/unit/bio/db/embl/test_common.rb +117 -0
- data/test/unit/bio/db/embl/test_embl.rb +214 -0
- data/test/unit/bio/db/embl/test_embl_rel89.rb +219 -0
- data/test/unit/bio/db/embl/test_embl_to_bioseq.rb +203 -0
- data/test/unit/bio/db/embl/test_sptr.rb +1812 -0
- data/test/unit/bio/db/embl/test_uniprot.rb +31 -0
- data/test/unit/bio/db/kegg/test_genes.rb +45 -0
- data/test/unit/bio/db/pdb/test_pdb.rb +152 -0
- data/test/unit/bio/db/test_aaindex.rb +197 -0
- data/test/unit/bio/db/test_fasta.rb +250 -0
- data/test/unit/bio/db/test_gff.rb +1190 -0
- data/test/unit/bio/db/test_lasergene.rb +95 -0
- data/test/unit/bio/db/test_medline.rb +127 -0
- data/test/unit/bio/db/test_newick.rb +293 -0
- data/test/unit/bio/db/test_nexus.rb +364 -0
- data/test/unit/bio/db/test_prosite.rb +1437 -0
- data/test/unit/bio/db/test_rebase.rb +101 -0
- data/test/unit/bio/db/test_soft.rb +138 -0
- data/test/unit/bio/db/test_url.rb +36 -0
- data/test/unit/bio/io/flatfile/test_autodetection.rb +375 -0
- data/test/unit/bio/io/flatfile/test_buffer.rb +251 -0
- data/test/unit/bio/io/flatfile/test_splitter.rb +369 -0
- data/test/unit/bio/io/test_ddbjxml.rb +80 -0
- data/test/unit/bio/io/test_ensembl.rb +109 -0
- data/test/unit/bio/io/test_fastacmd.rb +42 -0
- data/test/unit/bio/io/test_flatfile.rb +505 -0
- data/test/unit/bio/io/test_soapwsdl.rb +32 -0
- data/test/unit/bio/sequence/test_aa.rb +115 -0
- data/test/unit/bio/sequence/test_common.rb +373 -0
- data/test/unit/bio/sequence/test_compat.rb +69 -0
- data/test/unit/bio/sequence/test_dblink.rb +58 -0
- data/test/unit/bio/sequence/test_na.rb +330 -0
- data/test/unit/bio/shell/plugin/test_seq.rb +185 -0
- data/test/unit/bio/test_alignment.rb +1025 -0
- data/test/unit/bio/test_command.rb +349 -0
- data/test/unit/bio/test_db.rb +96 -0
- data/test/unit/bio/test_feature.rb +144 -0
- data/test/unit/bio/test_location.rb +599 -0
- data/test/unit/bio/test_map.rb +230 -0
- data/test/unit/bio/test_pathway.rb +499 -0
- data/test/unit/bio/test_reference.rb +252 -0
- data/test/unit/bio/test_sequence.rb +329 -0
- data/test/unit/bio/test_shell.rb +18 -0
- data/test/unit/bio/test_tree.rb +593 -0
- data/test/unit/bio/util/restriction_enzyme/analysis/test_calculated_cuts.rb +299 -0
- data/test/unit/bio/util/restriction_enzyme/analysis/test_cut_ranges.rb +103 -0
- data/test/unit/bio/util/restriction_enzyme/analysis/test_sequence_range.rb +240 -0
- data/test/unit/bio/util/restriction_enzyme/double_stranded/test_aligned_strands.rb +101 -0
- data/test/unit/bio/util/restriction_enzyme/double_stranded/test_cut_location_pair.rb +75 -0
- data/test/unit/bio/util/restriction_enzyme/double_stranded/test_cut_location_pair_in_enzyme_notation.rb +73 -0
- data/test/unit/bio/util/restriction_enzyme/double_stranded/test_cut_locations.rb +53 -0
- data/test/unit/bio/util/restriction_enzyme/double_stranded/test_cut_locations_in_enzyme_notation.rb +104 -0
- data/test/unit/bio/util/restriction_enzyme/single_strand/test_cut_locations_in_enzyme_notation.rb +83 -0
- data/test/unit/bio/util/restriction_enzyme/test_analysis.rb +246 -0
- data/test/unit/bio/util/restriction_enzyme/test_cut_symbol.rb +44 -0
- data/test/unit/bio/util/restriction_enzyme/test_double_stranded.rb +115 -0
- data/test/unit/bio/util/restriction_enzyme/test_single_strand.rb +147 -0
- data/test/unit/bio/util/restriction_enzyme/test_single_strand_complement.rb +147 -0
- data/test/unit/bio/util/restriction_enzyme/test_string_formatting.rb +60 -0
- data/test/unit/bio/util/test_color_scheme.rb +33 -0
- data/test/unit/bio/util/test_contingency_table.rb +94 -0
- data/test/unit/bio/util/test_restriction_enzyme.rb +42 -0
- data/test/unit/bio/util/test_sirna.rb +245 -0
- metadata +543 -0
data/lib/bio/map.rb
ADDED
@@ -0,0 +1,410 @@
|
|
1
|
+
#
|
2
|
+
# = bio/map.rb - biological mapping class
|
3
|
+
#
|
4
|
+
# Copyright:: Copyright (C) 2006 Jan Aerts <jan.aerts@bbsrc.ac.uk>
|
5
|
+
# License:: The Ruby License
|
6
|
+
#
|
7
|
+
# $Id: map.rb,v 1.11 2007/04/12 12:19:16 aerts Exp $
|
8
|
+
|
9
|
+
require 'bio/location'
|
10
|
+
|
11
|
+
module Bio
|
12
|
+
|
13
|
+
# == Description
|
14
|
+
#
|
15
|
+
# The Bio::Map contains classes that describe mapping information
|
16
|
+
# and can be used to contain linkage maps, radiation-hybrid maps,
|
17
|
+
# etc. As the same marker can be mapped to more than one map, and a
|
18
|
+
# single map typically contains more than one marker, the link
|
19
|
+
# between the markers and maps is handled by Bio::Map::Mapping
|
20
|
+
# objects. Therefore, to link a map to a marker, a Bio::Map::Mapping
|
21
|
+
# object is added to that Bio::Map. See usage below.
|
22
|
+
#
|
23
|
+
# Not only maps in the strict sense have map-like features (and
|
24
|
+
# similarly not only markers in the strict sense have marker-like
|
25
|
+
# features). For example, a microsatellite is something that can be
|
26
|
+
# mapped on a linkage map (and hence becomes a 'marker'), but a
|
27
|
+
# clone can also be mapped to a cytogenetic map. In that case, the
|
28
|
+
# clone acts as a marker and has marker-like properties. That same
|
29
|
+
# clone can also be considered a 'map' when BAC-end sequences are
|
30
|
+
# mapped to it. To reflect this flexibility, the modules
|
31
|
+
# Bio::Map::ActsLikeMap and Bio::Map::ActsLikeMarker define methods
|
32
|
+
# that are typical for maps and markers.
|
33
|
+
#
|
34
|
+
#--
|
35
|
+
# In a certain sense, a biological sequence also has map- and
|
36
|
+
# marker-like properties: things can be mapped to it at certain
|
37
|
+
# locations, and the sequence itself can be mapped to something else
|
38
|
+
# (e.g. the BAC-end sequence example above, or a BLAST-result).
|
39
|
+
#++
|
40
|
+
#
|
41
|
+
# == Usage
|
42
|
+
#
|
43
|
+
# my_marker1 = Bio::Map::Marker.new('marker1')
|
44
|
+
# my_marker2 = Bio::Map::Marker.new('marker2')
|
45
|
+
# my_marker3 = Bio::Map::Marker.new('marker3')
|
46
|
+
#
|
47
|
+
# my_map1 = Bio::Map::SimpleMap.new('RH_map_ABC (2006)', 'RH', 'cR')
|
48
|
+
# my_map2 = Bio::Map::SimpleMap.new('consensus', 'linkage', 'cM')
|
49
|
+
#
|
50
|
+
# my_map1.add_mapping_as_map(my_marker1, '17')
|
51
|
+
# my_map1.add_mapping_as_map(Bio::Map::Marker.new('marker2'), '5')
|
52
|
+
# my_marker3.add_mapping_as_marker(my_map1, '9')
|
53
|
+
#
|
54
|
+
# print "Does my_map1 contain marker3? => "
|
55
|
+
# puts my_map1.contains_marker?(my_marker3).to_s
|
56
|
+
# print "Does my_map2 contain marker3? => "
|
57
|
+
# puts my_map2.contains_marker?(my_marker3).to_s
|
58
|
+
#
|
59
|
+
# my_map1.mappings_as_map.sort.each do |mapping|
|
60
|
+
# puts [ mapping.map.name,
|
61
|
+
# mapping.marker.name,
|
62
|
+
# mapping.location.from.to_s,
|
63
|
+
# mapping.location.to.to_s ].join("\t")
|
64
|
+
# end
|
65
|
+
# puts my_map1.mappings_as_map.min.marker.name
|
66
|
+
#
|
67
|
+
# my_map2.mappings_as_map.each do |mapping|
|
68
|
+
# puts [ mapping.map.name,
|
69
|
+
# mapping.marker.name,
|
70
|
+
# mapping.location.from.to_s,
|
71
|
+
# mapping.location.to.to_s ].join("\t")
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
module Map
|
75
|
+
|
76
|
+
# == Description
|
77
|
+
#
|
78
|
+
# The Bio::Map::ActsLikeMap module contains methods that are typical for
|
79
|
+
# map-like things:
|
80
|
+
#
|
81
|
+
# * add markers with their locations (through Bio::Map::Mappings)
|
82
|
+
# * check if a given marker is mapped to it,
|
83
|
+
# and can be mixed into other classes (e.g. Bio::Map::SimpleMap)
|
84
|
+
#
|
85
|
+
# Classes that include this mixin should provide an array property
|
86
|
+
# called mappings_as_map.
|
87
|
+
#
|
88
|
+
# For example:
|
89
|
+
#
|
90
|
+
# class MyMapThing
|
91
|
+
# include Bio::Map::ActsLikeMap
|
92
|
+
#
|
93
|
+
# def initialize (name)
|
94
|
+
# @name = name
|
95
|
+
# @mappings_as_maps = Array.new
|
96
|
+
# end
|
97
|
+
# attr_accessor :name, :mappings_as_map
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
module ActsLikeMap
|
101
|
+
|
102
|
+
# == Description
|
103
|
+
#
|
104
|
+
# Adds a Bio::Map::Mappings object to its array of mappings.
|
105
|
+
#
|
106
|
+
# == Usage
|
107
|
+
#
|
108
|
+
# # suppose we have a Bio::Map::SimpleMap object called my_map
|
109
|
+
# my_map.add_mapping_as_map(Bio::Map::Marker.new('marker_a'), '5')
|
110
|
+
#
|
111
|
+
# ---
|
112
|
+
# *Arguments*:
|
113
|
+
# * _marker_ (required): Bio::Map::Marker object
|
114
|
+
# * _location_: location of mapping. Should be a _string_, not a _number_.
|
115
|
+
# *Returns*:: itself
|
116
|
+
def add_mapping_as_map(marker, location = nil)
|
117
|
+
unless marker.class.include?(Bio::Map::ActsLikeMarker)
|
118
|
+
raise "[Error] marker is not object that implements Bio::Map::ActsLikeMarker"
|
119
|
+
end
|
120
|
+
my_mapping = ( location.nil? ) ? Bio::Map::Mapping.new(self, marker, nil) : Bio::Map::Mapping.new(self, marker, Bio::Locations.new(location))
|
121
|
+
if ! marker.mapped_to?(self)
|
122
|
+
self.mappings_as_map.push(my_mapping)
|
123
|
+
marker.mappings_as_marker.push(my_mapping)
|
124
|
+
else
|
125
|
+
already_mapped = false
|
126
|
+
marker.positions_on(self).each do |loc|
|
127
|
+
if loc.equals?(Bio::Locations.new(location))
|
128
|
+
already_mapped = true
|
129
|
+
end
|
130
|
+
end
|
131
|
+
if ! already_mapped
|
132
|
+
self.mappings_as_map.push(my_mapping)
|
133
|
+
marker.mappings_as_marker.push(my_mapping)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
return self
|
138
|
+
end
|
139
|
+
|
140
|
+
# Checks whether a Bio::Map::Marker is mapped to this
|
141
|
+
# Bio::Map::SimpleMap.
|
142
|
+
#
|
143
|
+
# ---
|
144
|
+
# *Arguments*:
|
145
|
+
# * _marker_: a Bio::Map::Marker object
|
146
|
+
# *Returns*:: true or false
|
147
|
+
def contains_marker?(marker)
|
148
|
+
unless marker.class.include?(Bio::Map::ActsLikeMarker)
|
149
|
+
raise "[Error] marker is not object that implements Bio::Map::ActsLikeMarker"
|
150
|
+
end
|
151
|
+
contains = false
|
152
|
+
self.mappings_as_map.each do |mapping|
|
153
|
+
if mapping.marker == marker
|
154
|
+
contains = true
|
155
|
+
return contains
|
156
|
+
end
|
157
|
+
end
|
158
|
+
return contains
|
159
|
+
end
|
160
|
+
|
161
|
+
end # ActsLikeMap
|
162
|
+
|
163
|
+
# == Description
|
164
|
+
#
|
165
|
+
# The Bio::Map::ActsLikeMarker module contains methods that are
|
166
|
+
# typical for marker-like things:
|
167
|
+
#
|
168
|
+
# * map it to one or more maps
|
169
|
+
# * check if it's mapped to a given map
|
170
|
+
# and can be mixed into other classes (e.g. Bio::Map::Marker)
|
171
|
+
#
|
172
|
+
# Classes that include this mixin should provide an array property
|
173
|
+
# called mappings_as_marker.
|
174
|
+
#
|
175
|
+
# For example:
|
176
|
+
#
|
177
|
+
# class MyMarkerThing
|
178
|
+
# include Bio::Map::ActsLikeMarker
|
179
|
+
#
|
180
|
+
# def initialize (name)
|
181
|
+
# @name = name
|
182
|
+
# @mappings_as_marker = Array.new
|
183
|
+
# end
|
184
|
+
# attr_accessor :name, :mappings_as_marker
|
185
|
+
# end
|
186
|
+
#
|
187
|
+
module ActsLikeMarker
|
188
|
+
|
189
|
+
# == Description
|
190
|
+
#
|
191
|
+
# Adds a Bio::Map::Mappings object to its array of mappings.
|
192
|
+
#
|
193
|
+
# == Usage
|
194
|
+
#
|
195
|
+
# # suppose we have a Bio::Map::Marker object called marker_a
|
196
|
+
# marker_a.add_mapping_as_marker(Bio::Map::SimpleMap.new('my_map'), '5')
|
197
|
+
#
|
198
|
+
# ---
|
199
|
+
# *Arguments*:
|
200
|
+
# * _map_ (required): Bio::Map::SimpleMap object
|
201
|
+
# * _location_: location of mapping. Should be a _string_, not a _number_.
|
202
|
+
# *Returns*:: itself
|
203
|
+
def add_mapping_as_marker(map, location = nil)
|
204
|
+
unless map.class.include?(Bio::Map::ActsLikeMap)
|
205
|
+
raise "[Error] map is not object that implements Bio::Map::ActsLikeMap"
|
206
|
+
end
|
207
|
+
my_mapping = (location.nil?) ? Bio::Map::Mappings.new(map, self, nil) : Bio::Map::Mapping.new(map, self, Bio::Locations.new(location))
|
208
|
+
if ! self.mapped_to?(map)
|
209
|
+
self.mappings_as_marker.push(my_mapping)
|
210
|
+
map.mappings_as_map.push(my_mapping)
|
211
|
+
else
|
212
|
+
already_mapped = false
|
213
|
+
self.positions_on(map).each do |loc|
|
214
|
+
if loc.equals?(Bio::Locations.new(location))
|
215
|
+
already_mapped = true
|
216
|
+
end
|
217
|
+
end
|
218
|
+
if ! already_mapped
|
219
|
+
self.mappings_as_marker.push(my_mapping)
|
220
|
+
map.mappings_as_map.push(my_mapping)
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
# Check whether this marker is mapped to a given Bio::Map::SimpleMap.
|
226
|
+
# ---
|
227
|
+
# *Arguments*:
|
228
|
+
# * _map_: a Bio::Map::SimpleMap object
|
229
|
+
# *Returns*:: true or false
|
230
|
+
def mapped_to?(map)
|
231
|
+
unless map.class.include?(Bio::Map::ActsLikeMap)
|
232
|
+
raise "[Error] map is not object that implements Bio::Map::ActsLikeMap"
|
233
|
+
end
|
234
|
+
|
235
|
+
mapped = false
|
236
|
+
self.mappings_as_marker.each do |mapping|
|
237
|
+
if mapping.map == map
|
238
|
+
mapped = true
|
239
|
+
return mapped
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
return mapped
|
244
|
+
end
|
245
|
+
|
246
|
+
# Return all positions of this marker on a given map.
|
247
|
+
# ---
|
248
|
+
# *Arguments*:
|
249
|
+
# * _map_: an object that mixes in Bio::Map::ActsLikeMap
|
250
|
+
# *Returns*:: array of Bio::Location objects
|
251
|
+
def positions_on(map)
|
252
|
+
unless map.class.include?(Bio::Map::ActsLikeMap)
|
253
|
+
raise "[Error] map is not object that implements Bio::Map::ActsLikeMap"
|
254
|
+
end
|
255
|
+
|
256
|
+
positions = Array.new
|
257
|
+
self.mappings_as_marker.each do |mapping|
|
258
|
+
if mapping.map == map
|
259
|
+
positions.push(mapping.location)
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
return positions
|
264
|
+
end
|
265
|
+
|
266
|
+
# Return all mappings of this marker on a given map.
|
267
|
+
# ---
|
268
|
+
# *Arguments*:
|
269
|
+
# * _map_: an object that mixes in Bio::Map::ActsLikeMap
|
270
|
+
# *Returns*:: array of Bio::Map::Mapping objects
|
271
|
+
def mappings_on(map)
|
272
|
+
unless map.class.include?(Bio::Map::ActsLikeMap)
|
273
|
+
raise "[Error] map is not object that implements Bio::Map::ActsLikeMap"
|
274
|
+
end
|
275
|
+
|
276
|
+
m = Array.new
|
277
|
+
self.mappings_as_marker.each do |mapping|
|
278
|
+
if mapping.map == map
|
279
|
+
m.push(mapping)
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
return m
|
284
|
+
end
|
285
|
+
|
286
|
+
|
287
|
+
end # ActsLikeMarker
|
288
|
+
|
289
|
+
# == Description
|
290
|
+
#
|
291
|
+
# Creates a new Bio::Map::Mapping object, which links Bio::Map::ActsAsMap-
|
292
|
+
# and Bio::Map::ActsAsMarker-like objects. This class is typically not
|
293
|
+
# accessed directly, but through map- or marker-like objects.
|
294
|
+
class Mapping
|
295
|
+
|
296
|
+
include Comparable
|
297
|
+
|
298
|
+
# Creates a new Bio::Map::Mapping object
|
299
|
+
# ---
|
300
|
+
# *Arguments*:
|
301
|
+
# * _map_: a Bio::Map::SimpleMap object
|
302
|
+
# * _marker_: a Bio::Map::Marker object
|
303
|
+
# * _location_: a Bio::Locations object
|
304
|
+
def initialize (map, marker, location = nil)
|
305
|
+
@map, @marker, @location = map, marker, location
|
306
|
+
end
|
307
|
+
attr_accessor :map, :marker, :location
|
308
|
+
|
309
|
+
# Compares the location of this mapping to another mapping.
|
310
|
+
# ---
|
311
|
+
# *Arguments*:
|
312
|
+
# * other_mapping: Bio::Map::Mapping object
|
313
|
+
# *Returns*::
|
314
|
+
# * 1 if self < other location
|
315
|
+
# * -1 if self > other location
|
316
|
+
# * 0 if both location are the same
|
317
|
+
# * nil if the argument is not a Bio::Location object
|
318
|
+
def <=>(other)
|
319
|
+
unless other.kind_of?(Bio::Map::Mapping)
|
320
|
+
raise "[Error] markers are not comparable"
|
321
|
+
end
|
322
|
+
unless @map.equal?(other.map)
|
323
|
+
raise "[Error] maps have to be the same"
|
324
|
+
end
|
325
|
+
|
326
|
+
return self.location[0].<=>(other.location[0])
|
327
|
+
end
|
328
|
+
end # Mapping
|
329
|
+
|
330
|
+
# == Description
|
331
|
+
#
|
332
|
+
# This class handles the essential storage of name, type and units
|
333
|
+
# of a map. It includes Bio::Map::ActsLikeMap, and therefore
|
334
|
+
# supports the methods of that module.
|
335
|
+
#
|
336
|
+
# == Usage
|
337
|
+
#
|
338
|
+
# my_map1 = Bio::Map::SimpleMap.new('RH_map_ABC (2006)', 'RH', 'cR')
|
339
|
+
# my_map1.add_marker(Bio::Map::Marker.new('marker_a', '17')
|
340
|
+
# my_map1.add_marker(Bio::Map::Marker.new('marker_b', '5')
|
341
|
+
#
|
342
|
+
class SimpleMap
|
343
|
+
|
344
|
+
include Bio::Map::ActsLikeMap
|
345
|
+
|
346
|
+
# Builds a new Bio::Map::SimpleMap object
|
347
|
+
# ---
|
348
|
+
# *Arguments*:
|
349
|
+
# * name: name of the map
|
350
|
+
# * type: type of the map (e.g. linkage, radiation_hybrid, cytogenetic, ...)
|
351
|
+
# * units: unit of the map (e.g. cM, cR, ...)
|
352
|
+
# *Returns*:: new Bio::Map::SimpleMap object
|
353
|
+
def initialize (name = nil, type = nil, length = nil, units = nil)
|
354
|
+
@name, @type, @length, @units = name, type, length, units
|
355
|
+
@mappings_as_map = Array.new
|
356
|
+
end
|
357
|
+
|
358
|
+
# Name of the map
|
359
|
+
attr_accessor :name
|
360
|
+
|
361
|
+
# Type of the map
|
362
|
+
attr_accessor :type
|
363
|
+
|
364
|
+
# Length of the map
|
365
|
+
attr_accessor :length
|
366
|
+
|
367
|
+
# Units of the map
|
368
|
+
attr_accessor :units
|
369
|
+
|
370
|
+
# Mappings
|
371
|
+
attr_accessor :mappings_as_map
|
372
|
+
|
373
|
+
end # SimpleMap
|
374
|
+
|
375
|
+
# == Description
|
376
|
+
#
|
377
|
+
# This class handles markers that are anchored to a Bio::Map::SimpleMap.
|
378
|
+
# It includes Bio::Map::ActsLikeMarker, and therefore supports the
|
379
|
+
# methods of that module.
|
380
|
+
#
|
381
|
+
# == Usage
|
382
|
+
#
|
383
|
+
# marker_a = Bio::Map::Marker.new('marker_a')
|
384
|
+
# marker_b = Bio::Map::Marker.new('marker_b')
|
385
|
+
#
|
386
|
+
class Marker
|
387
|
+
|
388
|
+
include Bio::Map::ActsLikeMarker
|
389
|
+
|
390
|
+
# Builds a new Bio::Map::Marker object
|
391
|
+
# ---
|
392
|
+
# *Arguments*:
|
393
|
+
# * name: name of the marker
|
394
|
+
# *Returns*:: new Bio::Map::Marker object
|
395
|
+
def initialize(name)
|
396
|
+
@name = name
|
397
|
+
@mappings_as_marker = Array.new
|
398
|
+
end
|
399
|
+
|
400
|
+
# Name of the marker
|
401
|
+
attr_accessor :name
|
402
|
+
|
403
|
+
# Mappings
|
404
|
+
attr_accessor :mappings_as_marker
|
405
|
+
|
406
|
+
end # Marker
|
407
|
+
|
408
|
+
end # Map
|
409
|
+
|
410
|
+
end # Bio
|
data/lib/bio/pathway.rb
ADDED
@@ -0,0 +1,960 @@
|
|
1
|
+
#
|
2
|
+
# = bio/pathway.rb - Binary relations and Graph algorithms
|
3
|
+
#
|
4
|
+
# Copyright: Copyright (C) 2001
|
5
|
+
# Toshiaki Katayama <k@bioruby.org>,
|
6
|
+
# Shuichi Kawashima <shuichi@hgc.jp>
|
7
|
+
# License:: The Ruby License
|
8
|
+
#
|
9
|
+
# $Id:$
|
10
|
+
#
|
11
|
+
|
12
|
+
require 'matrix'
|
13
|
+
|
14
|
+
module Bio
|
15
|
+
|
16
|
+
# Bio::Pathway is a general graph object initially constructed by the
|
17
|
+
# list of the ((<Bio::Relation>)) objects. The basic concept of the
|
18
|
+
# Bio::Pathway object is to store a graph as an adjacency list (in the
|
19
|
+
# instance variable @graph), and converting the list into an adjacency
|
20
|
+
# matrix by calling to_matrix method on demand. However, in some
|
21
|
+
# cases, it is convenient to have the original list of the
|
22
|
+
# ((<Bio::Relation>))s, Bio::Pathway object also stores the list (as
|
23
|
+
# the instance variable @relations) redundantly.
|
24
|
+
#
|
25
|
+
# Note: you can clear the @relations list by calling clear_relations!
|
26
|
+
# method to reduce the memory usage, and the content of the @relations
|
27
|
+
# can be re-generated from the @graph by to_relations method.
|
28
|
+
class Pathway
|
29
|
+
|
30
|
+
# Initial graph (adjacency list) generation from the list of Relation.
|
31
|
+
#
|
32
|
+
# Generate Bio::Pathway object from the list of Bio::Relation objects.
|
33
|
+
# If the second argument is true, undirected graph is generated.
|
34
|
+
#
|
35
|
+
# r1 = Bio::Relation.new('a', 'b', 1)
|
36
|
+
# r2 = Bio::Relation.new('a', 'c', 5)
|
37
|
+
# r3 = Bio::Relation.new('b', 'c', 3)
|
38
|
+
# list = [ r1, r2, r3 ]
|
39
|
+
# g = Bio::Pathway.new(list, 'undirected')
|
40
|
+
#
|
41
|
+
def initialize(relations, undirected = false)
|
42
|
+
@undirected = undirected
|
43
|
+
@relations = relations
|
44
|
+
@graph = {} # adjacency list expression of the graph
|
45
|
+
@index = {} # numbering each node in matrix
|
46
|
+
@label = {} # additional information on each node
|
47
|
+
self.to_list # generate adjacency list
|
48
|
+
end
|
49
|
+
|
50
|
+
# Read-only accessor for the internal list of the Bio::Relation objects
|
51
|
+
attr_reader :relations
|
52
|
+
|
53
|
+
# Read-only accessor for the adjacency list of the graph.
|
54
|
+
attr_reader :graph
|
55
|
+
|
56
|
+
# Read-only accessor for the row/column index (@index) of the
|
57
|
+
# adjacency matrix. Contents of the hash @index is created by
|
58
|
+
# calling to_matrix method.
|
59
|
+
attr_reader :index
|
60
|
+
|
61
|
+
# Accessor for the hash of the label assigned to the each node. You can
|
62
|
+
# label some of the nodes in the graph by passing a hash to the label
|
63
|
+
# and select subgraphs which contain labeled nodes only by subgraph method.
|
64
|
+
#
|
65
|
+
# hash = { 1 => 'red', 2 => 'green', 5 => 'black' }
|
66
|
+
# g.label = hash
|
67
|
+
# g.label
|
68
|
+
# g.subgraph # => new graph consists of the node 1, 2, 5 only
|
69
|
+
#
|
70
|
+
attr_accessor :label
|
71
|
+
|
72
|
+
|
73
|
+
# Returns true or false respond to the internal state of the graph.
|
74
|
+
def directed?
|
75
|
+
@undirected ? false : true
|
76
|
+
end
|
77
|
+
|
78
|
+
# Returns true or false respond to the internal state of the graph.
|
79
|
+
def undirected?
|
80
|
+
@undirected ? true : false
|
81
|
+
end
|
82
|
+
|
83
|
+
# Changes the internal state of the graph from 'undirected' to
|
84
|
+
# 'directed' and re-generate adjacency list. The undirected graph
|
85
|
+
# can be converted to directed graph, however, the edge between two
|
86
|
+
# nodes will be simply doubled to both ends.
|
87
|
+
#
|
88
|
+
# Note: this method can not be used without the list of the
|
89
|
+
# Bio::Relation objects (internally stored in @relations variable).
|
90
|
+
# Thus if you already called clear_relations! method, call
|
91
|
+
# to_relations first.
|
92
|
+
def directed
|
93
|
+
if undirected?
|
94
|
+
@undirected = false
|
95
|
+
self.to_list
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Changes the internal state of the graph from 'directed' to
|
100
|
+
# 'undirected' and re-generate adjacency list.
|
101
|
+
#
|
102
|
+
# Note: this method can not be used without the list of the
|
103
|
+
# Bio::Relation objects (internally stored in @relations variable).
|
104
|
+
# Thus if you already called clear_relations! method, call
|
105
|
+
# to_relations first.
|
106
|
+
def undirected
|
107
|
+
if directed?
|
108
|
+
@undirected = true
|
109
|
+
self.to_list
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Clear @relations array to reduce the memory usage.
|
114
|
+
def clear_relations!
|
115
|
+
@relations.clear
|
116
|
+
end
|
117
|
+
|
118
|
+
# Reconstruct @relations from the adjacency list @graph.
|
119
|
+
def to_relations
|
120
|
+
@relations.clear
|
121
|
+
@graph.each_key do |from|
|
122
|
+
@graph[from].each do |to, w|
|
123
|
+
@relations << Relation.new(from, to, w)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
return @relations
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
# Graph (adjacency list) generation from the Relations
|
131
|
+
#
|
132
|
+
# Generate the adjcancecy list @graph from @relations (called by
|
133
|
+
# initialize and in some other cases when @relations has been changed).
|
134
|
+
def to_list
|
135
|
+
@graph.clear
|
136
|
+
@relations.each do |rel|
|
137
|
+
append(rel, false) # append to @graph without push to @relations
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# Add an Bio::Relation object 'rel' to the @graph and @relations.
|
142
|
+
# If the second argument is false, @relations is not modified (only
|
143
|
+
# useful when genarating @graph from @relations internally).
|
144
|
+
def append(rel, add_rel = true)
|
145
|
+
@relations.push(rel) if add_rel
|
146
|
+
if @graph[rel.from].nil?
|
147
|
+
@graph[rel.from] = {}
|
148
|
+
end
|
149
|
+
if @graph[rel.to].nil?
|
150
|
+
@graph[rel.to] = {}
|
151
|
+
end
|
152
|
+
@graph[rel.from][rel.to] = rel.relation
|
153
|
+
@graph[rel.to][rel.from] = rel.relation if @undirected
|
154
|
+
end
|
155
|
+
|
156
|
+
# Remove an edge indicated by the Bio::Relation object 'rel' from the
|
157
|
+
# @graph and the @relations.
|
158
|
+
def delete(rel)
|
159
|
+
@relations.delete_if do |x|
|
160
|
+
x === rel
|
161
|
+
end
|
162
|
+
@graph[rel.from].delete(rel.to)
|
163
|
+
@graph[rel.to].delete(rel.from) if @undirected
|
164
|
+
end
|
165
|
+
|
166
|
+
# Returns the number of the nodes in the graph.
|
167
|
+
def nodes
|
168
|
+
@graph.keys.length
|
169
|
+
end
|
170
|
+
|
171
|
+
# Returns the number of the edges in the graph.
|
172
|
+
def edges
|
173
|
+
edges = 0
|
174
|
+
@graph.each_value do |v|
|
175
|
+
edges += v.size
|
176
|
+
end
|
177
|
+
edges
|
178
|
+
end
|
179
|
+
|
180
|
+
|
181
|
+
# Convert adjacency list to adjacency matrix
|
182
|
+
#
|
183
|
+
# Returns the adjacency matrix expression of the graph as a Matrix
|
184
|
+
# object. If the first argument was assigned, the matrix will be
|
185
|
+
# filled with the given value. The second argument indicates the
|
186
|
+
# value of the diagonal constituents of the matrix besides the above.
|
187
|
+
#
|
188
|
+
# The result of this method depends on the order of Hash#each
|
189
|
+
# (and each_key, etc.), which may be variable with Ruby version
|
190
|
+
# and Ruby interpreter variations (JRuby, etc.).
|
191
|
+
# For a workaround to remove such dependency, you can use @index
|
192
|
+
# to set order of Hash keys. Note that this behavior might be
|
193
|
+
# changed in the future. Be careful that @index is overwritten by
|
194
|
+
# this method.
|
195
|
+
#
|
196
|
+
def to_matrix(default_value = nil, diagonal_value = nil)
|
197
|
+
|
198
|
+
#--
|
199
|
+
# Note: following code only fills the outer Array with the reference
|
200
|
+
# to the same inner Array object.
|
201
|
+
#
|
202
|
+
# matrix = Array.new(nodes, Array.new(nodes))
|
203
|
+
#
|
204
|
+
# so create a new Array object for each row as follows:
|
205
|
+
#++
|
206
|
+
|
207
|
+
matrix = Array.new
|
208
|
+
nodes.times do
|
209
|
+
matrix.push(Array.new(nodes, default_value))
|
210
|
+
end
|
211
|
+
|
212
|
+
if diagonal_value
|
213
|
+
nodes.times do |i|
|
214
|
+
matrix[i][i] = diagonal_value
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
# assign index number
|
219
|
+
if @index.empty? then
|
220
|
+
# assign index number for each node
|
221
|
+
@graph.keys.each_with_index do |k, i|
|
222
|
+
@index[k] = i
|
223
|
+
end
|
224
|
+
else
|
225
|
+
# begin workaround removing depencency to order of Hash#each
|
226
|
+
# assign index number from the preset @index
|
227
|
+
indices = @index.to_a
|
228
|
+
indices.sort! { |i0, i1| i0[1] <=> i1[1] }
|
229
|
+
indices.collect! { |i0| i0[0] }
|
230
|
+
@index.clear
|
231
|
+
v = 0
|
232
|
+
indices.each do |k, i|
|
233
|
+
if @graph[k] and !@index[k] then
|
234
|
+
@index[k] = v; v += 1
|
235
|
+
end
|
236
|
+
end
|
237
|
+
@graph.each_key do |k|
|
238
|
+
unless @index[k] then
|
239
|
+
@index[k] = v; v += 1
|
240
|
+
end
|
241
|
+
end
|
242
|
+
# end workaround removing depencency to order of Hash#each
|
243
|
+
end
|
244
|
+
|
245
|
+
if @relations.empty? # only used after clear_relations!
|
246
|
+
@graph.each do |from, hash|
|
247
|
+
hash.each do |to, relation|
|
248
|
+
x = @index[from]
|
249
|
+
y = @index[to]
|
250
|
+
matrix[x][y] = relation
|
251
|
+
end
|
252
|
+
end
|
253
|
+
else
|
254
|
+
@relations.each do |rel|
|
255
|
+
x = @index[rel.from]
|
256
|
+
y = @index[rel.to]
|
257
|
+
matrix[x][y] = rel.relation
|
258
|
+
matrix[y][x] = rel.relation if @undirected
|
259
|
+
end
|
260
|
+
end
|
261
|
+
Matrix[*matrix]
|
262
|
+
end
|
263
|
+
|
264
|
+
|
265
|
+
# Pretty printer of the adjacency matrix.
|
266
|
+
#
|
267
|
+
# The dump_matrix method accepts the same arguments as to_matrix.
|
268
|
+
# Useful when you want to check the internal state of the matrix
|
269
|
+
# (for debug purpose etc.) easily.
|
270
|
+
#
|
271
|
+
# This method internally calls to_matrix method.
|
272
|
+
# Read documents of to_matrix for important informations.
|
273
|
+
#
|
274
|
+
def dump_matrix(*arg)
|
275
|
+
matrix = self.to_matrix(*arg)
|
276
|
+
sorted = @index.sort {|a,b| a[1] <=> b[1]}
|
277
|
+
"[# " + sorted.collect{|x| x[0]}.join(", ") + "\n" +
|
278
|
+
matrix.to_a.collect{|row| ' ' + row.inspect}.join(",\n") + "\n]"
|
279
|
+
end
|
280
|
+
|
281
|
+
# Pretty printer of the adjacency list.
|
282
|
+
#
|
283
|
+
# Useful when you want to check the internal state of the adjacency
|
284
|
+
# list (for debug purpose etc.) easily.
|
285
|
+
#
|
286
|
+
# The result of this method depends on the order of Hash#each
|
287
|
+
# (and each_key, etc.), which may be variable with Ruby version
|
288
|
+
# and Ruby interpreter variations (JRuby, etc.).
|
289
|
+
# For a workaround to remove such dependency, you can use @index
|
290
|
+
# to set order of Hash keys. Note that this behavior might be
|
291
|
+
# changed in the future.
|
292
|
+
#
|
293
|
+
def dump_list
|
294
|
+
# begin workaround removing depencency to order of Hash#each
|
295
|
+
if @index.empty? then
|
296
|
+
pref = nil
|
297
|
+
enum = @graph
|
298
|
+
else
|
299
|
+
pref = {}.merge(@index)
|
300
|
+
i = pref.values.max
|
301
|
+
@graph.each_key do |node|
|
302
|
+
pref[node] ||= (i += 1)
|
303
|
+
end
|
304
|
+
graph_to_a = @graph.to_a
|
305
|
+
graph_to_a.sort! { |x, y| pref[x[0]] <=> pref[y[0]] }
|
306
|
+
enum = graph_to_a
|
307
|
+
end
|
308
|
+
# end workaround removing depencency to order of Hash#each
|
309
|
+
|
310
|
+
list = ""
|
311
|
+
enum.each do |from, hash|
|
312
|
+
list << "#{from} => "
|
313
|
+
# begin workaround removing depencency to order of Hash#each
|
314
|
+
if pref then
|
315
|
+
ary = hash.to_a
|
316
|
+
ary.sort! { |x,y| pref[x[0]] <=> pref[y[0]] }
|
317
|
+
hash = ary
|
318
|
+
end
|
319
|
+
# end workaround removing depencency to order of Hash#each
|
320
|
+
a = []
|
321
|
+
hash.each do |to, relation|
|
322
|
+
a.push("#{to} (#{relation})")
|
323
|
+
end
|
324
|
+
list << a.join(", ") + "\n"
|
325
|
+
end
|
326
|
+
list
|
327
|
+
end
|
328
|
+
|
329
|
+
# Select labeled nodes and generate subgraph
|
330
|
+
#
|
331
|
+
# This method select some nodes and returns new Bio::Pathway object
|
332
|
+
# consists of selected nodes only. If the list of the nodes (as
|
333
|
+
# Array) is assigned as the argument, use the list to select the
|
334
|
+
# nodes from the graph. If no argument is assigned, internal
|
335
|
+
# property of the graph @label is used to select the nodes.
|
336
|
+
#
|
337
|
+
# hash = { 'a' => 'secret', 'b' => 'important', 'c' => 'important' }
|
338
|
+
# g.label = hash
|
339
|
+
# g.subgraph
|
340
|
+
# list = [ 'a', 'b', 'c' ]
|
341
|
+
# g.subgraph(list)
|
342
|
+
#
|
343
|
+
def subgraph(list = nil)
|
344
|
+
if list
|
345
|
+
@label.clear
|
346
|
+
list.each do |node|
|
347
|
+
@label[node] = true
|
348
|
+
end
|
349
|
+
end
|
350
|
+
sub_graph = Pathway.new([], @undirected)
|
351
|
+
@graph.each do |from, hash|
|
352
|
+
next unless @label[from]
|
353
|
+
sub_graph.graph[from] ||= {}
|
354
|
+
hash.each do |to, relation|
|
355
|
+
next unless @label[to]
|
356
|
+
sub_graph.append(Relation.new(from, to, relation))
|
357
|
+
end
|
358
|
+
end
|
359
|
+
return sub_graph
|
360
|
+
end
|
361
|
+
|
362
|
+
|
363
|
+
# Not implemented yet.
|
364
|
+
def common_subgraph(graph)
|
365
|
+
raise NotImplementedError
|
366
|
+
end
|
367
|
+
|
368
|
+
|
369
|
+
# Not implemented yet.
|
370
|
+
def clique
|
371
|
+
raise NotImplementedError
|
372
|
+
end
|
373
|
+
|
374
|
+
|
375
|
+
# Returns completeness of the edge density among the surrounded nodes.
|
376
|
+
#
|
377
|
+
# Calculates the value of cliquishness around the 'node'. This value
|
378
|
+
# indicates completeness of the edge density among the surrounded nodes.
|
379
|
+
#
|
380
|
+
# Note: cliquishness (clustering coefficient) for a directed graph
|
381
|
+
# is also calculated.
|
382
|
+
# Reference: http://en.wikipedia.org/wiki/Clustering_coefficient
|
383
|
+
#
|
384
|
+
# Note: Cliquishness (clustering coefficient) for a node that has
|
385
|
+
# only one neighbor node is undefined. Currently, it returns NaN,
|
386
|
+
# but the behavior may be changed in the future.
|
387
|
+
#
|
388
|
+
def cliquishness(node)
|
389
|
+
neighbors = @graph[node].keys
|
390
|
+
sg = subgraph(neighbors)
|
391
|
+
if sg.graph.size != 0
|
392
|
+
edges = sg.edges
|
393
|
+
nodes = neighbors.size
|
394
|
+
complete = (nodes * (nodes - 1))
|
395
|
+
return edges.quo(complete)
|
396
|
+
else
|
397
|
+
return 0.0
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
# Returns frequency of the nodes having same number of edges as hash
|
402
|
+
#
|
403
|
+
# Calculates the frequency of the nodes having the same number of edges
|
404
|
+
# and returns the value as Hash.
|
405
|
+
def small_world
|
406
|
+
freq = Hash.new(0)
|
407
|
+
@graph.each_value do |v|
|
408
|
+
freq[v.size] += 1
|
409
|
+
end
|
410
|
+
return freq
|
411
|
+
end
|
412
|
+
|
413
|
+
# Breadth first search solves steps and path to the each node and
|
414
|
+
# forms a tree contains all reachable vertices from the root node.
|
415
|
+
# This method returns the result in 2 hashes - 1st one shows the
|
416
|
+
# steps from root node and 2nd hash shows the structure of the tree.
|
417
|
+
#
|
418
|
+
# The weight of the edges are not considered in this method.
|
419
|
+
def breadth_first_search(root)
|
420
|
+
visited = {}
|
421
|
+
distance = {}
|
422
|
+
predecessor = {}
|
423
|
+
|
424
|
+
visited[root] = true
|
425
|
+
distance[root] = 0
|
426
|
+
predecessor[root] = nil
|
427
|
+
|
428
|
+
queue = [ root ]
|
429
|
+
|
430
|
+
while from = queue.shift
|
431
|
+
next unless @graph[from]
|
432
|
+
@graph[from].each_key do |to|
|
433
|
+
unless visited[to]
|
434
|
+
visited[to] = true
|
435
|
+
distance[to] = distance[from] + 1
|
436
|
+
predecessor[to] = from
|
437
|
+
queue.push(to)
|
438
|
+
end
|
439
|
+
end
|
440
|
+
end
|
441
|
+
return distance, predecessor
|
442
|
+
end
|
443
|
+
|
444
|
+
# Alias for the breadth_first_search method.
|
445
|
+
alias bfs breadth_first_search
|
446
|
+
|
447
|
+
|
448
|
+
# Calculates the shortest path between two nodes by using
|
449
|
+
# breadth_first_search method and returns steps and the path as Array.
|
450
|
+
def bfs_shortest_path(node1, node2)
|
451
|
+
distance, route = breadth_first_search(node1)
|
452
|
+
step = distance[node2]
|
453
|
+
node = node2
|
454
|
+
path = [ node2 ]
|
455
|
+
while node != node1 and route[node]
|
456
|
+
node = route[node]
|
457
|
+
path.unshift(node)
|
458
|
+
end
|
459
|
+
return step, path
|
460
|
+
end
|
461
|
+
|
462
|
+
|
463
|
+
# Depth first search yields much information about the structure of
|
464
|
+
# the graph especially on the classification of the edges. This
|
465
|
+
# method returns 5 hashes - 1st one shows the timestamps of each
|
466
|
+
# node containing the first discoverd time and the search finished
|
467
|
+
# time in an array. The 2nd, 3rd, 4th, and 5th hashes contain 'tree
|
468
|
+
# edges', 'back edges', 'cross edges', 'forward edges' respectively.
|
469
|
+
#
|
470
|
+
# If $DEBUG is true (e.g. ruby -d), this method prints the progression
|
471
|
+
# of the search.
|
472
|
+
#
|
473
|
+
# The weight of the edges are not considered in this method.
|
474
|
+
#
|
475
|
+
# Note: The result of this method depends on the order of Hash#each
|
476
|
+
# (and each_key, etc.), which may be variable with Ruby version
|
477
|
+
# and Ruby interpreter variations (JRuby, etc.).
|
478
|
+
# For a workaround to remove such dependency, you can use @index
|
479
|
+
# to set order of Hash keys. Note that this bahavior might be
|
480
|
+
# changed in the future.
|
481
|
+
def depth_first_search
|
482
|
+
visited = {}
|
483
|
+
timestamp = {}
|
484
|
+
tree_edges = {}
|
485
|
+
back_edges = {}
|
486
|
+
cross_edges = {}
|
487
|
+
forward_edges = {}
|
488
|
+
count = 0
|
489
|
+
|
490
|
+
# begin workaround removing depencency to order of Hash#each
|
491
|
+
if @index.empty? then
|
492
|
+
preference_of_nodes = nil
|
493
|
+
else
|
494
|
+
preference_of_nodes = {}.merge(@index)
|
495
|
+
i = preference_of_nodes.values.max
|
496
|
+
@graph.each_key do |node0|
|
497
|
+
preference_of_nodes[node0] ||= (i += 1)
|
498
|
+
end
|
499
|
+
end
|
500
|
+
# end workaround removing depencency to order of Hash#each
|
501
|
+
|
502
|
+
dfs_visit = Proc.new { |from|
|
503
|
+
visited[from] = true
|
504
|
+
timestamp[from] = [count += 1]
|
505
|
+
ary = @graph[from].keys
|
506
|
+
# begin workaround removing depencency to order of Hash#each
|
507
|
+
if preference_of_nodes then
|
508
|
+
ary = ary.sort_by { |node0| preference_of_nodes[node0] }
|
509
|
+
end
|
510
|
+
# end workaround removing depencency to order of Hash#each
|
511
|
+
ary.each do |to|
|
512
|
+
if visited[to]
|
513
|
+
if timestamp[to].size > 1
|
514
|
+
if timestamp[from].first < timestamp[to].first
|
515
|
+
# forward edge (black)
|
516
|
+
p "#{from} -> #{to} : forward edge" if $DEBUG
|
517
|
+
forward_edges[from] = to
|
518
|
+
else
|
519
|
+
# cross edge (black)
|
520
|
+
p "#{from} -> #{to} : cross edge" if $DEBUG
|
521
|
+
cross_edges[from] = to
|
522
|
+
end
|
523
|
+
else
|
524
|
+
# back edge (gray)
|
525
|
+
p "#{from} -> #{to} : back edge" if $DEBUG
|
526
|
+
back_edges[from] = to
|
527
|
+
end
|
528
|
+
else
|
529
|
+
# tree edge (white)
|
530
|
+
p "#{from} -> #{to} : tree edge" if $DEBUG
|
531
|
+
tree_edges[to] = from
|
532
|
+
dfs_visit.call(to)
|
533
|
+
end
|
534
|
+
end
|
535
|
+
timestamp[from].push(count += 1)
|
536
|
+
}
|
537
|
+
|
538
|
+
ary = @graph.keys
|
539
|
+
# begin workaround removing depencency to order of Hash#each
|
540
|
+
if preference_of_nodes then
|
541
|
+
ary = ary.sort_by { |node0| preference_of_nodes[node0] }
|
542
|
+
end
|
543
|
+
# end workaround removing depencency to order of Hash#each
|
544
|
+
ary.each do |node|
|
545
|
+
unless visited[node]
|
546
|
+
dfs_visit.call(node)
|
547
|
+
end
|
548
|
+
end
|
549
|
+
return timestamp, tree_edges, back_edges, cross_edges, forward_edges
|
550
|
+
end
|
551
|
+
|
552
|
+
# Alias for the depth_first_search method.
|
553
|
+
alias dfs depth_first_search
|
554
|
+
|
555
|
+
|
556
|
+
# Topological sort of the directed acyclic graphs ("dags") by using
|
557
|
+
# depth_first_search.
|
558
|
+
def dfs_topological_sort
|
559
|
+
# sorted by finished time reversely and collect node names only
|
560
|
+
timestamp, = self.depth_first_search
|
561
|
+
timestamp.sort {|a,b| b[1][1] <=> a[1][1]}.collect {|x| x.first }
|
562
|
+
end
|
563
|
+
|
564
|
+
|
565
|
+
# Dijkstra method to solve the shortest path problem in the weighted graph.
|
566
|
+
def dijkstra(root)
|
567
|
+
distance, predecessor = initialize_single_source(root)
|
568
|
+
@graph[root].each do |k, v|
|
569
|
+
distance[k] = v
|
570
|
+
predecessor[k] = root
|
571
|
+
end
|
572
|
+
queue = distance.dup
|
573
|
+
queue.delete(root)
|
574
|
+
|
575
|
+
while queue.size != 0
|
576
|
+
min = queue.min {|a, b| a[1] <=> b[1]}
|
577
|
+
u = min[0] # extranct a node having minimal distance
|
578
|
+
@graph[u].each do |k, v|
|
579
|
+
# relaxing procedure of root -> 'u' -> 'k'
|
580
|
+
if distance[k] > distance[u] + v
|
581
|
+
distance[k] = distance[u] + v
|
582
|
+
predecessor[k] = u
|
583
|
+
end
|
584
|
+
end
|
585
|
+
queue.delete(u)
|
586
|
+
end
|
587
|
+
return distance, predecessor
|
588
|
+
end
|
589
|
+
|
590
|
+
# Bellman-Ford method for solving the single-source shortest-paths
|
591
|
+
# problem in the graph in which edge weights can be negative.
|
592
|
+
def bellman_ford(root)
|
593
|
+
distance, predecessor = initialize_single_source(root)
|
594
|
+
for i in 1 ..(self.nodes - 1) do
|
595
|
+
@graph.each_key do |u|
|
596
|
+
@graph[u].each do |v, w|
|
597
|
+
# relaxing procedure of root -> 'u' -> 'v'
|
598
|
+
if distance[v] > distance[u] + w
|
599
|
+
distance[v] = distance[u] + w
|
600
|
+
predecessor[v] = u
|
601
|
+
end
|
602
|
+
end
|
603
|
+
end
|
604
|
+
end
|
605
|
+
# negative cyclic loop check
|
606
|
+
@graph.each_key do |u|
|
607
|
+
@graph[u].each do |v, w|
|
608
|
+
if distance[v] > distance[u] + w
|
609
|
+
return false
|
610
|
+
end
|
611
|
+
end
|
612
|
+
end
|
613
|
+
return distance, predecessor
|
614
|
+
end
|
615
|
+
|
616
|
+
|
617
|
+
# Floyd-Wardshall alogrithm for solving the all-pairs shortest-paths
|
618
|
+
# problem on a directed graph G = (V, E).
|
619
|
+
def floyd_warshall
|
620
|
+
inf = 1 / 0.0
|
621
|
+
|
622
|
+
m = self.to_matrix(inf, 0)
|
623
|
+
d = m.dup
|
624
|
+
n = self.nodes
|
625
|
+
for k in 0 .. n - 1 do
|
626
|
+
for i in 0 .. n - 1 do
|
627
|
+
for j in 0 .. n - 1 do
|
628
|
+
if d[i, j] > d[i, k] + d[k, j]
|
629
|
+
d[i, j] = d[i, k] + d[k, j]
|
630
|
+
end
|
631
|
+
end
|
632
|
+
end
|
633
|
+
end
|
634
|
+
return d
|
635
|
+
end
|
636
|
+
|
637
|
+
# Alias for the floyd_warshall method.
|
638
|
+
alias floyd floyd_warshall
|
639
|
+
|
640
|
+
# Kruskal method for finding minimam spaninng trees
|
641
|
+
def kruskal
|
642
|
+
# initialize
|
643
|
+
rel = self.to_relations.sort{|a, b| a <=> b}
|
644
|
+
index = []
|
645
|
+
for i in 0 .. (rel.size - 1) do
|
646
|
+
for j in (i + 1) .. (rel.size - 1) do
|
647
|
+
if rel[i] == rel[j]
|
648
|
+
index << j
|
649
|
+
end
|
650
|
+
end
|
651
|
+
end
|
652
|
+
index.sort{|x, y| y<=>x}.each do |idx|
|
653
|
+
rel[idx, 1] = []
|
654
|
+
end
|
655
|
+
mst = []
|
656
|
+
seen = Hash.new()
|
657
|
+
@graph.each_key do |x|
|
658
|
+
seen[x] = nil
|
659
|
+
end
|
660
|
+
i = 1
|
661
|
+
# initialize end
|
662
|
+
|
663
|
+
rel.each do |r|
|
664
|
+
if seen[r.node[0]] == nil
|
665
|
+
seen[r.node[0]] = 0
|
666
|
+
end
|
667
|
+
if seen[r.node[1]] == nil
|
668
|
+
seen[r.node[1]] = 0
|
669
|
+
end
|
670
|
+
if seen[r.node[0]] == seen[r.node[1]] && seen[r.node[0]] == 0
|
671
|
+
mst << r
|
672
|
+
seen[r.node[0]] = i
|
673
|
+
seen[r.node[1]] = i
|
674
|
+
elsif seen[r.node[0]] != seen[r.node[1]]
|
675
|
+
mst << r
|
676
|
+
v1 = seen[r.node[0]].dup
|
677
|
+
v2 = seen[r.node[1]].dup
|
678
|
+
seen.each do |k, v|
|
679
|
+
if v == v1 || v == v2
|
680
|
+
seen[k] = i
|
681
|
+
end
|
682
|
+
end
|
683
|
+
end
|
684
|
+
i += 1
|
685
|
+
end
|
686
|
+
return Pathway.new(mst)
|
687
|
+
end
|
688
|
+
|
689
|
+
|
690
|
+
private
|
691
|
+
|
692
|
+
|
693
|
+
def initialize_single_source(root)
|
694
|
+
inf = 1 / 0.0 # inf.infinite? -> true
|
695
|
+
|
696
|
+
distance = {}
|
697
|
+
predecessor = {}
|
698
|
+
|
699
|
+
@graph.each_key do |k|
|
700
|
+
distance[k] = inf
|
701
|
+
predecessor[k] = nil
|
702
|
+
end
|
703
|
+
distance[root] = 0
|
704
|
+
return distance, predecessor
|
705
|
+
end
|
706
|
+
|
707
|
+
end # Pathway
|
708
|
+
|
709
|
+
|
710
|
+
|
711
|
+
# Bio::Relation is a simple object storing two nodes and the relation of them.
|
712
|
+
# The nodes and the edge (relation) can be any Ruby object. You can also
|
713
|
+
# compare Bio::Relation objects if the edges have Comparable property.
|
714
|
+
class Relation
|
715
|
+
|
716
|
+
# Create new binary relation object consists of the two object 'node1'
|
717
|
+
# and 'node2' with the 'edge' object as the relation of them.
|
718
|
+
def initialize(node1, node2, edge)
|
719
|
+
@node = [node1, node2]
|
720
|
+
@edge = edge
|
721
|
+
end
|
722
|
+
attr_accessor :node, :edge
|
723
|
+
|
724
|
+
# Returns one node.
|
725
|
+
def from
|
726
|
+
@node[0]
|
727
|
+
end
|
728
|
+
|
729
|
+
# Returns another node.
|
730
|
+
def to
|
731
|
+
@node[1]
|
732
|
+
end
|
733
|
+
|
734
|
+
def relation
|
735
|
+
@edge
|
736
|
+
end
|
737
|
+
|
738
|
+
# Used by eql? method
|
739
|
+
def hash
|
740
|
+
@node.sort.push(@edge).hash
|
741
|
+
end
|
742
|
+
|
743
|
+
# Compare with another Bio::Relation object whether havind same edges
|
744
|
+
# and same nodes. The == method compares Bio::Relation object's id,
|
745
|
+
# however this case equality === method compares the internal property
|
746
|
+
# of the Bio::Relation object.
|
747
|
+
def ===(rel)
|
748
|
+
if self.edge == rel.edge
|
749
|
+
if self.node[0] == rel.node[0] and self.node[1] == rel.node[1]
|
750
|
+
return true
|
751
|
+
elsif self.node[0] == rel.node[1] and self.node[1] == rel.node[0]
|
752
|
+
return true
|
753
|
+
else
|
754
|
+
return false
|
755
|
+
end
|
756
|
+
else
|
757
|
+
return false
|
758
|
+
end
|
759
|
+
end
|
760
|
+
|
761
|
+
# Method eql? is an alias of the === method and is used with hash method
|
762
|
+
# to make uniq arry of the Bio::Relation objects.
|
763
|
+
#
|
764
|
+
# a1 = Bio::Relation.new('a', 'b', 1)
|
765
|
+
# a2 = Bio::Relation.new('b', 'a', 1)
|
766
|
+
# a3 = Bio::Relation.new('b', 'c', 1)
|
767
|
+
# p [ a1, a2, a3 ].uniq
|
768
|
+
alias eql? ===
|
769
|
+
|
770
|
+
# Used by the each method to compare with another Bio::Relation object.
|
771
|
+
# This method is only usable when the edge objects have the property of
|
772
|
+
# the module Comparable.
|
773
|
+
def <=>(rel)
|
774
|
+
unless self.edge.kind_of? Comparable
|
775
|
+
raise "[Error] edges are not comparable"
|
776
|
+
end
|
777
|
+
if self.edge > rel.edge
|
778
|
+
return 1
|
779
|
+
elsif self.edge < rel.edge
|
780
|
+
return -1
|
781
|
+
elsif self.edge == rel.edge
|
782
|
+
return 0
|
783
|
+
end
|
784
|
+
end
|
785
|
+
|
786
|
+
end # Relation
|
787
|
+
|
788
|
+
end # Bio
|
789
|
+
|
790
|
+
|
791
|
+
|
792
|
+
if __FILE__ == $0
|
793
|
+
|
794
|
+
puts "--- Test === method true/false"
|
795
|
+
r1 = Bio::Relation.new('a', 'b', 1)
|
796
|
+
r2 = Bio::Relation.new('b', 'a', 1)
|
797
|
+
r3 = Bio::Relation.new('b', 'a', 2)
|
798
|
+
r4 = Bio::Relation.new('a', 'b', 1)
|
799
|
+
p r1 === r2
|
800
|
+
p r1 === r3
|
801
|
+
p r1 === r4
|
802
|
+
p [ r1, r2, r3, r4 ].uniq
|
803
|
+
p r1.eql?(r2)
|
804
|
+
p r3.eql?(r2)
|
805
|
+
|
806
|
+
# Sample Graph :
|
807
|
+
# +----------------+
|
808
|
+
# | |
|
809
|
+
# v |
|
810
|
+
# +---------(q)-->(t)------->(y)<----(r)
|
811
|
+
# | | | ^ |
|
812
|
+
# v | v | |
|
813
|
+
# +--(s)<--+ | (x)<---+ (u)<-----+
|
814
|
+
# | | | | |
|
815
|
+
# v | | v |
|
816
|
+
# (v)----->(w)<---+ (z)----+
|
817
|
+
|
818
|
+
data = [
|
819
|
+
[ 'q', 's', 1, ],
|
820
|
+
[ 'q', 't', 1, ],
|
821
|
+
[ 'q', 'w', 1, ],
|
822
|
+
[ 'r', 'u', 1, ],
|
823
|
+
[ 'r', 'y', 1, ],
|
824
|
+
[ 's', 'v', 1, ],
|
825
|
+
[ 't', 'x', 1, ],
|
826
|
+
[ 't', 'y', 1, ],
|
827
|
+
[ 'u', 'y', 1, ],
|
828
|
+
[ 'v', 'w', 1, ],
|
829
|
+
[ 'w', 's', 1, ],
|
830
|
+
[ 'x', 'z', 1, ],
|
831
|
+
[ 'y', 'q', 1, ],
|
832
|
+
[ 'z', 'x', 1, ],
|
833
|
+
]
|
834
|
+
|
835
|
+
ary = []
|
836
|
+
|
837
|
+
puts "--- List of relations"
|
838
|
+
data.each do |x|
|
839
|
+
ary << Bio::Relation.new(*x)
|
840
|
+
end
|
841
|
+
p ary
|
842
|
+
|
843
|
+
puts "--- Generate graph from list of relations"
|
844
|
+
graph = Bio::Pathway.new(ary)
|
845
|
+
p graph
|
846
|
+
|
847
|
+
puts "--- Test to_matrix method"
|
848
|
+
p graph.to_matrix
|
849
|
+
|
850
|
+
puts "--- Test dump_matrix method"
|
851
|
+
puts graph.dump_matrix(0)
|
852
|
+
|
853
|
+
puts "--- Test dump_list method"
|
854
|
+
puts graph.dump_list
|
855
|
+
|
856
|
+
puts "--- Labeling some nodes"
|
857
|
+
hash = { 'q' => "L1", 's' => "L2", 'v' => "L3", 'w' => "L4" }
|
858
|
+
graph.label = hash
|
859
|
+
p graph
|
860
|
+
|
861
|
+
puts "--- Extract subgraph by label"
|
862
|
+
p graph.subgraph
|
863
|
+
|
864
|
+
puts "--- Extract subgraph by list"
|
865
|
+
p graph.subgraph(['q', 't', 'x', 'y', 'z'])
|
866
|
+
|
867
|
+
puts "--- Test cliquishness of the node 'q'"
|
868
|
+
p graph.cliquishness('q')
|
869
|
+
|
870
|
+
puts "--- Test cliquishness of the node 'q' (undirected)"
|
871
|
+
u_graph = Bio::Pathway.new(ary, 'undirected')
|
872
|
+
p u_graph.cliquishness('q')
|
873
|
+
|
874
|
+
puts "--- Test small_world histgram"
|
875
|
+
p graph.small_world
|
876
|
+
|
877
|
+
puts "--- Test breadth_first_search method"
|
878
|
+
distance, predecessor = graph.breadth_first_search('q')
|
879
|
+
p distance
|
880
|
+
p predecessor
|
881
|
+
|
882
|
+
puts "--- Test bfs_shortest_path method"
|
883
|
+
step, path = graph.bfs_shortest_path('y', 'w')
|
884
|
+
p step
|
885
|
+
p path
|
886
|
+
|
887
|
+
puts "--- Test depth_first_search method"
|
888
|
+
timestamp, tree, back, cross, forward = graph.depth_first_search
|
889
|
+
p timestamp
|
890
|
+
print "tree edges : "; p tree
|
891
|
+
print "back edges : "; p back
|
892
|
+
print "cross edges : "; p cross
|
893
|
+
print "forward edges : "; p forward
|
894
|
+
|
895
|
+
puts "--- Test dfs_topological_sort method"
|
896
|
+
#
|
897
|
+
# Professor Bumstead topologically sorts his clothing when getting dressed.
|
898
|
+
#
|
899
|
+
# "undershorts" "socks"
|
900
|
+
# | | |
|
901
|
+
# v | v "watch"
|
902
|
+
# "pants" --+-------> "shoes"
|
903
|
+
# |
|
904
|
+
# v
|
905
|
+
# "belt" <----- "shirt" ----> "tie" ----> "jacket"
|
906
|
+
# | ^
|
907
|
+
# `---------------------------------------'
|
908
|
+
#
|
909
|
+
dag = Bio::Pathway.new([
|
910
|
+
Bio::Relation.new("undeershorts", "pants", true),
|
911
|
+
Bio::Relation.new("undeershorts", "shoes", true),
|
912
|
+
Bio::Relation.new("socks", "shoes", true),
|
913
|
+
Bio::Relation.new("watch", "watch", true),
|
914
|
+
Bio::Relation.new("pants", "belt", true),
|
915
|
+
Bio::Relation.new("pants", "shoes", true),
|
916
|
+
Bio::Relation.new("shirt", "belt", true),
|
917
|
+
Bio::Relation.new("shirt", "tie", true),
|
918
|
+
Bio::Relation.new("tie", "jacket", true),
|
919
|
+
Bio::Relation.new("belt", "jacket", true),
|
920
|
+
])
|
921
|
+
p dag.dfs_topological_sort
|
922
|
+
|
923
|
+
puts "--- Test dijkstra method"
|
924
|
+
distance, predecessor = graph.dijkstra('q')
|
925
|
+
p distance
|
926
|
+
p predecessor
|
927
|
+
|
928
|
+
puts "--- Test dijkstra method by weighted graph"
|
929
|
+
#
|
930
|
+
# 'a' --> 'b'
|
931
|
+
# | 1 | 3
|
932
|
+
# |5 v
|
933
|
+
# `----> 'c'
|
934
|
+
#
|
935
|
+
r1 = Bio::Relation.new('a', 'b', 1)
|
936
|
+
r2 = Bio::Relation.new('a', 'c', 5)
|
937
|
+
r3 = Bio::Relation.new('b', 'c', 3)
|
938
|
+
w_graph = Bio::Pathway.new([r1, r2, r3])
|
939
|
+
p w_graph
|
940
|
+
p w_graph.dijkstra('a')
|
941
|
+
|
942
|
+
puts "--- Test bellman_ford method by negative weighted graph"
|
943
|
+
#
|
944
|
+
# ,-- 'a' --> 'b'
|
945
|
+
# | | 1 | 3
|
946
|
+
# | |5 v
|
947
|
+
# | `----> 'c'
|
948
|
+
# | ^
|
949
|
+
# |2 | -5
|
950
|
+
# `--> 'd' ----'
|
951
|
+
#
|
952
|
+
r4 = Bio::Relation.new('a', 'd', 2)
|
953
|
+
r5 = Bio::Relation.new('d', 'c', -5)
|
954
|
+
w_graph.append(r4)
|
955
|
+
w_graph.append(r5)
|
956
|
+
p w_graph.bellman_ford('a')
|
957
|
+
p graph.bellman_ford('q')
|
958
|
+
|
959
|
+
end
|
960
|
+
|