wwood-bioruby 1.2.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (386) hide show
  1. data/README.rdoc +205 -0
  2. data/README_DEV.rdoc +285 -0
  3. data/VERSION.yml +4 -0
  4. data/bin/bioruby +44 -0
  5. data/bin/br_biofetch.rb +47 -0
  6. data/bin/br_bioflat.rb +293 -0
  7. data/bin/br_biogetseq.rb +45 -0
  8. data/bin/br_pmfetch.rb +421 -0
  9. data/lib/bio.rb +306 -0
  10. data/lib/bio/alignment.rb +2518 -0
  11. data/lib/bio/appl/bl2seq/report.rb +334 -0
  12. data/lib/bio/appl/blast.rb +505 -0
  13. data/lib/bio/appl/blast/ddbj.rb +142 -0
  14. data/lib/bio/appl/blast/format0.rb +1438 -0
  15. data/lib/bio/appl/blast/format8.rb +83 -0
  16. data/lib/bio/appl/blast/genomenet.rb +263 -0
  17. data/lib/bio/appl/blast/ncbioptions.rb +220 -0
  18. data/lib/bio/appl/blast/remote.rb +105 -0
  19. data/lib/bio/appl/blast/report.rb +767 -0
  20. data/lib/bio/appl/blast/rexml.rb +144 -0
  21. data/lib/bio/appl/blast/rpsblast.rb +277 -0
  22. data/lib/bio/appl/blast/wublast.rb +635 -0
  23. data/lib/bio/appl/blast/xmlparser.rb +236 -0
  24. data/lib/bio/appl/blat/report.rb +530 -0
  25. data/lib/bio/appl/clustalw.rb +219 -0
  26. data/lib/bio/appl/clustalw/report.rb +152 -0
  27. data/lib/bio/appl/emboss.rb +203 -0
  28. data/lib/bio/appl/fasta.rb +235 -0
  29. data/lib/bio/appl/fasta/format10.rb +325 -0
  30. data/lib/bio/appl/gcg/msf.rb +212 -0
  31. data/lib/bio/appl/gcg/seq.rb +195 -0
  32. data/lib/bio/appl/genscan/report.rb +552 -0
  33. data/lib/bio/appl/hmmer.rb +126 -0
  34. data/lib/bio/appl/hmmer/report.rb +683 -0
  35. data/lib/bio/appl/iprscan/report.rb +374 -0
  36. data/lib/bio/appl/mafft.rb +259 -0
  37. data/lib/bio/appl/mafft/report.rb +226 -0
  38. data/lib/bio/appl/muscle.rb +52 -0
  39. data/lib/bio/appl/paml/baseml.rb +95 -0
  40. data/lib/bio/appl/paml/baseml/report.rb +32 -0
  41. data/lib/bio/appl/paml/codeml.rb +242 -0
  42. data/lib/bio/appl/paml/codeml/rates.rb +67 -0
  43. data/lib/bio/appl/paml/codeml/report.rb +67 -0
  44. data/lib/bio/appl/paml/common.rb +348 -0
  45. data/lib/bio/appl/paml/common_report.rb +38 -0
  46. data/lib/bio/appl/paml/yn00.rb +103 -0
  47. data/lib/bio/appl/paml/yn00/report.rb +32 -0
  48. data/lib/bio/appl/phylip/alignment.rb +133 -0
  49. data/lib/bio/appl/phylip/distance_matrix.rb +96 -0
  50. data/lib/bio/appl/probcons.rb +41 -0
  51. data/lib/bio/appl/psort.rb +548 -0
  52. data/lib/bio/appl/psort/report.rb +542 -0
  53. data/lib/bio/appl/pts1.rb +263 -0
  54. data/lib/bio/appl/sim4.rb +124 -0
  55. data/lib/bio/appl/sim4/report.rb +485 -0
  56. data/lib/bio/appl/sosui/report.rb +151 -0
  57. data/lib/bio/appl/spidey/report.rb +593 -0
  58. data/lib/bio/appl/targetp/report.rb +267 -0
  59. data/lib/bio/appl/tcoffee.rb +55 -0
  60. data/lib/bio/appl/tmhmm/report.rb +231 -0
  61. data/lib/bio/command.rb +593 -0
  62. data/lib/bio/compat/features.rb +157 -0
  63. data/lib/bio/compat/references.rb +128 -0
  64. data/lib/bio/data/aa.rb +353 -0
  65. data/lib/bio/data/codontable.rb +722 -0
  66. data/lib/bio/data/na.rb +223 -0
  67. data/lib/bio/db.rb +329 -0
  68. data/lib/bio/db/aaindex.rb +357 -0
  69. data/lib/bio/db/biosql/biosql_to_biosequence.rb +67 -0
  70. data/lib/bio/db/biosql/sequence.rb +508 -0
  71. data/lib/bio/db/embl/common.rb +352 -0
  72. data/lib/bio/db/embl/embl.rb +500 -0
  73. data/lib/bio/db/embl/embl_to_biosequence.rb +85 -0
  74. data/lib/bio/db/embl/format_embl.rb +190 -0
  75. data/lib/bio/db/embl/sptr.rb +1283 -0
  76. data/lib/bio/db/embl/swissprot.rb +42 -0
  77. data/lib/bio/db/embl/trembl.rb +41 -0
  78. data/lib/bio/db/embl/uniprot.rb +42 -0
  79. data/lib/bio/db/fantom.rb +597 -0
  80. data/lib/bio/db/fasta.rb +410 -0
  81. data/lib/bio/db/fasta/defline.rb +532 -0
  82. data/lib/bio/db/fasta/fasta_to_biosequence.rb +63 -0
  83. data/lib/bio/db/fasta/format_fasta.rb +97 -0
  84. data/lib/bio/db/genbank/common.rb +307 -0
  85. data/lib/bio/db/genbank/ddbj.rb +22 -0
  86. data/lib/bio/db/genbank/format_genbank.rb +187 -0
  87. data/lib/bio/db/genbank/genbank.rb +250 -0
  88. data/lib/bio/db/genbank/genbank_to_biosequence.rb +86 -0
  89. data/lib/bio/db/genbank/genpept.rb +60 -0
  90. data/lib/bio/db/genbank/refseq.rb +18 -0
  91. data/lib/bio/db/gff.rb +1846 -0
  92. data/lib/bio/db/go.rb +481 -0
  93. data/lib/bio/db/kegg/brite.rb +41 -0
  94. data/lib/bio/db/kegg/compound.rb +131 -0
  95. data/lib/bio/db/kegg/drug.rb +98 -0
  96. data/lib/bio/db/kegg/enzyme.rb +148 -0
  97. data/lib/bio/db/kegg/expression.rb +155 -0
  98. data/lib/bio/db/kegg/genes.rb +263 -0
  99. data/lib/bio/db/kegg/genome.rb +241 -0
  100. data/lib/bio/db/kegg/glycan.rb +166 -0
  101. data/lib/bio/db/kegg/keggtab.rb +357 -0
  102. data/lib/bio/db/kegg/kgml.rb +256 -0
  103. data/lib/bio/db/kegg/orthology.rb +136 -0
  104. data/lib/bio/db/kegg/reaction.rb +82 -0
  105. data/lib/bio/db/kegg/taxonomy.rb +331 -0
  106. data/lib/bio/db/lasergene.rb +209 -0
  107. data/lib/bio/db/litdb.rb +107 -0
  108. data/lib/bio/db/medline.rb +326 -0
  109. data/lib/bio/db/nbrf.rb +191 -0
  110. data/lib/bio/db/newick.rb +658 -0
  111. data/lib/bio/db/nexus.rb +1854 -0
  112. data/lib/bio/db/pdb.rb +29 -0
  113. data/lib/bio/db/pdb/atom.rb +77 -0
  114. data/lib/bio/db/pdb/chain.rb +210 -0
  115. data/lib/bio/db/pdb/chemicalcomponent.rb +224 -0
  116. data/lib/bio/db/pdb/model.rb +148 -0
  117. data/lib/bio/db/pdb/pdb.rb +1911 -0
  118. data/lib/bio/db/pdb/residue.rb +176 -0
  119. data/lib/bio/db/pdb/utils.rb +399 -0
  120. data/lib/bio/db/prosite.rb +597 -0
  121. data/lib/bio/db/rebase.rb +456 -0
  122. data/lib/bio/db/soft.rb +404 -0
  123. data/lib/bio/db/transfac.rb +375 -0
  124. data/lib/bio/db/url.rb +42 -0
  125. data/lib/bio/feature.rb +139 -0
  126. data/lib/bio/io/biosql/biodatabase.rb +64 -0
  127. data/lib/bio/io/biosql/bioentry.rb +29 -0
  128. data/lib/bio/io/biosql/bioentry_dbxref.rb +11 -0
  129. data/lib/bio/io/biosql/bioentry_path.rb +12 -0
  130. data/lib/bio/io/biosql/bioentry_qualifier_value.rb +10 -0
  131. data/lib/bio/io/biosql/bioentry_reference.rb +10 -0
  132. data/lib/bio/io/biosql/bioentry_relationship.rb +10 -0
  133. data/lib/bio/io/biosql/biosequence.rb +11 -0
  134. data/lib/bio/io/biosql/comment.rb +7 -0
  135. data/lib/bio/io/biosql/config/database.yml +20 -0
  136. data/lib/bio/io/biosql/dbxref.rb +13 -0
  137. data/lib/bio/io/biosql/dbxref_qualifier_value.rb +12 -0
  138. data/lib/bio/io/biosql/location.rb +32 -0
  139. data/lib/bio/io/biosql/location_qualifier_value.rb +11 -0
  140. data/lib/bio/io/biosql/ontology.rb +10 -0
  141. data/lib/bio/io/biosql/reference.rb +9 -0
  142. data/lib/bio/io/biosql/seqfeature.rb +32 -0
  143. data/lib/bio/io/biosql/seqfeature_dbxref.rb +11 -0
  144. data/lib/bio/io/biosql/seqfeature_path.rb +11 -0
  145. data/lib/bio/io/biosql/seqfeature_qualifier_value.rb +20 -0
  146. data/lib/bio/io/biosql/seqfeature_relationship.rb +11 -0
  147. data/lib/bio/io/biosql/taxon.rb +12 -0
  148. data/lib/bio/io/biosql/taxon_name.rb +9 -0
  149. data/lib/bio/io/biosql/term.rb +27 -0
  150. data/lib/bio/io/biosql/term_dbxref.rb +11 -0
  151. data/lib/bio/io/biosql/term_path.rb +12 -0
  152. data/lib/bio/io/biosql/term_relationship.rb +13 -0
  153. data/lib/bio/io/biosql/term_relationship_term.rb +11 -0
  154. data/lib/bio/io/biosql/term_synonym.rb +10 -0
  155. data/lib/bio/io/das.rb +461 -0
  156. data/lib/bio/io/dbget.rb +194 -0
  157. data/lib/bio/io/ddbjxml.rb +638 -0
  158. data/lib/bio/io/ebisoap.rb +158 -0
  159. data/lib/bio/io/ensembl.rb +229 -0
  160. data/lib/bio/io/fastacmd.rb +163 -0
  161. data/lib/bio/io/fetch.rb +195 -0
  162. data/lib/bio/io/flatfile.rb +482 -0
  163. data/lib/bio/io/flatfile/autodetection.rb +545 -0
  164. data/lib/bio/io/flatfile/bdb.rb +253 -0
  165. data/lib/bio/io/flatfile/buffer.rb +237 -0
  166. data/lib/bio/io/flatfile/index.rb +1381 -0
  167. data/lib/bio/io/flatfile/indexer.rb +805 -0
  168. data/lib/bio/io/flatfile/splitter.rb +297 -0
  169. data/lib/bio/io/higet.rb +73 -0
  170. data/lib/bio/io/hinv.rb +442 -0
  171. data/lib/bio/io/keggapi.rb +805 -0
  172. data/lib/bio/io/ncbirest.rb +733 -0
  173. data/lib/bio/io/ncbisoap.rb +155 -0
  174. data/lib/bio/io/pubmed.rb +307 -0
  175. data/lib/bio/io/registry.rb +292 -0
  176. data/lib/bio/io/soapwsdl.rb +119 -0
  177. data/lib/bio/io/sql.rb +186 -0
  178. data/lib/bio/location.rb +867 -0
  179. data/lib/bio/map.rb +410 -0
  180. data/lib/bio/pathway.rb +960 -0
  181. data/lib/bio/reference.rb +602 -0
  182. data/lib/bio/sequence.rb +456 -0
  183. data/lib/bio/sequence/aa.rb +152 -0
  184. data/lib/bio/sequence/adapter.rb +108 -0
  185. data/lib/bio/sequence/common.rb +310 -0
  186. data/lib/bio/sequence/compat.rb +123 -0
  187. data/lib/bio/sequence/dblink.rb +54 -0
  188. data/lib/bio/sequence/format.rb +358 -0
  189. data/lib/bio/sequence/format_raw.rb +23 -0
  190. data/lib/bio/sequence/generic.rb +24 -0
  191. data/lib/bio/sequence/na.rb +491 -0
  192. data/lib/bio/shell.rb +44 -0
  193. data/lib/bio/shell/core.rb +578 -0
  194. data/lib/bio/shell/demo.rb +146 -0
  195. data/lib/bio/shell/interface.rb +218 -0
  196. data/lib/bio/shell/irb.rb +95 -0
  197. data/lib/bio/shell/object.rb +71 -0
  198. data/lib/bio/shell/plugin/blast.rb +42 -0
  199. data/lib/bio/shell/plugin/codon.rb +218 -0
  200. data/lib/bio/shell/plugin/das.rb +58 -0
  201. data/lib/bio/shell/plugin/emboss.rb +23 -0
  202. data/lib/bio/shell/plugin/entry.rb +105 -0
  203. data/lib/bio/shell/plugin/flatfile.rb +101 -0
  204. data/lib/bio/shell/plugin/keggapi.rb +181 -0
  205. data/lib/bio/shell/plugin/midi.rb +430 -0
  206. data/lib/bio/shell/plugin/obda.rb +45 -0
  207. data/lib/bio/shell/plugin/psort.rb +56 -0
  208. data/lib/bio/shell/plugin/seq.rb +247 -0
  209. data/lib/bio/shell/plugin/soap.rb +87 -0
  210. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/bioruby_generator.rb +29 -0
  211. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_classes.rhtml +4 -0
  212. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_log.rhtml +27 -0
  213. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_methods.rhtml +11 -0
  214. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_modules.rhtml +4 -0
  215. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/_variables.rhtml +7 -0
  216. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby-bg.gif +0 -0
  217. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby-gem.png +0 -0
  218. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby-link.gif +0 -0
  219. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby.css +368 -0
  220. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby.rhtml +47 -0
  221. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby_controller.rb +144 -0
  222. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/bioruby_helper.rb +47 -0
  223. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/commands.rhtml +8 -0
  224. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/history.rhtml +10 -0
  225. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/index.rhtml +26 -0
  226. data/lib/bio/shell/rails/vendor/plugins/bioruby/generators/bioruby/templates/spinner.gif +0 -0
  227. data/lib/bio/shell/script.rb +25 -0
  228. data/lib/bio/shell/setup.rb +109 -0
  229. data/lib/bio/shell/web.rb +102 -0
  230. data/lib/bio/tree.rb +852 -0
  231. data/lib/bio/util/color_scheme.rb +191 -0
  232. data/lib/bio/util/color_scheme/buried.rb +59 -0
  233. data/lib/bio/util/color_scheme/helix.rb +59 -0
  234. data/lib/bio/util/color_scheme/hydropathy.rb +64 -0
  235. data/lib/bio/util/color_scheme/nucleotide.rb +31 -0
  236. data/lib/bio/util/color_scheme/strand.rb +59 -0
  237. data/lib/bio/util/color_scheme/taylor.rb +50 -0
  238. data/lib/bio/util/color_scheme/turn.rb +59 -0
  239. data/lib/bio/util/color_scheme/zappo.rb +50 -0
  240. data/lib/bio/util/contingency_table.rb +370 -0
  241. data/lib/bio/util/restriction_enzyme.rb +228 -0
  242. data/lib/bio/util/restriction_enzyme/analysis.rb +249 -0
  243. data/lib/bio/util/restriction_enzyme/analysis_basic.rb +217 -0
  244. data/lib/bio/util/restriction_enzyme/cut_symbol.rb +107 -0
  245. data/lib/bio/util/restriction_enzyme/double_stranded.rb +321 -0
  246. data/lib/bio/util/restriction_enzyme/double_stranded/aligned_strands.rb +130 -0
  247. data/lib/bio/util/restriction_enzyme/double_stranded/cut_location_pair.rb +103 -0
  248. data/lib/bio/util/restriction_enzyme/double_stranded/cut_location_pair_in_enzyme_notation.rb +38 -0
  249. data/lib/bio/util/restriction_enzyme/double_stranded/cut_locations.rb +76 -0
  250. data/lib/bio/util/restriction_enzyme/double_stranded/cut_locations_in_enzyme_notation.rb +107 -0
  251. data/lib/bio/util/restriction_enzyme/enzymes.yaml +7061 -0
  252. data/lib/bio/util/restriction_enzyme/range/cut_range.rb +24 -0
  253. data/lib/bio/util/restriction_enzyme/range/cut_ranges.rb +47 -0
  254. data/lib/bio/util/restriction_enzyme/range/horizontal_cut_range.rb +67 -0
  255. data/lib/bio/util/restriction_enzyme/range/sequence_range.rb +257 -0
  256. data/lib/bio/util/restriction_enzyme/range/sequence_range/calculated_cuts.rb +242 -0
  257. data/lib/bio/util/restriction_enzyme/range/sequence_range/fragment.rb +51 -0
  258. data/lib/bio/util/restriction_enzyme/range/sequence_range/fragments.rb +41 -0
  259. data/lib/bio/util/restriction_enzyme/range/vertical_cut_range.rb +77 -0
  260. data/lib/bio/util/restriction_enzyme/single_strand.rb +200 -0
  261. data/lib/bio/util/restriction_enzyme/single_strand/cut_locations_in_enzyme_notation.rb +135 -0
  262. data/lib/bio/util/restriction_enzyme/single_strand_complement.rb +23 -0
  263. data/lib/bio/util/restriction_enzyme/string_formatting.rb +111 -0
  264. data/lib/bio/util/sirna.rb +288 -0
  265. data/test/data/HMMER/hmmpfam.out +64 -0
  266. data/test/data/HMMER/hmmsearch.out +88 -0
  267. data/test/data/SOSUI/sample.report +11 -0
  268. data/test/data/TMHMM/sample.report +21 -0
  269. data/test/data/aaindex/DAYM780301 +30 -0
  270. data/test/data/aaindex/PRAM900102 +20 -0
  271. data/test/data/bl2seq/cd8a_cd8b_blastp.bl2seq +53 -0
  272. data/test/data/bl2seq/cd8a_p53_e-5blastp.bl2seq +37 -0
  273. data/test/data/blast/2.2.15.blastp.m7 +876 -0
  274. data/test/data/blast/b0002.faa +15 -0
  275. data/test/data/blast/b0002.faa.m0 +128 -0
  276. data/test/data/blast/b0002.faa.m7 +65 -0
  277. data/test/data/blast/b0002.faa.m8 +1 -0
  278. data/test/data/blast/blastp-multi.m7 +188 -0
  279. data/test/data/command/echoarg2.bat +1 -0
  280. data/test/data/embl/AB090716.embl +65 -0
  281. data/test/data/embl/AB090716.embl.rel89 +63 -0
  282. data/test/data/fasta/example1.txt +75 -0
  283. data/test/data/fasta/example2.txt +21 -0
  284. data/test/data/genscan/sample.report +63 -0
  285. data/test/data/iprscan/merged.raw +32 -0
  286. data/test/data/iprscan/merged.txt +74 -0
  287. data/test/data/paml/codeml/control_file.txt +30 -0
  288. data/test/data/paml/codeml/output.txt +78 -0
  289. data/test/data/paml/codeml/rates +217 -0
  290. data/test/data/prosite/prosite.dat +2233 -0
  291. data/test/data/refseq/nm_126355.entret +64 -0
  292. data/test/data/rpsblast/misc.rpsblast +193 -0
  293. data/test/data/soft/GDS100_partial.soft +92 -0
  294. data/test/data/soft/GSE3457_family_partial.soft +874 -0
  295. data/test/data/uniprot/p53_human.uniprot +1456 -0
  296. data/test/functional/bio/appl/test_pts1.rb +115 -0
  297. data/test/functional/bio/io/test_ensembl.rb +229 -0
  298. data/test/functional/bio/io/test_soapwsdl.rb +52 -0
  299. data/test/functional/bio/sequence/test_output_embl.rb +51 -0
  300. data/test/functional/bio/test_command.rb +301 -0
  301. data/test/runner.rb +14 -0
  302. data/test/unit/bio/appl/bl2seq/test_report.rb +134 -0
  303. data/test/unit/bio/appl/blast/test_ncbioptions.rb +112 -0
  304. data/test/unit/bio/appl/blast/test_report.rb +1135 -0
  305. data/test/unit/bio/appl/blast/test_rpsblast.rb +398 -0
  306. data/test/unit/bio/appl/genscan/test_report.rb +182 -0
  307. data/test/unit/bio/appl/hmmer/test_report.rb +342 -0
  308. data/test/unit/bio/appl/iprscan/test_report.rb +338 -0
  309. data/test/unit/bio/appl/mafft/test_report.rb +63 -0
  310. data/test/unit/bio/appl/paml/codeml/test_rates.rb +45 -0
  311. data/test/unit/bio/appl/paml/codeml/test_report.rb +45 -0
  312. data/test/unit/bio/appl/paml/test_codeml.rb +174 -0
  313. data/test/unit/bio/appl/sosui/test_report.rb +81 -0
  314. data/test/unit/bio/appl/targetp/test_report.rb +146 -0
  315. data/test/unit/bio/appl/test_blast.rb +277 -0
  316. data/test/unit/bio/appl/test_fasta.rb +130 -0
  317. data/test/unit/bio/appl/test_psort.rb +57 -0
  318. data/test/unit/bio/appl/test_pts1.rb +77 -0
  319. data/test/unit/bio/appl/tmhmm/test_report.rb +126 -0
  320. data/test/unit/bio/data/test_aa.rb +90 -0
  321. data/test/unit/bio/data/test_codontable.rb +107 -0
  322. data/test/unit/bio/data/test_na.rb +80 -0
  323. data/test/unit/bio/db/embl/test_common.rb +117 -0
  324. data/test/unit/bio/db/embl/test_embl.rb +214 -0
  325. data/test/unit/bio/db/embl/test_embl_rel89.rb +219 -0
  326. data/test/unit/bio/db/embl/test_embl_to_bioseq.rb +203 -0
  327. data/test/unit/bio/db/embl/test_sptr.rb +1812 -0
  328. data/test/unit/bio/db/embl/test_uniprot.rb +31 -0
  329. data/test/unit/bio/db/kegg/test_genes.rb +45 -0
  330. data/test/unit/bio/db/pdb/test_pdb.rb +152 -0
  331. data/test/unit/bio/db/test_aaindex.rb +197 -0
  332. data/test/unit/bio/db/test_fasta.rb +250 -0
  333. data/test/unit/bio/db/test_gff.rb +1190 -0
  334. data/test/unit/bio/db/test_lasergene.rb +95 -0
  335. data/test/unit/bio/db/test_medline.rb +127 -0
  336. data/test/unit/bio/db/test_newick.rb +293 -0
  337. data/test/unit/bio/db/test_nexus.rb +364 -0
  338. data/test/unit/bio/db/test_prosite.rb +1437 -0
  339. data/test/unit/bio/db/test_rebase.rb +101 -0
  340. data/test/unit/bio/db/test_soft.rb +138 -0
  341. data/test/unit/bio/db/test_url.rb +36 -0
  342. data/test/unit/bio/io/flatfile/test_autodetection.rb +375 -0
  343. data/test/unit/bio/io/flatfile/test_buffer.rb +251 -0
  344. data/test/unit/bio/io/flatfile/test_splitter.rb +369 -0
  345. data/test/unit/bio/io/test_ddbjxml.rb +80 -0
  346. data/test/unit/bio/io/test_ensembl.rb +109 -0
  347. data/test/unit/bio/io/test_fastacmd.rb +42 -0
  348. data/test/unit/bio/io/test_flatfile.rb +505 -0
  349. data/test/unit/bio/io/test_soapwsdl.rb +32 -0
  350. data/test/unit/bio/sequence/test_aa.rb +115 -0
  351. data/test/unit/bio/sequence/test_common.rb +373 -0
  352. data/test/unit/bio/sequence/test_compat.rb +69 -0
  353. data/test/unit/bio/sequence/test_dblink.rb +58 -0
  354. data/test/unit/bio/sequence/test_na.rb +330 -0
  355. data/test/unit/bio/shell/plugin/test_seq.rb +185 -0
  356. data/test/unit/bio/test_alignment.rb +1025 -0
  357. data/test/unit/bio/test_command.rb +349 -0
  358. data/test/unit/bio/test_db.rb +96 -0
  359. data/test/unit/bio/test_feature.rb +144 -0
  360. data/test/unit/bio/test_location.rb +599 -0
  361. data/test/unit/bio/test_map.rb +230 -0
  362. data/test/unit/bio/test_pathway.rb +499 -0
  363. data/test/unit/bio/test_reference.rb +252 -0
  364. data/test/unit/bio/test_sequence.rb +329 -0
  365. data/test/unit/bio/test_shell.rb +18 -0
  366. data/test/unit/bio/test_tree.rb +593 -0
  367. data/test/unit/bio/util/restriction_enzyme/analysis/test_calculated_cuts.rb +299 -0
  368. data/test/unit/bio/util/restriction_enzyme/analysis/test_cut_ranges.rb +103 -0
  369. data/test/unit/bio/util/restriction_enzyme/analysis/test_sequence_range.rb +240 -0
  370. data/test/unit/bio/util/restriction_enzyme/double_stranded/test_aligned_strands.rb +101 -0
  371. data/test/unit/bio/util/restriction_enzyme/double_stranded/test_cut_location_pair.rb +75 -0
  372. data/test/unit/bio/util/restriction_enzyme/double_stranded/test_cut_location_pair_in_enzyme_notation.rb +73 -0
  373. data/test/unit/bio/util/restriction_enzyme/double_stranded/test_cut_locations.rb +53 -0
  374. data/test/unit/bio/util/restriction_enzyme/double_stranded/test_cut_locations_in_enzyme_notation.rb +104 -0
  375. data/test/unit/bio/util/restriction_enzyme/single_strand/test_cut_locations_in_enzyme_notation.rb +83 -0
  376. data/test/unit/bio/util/restriction_enzyme/test_analysis.rb +246 -0
  377. data/test/unit/bio/util/restriction_enzyme/test_cut_symbol.rb +44 -0
  378. data/test/unit/bio/util/restriction_enzyme/test_double_stranded.rb +115 -0
  379. data/test/unit/bio/util/restriction_enzyme/test_single_strand.rb +147 -0
  380. data/test/unit/bio/util/restriction_enzyme/test_single_strand_complement.rb +147 -0
  381. data/test/unit/bio/util/restriction_enzyme/test_string_formatting.rb +60 -0
  382. data/test/unit/bio/util/test_color_scheme.rb +33 -0
  383. data/test/unit/bio/util/test_contingency_table.rb +94 -0
  384. data/test/unit/bio/util/test_restriction_enzyme.rb +42 -0
  385. data/test/unit/bio/util/test_sirna.rb +245 -0
  386. metadata +543 -0
@@ -0,0 +1,1381 @@
1
+ #
2
+ # = bio/io/flatfile/index.rb - OBDA flatfile index
3
+ #
4
+ # Copyright:: Copyright (C) 2002
5
+ # GOTO Naohisa <ng@bioruby.org>
6
+ # License:: The Ruby License
7
+ #
8
+ # $Id: index.rb,v 1.19 2007/04/05 23:35:41 trevor Exp $
9
+ #
10
+ # = About Bio::FlatFileIndex
11
+ #
12
+ # Please refer documents of following classes.
13
+ # Classes/modules marked '#' are internal use only.
14
+ #
15
+ # == Classes/modules in index.rb
16
+ # * class Bio::FlatFileIndex
17
+ # * class Bio::FlatFileIndex::Results
18
+ # * module Bio::FlatFileIndex::DEBUG
19
+ # * #module Bio::FlatFileIndex::Template
20
+ # * #class Bio::FlatFileIndex::Template::NameSpace
21
+ # * #class Bio::FlatFileIndex::FileID
22
+ # * #class Bio::FlatFileIndex::FileIDs
23
+ # * #module Bio::FlatFileIndex::Flat_1
24
+ # * #class Bio::FlatFileIndex::Flat_1::Record
25
+ # * #class Bio::FlatFileIndex::Flat_1::FlatMappingFile
26
+ # * #class Bio::FlatFileIndex::Flat_1::PrimaryNameSpace
27
+ # * #class Bio::FlatFileIndex::Flat_1::SecondaryNameSpace
28
+ # * #class Bio::FlatFileIndex::NameSpaces
29
+ # * #class Bio::FlatFileIndex::DataBank
30
+ #
31
+ # == Classes/modules in indexer.rb
32
+ # * module Bio::FlatFileIndex::Indexer
33
+ # * #class Bio::FlatFileIndex::Indexer::NameSpace
34
+ # * #class Bio::FlatFileIndex::Indexer::NameSpaces
35
+ # * #module Bio::FlatFileIndex::Indexer::Parser
36
+ # * #class Bio::FlatFileIndex::Indexer::Parser::TemplateParser
37
+ # * #class Bio::FlatFileIndex::Indexer::Parser::GenBankParser
38
+ # * #class Bio::FlatFileIndex::Indexer::Parser::GenPeptParser
39
+ # * #class Bio::FlatFileIndex::Indexer::Parser::EMBLParser
40
+ # * #class Bio::FlatFileIndex::Indexer::Parser::SPTRParser
41
+ # * #class Bio::FlatFileIndex::Indexer::Parser::FastaFormatParser
42
+ # * #class Bio::FlatFileIndex::Indexer::Parser::MaXMLSequenceParser
43
+ # * #class Bio::FlatFileIndex::Indexer::Parser::MaXMLClusterParser
44
+ # * #class Bio::FlatFileIndex::Indexer::Parser::BlastDefaultParser
45
+ # * #class Bio::FlatFileIndex::Indexer::Parser::PDBChemicalComponentParser
46
+ #
47
+ # == Classes/modules in bdb.rb
48
+ # * #module Bio::FlatFileIndex::BDBDefault
49
+ # * #class Bio::FlatFileIndex::BDBWrapper
50
+ # * #module Bio::FlatFileIndex::BDB_1
51
+ # * #class Bio::FlatFileIndex::BDB_1::BDBMappingFile
52
+ # * #class Bio::FlatFileIndex::BDB_1::PrimaryNameSpace
53
+ # * #class Bio::FlatFileIndex::BDB_1::SecondaryNameSpace
54
+ #
55
+ # = References
56
+ # * ((<URL:http://obda.open-bio.org/>))
57
+ # * ((<URL:http://cvs.open-bio.org/cgi-bin/viewcvs/viewcvs.cgi/obda-specs/?cvsroot=obf-common>))
58
+ #
59
+
60
+ require 'bio/io/flatfile/indexer'
61
+
62
+ module Bio
63
+
64
+
65
+ # Bio::FlatFileIndex is a class for OBDA flatfile index.
66
+ class FlatFileIndex
67
+
68
+ autoload :Indexer, 'bio/io/flatfile/indexer'
69
+ autoload :BDBdefault, 'bio/io/flatfile/bdb'
70
+ autoload :BDBwrapper, 'bio/io/flatfile/bdb'
71
+ autoload :BDB_1, 'bio/io/flatfile/bdb'
72
+
73
+ # magic string for flat/1 index
74
+ MAGIC_FLAT = 'flat/1'
75
+
76
+ # magic string for BerkeleyDB/1 index
77
+ MAGIC_BDB = 'BerkeleyDB/1'
78
+
79
+ #########################################################
80
+
81
+ # Opens existing databank. Databank is a directory which contains
82
+ # indexed files and configuration files. The type of the databank
83
+ # (flat or BerkeleyDB) are determined automatically.
84
+ #
85
+ # If block is given, the databank object is passed to the block.
86
+ # The databank will be automatically closed when the block terminates.
87
+ #
88
+ def self.open(name)
89
+ if block_given? then
90
+ begin
91
+ i = self.new(name)
92
+ r = yield i
93
+ ensure
94
+ if i then
95
+ begin
96
+ i.close
97
+ rescue IOError
98
+ end
99
+ end
100
+ end
101
+ else
102
+ r = self.new(name)
103
+ end
104
+ r
105
+ end
106
+
107
+ # Opens existing databank. Databank is a directory which contains
108
+ # indexed files and configuration files. The type of the databank
109
+ # (flat or BerkeleyDB) are determined automatically.
110
+ #
111
+ # Unlike +FlatFileIndex.open+, block is not allowed.
112
+ #
113
+ def initialize(name)
114
+ @db = DataBank.open(name)
115
+ end
116
+
117
+ # common interface defined in registry.rb
118
+ # Searching databank and returns entry (or entries) as a string.
119
+ # Multiple entries (contatinated to one string) may be returned.
120
+ # Returns empty string if not found.
121
+ #
122
+ def get_by_id(key)
123
+ search(key).to_s
124
+ end
125
+
126
+ #--
127
+ # original methods
128
+ #++
129
+
130
+ # Closes the databank.
131
+ # Returns nil.
132
+ def close
133
+ check_closed?
134
+ @db.close
135
+ @db = nil
136
+ end
137
+
138
+ # Returns true if already closed. Otherwise, returns false.
139
+ def closed?
140
+ if @db then
141
+ false
142
+ else
143
+ true
144
+ end
145
+ end
146
+
147
+ # Set default namespaces.
148
+ # <code>default_namespaces = nil</code>
149
+ # means all namespaces in the databank.
150
+ #
151
+ # <code>default_namespaces= [ str1, str2, ... ]</code>
152
+ # means set default namespeces to str1, str2, ...
153
+ #
154
+ # Default namespaces specified in this method only affect
155
+ # #get_by_id, #search, and #include? methods.
156
+ #
157
+ # Default of default namespaces is nil (that is, all namespaces
158
+ # are search destinations by default).
159
+ #
160
+ def default_namespaces=(names)
161
+ if names then
162
+ @names = []
163
+ names.each { |x| @names.push(x.dup) }
164
+ else
165
+ @names = nil
166
+ end
167
+ end
168
+
169
+ # Returns default namespaces.
170
+ # Returns an array of strings or nil.
171
+ # nil means all namespaces.
172
+ def default_namespaces
173
+ @names
174
+ end
175
+
176
+ # Searching databank and returns a Bio::FlatFileIndex::Results object.
177
+ def search(key)
178
+ check_closed?
179
+ if @names then
180
+ @db.search_namespaces(key, *@names)
181
+ else
182
+ @db.search_all(key)
183
+ end
184
+ end
185
+
186
+ # Searching only specified namespeces.
187
+ # Returns a Bio::FlatFileIndex::Results object.
188
+ #
189
+ def search_namespaces(key, *names)
190
+ check_closed?
191
+ @db.search_namespaces(key, *names)
192
+ end
193
+
194
+ # Searching only primary namespece.
195
+ # Returns a Bio::FlatFileIndex::Results object.
196
+ #
197
+ def search_primary(key)
198
+ check_closed?
199
+ @db.search_primary(key)
200
+ end
201
+
202
+ # Searching databank.
203
+ # If some entries are found, returns an array of
204
+ # unique IDs (primary identifiers).
205
+ # If not found anything, returns nil.
206
+ #
207
+ # This method is useful when search result is very large and
208
+ # #search method is very slow.
209
+ #
210
+ def include?(key)
211
+ check_closed?
212
+ if @names then
213
+ r = @db.search_namespaces_get_unique_id(key, *@names)
214
+ else
215
+ r = @db.search_all_get_unique_id(key)
216
+ end
217
+ if r.empty? then
218
+ nil
219
+ else
220
+ r
221
+ end
222
+ end
223
+
224
+ # Same as #include?, but serching only specified namespaces.
225
+ #
226
+ def include_in_namespaces?(key, *names)
227
+ check_closed?
228
+ r = @db.search_namespaces_get_unique_id(key, *names)
229
+ if r.empty? then
230
+ nil
231
+ else
232
+ r
233
+ end
234
+ end
235
+
236
+ # Same as #include?, but serching only primary namespace.
237
+ #
238
+ def include_in_primary?(key)
239
+ check_closed?
240
+ r = @db.search_primary_get_unique_id(key)
241
+ if r.empty? then
242
+ nil
243
+ else
244
+ r
245
+ end
246
+ end
247
+
248
+ # Returns names of namespaces defined in the databank.
249
+ # (example: [ 'LOCUS', 'ACCESSION', 'VERSION' ] )
250
+ #
251
+ def namespaces
252
+ check_closed?
253
+ r = secondary_namespaces
254
+ r.unshift primary_namespace
255
+ r
256
+ end
257
+
258
+ # Returns name of primary namespace as a string.
259
+ def primary_namespace
260
+ check_closed?
261
+ @db.primary.name
262
+ end
263
+
264
+ # Returns names of secondary namespaces as an array of strings.
265
+ def secondary_namespaces
266
+ check_closed?
267
+ @db.secondary.names
268
+ end
269
+
270
+ # Check consistency between the databank(index) and original flat files.
271
+ #
272
+ # If the original flat files are changed after creating
273
+ # the databank, raises RuntimeError.
274
+ #
275
+ # Note that this check only compares file sizes as
276
+ # described in the OBDA specification.
277
+ #
278
+ def check_consistency
279
+ check_closed?
280
+ @db.check_consistency
281
+ end
282
+
283
+ # If true is given, consistency checks will be performed every time
284
+ # accessing flatfiles. If nil/false, no checks are performed.
285
+ #
286
+ # By default, always_check_consistency is true.
287
+ #
288
+ def always_check_consistency=(bool)
289
+ @db.always_check=(bool)
290
+ end
291
+
292
+ # If true, consistency checks will be performed every time
293
+ # accessing flatfiles. If nil/false, no checks are performed.
294
+ #
295
+ # By default, always_check_consistency is true.
296
+ #
297
+ def always_check_consistency(bool)
298
+ @db.always_check
299
+ end
300
+
301
+ #--
302
+ # private methods
303
+ #++
304
+
305
+ # If the databank is closed, raises IOError.
306
+ def check_closed?
307
+ @db or raise IOError, 'closed databank'
308
+ end
309
+ private :check_closed?
310
+
311
+ #--
312
+ #########################################################
313
+ #++
314
+
315
+ # <code>Results</code> stores search results created by
316
+ # <code>Bio::FlatFileIndex</code> methods.
317
+ #
318
+ # Currently, this class inherits Hash, but internal
319
+ # structure of this class may be changed anytime.
320
+ # Only using methods described below are strongly recomended.
321
+ #
322
+ class Results < Hash
323
+
324
+ # Add search results.
325
+ # "a + b" means "a OR b".
326
+ # * Example
327
+ # # I want to search 'ADH_IRON_1' OR 'ADH_IRON_2'
328
+ # db = Bio::FlatFIleIndex.new(location)
329
+ # a1 = db.search('ADH_IRON_1')
330
+ # a2 = db.search('ADH_IRON_2')
331
+ # # a1 and a2 are Bio::FlatFileIndex::Results objects.
332
+ # print a1 + a2
333
+ #
334
+ def +(a)
335
+ raise 'argument must be Results class' unless a.is_a?(self.class)
336
+ res = self.dup
337
+ res.update(a)
338
+ res
339
+ end
340
+
341
+ # Returns set intersection of results.
342
+ # "a * b" means "a AND b".
343
+ # * Example
344
+ # # I want to search 'HIS_KIN' AND 'human'
345
+ # db = Bio::FlatFIleIndex.new(location)
346
+ # hk = db.search('HIS_KIN')
347
+ # hu = db.search('human')
348
+ # # hk and hu are Bio::FlatFileIndex::Results objects.
349
+ # print hk * hu
350
+ #
351
+ def *(a)
352
+ raise 'argument must be Results class' unless a.is_a?(self.class)
353
+ res = self.class.new
354
+ a.each_key { |x| res.store(x, a[x]) if self[x] }
355
+ res
356
+ end
357
+
358
+ # Returns a string. (concatinated if multiple results exists).
359
+ # Same as <code>to_a.join('')</code>.
360
+ #
361
+ def to_s
362
+ self.values.join
363
+ end
364
+
365
+ #--
366
+ #alias each_orig each
367
+ #++
368
+
369
+ # alias for each_value.
370
+ alias each each_value
371
+
372
+ # Iterates over each result (string).
373
+ # Same as to_a.each.
374
+ def each(&x) #:yields: str
375
+ each_value(&x)
376
+ end if false #dummy for RDoc
377
+
378
+ #--
379
+ #alias to_a_orig to_a
380
+ #++
381
+
382
+ # alias for to_a.
383
+ alias to_a values
384
+
385
+ # Returns an array of strings.
386
+ # If no search results are exist, returns an empty array.
387
+ #
388
+ def to_a; values; end if false #dummy for RDoc
389
+
390
+ # Returns number of results.
391
+ # Same as to_a.size.
392
+ def size; end if false #dummy for RDoc
393
+
394
+ end #class Results
395
+
396
+ #########################################################
397
+
398
+ # Module for output debug messages.
399
+ # Default setting: If $DEBUG or $VERBOSE is true, output debug
400
+ # messages to $stderr; Otherwise, don't output messages.
401
+ #
402
+ module DEBUG
403
+ @@out = $stderr
404
+ @@flag = nil
405
+
406
+ # Set debug messages output destination.
407
+ # If true is given, outputs to $stderr.
408
+ # If nil is given, outputs nothing.
409
+ # This method affects ALL of FlatFileIndex related objects/methods.
410
+ #
411
+ def self.out=(io)
412
+ if io then
413
+ @@out = io
414
+ @@out = $stderr if io == true
415
+ @@flag = true
416
+ else
417
+ @@out = nil
418
+ @@flag = nil
419
+ end
420
+ @@out
421
+ end
422
+
423
+ # get current debug messeages output destination
424
+ def self.out
425
+ @@out
426
+ end
427
+
428
+ # prints debug messages
429
+ def self.print(*arg)
430
+ @@flag = true if $DEBUG or $VERBOSE
431
+ @@out.print(*arg) if @@out and @@flag
432
+ end
433
+ end #module DEBUG
434
+
435
+ #########################################################
436
+
437
+ # Templates
438
+ #
439
+ # Internal use only.
440
+ module Template
441
+
442
+ # templates of namespace
443
+ #
444
+ # Internal use only.
445
+ class NameSpace
446
+ def filename
447
+ # should be redifined in child class
448
+ raise NotImplementedError, "should be redefined in child class"
449
+ end
450
+
451
+ def mapping(filename)
452
+ # should be redifined in child class
453
+ raise NotImplementedError, "should be redefined in child class"
454
+ #Flat_1::FlatMappingFile.new(filename)
455
+ end
456
+
457
+ def initialize(dbname, name)
458
+ @dbname = dbname
459
+ @name = name.dup
460
+ @name.freeze
461
+ @file = mapping(filename)
462
+ end
463
+ attr_reader :dbname, :name, :file
464
+
465
+ def search(key)
466
+ @file.open
467
+ @file.search(key)
468
+ end
469
+
470
+ def close
471
+ @file.close
472
+ end
473
+
474
+ def include?(key)
475
+ r = search(key)
476
+ unless r.empty? then
477
+ key
478
+ else
479
+ nil
480
+ end
481
+ end
482
+ end #class NameSpace
483
+ end #module Template
484
+
485
+ # FileID class.
486
+ #
487
+ # Internal use only.
488
+ class FileID
489
+ def self.new_from_string(str)
490
+ a = str.split("\t", 2)
491
+ a[1] = a[1].to_i if a[1]
492
+ self.new(a[0], a[1])
493
+ end
494
+
495
+ def initialize(filename, filesize = nil)
496
+ @filename = filename
497
+ @filesize = filesize
498
+ @io = nil
499
+ end
500
+ attr_reader :filename, :filesize
501
+
502
+ def check
503
+ begin
504
+ fsize = File.size(@filename)
505
+ r = ( fsize == @filesize)
506
+ rescue Errno::ENOENT
507
+ fsize = -1
508
+ r = nil
509
+ end
510
+ DEBUG.print "FileID: File.size(#{@filename.inspect}) = ",
511
+ fsize, (r ? ' == ' : ' != ') , @filesize,
512
+ (r ? '' : ' bad!'), "\n"
513
+ r
514
+ end
515
+
516
+ def recalc
517
+ @filesize = File.size(@filename)
518
+ end
519
+
520
+ def to_s(i = nil)
521
+ if i then
522
+ str = "fileid_#{i}\t"
523
+ else
524
+ str = ''
525
+ end
526
+ str << "#{@filename}\t#{@filesize}"
527
+ str
528
+ end
529
+
530
+ def open
531
+ unless @io then
532
+ DEBUG.print "FileID: open #{@filename}\n"
533
+ @io = File.open(@filename, 'rb')
534
+ true
535
+ else
536
+ nil
537
+ end
538
+ end
539
+
540
+ def close
541
+ if @io then
542
+ DEBUG.print "FileID: close #{@filename}\n"
543
+ @io.close
544
+ @io = nil
545
+ nil
546
+ else
547
+ true
548
+ end
549
+ end
550
+
551
+ def seek(*arg)
552
+ @io.seek(*arg)
553
+ end
554
+
555
+ def read(size)
556
+ @io.read(size)
557
+ end
558
+
559
+ def get(pos, length)
560
+ open
561
+ seek(pos, IO::SEEK_SET)
562
+ data = read(length)
563
+ close
564
+ data
565
+ end
566
+ end #class FileID
567
+
568
+ # FileIDs class.
569
+ #
570
+ # Internal use only.
571
+ class FileIDs < Array
572
+ def initialize(prefix, hash)
573
+ @hash = hash
574
+ @prefix = prefix
575
+ end
576
+
577
+ def [](n)
578
+ r = super(n)
579
+ if r then
580
+ r
581
+ else
582
+ data = @hash["#{@prefix}#{n}"]
583
+ if data then
584
+ self[n] = data
585
+ end
586
+ super(n)
587
+ end
588
+ end
589
+
590
+ def []=(n, data)
591
+ if data.is_a?(FileID) then
592
+ super(n, data)
593
+ elsif data then
594
+ super(n, FileID.new_from_string(data))
595
+ else
596
+ # data is nil
597
+ super(n, nil)
598
+ end
599
+ self[n]
600
+ end
601
+
602
+ def add(*arg)
603
+ arg.each do |filename|
604
+ self << FileID.new(filename)
605
+ end
606
+ end
607
+
608
+ def cache_all
609
+ a = @hash.keys.collect do |k|
610
+ if k =~ /\A#{Regexp.escape(@prefix)}(\d+)/ then
611
+ $1.to_i
612
+ else
613
+ nil
614
+ end
615
+ end
616
+ a.compact!
617
+ a.each do |i|
618
+ self[i]
619
+ end
620
+ a
621
+ end
622
+
623
+ def each
624
+ (0...self.size).each do |i|
625
+ x = self[i]
626
+ yield(x) if x
627
+ end
628
+ self
629
+ end
630
+
631
+ def each_with_index
632
+ (0...self.size).each do |i|
633
+ x = self[i]
634
+ yield(x, i) if x
635
+ end
636
+ self
637
+ end
638
+
639
+ def keys
640
+ self.cache_all
641
+ a = []
642
+ (0...self.size).each do |i|
643
+ a << i if self[i]
644
+ end
645
+ a
646
+ end
647
+
648
+ def filenames
649
+ self.cache_all
650
+ a = []
651
+ self.each do |x|
652
+ a << x.filename
653
+ end
654
+ a
655
+ end
656
+
657
+ def check_all
658
+ self.cache_all
659
+ r = true
660
+ self.each do |x|
661
+ r = x.check
662
+ break unless r
663
+ end
664
+ r
665
+ end
666
+ alias check check_all
667
+
668
+ def close_all
669
+ self.each do |x|
670
+ x.close
671
+ end
672
+ nil
673
+ end
674
+ alias close close_all
675
+
676
+ def recalc_all
677
+ self.cache_all
678
+ self.each do |x|
679
+ x.recalc
680
+ end
681
+ true
682
+ end
683
+ alias recalc recalc_all
684
+
685
+ end #class FileIDs
686
+
687
+ # module for flat/1 databank
688
+ #
689
+ # Internal use only.
690
+ module Flat_1
691
+
692
+ # Record class.
693
+ #
694
+ # Internal use only.
695
+ class Record
696
+ def initialize(str, size = nil)
697
+ a = str.split("\t")
698
+ a.each { |x| x.to_s.gsub!(/[\000 ]+\z/, '') }
699
+ @key = a.shift.to_s
700
+ @val = a
701
+ @size = (size or str.length)
702
+ #DEBUG.print "key=#{@key.inspect},val=#{@val.inspect},size=#{@size}\n"
703
+ end
704
+ attr_reader :key, :val, :size
705
+
706
+ def to_s
707
+ self.class.to_string(@size, @key, @val)
708
+ end
709
+
710
+ def self.to_string(size, key, val)
711
+ sprintf("%-*s", size, key + "\t" + val.join("\t"))
712
+ end
713
+
714
+ def self.create(size, key, val)
715
+ self.new(self.to_string(size, key, val))
716
+ end
717
+
718
+ def ==(x)
719
+ self.to_s == x.to_s
720
+ end
721
+ end #class Record
722
+
723
+ # FlatMappingFile class.
724
+ #
725
+ # Internal use only.
726
+ class FlatMappingFile
727
+ @@recsize_width = 4
728
+ @@recsize_regex = /\A\d{4}\z/
729
+
730
+ def self.open(*arg)
731
+ self.new(*arg)
732
+ end
733
+
734
+ def initialize(filename, mode = 'rb')
735
+ @filename = filename
736
+ @mode = mode
737
+ @file = nil
738
+ #@file = File.open(filename, mode)
739
+ @record_size = nil
740
+ @records = nil
741
+ end
742
+ attr_accessor :mode
743
+ attr_reader :filename
744
+
745
+ def open
746
+ unless @file then
747
+ DEBUG.print "FlatMappingFile: open #{@filename}\n"
748
+ @file = File.open(@filename, @mode)
749
+ true
750
+ else
751
+ nil
752
+ end
753
+ end
754
+
755
+ def close
756
+ if @file then
757
+ DEBUG.print "FlatMappingFile: close #{@filename}\n"
758
+ @file.close
759
+ @file = nil
760
+ end
761
+ nil
762
+ end
763
+
764
+ def record_size
765
+ unless @record_size then
766
+ open
767
+ @file.seek(0, IO::SEEK_SET)
768
+ s = @file.read(@@recsize_width)
769
+ raise 'strange record size' unless s =~ @@recsize_regex
770
+ @record_size = s.to_i
771
+ DEBUG.print "FlatMappingFile: record_size: #{@record_size}\n"
772
+ end
773
+ @record_size
774
+ end
775
+
776
+ def get_record(i)
777
+ rs = record_size
778
+ seek(i)
779
+ str = @file.read(rs)
780
+ #DEBUG.print "get_record(#{i})=#{str.inspect}\n"
781
+ str
782
+ end
783
+
784
+ def seek(i)
785
+ rs = record_size
786
+ @file.seek(@@recsize_width + rs * i)
787
+ end
788
+
789
+ def records
790
+ unless @records then
791
+ rs = record_size
792
+ @records = (@file.stat.size - @@recsize_width) / rs
793
+ DEBUG.print "FlatMappingFile: records: #{@records}\n"
794
+ end
795
+ @records
796
+ end
797
+ alias size records
798
+
799
+ # methods for writing file
800
+ def write_record(str)
801
+ rs = record_size
802
+ rec = sprintf("%-*s", rs, str)[0..rs]
803
+ @file.write(rec)
804
+ end
805
+
806
+ def add_record(str)
807
+ n = records
808
+ rs = record_size
809
+ @file.seek(0, IO::SEEK_END)
810
+ write_record(str)
811
+ @records += 1
812
+ end
813
+
814
+ def put_record(i, str)
815
+ n = records
816
+ rs = record_size
817
+ if i >= n then
818
+ @file.seek(0, IO::SEEK_END)
819
+ @file.write(sprintf("%-*s", rs, '') * (i - n))
820
+ @records = i + 1
821
+ else
822
+ seek(i)
823
+ end
824
+ write_record(str)
825
+ end
826
+
827
+ def init(rs)
828
+ unless 0 < rs and rs < 10 ** @@recsize_width then
829
+ raise 'record size out of range'
830
+ end
831
+ open
832
+ @record_size = rs
833
+ str = sprintf("%0*d", @@recsize_width, rs)
834
+ @file.truncate(0)
835
+ @file.seek(0, IO::SEEK_SET)
836
+ @file.write(str)
837
+ @records = 0
838
+ end
839
+
840
+ # export/import/edit data
841
+ def each
842
+ n = records
843
+ seek(0)
844
+ (0...n).each do |i|
845
+ yield Record.new(get_record(i))
846
+ end
847
+ self
848
+ end
849
+
850
+ def export_tsv(stream)
851
+ self.each do |x|
852
+ stream << "#{x.to_s}\n"
853
+ end
854
+ stream
855
+ end
856
+
857
+ def init_with_sorted_tsv_file(filename, flag_primary = false)
858
+ rec_size = 1
859
+ f = File.open(filename)
860
+ f.each do |y|
861
+ rec_size = y.chomp.length if rec_size < y.chomp.length
862
+ end
863
+ self.init(rec_size)
864
+
865
+ prev = nil
866
+ f.rewind
867
+ if flag_primary then
868
+ f.each do |y|
869
+ x = Record.new(y.chomp, rec_size)
870
+ if prev then
871
+ if x.key == prev.key
872
+ DEBUG.print "Warining: overwrote unique id #{x.key.inspect}\n"
873
+ else
874
+ self.add_record(prev.to_s)
875
+ end
876
+ end
877
+ prev = x
878
+ end
879
+ self.add_record(prev.to_s) if prev
880
+ else
881
+ f.each do |y|
882
+ x = Record.new(y.chomp, rec_size)
883
+ self.add_record(x.to_s) if x != prev
884
+ prev = x
885
+ end
886
+ end
887
+ f.close
888
+ self
889
+ end
890
+
891
+ def self.external_sort_proc(sort_program = [ '/usr/bin/env',
892
+ 'LC_ALL=C',
893
+ '/usr/bin/sort' ])
894
+ Proc.new do |out, in1, *files|
895
+ cmd = sort_program + [ '-o', out, in1, *files ]
896
+ system(*cmd)
897
+ end
898
+ end
899
+
900
+ def self.external_merge_sort_proc(sort_program = [ '/usr/bin/env',
901
+ 'LC_ALL=C',
902
+ '/usr/bin/sort' ])
903
+ Proc.new do |out, in1, *files|
904
+ # (in1 may be sorted)
905
+ tf_all = []
906
+ tfn_all = []
907
+ files.each do |fn|
908
+ tf = Tempfile.open('sort')
909
+ tf.close(false)
910
+ cmd = sort_program + [ '-o', tf.path, fn ]
911
+ system(*cmd)
912
+ tf_all << tf
913
+ tfn_all << tf.path
914
+ end
915
+ cmd_fin = sort_program + [ '-m', '-o', out, in1, *tfn_all ]
916
+ system(*cmd_fin)
917
+ tf_all.each do |tf|
918
+ tf.close(true)
919
+ end
920
+ end
921
+ end
922
+
923
+ def self.external_merge_proc(sort_program = [ '/usr/bin/env',
924
+ 'LC_ALL=C',
925
+ '/usr/bin/sort' ])
926
+ Proc.new do |out, in1, *files|
927
+ # files (and in1) must be sorted
928
+ cmd = sort_program + [ '-m', '-o', out, in1, *files ]
929
+ system(*cmd)
930
+ end
931
+ end
932
+
933
+ def self.internal_sort_proc
934
+ Proc.new do |out, in1, *files|
935
+ a = IO.readlines(in1)
936
+ files.each do |fn|
937
+ IO.foreach(fn) do |x|
938
+ a << x
939
+ end
940
+ end
941
+ a.sort!
942
+ of = File.open(out, 'w')
943
+ a.each { |x| of << x }
944
+ of.close
945
+ end
946
+ end
947
+
948
+ def import_tsv_files(flag_primary, mode, sort_proc, *files)
949
+ require 'tempfile'
950
+
951
+ tmpfile1 = Tempfile.open('flat')
952
+ self.export_tsv(tmpfile1) unless mode == :new
953
+ tmpfile1.close(false)
954
+
955
+ tmpfile0 = Tempfile.open('sorted')
956
+ tmpfile0.close(false)
957
+
958
+ sort_proc.call(tmpfile0.path, tmpfile1.path, *files)
959
+
960
+ tmpmap = self.class.new(self.filename + ".#{$$}.tmp~", 'wb+')
961
+ tmpmap.init_with_sorted_tsv_file(tmpfile0.path, flag_primary)
962
+ tmpmap.close
963
+ self.close
964
+
965
+ begin
966
+ File.rename(self.filename, self.filename + ".#{$$}.bak~")
967
+ rescue Errno::ENOENT
968
+ end
969
+ File.rename(tmpmap.filename, self.filename)
970
+ begin
971
+ File.delete(self.filename + ".#{$$}.bak~")
972
+ rescue Errno::ENOENT
973
+ end
974
+
975
+ tmpfile0.close(true)
976
+ tmpfile1.close(true)
977
+ self
978
+ end
979
+
980
+
981
+ # methods for searching
982
+ def search(key)
983
+ n = records
984
+ return [] if n <= 0
985
+ i = n / 2
986
+ i_prev = nil
987
+ DEBUG.print "binary search starts...\n"
988
+ begin
989
+ rec = Record.new(get_record(i))
990
+ i_prev = i
991
+ if key < rec.key then
992
+ n = i
993
+ i = i / 2
994
+ elsif key > rec.key then
995
+ i = (i + n) / 2
996
+ else # key == rec.key
997
+ result = [ rec.val ]
998
+ j = i - 1
999
+ while j >= 0 and
1000
+ (rec = Record.new(get_record(j))).key == key
1001
+ result << rec.val
1002
+ j = j - 1
1003
+ end
1004
+ result.reverse!
1005
+ j = i + 1
1006
+ while j < n and
1007
+ (rec = Record.new(get_record(j))).key == key
1008
+ result << rec.val
1009
+ j = j + 1
1010
+ end
1011
+ DEBUG.print "#{result.size} hits found!!\n"
1012
+ return result
1013
+ end
1014
+ end until i_prev == i
1015
+ DEBUG.print "no hits found\n"
1016
+ #nil
1017
+ []
1018
+ end
1019
+ end #class FlatMappingFile
1020
+
1021
+ # primary name space
1022
+ #
1023
+ # Internal use only.
1024
+ class PrimaryNameSpace < Template::NameSpace
1025
+ def mapping(filename)
1026
+ FlatMappingFile.new(filename)
1027
+ end
1028
+ def filename
1029
+ File.join(dbname, "key_#{name}.key")
1030
+ end
1031
+ end #class PrimaryNameSpace
1032
+
1033
+ # secondary name space
1034
+ #
1035
+ # Internal use only.
1036
+ class SecondaryNameSpace < Template::NameSpace
1037
+ def mapping(filename)
1038
+ FlatMappingFile.new(filename)
1039
+ end
1040
+ def filename
1041
+ File.join(dbname, "id_#{name}.index")
1042
+ end
1043
+ def search(key)
1044
+ r = super(key)
1045
+ file.close
1046
+ r.flatten!
1047
+ r
1048
+ end
1049
+ end #class SecondaryNameSpace
1050
+ end #module Flat_1
1051
+
1052
+ # namespaces
1053
+ #
1054
+ # Internal use only.
1055
+ class NameSpaces < Hash
1056
+ def initialize(dbname, nsclass, arg)
1057
+ @dbname = dbname
1058
+ @nsclass = nsclass
1059
+ if arg.is_a?(String) then
1060
+ a = arg.split("\t")
1061
+ else
1062
+ a = arg
1063
+ end
1064
+ a.each do |x|
1065
+ self[x] = @nsclass.new(@dbname, x)
1066
+ end
1067
+ self
1068
+ end
1069
+
1070
+ def each_names
1071
+ self.names.each do |x|
1072
+ yield x
1073
+ end
1074
+ end
1075
+
1076
+ def each_files
1077
+ self.values.each do |x|
1078
+ yield x
1079
+ end
1080
+ end
1081
+
1082
+ def names
1083
+ keys
1084
+ end
1085
+
1086
+ def close_all
1087
+ values.each { |x| x.file.close }
1088
+ end
1089
+ alias close close_all
1090
+
1091
+ def search(key)
1092
+ r = []
1093
+ values.each do |ns|
1094
+ r.concat ns.search(key)
1095
+ end
1096
+ r.sort!
1097
+ r.uniq!
1098
+ r
1099
+ end
1100
+
1101
+ def search_names(key, *names)
1102
+ r = []
1103
+ names.each do |x|
1104
+ ns = self[x]
1105
+ raise "undefined namespace #{x.inspect}" unless ns
1106
+ r.concat ns.search(key)
1107
+ end
1108
+ r
1109
+ end
1110
+
1111
+ def to_s
1112
+ names.join("\t")
1113
+ end
1114
+ end #class NameSpaces
1115
+
1116
+ # databank
1117
+ #
1118
+ # Internal use only.
1119
+ class DataBank
1120
+ def self.file2hash(fileobj)
1121
+ hash = {}
1122
+ fileobj.each do |line|
1123
+ line.chomp!
1124
+ a = line.split("\t", 2)
1125
+ hash[a[0]] = a[1]
1126
+ end
1127
+ hash
1128
+ end
1129
+ private_class_method :file2hash
1130
+
1131
+ def self.filename(dbname)
1132
+ File.join(dbname, 'config.dat')
1133
+ end
1134
+
1135
+ def self.read(name, mode = 'rb', *bdbarg)
1136
+ f = File.open(filename(name), mode)
1137
+ hash = file2hash(f)
1138
+ f.close
1139
+ db = self.new(name, nil, hash)
1140
+ db.bdb_open(*bdbarg)
1141
+ db
1142
+ end
1143
+
1144
+ def self.open(*arg)
1145
+ self.read(*arg)
1146
+ end
1147
+
1148
+ def initialize(name, idx_type = nil, hash = {})
1149
+ @dbname = name.dup
1150
+ @dbname.freeze
1151
+ @bdb = nil
1152
+
1153
+ @always_check = true
1154
+ self.index_type = (hash['index'] or idx_type)
1155
+
1156
+ if @bdb then
1157
+ @config = BDBwrapper.new(@dbname, 'config')
1158
+ @bdb_fileids = BDBwrapper.new(@dbname, 'fileids')
1159
+ @nsclass_pri = BDB_1::PrimaryNameSpace
1160
+ @nsclass_sec = BDB_1::SecondaryNameSpace
1161
+ else
1162
+ @config = hash
1163
+ @nsclass_pri = Flat_1::PrimaryNameSpace
1164
+ @nsclass_sec = Flat_1::SecondaryNameSpace
1165
+ end
1166
+ true
1167
+ end
1168
+
1169
+ attr_reader :dbname, :index_type
1170
+
1171
+ def index_type=(str)
1172
+ case str
1173
+ when MAGIC_BDB
1174
+ @index_type = MAGIC_BDB
1175
+ @bdb = true
1176
+ unless defined?(BDB)
1177
+ raise RuntimeError, "Berkeley DB support not found"
1178
+ end
1179
+ when MAGIC_FLAT, '', nil, false
1180
+ @index_type = MAGIC_FLAT
1181
+ @bdb = false
1182
+ else
1183
+ raise 'unknown or unsupported index type'
1184
+ end
1185
+ end
1186
+
1187
+ def to_s
1188
+ a = ""
1189
+ a << "index\t#{@index_type}\n"
1190
+
1191
+ unless @bdb then
1192
+ a << "format\t#{@format}\n"
1193
+ @fileids.each_with_index do |x, i|
1194
+ a << "#{x.to_s(i)}\n"
1195
+ end
1196
+ a << "primary_namespace\t#{@primary.name}\n"
1197
+ a << "secondary_namespaces\t"
1198
+ a << @secondary.names.join("\t")
1199
+ a << "\n"
1200
+ end
1201
+ a
1202
+ end
1203
+
1204
+ def bdb_open(*bdbarg)
1205
+ if @bdb then
1206
+ @config.close
1207
+ @config.open(*bdbarg)
1208
+ @bdb_fileids.close
1209
+ @bdb_fileids.open(*bdbarg)
1210
+ true
1211
+ else
1212
+ nil
1213
+ end
1214
+ end
1215
+
1216
+ def write(mode = 'wb', *bdbarg)
1217
+ unless FileTest.directory?(@dbname) then
1218
+ Dir.mkdir(@dbname)
1219
+ end
1220
+ f = File.open(self.class.filename(@dbname), mode)
1221
+ f.write self.to_s
1222
+ f.close
1223
+
1224
+ if @bdb then
1225
+ bdb_open(*bdbarg)
1226
+ @config['format'] = format
1227
+ @config['primary_namespace'] = @primary.name
1228
+ @config['secondary_namespaces'] = @secondary.names.join("\t")
1229
+ @bdb_fileids.writeback_array('', fileids, *bdbarg)
1230
+ end
1231
+ true
1232
+ end
1233
+
1234
+ def close
1235
+ DEBUG.print "DataBank: close #{@dbname}\n"
1236
+ primary.close
1237
+ secondary.close
1238
+ fileids.close
1239
+ if @bdb then
1240
+ @config.close
1241
+ @bdb_fileids.close
1242
+ end
1243
+ nil
1244
+ end
1245
+
1246
+ ##parameters
1247
+ def primary
1248
+ unless @primary then
1249
+ self.primary = @config['primary_namespace']
1250
+ end
1251
+ @primary
1252
+ end
1253
+
1254
+ def primary=(pri_name)
1255
+ if !pri_name or pri_name.empty? then
1256
+ pri_name = 'UNIQUE'
1257
+ end
1258
+ @primary = @nsclass_pri.new(@dbname, pri_name)
1259
+ @primary
1260
+ end
1261
+
1262
+ def secondary
1263
+ unless @secondary then
1264
+ self.secondary = @config['secondary_namespaces']
1265
+ end
1266
+ @secondary
1267
+ end
1268
+
1269
+ def secondary=(sec_names)
1270
+ if !sec_names then
1271
+ sec_names = []
1272
+ end
1273
+ @secondary = NameSpaces.new(@dbname, @nsclass_sec, sec_names)
1274
+ @secondary
1275
+ end
1276
+
1277
+ def format=(str)
1278
+ @format = str.to_s.dup
1279
+ end
1280
+
1281
+ def format
1282
+ unless @format then
1283
+ self.format = @config['format']
1284
+ end
1285
+ @format
1286
+ end
1287
+
1288
+ def fileids
1289
+ unless @fileids then
1290
+ init_fileids
1291
+ end
1292
+ @fileids
1293
+ end
1294
+
1295
+ def init_fileids
1296
+ if @bdb then
1297
+ @fileids = FileIDs.new('', @bdb_fileids)
1298
+ else
1299
+ @fileids = FileIDs.new('fileid_', @config)
1300
+ end
1301
+ @fileids
1302
+ end
1303
+
1304
+ # high level methods
1305
+ def always_check=(bool)
1306
+ if bool then
1307
+ @always_check = true
1308
+ else
1309
+ @always_check = false
1310
+ end
1311
+ end
1312
+ attr_reader :always_check
1313
+
1314
+ def get_flatfile_data(f, pos, length)
1315
+ fi = fileids[f.to_i]
1316
+ if @always_check then
1317
+ raise "flatfile #{fi.filename.inspect} may be modified" unless fi.check
1318
+ end
1319
+ fi.get(pos.to_i, length.to_i)
1320
+ end
1321
+
1322
+ def search_all_get_unique_id(key)
1323
+ s = secondary.search(key)
1324
+ p = primary.include?(key)
1325
+ s.push p if p
1326
+ s.sort!
1327
+ s.uniq!
1328
+ s
1329
+ end
1330
+
1331
+ def search_primary(*arg)
1332
+ r = Results.new
1333
+ arg.each do |x|
1334
+ a = primary.search(x)
1335
+ # a is empty or a.size==1 because primary key must be unique
1336
+ r.store(x, get_flatfile_data(*a[0])) unless a.empty?
1337
+ end
1338
+ r
1339
+ end
1340
+
1341
+ def search_all(key)
1342
+ s = search_all_get_unique_id(key)
1343
+ search_primary(*s)
1344
+ end
1345
+
1346
+ def search_primary_get_unique_id(key)
1347
+ s = []
1348
+ p = primary.include?(key)
1349
+ s.push p if p
1350
+ s
1351
+ end
1352
+
1353
+ def search_namespaces_get_unique_id(key, *names)
1354
+ if names.include?(primary.name) then
1355
+ n2 = names.dup
1356
+ n2.delete(primary.name)
1357
+ p = primary.include?(key)
1358
+ else
1359
+ n2 = names
1360
+ p = nil
1361
+ end
1362
+ s = secondary.search_names(key, *n2)
1363
+ s.push p if p
1364
+ s.sort!
1365
+ s.uniq!
1366
+ s
1367
+ end
1368
+
1369
+ def search_namespaces(key, *names)
1370
+ s = search_namespaces_get_unique_id(key, *names)
1371
+ search_primary(*s)
1372
+ end
1373
+
1374
+ def check_consistency
1375
+ fileids.check_all
1376
+ end
1377
+ end #class DataBank
1378
+
1379
+ end #class FlatFileIndex
1380
+ end #module Bio
1381
+