biosyntax 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,7 +1,10 @@
1
1
  # ruby-biosyntax
2
2
 
3
3
  [![CI](https://github.com/kojix2/ruby-biosyntax/actions/workflows/ci.yml/badge.svg)](https://github.com/kojix2/ruby-biosyntax/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/biosyntax.svg)](https://badge.fury.io/rb/biosyntax)
4
5
  [![Lines of Code](https://img.shields.io/endpoint?url=https%3A%2F%2Ftokei.kojix2.net%2Fbadge%2Fgithub%2Fkojix2%2Fruby-biosyntax%2Flines)](https://tokei.kojix2.net/github/kojix2/ruby-biosyntax)
6
+ [![DOI](https://zenodo.org/badge/1269890086.svg)](https://doi.org/10.5281/zenodo.20698478)
7
+
5
8
 
6
9
 
7
10
  :dna: [bioSyntax](https://github.com/bioSyntax/bioSyntax) - Syntax highlighting for biological data formats - for Ruby.
@@ -98,20 +101,18 @@ BioSyntax.guess_format("a.vcf.gz") # :vcf
98
101
  The metadata is generated from `libbiosyntax` at load time. The Ruby side does
99
102
  not maintain a separate hand-written table of formats or kinds.
100
103
 
101
- ## Examples
104
+ ## Command line
102
105
 
103
- This gem does not install a CLI. See `examples/` for small scripts:
106
+ Installing the gem also installs `biocat`:
104
107
 
105
108
  ```sh
106
- ruby examples/bcat.rb sample.vcf
107
- ruby examples/bcat.rb -l fastq reads.fastq
108
- ruby examples/bcat.rb -l
109
- ruby examples/inspect_spans.rb sample.vcf
109
+ biocat sample.vcf
110
+ biocat --format fastq reads.fastq
111
+ biocat -l
110
112
  ```
111
113
 
112
- `bcat.rb` guesses the format from the file name when possible. Use `-l` /
113
- `--language` to pass a format explicitly. Calling `-l` without an argument
114
- prints the supported format names.
114
+ `.gz`/`.bgz` are decompressed automatically. BAM/CRAM/BCF require optional
115
+ `ruby-htslib` (`gem install htslib`).
115
116
 
116
117
  ## Development tasks
117
118
 
@@ -146,9 +147,10 @@ bundle exec rake
146
147
 
147
148
  ## License
148
149
 
149
- `biosyntax` vendors `libbiosyntax`, which is licensed under the GNU General
150
- Public License version 3 only. This gem is therefore distributed under
151
- `GPL-3.0-only`. See `LICENSE.md`.
150
+ The Ruby code and native binding are licensed under the [MIT License](LICENSE.md).
151
+ The vendored `libbiosyntax` source is licensed under
152
+ [LGPL-2.1-or-later](LICENSE.libbiosyntax.md), and the compiled extension is
153
+ subject to its terms.
152
154
 
153
155
  This project is inspired by the original bioSyntax project:
154
156
  <https://github.com/bioSyntax/bioSyntax>
data/exe/biocat ADDED
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'optparse'
5
+ require 'zlib'
6
+ require 'biosyntax'
7
+
8
+ options = { format: nil, list_formats: false }
9
+
10
+ BINARY_FORMATS = {
11
+ '.bam' => :sam,
12
+ '.cram' => :sam,
13
+ '.bcf' => :vcf
14
+ }.freeze
15
+
16
+ def binary_format_for(path, requested_format)
17
+ unless path == '-'
18
+ ext = File.extname(path).downcase
19
+ return BINARY_FORMATS[ext] if BINARY_FORMATS.key?(ext)
20
+ end
21
+
22
+ case requested_format&.to_s&.downcase
23
+ when 'bam', 'cram' then :sam
24
+ when 'bcf' then :vcf
25
+ end
26
+ end
27
+
28
+ def require_htslib!
29
+ require 'htslib'
30
+ rescue LoadError => e
31
+ warn "[biocat] ruby-htslib is required to read BAM/CRAM/BCF files: #{e.message}"
32
+ warn '[biocat] install it with `gem install htslib`'
33
+ exit 1
34
+ end
35
+
36
+ def emit_colored(text, highlighter)
37
+ text.each_line do |line|
38
+ print(highlighter ? highlighter.colorize(line) : line)
39
+ end
40
+ end
41
+
42
+ def emit_record(record, highlighter)
43
+ line = record.to_s
44
+ line = "#{line}\n" unless line.end_with?("\n")
45
+ emit_colored(line, highlighter)
46
+ end
47
+
48
+ def emit_htslib_file(path, format, highlighter)
49
+ require_htslib!
50
+
51
+ case format
52
+ when :sam
53
+ HTS::Bam.open(path) do |bam|
54
+ emit_colored(bam.header.to_s, highlighter)
55
+ bam.each { |record| emit_record(record, highlighter) }
56
+ end
57
+ when :vcf
58
+ HTS::Bcf.open(path) do |bcf|
59
+ emit_colored(bcf.header.to_s, highlighter)
60
+ bcf.each { |record| emit_record(record, highlighter) }
61
+ end
62
+ end
63
+ end
64
+
65
+ parser = OptionParser.new do |opts|
66
+ opts.banner = 'usage: biocat [options] [FILE ...]'
67
+
68
+ opts.on('-f', '--format FORMAT', 'Highlight as FORMAT') do |format|
69
+ options[:format] = format
70
+ end
71
+
72
+ opts.on('-l', '--list-formats', 'Print supported format names') do
73
+ options[:list_formats] = true
74
+ end
75
+
76
+ opts.on('-h', '--help', 'Print this help') do
77
+ puts opts
78
+ exit
79
+ end
80
+ end
81
+
82
+ begin
83
+ parser.parse!
84
+
85
+ if options[:list_formats]
86
+ puts BioSyntax::FORMAT_NAMES.join("\n")
87
+ exit
88
+ end
89
+
90
+ paths = ARGV.empty? ? ['-'] : ARGV
91
+
92
+ paths.each do |path|
93
+ binary_format = binary_format_for(path, options[:format])
94
+ format = binary_format || options[:format]
95
+ format ||= BioSyntax.guess_format(path) unless path == '-'
96
+
97
+ highlighter = format ? BioSyntax[format] : nil
98
+ if binary_format
99
+ emit_htslib_file(path, binary_format, highlighter)
100
+ next
101
+ end
102
+
103
+ input =
104
+ if path == '-'
105
+ $stdin
106
+ elsif path.downcase.end_with?('.gz', '.bgz')
107
+ Zlib::GzipReader.open(path)
108
+ else
109
+ File.open(path, 'rb')
110
+ end
111
+
112
+ begin
113
+ emit_colored(input, highlighter)
114
+ ensure
115
+ input.close unless path == '-'
116
+ end
117
+ end
118
+ rescue OptionParser::ParseError, BioSyntax::Error => e
119
+ warn "[biocat] #{e.message}"
120
+ warn parser
121
+ exit 2
122
+ rescue SystemCallError => e
123
+ warn "[biocat] #{e.message}"
124
+ exit 1
125
+ rescue Zlib::GzipFile::Error => e
126
+ warn "[biocat] #{e.message}"
127
+ exit 1
128
+ rescue StandardError => e
129
+ warn "[biocat] #{e.message}"
130
+ exit 1
131
+ end
@@ -1,7 +1,7 @@
1
1
 
2
2
  /*
3
3
  * libbiosyntax: dependency-free C tokenizer/highlighter core for biological files.
4
- * SPDX-License-Identifier: GPL-3.0-only
4
+ * SPDX-License-Identifier: LGPL-2.1-or-later
5
5
  */
6
6
  #define BIOSYN_BUILDING_LIBRARY
7
7
  #include "biosyntax.h"
@@ -431,25 +431,25 @@ static uint32_t mapq_score(int64_t v) { if (v == 255 || v < 10) return BIOSYN_CL
431
431
 
432
432
  static void hi_fasta_scheme(writer_t *w, const char *s, size_t n, uint32_t scheme) { n = trim_eol(s, n); if (!n) return; if (s[0] == '>') emit(w, 0, n, BIOSYN_CLASS_HEADER); else if (s[0] == ';' || s[0] == '#') emit(w, 0, n, BIOSYN_CLASS_COMMENT); else seq_runs_scheme(w, s, 0, n, scheme); }
433
433
  static int is_start_codon(const char *s, size_t i, size_t n) {
434
- unsigned char a, b, c;
434
+ int a, b, c;
435
435
  if (i + 3 > n) return 0;
436
436
  a = up((unsigned char)s[i]); b = up((unsigned char)s[i + 1]); c = up((unsigned char)s[i + 2]);
437
437
  return a == 'A' && b == 'T' && c == 'G';
438
438
  }
439
439
  static int is_stop_codon(const char *s, size_t i, size_t n) {
440
- unsigned char a, b, c;
440
+ int a, b, c;
441
441
  if (i + 3 > n) return 0;
442
442
  a = up((unsigned char)s[i]); b = up((unsigned char)s[i + 1]); c = up((unsigned char)s[i + 2]);
443
443
  return a == 'T' && ((b == 'A' && (c == 'A' || c == 'G')) || (b == 'G' && c == 'A'));
444
444
  }
445
445
  static int is_rna_start_codon(const char *s, size_t i, size_t n) {
446
- unsigned char a, b, c;
446
+ int a, b, c;
447
447
  if (i + 3 > n) return 0;
448
448
  a = up((unsigned char)s[i]); b = up((unsigned char)s[i + 1]); c = up((unsigned char)s[i + 2]);
449
449
  return a == 'A' && b == 'U' && c == 'G';
450
450
  }
451
451
  static int is_rna_stop_codon(const char *s, size_t i, size_t n) {
452
- unsigned char a, b, c;
452
+ int a, b, c;
453
453
  if (i + 3 > n) return 0;
454
454
  a = up((unsigned char)s[i]); b = up((unsigned char)s[i + 1]); c = up((unsigned char)s[i + 2]);
455
455
  return a == 'U' && ((b == 'A' && (c == 'A' || c == 'G')) || (b == 'G' && c == 'A'));
@@ -4,7 +4,7 @@
4
4
  /*
5
5
  * libbiosyntax: dependency-free C tokenizer/highlighter core for biological files.
6
6
  * The core performs no IO; callers pass one already-read text line at a time.
7
- * SPDX-License-Identifier: GPL-3.0-only
7
+ * SPDX-License-Identifier: LGPL-2.1-or-later
8
8
  */
9
9
 
10
10
  #include <stddef.h>
@@ -32,7 +32,7 @@ extern "C" {
32
32
 
33
33
  #define BIOSYN_VERSION_MAJOR 0
34
34
  #define BIOSYN_VERSION_MINOR 1
35
- #define BIOSYN_VERSION_PATCH 0
35
+ #define BIOSYN_VERSION_PATCH 1
36
36
  #define BIOSYN_ABI_VERSION 1u
37
37
 
38
38
  #define BIOSYN_STRINGIFY_DETAIL(x) #x
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  * Ruby native extension for libbiosyntax.
3
- * SPDX-License-Identifier: GPL-3.0-only
3
+ * SPDX-License-Identifier: MIT
4
4
  */
5
5
 
6
6
  #include "ruby.h"
@@ -1,5 +1,5 @@
1
1
  module BioSyntax
2
2
  # Ruby gem version.
3
3
  # @return [String]
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.2'
5
5
  end
data/lib/biosyntax.rb CHANGED
@@ -436,8 +436,8 @@ module BioSyntax
436
436
  else
437
437
  name = value.to_s.downcase
438
438
  FORMATS[name.to_sym] ||
439
- FORMATS[name.tr('_', '-').to_sym] ||
440
- FORMATS_BY_ID[Native.format_id_from_name(name)]
439
+ FORMATS[name.tr('_', '-').to_sym] ||
440
+ FORMATS_BY_ID[Native.format_id_from_name(name)]
441
441
  end
442
442
 
443
443
  return found if found
metadata CHANGED
@@ -1,31 +1,34 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: biosyntax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kojix2
8
- bindir: bin
8
+ bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  email: 2xijok@gmail.com
13
- executables: []
13
+ executables:
14
+ - biocat
14
15
  extensions:
15
16
  - ext/biosyntax/extconf.rb
16
17
  extra_rdoc_files: []
17
18
  files:
19
+ - LICENSE.libbiosyntax.md
18
20
  - LICENSE.md
19
21
  - README.md
22
+ - exe/biocat
20
23
  - ext/biosyntax/biosyntax.c
21
24
  - ext/biosyntax/biosyntax.h
22
25
  - ext/biosyntax/biosyntax_ext.c
23
26
  - ext/biosyntax/extconf.rb
24
27
  - lib/biosyntax.rb
25
28
  - lib/biosyntax/version.rb
26
- homepage: https://github.com/kojix2/biosyntax
29
+ homepage: https://github.com/kojix2/ruby-biosyntax
27
30
  licenses:
28
- - GPL-3.0-only
31
+ - MIT
29
32
  metadata: {}
30
33
  rdoc_options: []
31
34
  require_paths:
@@ -41,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
44
  - !ruby/object:Gem::Version
42
45
  version: '0'
43
46
  requirements: []
44
- rubygems_version: 4.0.10
47
+ rubygems_version: 4.0.16
45
48
  specification_version: 4
46
49
  summary: Ruby native binding for libbiosyntax
47
50
  test_files: []