jandot-bio 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (335) hide show
  1. data/bin/bioruby +44 -0
  2. data/bin/br_biofetch.rb +47 -0
  3. data/bin/br_bioflat.rb +282 -0
  4. data/bin/br_biogetseq.rb +45 -0
  5. data/bin/br_pmfetch.rb +421 -0
  6. data/doc/Changes-0.7.rd +369 -0
  7. data/doc/KEGG_API.rd +1843 -0
  8. data/doc/KEGG_API.rd.ja +1834 -0
  9. data/doc/Tutorial.rd +1296 -0
  10. data/doc/Tutorial.rd.ja +2640 -0
  11. data/etc/bioinformatics/seqdatabase.ini +210 -0
  12. data/lib/bio.rb +279 -0
  13. data/lib/bio/alignment.rb +2518 -0
  14. data/lib/bio/appl/bl2seq/report.rb +334 -0
  15. data/lib/bio/appl/blast.rb +351 -0
  16. data/lib/bio/appl/blast/format0.rb +1438 -0
  17. data/lib/bio/appl/blast/format8.rb +83 -0
  18. data/lib/bio/appl/blast/report.rb +516 -0
  19. data/lib/bio/appl/blast/rexml.rb +135 -0
  20. data/lib/bio/appl/blast/rpsblast.rb +176 -0
  21. data/lib/bio/appl/blast/wublast.rb +550 -0
  22. data/lib/bio/appl/blast/xmlparser.rb +228 -0
  23. data/lib/bio/appl/blat/report.rb +489 -0
  24. data/lib/bio/appl/clustalw.rb +219 -0
  25. data/lib/bio/appl/clustalw/report.rb +152 -0
  26. data/lib/bio/appl/emboss.rb +203 -0
  27. data/lib/bio/appl/fasta.rb +237 -0
  28. data/lib/bio/appl/fasta/format10.rb +325 -0
  29. data/lib/bio/appl/gcg/msf.rb +212 -0
  30. data/lib/bio/appl/gcg/seq.rb +195 -0
  31. data/lib/bio/appl/genscan/report.rb +552 -0
  32. data/lib/bio/appl/hmmer.rb +126 -0
  33. data/lib/bio/appl/hmmer/report.rb +683 -0
  34. data/lib/bio/appl/iprscan/report.rb +374 -0
  35. data/lib/bio/appl/mafft.rb +259 -0
  36. data/lib/bio/appl/mafft/report.rb +226 -0
  37. data/lib/bio/appl/muscle.rb +52 -0
  38. data/lib/bio/appl/phylip/alignment.rb +129 -0
  39. data/lib/bio/appl/phylip/distance_matrix.rb +96 -0
  40. data/lib/bio/appl/probcons.rb +41 -0
  41. data/lib/bio/appl/psort.rb +548 -0
  42. data/lib/bio/appl/psort/report.rb +457 -0
  43. data/lib/bio/appl/pts1.rb +263 -0
  44. data/lib/bio/appl/sim4.rb +124 -0
  45. data/lib/bio/appl/sim4/report.rb +485 -0
  46. data/lib/bio/appl/sosui/report.rb +151 -0
  47. data/lib/bio/appl/spidey/report.rb +593 -0
  48. data/lib/bio/appl/targetp/report.rb +267 -0
  49. data/lib/bio/appl/tcoffee.rb +55 -0
  50. data/lib/bio/appl/tmhmm/report.rb +222 -0
  51. data/lib/bio/command.rb +337 -0
  52. data/lib/bio/data/aa.rb +349 -0
  53. data/lib/bio/data/codontable.rb +722 -0
  54. data/lib/bio/data/na.rb +223 -0
  55. data/lib/bio/db.rb +329 -0
  56. data/lib/bio/db/aaindex.rb +357 -0
  57. data/lib/bio/db/embl/common.rb +336 -0
  58. data/lib/bio/db/embl/embl.rb +402 -0
  59. data/lib/bio/db/embl/sptr.rb +1283 -0
  60. data/lib/bio/db/embl/swissprot.rb +42 -0
  61. data/lib/bio/db/embl/trembl.rb +41 -0
  62. data/lib/bio/db/embl/uniprot.rb +42 -0
  63. data/lib/bio/db/fantom.rb +599 -0
  64. data/lib/bio/db/fasta.rb +907 -0
  65. data/lib/bio/db/genbank/common.rb +290 -0
  66. data/lib/bio/db/genbank/ddbj.rb +22 -0
  67. data/lib/bio/db/genbank/genbank.rb +215 -0
  68. data/lib/bio/db/genbank/genpept.rb +60 -0
  69. data/lib/bio/db/genbank/refseq.rb +18 -0
  70. data/lib/bio/db/gff.rb +174 -0
  71. data/lib/bio/db/go.rb +481 -0
  72. data/lib/bio/db/kegg/brite.rb +41 -0
  73. data/lib/bio/db/kegg/compound.rb +131 -0
  74. data/lib/bio/db/kegg/drug.rb +98 -0
  75. data/lib/bio/db/kegg/enzyme.rb +148 -0
  76. data/lib/bio/db/kegg/expression.rb +155 -0
  77. data/lib/bio/db/kegg/genes.rb +263 -0
  78. data/lib/bio/db/kegg/genome.rb +241 -0
  79. data/lib/bio/db/kegg/glycan.rb +170 -0
  80. data/lib/bio/db/kegg/keggtab.rb +357 -0
  81. data/lib/bio/db/kegg/kgml.rb +256 -0
  82. data/lib/bio/db/kegg/orthology.rb +136 -0
  83. data/lib/bio/db/kegg/reaction.rb +82 -0
  84. data/lib/bio/db/kegg/taxonomy.rb +331 -0
  85. data/lib/bio/db/lasergene.rb +209 -0
  86. data/lib/bio/db/litdb.rb +107 -0
  87. data/lib/bio/db/medline.rb +323 -0
  88. data/lib/bio/db/nbrf.rb +191 -0
  89. data/lib/bio/db/newick.rb +658 -0
  90. data/lib/bio/db/nexus.rb +1854 -0
  91. data/lib/bio/db/pdb.rb +29 -0
  92. data/lib/bio/db/pdb/atom.rb +77 -0
  93. data/lib/bio/db/pdb/chain.rb +210 -0
  94. data/lib/bio/db/pdb/chemicalcomponent.rb +224 -0
  95. data/lib/bio/db/pdb/model.rb +148 -0
  96. data/lib/bio/db/pdb/pdb.rb +1911 -0
  97. data/lib/bio/db/pdb/residue.rb +176 -0
  98. data/lib/bio/db/pdb/utils.rb +399 -0
  99. data/lib/bio/db/prosite.rb +597 -0
  100. data/lib/bio/db/rebase.rb +457 -0
  101. data/lib/bio/db/soft.rb +404 -0
  102. data/lib/bio/db/transfac.rb +375 -0
  103. data/lib/bio/feature.rb +226 -0
  104. data/lib/bio/io/das.rb +461 -0
  105. data/lib/bio/io/dbget.rb +194 -0
  106. data/lib/bio/io/ddbjxml.rb +581 -0
  107. data/lib/bio/io/ebisoap.rb +158 -0
  108. data/lib/bio/io/ensembl.rb +229 -0
  109. data/lib/bio/io/fastacmd.rb +163 -0
  110. data/lib/bio/io/fetch.rb +181 -0
  111. data/lib/bio/io/flatfile.rb +1309 -0
  112. data/lib/bio/io/flatfile/bdb.rb +253 -0
  113. data/lib/bio/io/flatfile/index.rb +1371 -0
  114. data/lib/bio/io/flatfile/indexer.rb +787 -0
  115. data/lib/bio/io/higet.rb +73 -0
  116. data/lib/bio/io/hinv.rb +442 -0
  117. data/lib/bio/io/keggapi.rb +805 -0
  118. data/lib/bio/io/ncbirest.rb +256 -0
  119. data/lib/bio/io/ncbisoap.rb +155 -0
  120. data/lib/bio/io/pubmed.rb +307 -0
  121. data/lib/bio/io/registry.rb +292 -0
  122. data/lib/bio/io/soapwsdl.rb +119 -0
  123. data/lib/bio/io/sql.rb +365 -0
  124. data/lib/bio/location.rb +772 -0
  125. data/lib/bio/map.rb +410 -0
  126. data/lib/bio/pathway.rb +854 -0
  127. data/lib/bio/reference.rb +623 -0
  128. data/lib/bio/sequence.rb +475 -0
  129. data/lib/bio/sequence/aa.rb +125 -0
  130. data/lib/bio/sequence/common.rb +333 -0
  131. data/lib/bio/sequence/compat.rb +123 -0
  132. data/lib/bio/sequence/format.rb +181 -0
  133. data/lib/bio/sequence/generic.rb +24 -0
  134. data/lib/bio/sequence/na.rb +491 -0
  135. data/lib/bio/shell.rb +44 -0
  136. data/lib/bio/shell/core.rb +578 -0
  137. data/lib/bio/shell/demo.rb +146 -0
  138. data/lib/bio/shell/interface.rb +218 -0
  139. data/lib/bio/shell/irb.rb +95 -0
  140. data/lib/bio/shell/object.rb +71 -0
  141. data/lib/bio/shell/plugin/blast.rb +42 -0
  142. data/lib/bio/shell/plugin/codon.rb +218 -0
  143. data/lib/bio/shell/plugin/das.rb +58 -0
  144. data/lib/bio/shell/plugin/emboss.rb +23 -0
  145. data/lib/bio/shell/plugin/entry.rb +105 -0
  146. data/lib/bio/shell/plugin/flatfile.rb +101 -0
  147. data/lib/bio/shell/plugin/keggapi.rb +181 -0
  148. data/lib/bio/shell/plugin/midi.rb +430 -0
  149. data/lib/bio/shell/plugin/obda.rb +45 -0
  150. data/lib/bio/shell/plugin/psort.rb +56 -0
  151. data/lib/bio/shell/plugin/seq.rb +247 -0
  152. data/lib/bio/shell/plugin/soap.rb +87 -0
  153. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/bioruby_generator.rb +29 -0
  154. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_classes.rhtml +4 -0
  155. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_log.rhtml +27 -0
  156. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_methods.rhtml +11 -0
  157. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_modules.rhtml +4 -0
  158. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_variables.rhtml +7 -0
  159. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby-bg.gif +0 -0
  160. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby-gem.png +0 -0
  161. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby-link.gif +0 -0
  162. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby.css +368 -0
  163. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby.rhtml +47 -0
  164. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby_controller.rb +144 -0
  165. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby_helper.rb +47 -0
  166. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/commands.rhtml +8 -0
  167. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/history.rhtml +10 -0
  168. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/index.rhtml +26 -0
  169. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/spinner.gif +0 -0
  170. data/lib/bio/shell/script.rb +25 -0
  171. data/lib/bio/shell/setup.rb +109 -0
  172. data/lib/bio/shell/web.rb +102 -0
  173. data/lib/bio/tree.rb +850 -0
  174. data/lib/bio/util/color_scheme.rb +191 -0
  175. data/lib/bio/util/color_scheme/buried.rb +59 -0
  176. data/lib/bio/util/color_scheme/helix.rb +59 -0
  177. data/lib/bio/util/color_scheme/hydropathy.rb +64 -0
  178. data/lib/bio/util/color_scheme/nucleotide.rb +31 -0
  179. data/lib/bio/util/color_scheme/strand.rb +59 -0
  180. data/lib/bio/util/color_scheme/taylor.rb +50 -0
  181. data/lib/bio/util/color_scheme/turn.rb +59 -0
  182. data/lib/bio/util/color_scheme/zappo.rb +50 -0
  183. data/lib/bio/util/contingency_table.rb +370 -0
  184. data/lib/bio/util/restriction_enzyme.rb +228 -0
  185. data/lib/bio/util/restriction_enzyme/analysis.rb +249 -0
  186. data/lib/bio/util/restriction_enzyme/analysis_basic.rb +217 -0
  187. data/lib/bio/util/restriction_enzyme/cut_symbol.rb +107 -0
  188. data/lib/bio/util/restriction_enzyme/double_stranded.rb +321 -0
  189. data/lib/bio/util/restriction_enzyme/double_stranded/aligned_strands.rb +130 -0
  190. data/lib/bio/util/restriction_enzyme/double_stranded/cut_location_pair.rb +103 -0
  191. data/lib/bio/util/restriction_enzyme/double_stranded/cut_location_pair_in_enzyme_notation.rb +38 -0
  192. data/lib/bio/util/restriction_enzyme/double_stranded/cut_locations.rb +76 -0
  193. data/lib/bio/util/restriction_enzyme/double_stranded/cut_locations_in_enzyme_notation.rb +107 -0
  194. data/lib/bio/util/restriction_enzyme/enzymes.yaml +7061 -0
  195. data/lib/bio/util/restriction_enzyme/range/cut_range.rb +24 -0
  196. data/lib/bio/util/restriction_enzyme/range/cut_ranges.rb +47 -0
  197. data/lib/bio/util/restriction_enzyme/range/horizontal_cut_range.rb +67 -0
  198. data/lib/bio/util/restriction_enzyme/range/sequence_range.rb +257 -0
  199. data/lib/bio/util/restriction_enzyme/range/sequence_range/calculated_cuts.rb +242 -0
  200. data/lib/bio/util/restriction_enzyme/range/sequence_range/fragment.rb +51 -0
  201. data/lib/bio/util/restriction_enzyme/range/sequence_range/fragments.rb +41 -0
  202. data/lib/bio/util/restriction_enzyme/range/vertical_cut_range.rb +77 -0
  203. data/lib/bio/util/restriction_enzyme/single_strand.rb +199 -0
  204. data/lib/bio/util/restriction_enzyme/single_strand/cut_locations_in_enzyme_notation.rb +135 -0
  205. data/lib/bio/util/restriction_enzyme/single_strand_complement.rb +23 -0
  206. data/lib/bio/util/restriction_enzyme/string_formatting.rb +111 -0
  207. data/lib/bio/util/sirna.rb +288 -0
  208. data/sample/any2fasta.rb +59 -0
  209. data/sample/biofetch.rb +475 -0
  210. data/sample/color_scheme_na.rb +91 -0
  211. data/sample/dbget +37 -0
  212. data/sample/enzymes.rb +78 -0
  213. data/sample/fasta2tab.rb +99 -0
  214. data/sample/fastagrep.rb +72 -0
  215. data/sample/fastasort.rb +54 -0
  216. data/sample/fsplit.rb +51 -0
  217. data/sample/gb2fasta.rb +30 -0
  218. data/sample/gb2tab.rb +325 -0
  219. data/sample/gbtab2mysql.rb +161 -0
  220. data/sample/genes2nuc.rb +33 -0
  221. data/sample/genes2pep.rb +33 -0
  222. data/sample/genes2tab.rb +81 -0
  223. data/sample/genome2rb.rb +29 -0
  224. data/sample/genome2tab.rb +76 -0
  225. data/sample/goslim.rb +303 -0
  226. data/sample/gt2fasta.rb +47 -0
  227. data/sample/na2aa.rb +34 -0
  228. data/sample/pmfetch.rb +42 -0
  229. data/sample/pmsearch.rb +42 -0
  230. data/sample/psortplot_html.rb +214 -0
  231. data/sample/ssearch2tab.rb +96 -0
  232. data/sample/tdiary.rb +158 -0
  233. data/sample/tfastx2tab.rb +100 -0
  234. data/sample/vs-genes.rb +212 -0
  235. data/test/data/HMMER/hmmpfam.out +64 -0
  236. data/test/data/HMMER/hmmsearch.out +88 -0
  237. data/test/data/SOSUI/sample.report +11 -0
  238. data/test/data/TMHMM/sample.report +21 -0
  239. data/test/data/aaindex/DAYM780301 +30 -0
  240. data/test/data/aaindex/PRAM900102 +20 -0
  241. data/test/data/bl2seq/cd8a_cd8b_blastp.bl2seq +53 -0
  242. data/test/data/bl2seq/cd8a_p53_e-5blastp.bl2seq +37 -0
  243. data/test/data/blast/2.2.15.blastp.m7 +876 -0
  244. data/test/data/blast/b0002.faa +15 -0
  245. data/test/data/blast/b0002.faa.m0 +128 -0
  246. data/test/data/blast/b0002.faa.m7 +65 -0
  247. data/test/data/blast/b0002.faa.m8 +1 -0
  248. data/test/data/embl/AB090716.embl +65 -0
  249. data/test/data/embl/AB090716.embl.rel89 +63 -0
  250. data/test/data/fasta/example1.txt +75 -0
  251. data/test/data/fasta/example2.txt +21 -0
  252. data/test/data/genscan/sample.report +63 -0
  253. data/test/data/iprscan/merged.raw +32 -0
  254. data/test/data/iprscan/merged.txt +74 -0
  255. data/test/data/prosite/prosite.dat +2233 -0
  256. data/test/data/refseq/nm_126355.entret +64 -0
  257. data/test/data/soft/GDS100_partial.soft +92 -0
  258. data/test/data/soft/GSE3457_family_partial.soft +874 -0
  259. data/test/data/uniprot/p53_human.uniprot +1456 -0
  260. data/test/functional/bio/io/test_ensembl.rb +186 -0
  261. data/test/functional/bio/io/test_soapwsdl.rb +52 -0
  262. data/test/runner.rb +14 -0
  263. data/test/unit/bio/appl/bl2seq/test_report.rb +134 -0
  264. data/test/unit/bio/appl/blast/test_report.rb +417 -0
  265. data/test/unit/bio/appl/blast/test_xmlparser.rb +388 -0
  266. data/test/unit/bio/appl/genscan/test_report.rb +182 -0
  267. data/test/unit/bio/appl/hmmer/test_report.rb +342 -0
  268. data/test/unit/bio/appl/iprscan/test_report.rb +338 -0
  269. data/test/unit/bio/appl/mafft/test_report.rb +63 -0
  270. data/test/unit/bio/appl/sosui/test_report.rb +81 -0
  271. data/test/unit/bio/appl/targetp/test_report.rb +146 -0
  272. data/test/unit/bio/appl/test_blast.rb +163 -0
  273. data/test/unit/bio/appl/test_fasta.rb +130 -0
  274. data/test/unit/bio/appl/test_pts1.rb +140 -0
  275. data/test/unit/bio/appl/tmhmm/test_report.rb +126 -0
  276. data/test/unit/bio/data/test_aa.rb +90 -0
  277. data/test/unit/bio/data/test_codontable.rb +107 -0
  278. data/test/unit/bio/data/test_na.rb +80 -0
  279. data/test/unit/bio/db/embl/test_common.rb +117 -0
  280. data/test/unit/bio/db/embl/test_embl.rb +214 -0
  281. data/test/unit/bio/db/embl/test_embl_rel89.rb +219 -0
  282. data/test/unit/bio/db/embl/test_sptr.rb +1775 -0
  283. data/test/unit/bio/db/embl/test_uniprot.rb +31 -0
  284. data/test/unit/bio/db/kegg/test_genes.rb +45 -0
  285. data/test/unit/bio/db/pdb/test_pdb.rb +152 -0
  286. data/test/unit/bio/db/test_aaindex.rb +197 -0
  287. data/test/unit/bio/db/test_fasta.rb +250 -0
  288. data/test/unit/bio/db/test_gff.rb +127 -0
  289. data/test/unit/bio/db/test_lasergene.rb +95 -0
  290. data/test/unit/bio/db/test_newick.rb +293 -0
  291. data/test/unit/bio/db/test_nexus.rb +360 -0
  292. data/test/unit/bio/db/test_prosite.rb +1437 -0
  293. data/test/unit/bio/db/test_rebase.rb +101 -0
  294. data/test/unit/bio/db/test_soft.rb +138 -0
  295. data/test/unit/bio/io/test_ddbjxml.rb +75 -0
  296. data/test/unit/bio/io/test_ensembl.rb +109 -0
  297. data/test/unit/bio/io/test_fastacmd.rb +42 -0
  298. data/test/unit/bio/io/test_flatfile.rb +237 -0
  299. data/test/unit/bio/io/test_soapwsdl.rb +32 -0
  300. data/test/unit/bio/sequence/test_aa.rb +103 -0
  301. data/test/unit/bio/sequence/test_common.rb +174 -0
  302. data/test/unit/bio/sequence/test_compat.rb +69 -0
  303. data/test/unit/bio/sequence/test_na.rb +330 -0
  304. data/test/unit/bio/shell/plugin/test_seq.rb +185 -0
  305. data/test/unit/bio/test_alignment.rb +1025 -0
  306. data/test/unit/bio/test_command.rb +288 -0
  307. data/test/unit/bio/test_db.rb +96 -0
  308. data/test/unit/bio/test_feature.rb +116 -0
  309. data/test/unit/bio/test_location.rb +39 -0
  310. data/test/unit/bio/test_map.rb +230 -0
  311. data/test/unit/bio/test_pathway.rb +473 -0
  312. data/test/unit/bio/test_reference.rb +224 -0
  313. data/test/unit/bio/test_sequence.rb +329 -0
  314. data/test/unit/bio/test_shell.rb +18 -0
  315. data/test/unit/bio/test_tree.rb +593 -0
  316. data/test/unit/bio/util/restriction_enzyme/analysis/test_calculated_cuts.rb +299 -0
  317. data/test/unit/bio/util/restriction_enzyme/analysis/test_cut_ranges.rb +103 -0
  318. data/test/unit/bio/util/restriction_enzyme/analysis/test_sequence_range.rb +240 -0
  319. data/test/unit/bio/util/restriction_enzyme/double_stranded/test_aligned_strands.rb +101 -0
  320. data/test/unit/bio/util/restriction_enzyme/double_stranded/test_cut_location_pair.rb +75 -0
  321. data/test/unit/bio/util/restriction_enzyme/double_stranded/test_cut_location_pair_in_enzyme_notation.rb +73 -0
  322. data/test/unit/bio/util/restriction_enzyme/double_stranded/test_cut_locations.rb +53 -0
  323. data/test/unit/bio/util/restriction_enzyme/double_stranded/test_cut_locations_in_enzyme_notation.rb +104 -0
  324. data/test/unit/bio/util/restriction_enzyme/single_strand/test_cut_locations_in_enzyme_notation.rb +83 -0
  325. data/test/unit/bio/util/restriction_enzyme/test_analysis.rb +246 -0
  326. data/test/unit/bio/util/restriction_enzyme/test_cut_symbol.rb +44 -0
  327. data/test/unit/bio/util/restriction_enzyme/test_double_stranded.rb +115 -0
  328. data/test/unit/bio/util/restriction_enzyme/test_single_strand.rb +147 -0
  329. data/test/unit/bio/util/restriction_enzyme/test_single_strand_complement.rb +147 -0
  330. data/test/unit/bio/util/restriction_enzyme/test_string_formatting.rb +60 -0
  331. data/test/unit/bio/util/test_color_scheme.rb +33 -0
  332. data/test/unit/bio/util/test_contingency_table.rb +94 -0
  333. data/test/unit/bio/util/test_restriction_enzyme.rb +42 -0
  334. data/test/unit/bio/util/test_sirna.rb +245 -0
  335. metadata +479 -0
@@ -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
@@ -0,0 +1,854 @@
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: pathway.rb,v 1.36 2007/04/05 23:35:39 trevor Exp $
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
+ def to_matrix(default_value = nil, diagonal_value = nil)
188
+
189
+ #--
190
+ # Note: following code only fills the outer Array with the reference
191
+ # to the same inner Array object.
192
+ #
193
+ # matrix = Array.new(nodes, Array.new(nodes))
194
+ #
195
+ # so create a new Array object for each row as follows:
196
+ #++
197
+
198
+ matrix = Array.new
199
+ nodes.times do
200
+ matrix.push(Array.new(nodes, default_value))
201
+ end
202
+
203
+ if diagonal_value
204
+ nodes.times do |i|
205
+ matrix[i][i] = diagonal_value
206
+ end
207
+ end
208
+
209
+ # assign index number for each node
210
+ @graph.keys.each_with_index do |k, i|
211
+ @index[k] = i
212
+ end
213
+
214
+ if @relations.empty? # only used after clear_relations!
215
+ @graph.each do |from, hash|
216
+ hash.each do |to, relation|
217
+ x = @index[from]
218
+ y = @index[to]
219
+ matrix[x][y] = relation
220
+ end
221
+ end
222
+ else
223
+ @relations.each do |rel|
224
+ x = @index[rel.from]
225
+ y = @index[rel.to]
226
+ matrix[x][y] = rel.relation
227
+ matrix[y][x] = rel.relation if @undirected
228
+ end
229
+ end
230
+ Matrix[*matrix]
231
+ end
232
+
233
+
234
+ # Pretty printer of the adjacency matrix.
235
+ #
236
+ # The dump_matrix method accepts the same arguments as to_matrix.
237
+ # Useful when you want to check the internal state of the matrix
238
+ # (for debug purpose etc.) easily.
239
+ def dump_matrix(*arg)
240
+ matrix = self.to_matrix(*arg)
241
+ sorted = @index.sort {|a,b| a[1] <=> b[1]}
242
+ "[# " + sorted.collect{|x| x[0]}.join(", ") + "\n" +
243
+ matrix.to_a.collect{|row| ' ' + row.inspect}.join(",\n") + "\n]"
244
+ end
245
+
246
+ # Pretty printer of the adjacency list.
247
+ #
248
+ # The dump_matrix method accepts the same arguments as to_matrix.
249
+ # Useful when you want to check the internal state of the adjacency
250
+ # list (for debug purpose etc.) easily.
251
+ def dump_list
252
+ list = ""
253
+ @graph.each do |from, hash|
254
+ list << "#{from} => "
255
+ a = []
256
+ hash.each do |to, relation|
257
+ a.push("#{to} (#{relation})")
258
+ end
259
+ list << a.join(", ") + "\n"
260
+ end
261
+ list
262
+ end
263
+
264
+ # Select labeled nodes and generate subgraph
265
+ #
266
+ # This method select some nodes and returns new Bio::Pathway object
267
+ # consists of selected nodes only. If the list of the nodes (as
268
+ # Array) is assigned as the argument, use the list to select the
269
+ # nodes from the graph. If no argument is assigned, internal
270
+ # property of the graph @label is used to select the nodes.
271
+ #
272
+ # hash = { 'a' => 'secret', 'b' => 'important', 'c' => 'important' }
273
+ # g.label = hash
274
+ # g.subgraph
275
+ # list = [ 'a', 'b', 'c' ]
276
+ # g.subgraph(list)
277
+ #
278
+ def subgraph(list = nil)
279
+ if list
280
+ @label.clear
281
+ list.each do |node|
282
+ @label[node] = true
283
+ end
284
+ end
285
+ sub_graph = Pathway.new([], @undirected)
286
+ @graph.each do |from, hash|
287
+ next unless @label[from]
288
+ hash.each do |to, relation|
289
+ next unless @label[to]
290
+ sub_graph.append(Relation.new(from, to, relation))
291
+ end
292
+ end
293
+ return sub_graph
294
+ end
295
+
296
+
297
+ # Not implemented yet.
298
+ def common_subgraph(graph)
299
+ raise NotImplementedError
300
+ end
301
+
302
+
303
+ # Not implemented yet.
304
+ def clique
305
+ raise NotImplementedError
306
+ end
307
+
308
+
309
+ # Returns completeness of the edge density among the surrounded nodes.
310
+ #
311
+ # Calculates the value of cliquishness around the 'node'. This value
312
+ # indicates completeness of the edge density among the surrounded nodes.
313
+ def cliquishness(node)
314
+ neighbors = @graph[node].keys
315
+ sg = subgraph(neighbors)
316
+ if sg.graph.size != 0
317
+ edges = sg.edges / 2.0
318
+ nodes = sg.nodes
319
+ complete = (nodes * (nodes - 1)) / 2.0
320
+ return edges/complete
321
+ else
322
+ return 0.0
323
+ end
324
+ end
325
+
326
+ # Returns frequency of the nodes having same number of edges as hash
327
+ #
328
+ # Calculates the frequency of the nodes having the same number of edges
329
+ # and returns the value as Hash.
330
+ def small_world
331
+ freq = Hash.new(0)
332
+ @graph.each_value do |v|
333
+ freq[v.size] += 1
334
+ end
335
+ return freq
336
+ end
337
+
338
+ # Breadth first search solves steps and path to the each node and
339
+ # forms a tree contains all reachable vertices from the root node.
340
+ # This method returns the result in 2 hashes - 1st one shows the
341
+ # steps from root node and 2nd hash shows the structure of the tree.
342
+ #
343
+ # The weight of the edges are not considered in this method.
344
+ def breadth_first_search(root)
345
+ visited = {}
346
+ distance = {}
347
+ predecessor = {}
348
+
349
+ visited[root] = true
350
+ distance[root] = 0
351
+ predecessor[root] = nil
352
+
353
+ queue = [ root ]
354
+
355
+ while from = queue.shift
356
+ next unless @graph[from]
357
+ @graph[from].each_key do |to|
358
+ unless visited[to]
359
+ visited[to] = true
360
+ distance[to] = distance[from] + 1
361
+ predecessor[to] = from
362
+ queue.push(to)
363
+ end
364
+ end
365
+ end
366
+ return distance, predecessor
367
+ end
368
+
369
+ # Alias for the breadth_first_search method.
370
+ alias bfs breadth_first_search
371
+
372
+
373
+ # Calculates the shortest path between two nodes by using
374
+ # breadth_first_search method and returns steps and the path as Array.
375
+ def bfs_shortest_path(node1, node2)
376
+ distance, route = breadth_first_search(node1)
377
+ step = distance[node2]
378
+ node = node2
379
+ path = [ node2 ]
380
+ while node != node1 and route[node]
381
+ node = route[node]
382
+ path.unshift(node)
383
+ end
384
+ return step, path
385
+ end
386
+
387
+
388
+ # Depth first search yields much information about the structure of
389
+ # the graph especially on the classification of the edges. This
390
+ # method returns 5 hashes - 1st one shows the timestamps of each
391
+ # node containing the first discoverd time and the search finished
392
+ # time in an array. The 2nd, 3rd, 4th, and 5th hashes contain 'tree
393
+ # edges', 'back edges', 'cross edges', 'forward edges' respectively.
394
+ #
395
+ # If $DEBUG is true (e.g. ruby -d), this method prints the progression
396
+ # of the search.
397
+ #
398
+ # The weight of the edges are not considered in this method.
399
+ def depth_first_search
400
+ visited = {}
401
+ timestamp = {}
402
+ tree_edges = {}
403
+ back_edges = {}
404
+ cross_edges = {}
405
+ forward_edges = {}
406
+ count = 0
407
+
408
+ dfs_visit = Proc.new { |from|
409
+ visited[from] = true
410
+ timestamp[from] = [count += 1]
411
+ @graph[from].each_key do |to|
412
+ if visited[to]
413
+ if timestamp[to].size > 1
414
+ if timestamp[from].first < timestamp[to].first
415
+ # forward edge (black)
416
+ p "#{from} -> #{to} : forward edge" if $DEBUG
417
+ forward_edges[from] = to
418
+ else
419
+ # cross edge (black)
420
+ p "#{from} -> #{to} : cross edge" if $DEBUG
421
+ cross_edges[from] = to
422
+ end
423
+ else
424
+ # back edge (gray)
425
+ p "#{from} -> #{to} : back edge" if $DEBUG
426
+ back_edges[from] = to
427
+ end
428
+ else
429
+ # tree edge (white)
430
+ p "#{from} -> #{to} : tree edge" if $DEBUG
431
+ tree_edges[to] = from
432
+ dfs_visit.call(to)
433
+ end
434
+ end
435
+ timestamp[from].push(count += 1)
436
+ }
437
+
438
+ @graph.each_key do |node|
439
+ unless visited[node]
440
+ dfs_visit.call(node)
441
+ end
442
+ end
443
+ return timestamp, tree_edges, back_edges, cross_edges, forward_edges
444
+ end
445
+
446
+ # Alias for the depth_first_search method.
447
+ alias dfs depth_first_search
448
+
449
+
450
+ # Topological sort of the directed acyclic graphs ("dags") by using
451
+ # depth_first_search.
452
+ def dfs_topological_sort
453
+ # sorted by finished time reversely and collect node names only
454
+ timestamp, = self.depth_first_search
455
+ timestamp.sort {|a,b| b[1][1] <=> a[1][1]}.collect {|x| x.first }
456
+ end
457
+
458
+
459
+ # Dijkstra method to solve the shortest path problem in the weighted graph.
460
+ def dijkstra(root)
461
+ distance, predecessor = initialize_single_source(root)
462
+ @graph[root].each do |k, v|
463
+ distance[k] = v
464
+ predecessor[k] = root
465
+ end
466
+ queue = distance.dup
467
+ queue.delete(root)
468
+
469
+ while queue.size != 0
470
+ min = queue.min {|a, b| a[1] <=> b[1]}
471
+ u = min[0] # extranct a node having minimal distance
472
+ @graph[u].each do |k, v|
473
+ # relaxing procedure of root -> 'u' -> 'k'
474
+ if distance[k] > distance[u] + v
475
+ distance[k] = distance[u] + v
476
+ predecessor[k] = u
477
+ end
478
+ end
479
+ queue.delete(u)
480
+ end
481
+ return distance, predecessor
482
+ end
483
+
484
+ # Bellman-Ford method for solving the single-source shortest-paths
485
+ # problem in the graph in which edge weights can be negative.
486
+ def bellman_ford(root)
487
+ distance, predecessor = initialize_single_source(root)
488
+ for i in 1 ..(self.nodes - 1) do
489
+ @graph.each_key do |u|
490
+ @graph[u].each do |v, w|
491
+ # relaxing procedure of root -> 'u' -> 'v'
492
+ if distance[v] > distance[u] + w
493
+ distance[v] = distance[u] + w
494
+ predecessor[v] = u
495
+ end
496
+ end
497
+ end
498
+ end
499
+ # negative cyclic loop check
500
+ @graph.each_key do |u|
501
+ @graph[u].each do |v, w|
502
+ if distance[v] > distance[u] + w
503
+ return false
504
+ end
505
+ end
506
+ end
507
+ return distance, predecessor
508
+ end
509
+
510
+
511
+ # Floyd-Wardshall alogrithm for solving the all-pairs shortest-paths
512
+ # problem on a directed graph G = (V, E).
513
+ def floyd_warshall
514
+ inf = 1 / 0.0
515
+
516
+ m = self.to_matrix(inf, 0)
517
+ d = m.dup
518
+ n = self.nodes
519
+ for k in 0 .. n - 1 do
520
+ for i in 0 .. n - 1 do
521
+ for j in 0 .. n - 1 do
522
+ if d[i, j] > d[i, k] + d[k, j]
523
+ d[i, j] = d[i, k] + d[k, j]
524
+ end
525
+ end
526
+ end
527
+ end
528
+ return d
529
+ end
530
+
531
+ # Alias for the floyd_warshall method.
532
+ alias floyd floyd_warshall
533
+
534
+ # Kruskal method for finding minimam spaninng trees
535
+ def kruskal
536
+ # initialize
537
+ rel = self.to_relations.sort{|a, b| a <=> b}
538
+ index = []
539
+ for i in 0 .. (rel.size - 1) do
540
+ for j in (i + 1) .. (rel.size - 1) do
541
+ if rel[i] == rel[j]
542
+ index << j
543
+ end
544
+ end
545
+ end
546
+ index.sort{|x, y| y<=>x}.each do |i|
547
+ rel[i, 1] = []
548
+ end
549
+ mst = []
550
+ seen = Hash.new()
551
+ @graph.each_key do |x|
552
+ seen[x] = nil
553
+ end
554
+ i = 1
555
+ # initialize end
556
+
557
+ rel.each do |r|
558
+ if seen[r.node[0]] == nil
559
+ seen[r.node[0]] = 0
560
+ end
561
+ if seen[r.node[1]] == nil
562
+ seen[r.node[1]] = 0
563
+ end
564
+ if seen[r.node[0]] == seen[r.node[1]] && seen[r.node[0]] == 0
565
+ mst << r
566
+ seen[r.node[0]] = i
567
+ seen[r.node[1]] = i
568
+ elsif seen[r.node[0]] != seen[r.node[1]]
569
+ mst << r
570
+ v1 = seen[r.node[0]].dup
571
+ v2 = seen[r.node[1]].dup
572
+ seen.each do |k, v|
573
+ if v == v1 || v == v2
574
+ seen[k] = i
575
+ end
576
+ end
577
+ end
578
+ i += 1
579
+ end
580
+ return Pathway.new(mst)
581
+ end
582
+
583
+
584
+ private
585
+
586
+
587
+ def initialize_single_source(root)
588
+ inf = 1 / 0.0 # inf.infinite? -> true
589
+
590
+ distance = {}
591
+ predecessor = {}
592
+
593
+ @graph.each_key do |k|
594
+ distance[k] = inf
595
+ predecessor[k] = nil
596
+ end
597
+ distance[root] = 0
598
+ return distance, predecessor
599
+ end
600
+
601
+ end # Pathway
602
+
603
+
604
+
605
+ # Bio::Relation is a simple object storing two nodes and the relation of them.
606
+ # The nodes and the edge (relation) can be any Ruby object. You can also
607
+ # compare Bio::Relation objects if the edges have Comparable property.
608
+ class Relation
609
+
610
+ # Create new binary relation object consists of the two object 'node1'
611
+ # and 'node2' with the 'edge' object as the relation of them.
612
+ def initialize(node1, node2, edge)
613
+ @node = [node1, node2]
614
+ @edge = edge
615
+ end
616
+ attr_accessor :node, :edge
617
+
618
+ # Returns one node.
619
+ def from
620
+ @node[0]
621
+ end
622
+
623
+ # Returns another node.
624
+ def to
625
+ @node[1]
626
+ end
627
+
628
+ def relation
629
+ @edge
630
+ end
631
+
632
+ # Used by eql? method
633
+ def hash
634
+ @node.sort.push(@edge).hash
635
+ end
636
+
637
+ # Compare with another Bio::Relation object whether havind same edges
638
+ # and same nodes. The == method compares Bio::Relation object's id,
639
+ # however this case equality === method compares the internal property
640
+ # of the Bio::Relation object.
641
+ def ===(rel)
642
+ if self.edge == rel.edge
643
+ if self.node[0] == rel.node[0] and self.node[1] == rel.node[1]
644
+ return true
645
+ elsif self.node[0] == rel.node[1] and self.node[1] == rel.node[0]
646
+ return true
647
+ else
648
+ return false
649
+ end
650
+ else
651
+ return false
652
+ end
653
+ end
654
+
655
+ # Method eql? is an alias of the === method and is used with hash method
656
+ # to make uniq arry of the Bio::Relation objects.
657
+ #
658
+ # a1 = Bio::Relation.new('a', 'b', 1)
659
+ # a2 = Bio::Relation.new('b', 'a', 1)
660
+ # a3 = Bio::Relation.new('b', 'c', 1)
661
+ # p [ a1, a2, a3 ].uniq
662
+ alias eql? ===
663
+
664
+ # Used by the each method to compare with another Bio::Relation object.
665
+ # This method is only usable when the edge objects have the property of
666
+ # the module Comparable.
667
+ def <=>(rel)
668
+ unless self.edge.kind_of? Comparable
669
+ raise "[Error] edges are not comparable"
670
+ end
671
+ if self.edge > rel.edge
672
+ return 1
673
+ elsif self.edge < rel.edge
674
+ return -1
675
+ elsif self.edge == rel.edge
676
+ return 0
677
+ end
678
+ end
679
+
680
+ end # Relation
681
+
682
+ end # Bio
683
+
684
+
685
+
686
+ if __FILE__ == $0
687
+
688
+ puts "--- Test === method true/false"
689
+ r1 = Bio::Relation.new('a', 'b', 1)
690
+ r2 = Bio::Relation.new('b', 'a', 1)
691
+ r3 = Bio::Relation.new('b', 'a', 2)
692
+ r4 = Bio::Relation.new('a', 'b', 1)
693
+ p r1 === r2
694
+ p r1 === r3
695
+ p r1 === r4
696
+ p [ r1, r2, r3, r4 ].uniq
697
+ p r1.eql?(r2)
698
+ p r3.eql?(r2)
699
+
700
+ # Sample Graph :
701
+ # +----------------+
702
+ # | |
703
+ # v |
704
+ # +---------(q)-->(t)------->(y)<----(r)
705
+ # | | | ^ |
706
+ # v | v | |
707
+ # +--(s)<--+ | (x)<---+ (u)<-----+
708
+ # | | | | |
709
+ # v | | v |
710
+ # (v)----->(w)<---+ (z)----+
711
+
712
+ data = [
713
+ [ 'q', 's', 1, ],
714
+ [ 'q', 't', 1, ],
715
+ [ 'q', 'w', 1, ],
716
+ [ 'r', 'u', 1, ],
717
+ [ 'r', 'y', 1, ],
718
+ [ 's', 'v', 1, ],
719
+ [ 't', 'x', 1, ],
720
+ [ 't', 'y', 1, ],
721
+ [ 'u', 'y', 1, ],
722
+ [ 'v', 'w', 1, ],
723
+ [ 'w', 's', 1, ],
724
+ [ 'x', 'z', 1, ],
725
+ [ 'y', 'q', 1, ],
726
+ [ 'z', 'x', 1, ],
727
+ ]
728
+
729
+ ary = []
730
+
731
+ puts "--- List of relations"
732
+ data.each do |x|
733
+ ary << Bio::Relation.new(*x)
734
+ end
735
+ p ary
736
+
737
+ puts "--- Generate graph from list of relations"
738
+ graph = Bio::Pathway.new(ary)
739
+ p graph
740
+
741
+ puts "--- Test to_matrix method"
742
+ p graph.to_matrix
743
+
744
+ puts "--- Test dump_matrix method"
745
+ puts graph.dump_matrix(0)
746
+
747
+ puts "--- Test dump_list method"
748
+ puts graph.dump_list
749
+
750
+ puts "--- Labeling some nodes"
751
+ hash = { 'q' => "L1", 's' => "L2", 'v' => "L3", 'w' => "L4" }
752
+ graph.label = hash
753
+ p graph
754
+
755
+ puts "--- Extract subgraph by label"
756
+ p graph.subgraph
757
+
758
+ puts "--- Extract subgraph by list"
759
+ p graph.subgraph(['q', 't', 'x', 'y', 'z'])
760
+
761
+ puts "--- Test cliquishness of the node 'q'"
762
+ p graph.cliquishness('q')
763
+
764
+ puts "--- Test cliquishness of the node 'q' (undirected)"
765
+ u_graph = Bio::Pathway.new(ary, 'undirected')
766
+ p u_graph.cliquishness('q')
767
+
768
+ puts "--- Test small_world histgram"
769
+ p graph.small_world
770
+
771
+ puts "--- Test breadth_first_search method"
772
+ distance, predecessor = graph.breadth_first_search('q')
773
+ p distance
774
+ p predecessor
775
+
776
+ puts "--- Test bfs_shortest_path method"
777
+ step, path = graph.bfs_shortest_path('y', 'w')
778
+ p step
779
+ p path
780
+
781
+ puts "--- Test depth_first_search method"
782
+ timestamp, tree, back, cross, forward = graph.depth_first_search
783
+ p timestamp
784
+ print "tree edges : "; p tree
785
+ print "back edges : "; p back
786
+ print "cross edges : "; p cross
787
+ print "forward edges : "; p forward
788
+
789
+ puts "--- Test dfs_topological_sort method"
790
+ #
791
+ # Professor Bumstead topologically sorts his clothing when getting dressed.
792
+ #
793
+ # "undershorts" "socks"
794
+ # | | |
795
+ # v | v "watch"
796
+ # "pants" --+-------> "shoes"
797
+ # |
798
+ # v
799
+ # "belt" <----- "shirt" ----> "tie" ----> "jacket"
800
+ # | ^
801
+ # `---------------------------------------'
802
+ #
803
+ dag = Bio::Pathway.new([
804
+ Bio::Relation.new("undeershorts", "pants", true),
805
+ Bio::Relation.new("undeershorts", "shoes", true),
806
+ Bio::Relation.new("socks", "shoes", true),
807
+ Bio::Relation.new("watch", "watch", true),
808
+ Bio::Relation.new("pants", "belt", true),
809
+ Bio::Relation.new("pants", "shoes", true),
810
+ Bio::Relation.new("shirt", "belt", true),
811
+ Bio::Relation.new("shirt", "tie", true),
812
+ Bio::Relation.new("tie", "jacket", true),
813
+ Bio::Relation.new("belt", "jacket", true),
814
+ ])
815
+ p dag.dfs_topological_sort
816
+
817
+ puts "--- Test dijkstra method"
818
+ distance, predecessor = graph.dijkstra('q')
819
+ p distance
820
+ p predecessor
821
+
822
+ puts "--- Test dijkstra method by weighted graph"
823
+ #
824
+ # 'a' --> 'b'
825
+ # | 1 | 3
826
+ # |5 v
827
+ # `----> 'c'
828
+ #
829
+ r1 = Bio::Relation.new('a', 'b', 1)
830
+ r2 = Bio::Relation.new('a', 'c', 5)
831
+ r3 = Bio::Relation.new('b', 'c', 3)
832
+ w_graph = Bio::Pathway.new([r1, r2, r3])
833
+ p w_graph
834
+ p w_graph.dijkstra('a')
835
+
836
+ puts "--- Test bellman_ford method by negative weighted graph"
837
+ #
838
+ # ,-- 'a' --> 'b'
839
+ # | | 1 | 3
840
+ # | |5 v
841
+ # | `----> 'c'
842
+ # | ^
843
+ # |2 | -5
844
+ # `--> 'd' ----'
845
+ #
846
+ r4 = Bio::Relation.new('a', 'd', 2)
847
+ r5 = Bio::Relation.new('d', 'c', -5)
848
+ w_graph.append(r4)
849
+ w_graph.append(r5)
850
+ p w_graph.bellman_ford('a')
851
+ p graph.bellman_ford('q')
852
+
853
+ end
854
+