Skip to content

Commit

Permalink
Pulled source of BioD back into trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
pjotrp committed Nov 30, 2020
1 parent 0dca9f4 commit 9ee9726
Show file tree
Hide file tree
Showing 82 changed files with 33,165 additions and 7 deletions.
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
.dub/
dub.selections.json
BioD/
BioD
./BioD
undeaD/
lz4/
core
bin/
test.log
build/
Expand Down
31 changes: 31 additions & 0 deletions BioD/bio/etc/ragel/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
all: fastrecordparser recordparser regionparser

.PHONY : fastrecordparser

.PHONY : recordparser

.PHONY : regionparser

fastrecordparser:
ragel sam_alignment.rl -D -G2
./workarounds/fix_switch_case_fallthrough.sh sam_alignment.d
echo 'module bio.sam.utils.fastrecordparser;' | cat - sam_alignment.d > .sam_alignment.d.tmp
rm sam_alignment.d
mv .sam_alignment.d.tmp fastrecordparser.d
mv fastrecordparser.d ../bio/sam/utils/fastrecordparser.d

recordparser:
ragel sam_alignment.rl -D
./workarounds/fix_static_const.sh sam_alignment.d
echo 'module bio.sam.utils.recordparser;' | cat - sam_alignment.d > .sam_alignment.d.tmp
rm sam_alignment.d
mv .sam_alignment.d.tmp recordparser.d
mv recordparser.d ../bio/sam/utils/recordparser.d

regionparser:
ragel region.rl -D
./workarounds/fix_static_const.sh region.d
mv region.d ../bio/core/region.d

clean:
rm -f *parser.d region.d
153 changes: 153 additions & 0 deletions BioD/bio/etc/ragel/maf_block.rl
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
This file is part of BioD.
Copyright (C) 2013 Artem Tarasov <lomereiter@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

*/
module bio.maf.parser;
import std.conv, std.array;
import bio.maf.block;

%%{
machine maf_block;

# Common utilities for parsing integers and floats
action update_sign { current_sign = fc == '-' ? -1 : 1; }
action init_integer { int_value = 0; }
action consume_next_digit { int_value *= 10; int_value += fc - '0'; }
action take_sign_into_account { int_value *= current_sign; current_sign = 1; }

sign = [\-+];

uint = ([0-9]{1,18}) > init_integer $ consume_next_digit ;
int = (sign >update_sign)? uint % take_sign_into_account ;

action mark_float_start { float_beg = p - line.ptr; }
action update_float_value {
float_value = to!float(line[float_beg .. p - line.ptr]);
}

float = ((sign? ((digit* '.'? digit+ ([eE] sign? digit+)?) | "inf") ) | "nan")
> mark_float_start % update_float_value ;
# --------------------------------------------------------------------------

action set_score { block.score = float_value; }
action set_pass { block.pass = int_value; }
# Alignment block line
score_vp = "score=" float % set_score;
pass_vp = "pass=" uint % set_pass ;
ab_name_value_pair = score_vp | pass_vp ;
alignment_block_line = 'a' (space+ ab_name_value_pair)* ;

# Common
action src_begin { src_beg = p - line.ptr; }
action set_src { sequence.source = line[src_beg .. p - line.ptr]; }
action set_start { sequence.start = int_value; }
action set_size { sequence.size = int_value; }
action set_strand { sequence.strand = fc; }
action set_src_size { sequence.source_size = int_value; }
action add_sequence { sequences.put(sequence); sequence = MafSequence.init; }
action check_sequence { assert(line[src_beg .. p - line.ptr] == sequences.data.back.source); }
src = (^space)+ > src_begin % set_src ;
start = uint % set_start ;
size = uint % set_size ;
strand = ('+' | '-') > set_strand ;
srcSize = uint % set_src_size ;

# Sequence line
action text_begin { text_beg = p - line.ptr; }
action set_text { sequence.text = line[text_beg .. p - line.ptr]; }
text = (^space)+ ;
s_line = ('s'
space+ src
space+ start
space+ size
space+ strand
space+ srcSize
space+ text > text_begin % set_text) % add_sequence ;

# 'i' line
action set_left_status { sequences.data.back.left_status = fc; }
action set_left_count { sequences.data.back.left_count = int_value; }
action set_right_status { sequences.data.back.right_status = fc; }
action set_right_count { sequences.data.back.right_count = int_value; }
i_status = [CINnMT] ;
leftStatus = i_status ;
leftCount = uint ;
rightStatus = i_status ;
rightCount = uint ;
i_line = 'i'
space+ (src > src_begin % check_sequence)
space+ leftStatus > set_left_status
space+ leftCount % set_left_count
space+ rightStatus > set_right_status
space+ rightCount % set_right_count ;

# 'e' line
action set_empty_status { sequence.empty_status = *p; }
e_status = [CIMn] ;
e_line = ('e'
space+ src
space+ start
space+ size
space+ strand
space+ srcSize
space+ (e_status > set_empty_status)) % add_sequence ;

# 'q' line
action qual_begin { qual_beg = p - line.ptr; }
action set_qual { sequences.data.back.quality = line[qual_beg .. p - line.ptr]; }
q_value = (digit | 'F' | '-')+ ;
q_line = 'q'
space+ (src > src_begin % check_sequence)
space+ (q_value > qual_begin % set_qual);

newline = "\n" | "\r\n" ;
block := alignment_block_line space*
(newline ((s_line | i_line | e_line | q_line) space*))+ ;

write data;
}%%

MafBlock parseMafBlock(string line) {
char* p = cast(char*)line.ptr;
char* pe = p + line.length;
char* eof = pe;
int cs;

int current_sign;
int int_value;
double float_value;
size_t float_beg;

MafBlock block;
MafSequence sequence;
auto sequences = Appender!(MafSequence[])();

size_t src_beg;
size_t text_beg;
size_t qual_beg;

%%write init;
%%write exec;

block.sequences = sequences.data;
return block;
}
86 changes: 86 additions & 0 deletions BioD/bio/etc/ragel/region.rl
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
This file is part of BioD.
Copyright (C) 2012 Artem Tarasov <lomereiter@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

*/
module bio.core.region;

%%{
machine region_parser;

action init_integer { uint_value = 0; }
action consume_next_digit { if (fc != ',') uint_value *= 10, uint_value += fc - '0'; }
integer = [,0-9]+ > init_integer @consume_next_digit ;

action set_reference { region.reference = str[0 .. p - str.ptr]; }
action set_left_end { region.beg = to!uint(uint_value - 1); }
action set_right_end { region.end = to!uint(uint_value); }

reference = ([!-()+-<>-~] [!-~]*) % set_reference ;
reference_and_left_end = reference :> ':' integer % set_left_end ;
reference_and_both_ends = reference_and_left_end '-' integer % set_right_end ;

region := (reference @ 0) | (reference_and_left_end @ 1) | (reference_and_both_ends @ 1);

write data;
}%%

import std.conv;

struct Region {
string reference;
uint beg;
uint end;
}

Region parseRegion(string str) {
char* p = cast(char*)str.ptr;
char* pe = p + str.length;
char* eof = pe;
int cs;
long uint_value;

Region region;
region.beg = 0;
region.end = uint.max;

%%write init;
%%write exec;

return region;
}

unittest {
auto region1 = parseRegion("chr1:1,000-2000");
assert(region1.reference == "chr1");
assert(region1.beg == 999);
assert(region1.end == 2000);

auto region2 = parseRegion("chr2");
assert(region2.reference == "chr2");
assert(region2.beg == 0);
assert(region2.end == uint.max);

auto region3 = parseRegion("chr3:1,000,000");
assert(region3.reference == "chr3");
assert(region3.beg == 999_999);
assert(region3.end == uint.max);
}
Loading

0 comments on commit 9ee9726

Please sign in to comment.