1 /*
2 This file is part of BioD.
3 Copyright (C) 2012 Artem Tarasov <lomereiter@gmail.com>
4 5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
11 12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 DEALINGS IN THE SOFTWARE.
22 23 */24 modulebio.std.hts.utils.value;
25 26 importbio.std.hts.bam.tagvalue;
27 importstd.exception;
28 importstd.array;
29 30 private {
31 staticubyte* arrayPointer(refValuev) {
32 // if value contains array, as a tagged union, it has the following layout:33 //34 // Value = size_t35 // void*36 // other stuff...37 returncast(ubyte*)(*(cast(size_t*)(&v) + 1));
38 }
39 }
40 41 /// Emplace value at address $(D p).42 /// Assumes that enough memory is allocated at that address.43 /// (You can find needed amount of memory with $(D sizeInBytes) function)44 voidemplaceValue(ubyte* p, refValuev) {
45 enforce(!v.is_nothing, "null value can't be stored in BAM");
46 47 autotag = v.tag;
48 autosize = tag >> 5; // element size49 50 if ((tag & 1) == 0) { // primitive type51 *p++ = cast(ubyte)v.bam_typeid;
52 53 p[0 .. size] = (cast(ubyte*)(&v))[0 .. size];
54 } else {
55 56 autobytes = *cast(size_t*)(&v) * (tag >> 5);
57 58 if (v.is_string) {
59 *p++ = cast(ubyte)v.bam_typeid;
60 61 p[0 .. bytes] = arrayPointer(v)[0 .. bytes];
62 p[bytes] = 0; // trailing zero63 64 } else {
65 *p++ = cast(ubyte)'B';
66 *p++ = cast(ubyte)v.bam_typeid;
67 68 *(cast(uint*)p) = cast(uint)(bytes / size); // # of elements69 p += uint.sizeof;
70 71 p[0 .. bytes] = arrayPointer(v)[0 .. bytes];
72 }
73 }
74 }
75 76 /// Put a value to an Appender!(ubyte[]) struct77 voidemplaceValue(refAppender!(ubyte[]) appender, refValuev) {
78 enforce(!v.is_nothing, "null value can't be stored in BAM");
79 80 autotag = v.tag;
81 autosize = tag >> 5; // element size82 83 if ((tag & 1) == 0) { // primitive type84 appender.put(cast(ubyte)v.bam_typeid);
85 appender.put((cast(ubyte*)(&v))[0 .. size]);
86 } else {
87 88 autobytes = *cast(size_t*)(&v) * (tag >> 5);
89 90 if (v.is_string) {
91 appender.put(cast(ubyte)v.bam_typeid);
92 appender.put(arrayPointer(v)[0..bytes]);
93 appender.put(cast(ubyte)0); // trailing zero94 } else {
95 appender.put(cast(ubyte)'B');
96 appender.put(cast(ubyte)v.bam_typeid);
97 uintnumber_of_elems = cast(uint)(bytes / size);
98 appender.put((cast(ubyte*)(&number_of_elems))[0..4]);
99 appender.put(arrayPointer(v)[0 .. bytes]);
100 }
101 }
102 }
103 104 /// Calculate size in bytes which value will consume in BAM file.105 size_tsizeInBytes(refValuev) {
106 enforce(!v.is_nothing, "null value can't be stored in BAM");
107 108 autotag = v.tag;
109 110 if ((tag & 1) == 0) {
111 returnchar.sizeof + (tag >> 5); // primitive type112 }
113 114 autobytes = *cast(size_t*)(&v) * (tag >> 5);
115 116 if (v.is_string) {
117 returnchar.sizeof + bytes + char.sizeof; // trailing zero118 } else {
119 return2 * char.sizeof + uint.sizeof + bytes;
120 }
121 }