| 1 |
/* |
| 2 |
* Copyright [2015-2024] EMBL-European Bioinformatics Institute |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "EXTERN.h" |
| 18 |
#include "perl.h" |
| 19 |
#include "XSUB.h" |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "htslib/faidx.h" |
| 25 |
#ifndef Newx |
| 26 |
# define Newx(v,n,t) New(0,v,n,t) |
| 27 |
#endif |
| 28 |
|
| 29 |
|
| 30 |
// Code is written to use a blessed int pointer to this strut as an object |
| 31 |
// You cannot use Data::Dumper to inspect the Faidx object. Sorry |
| 32 |
typedef struct |
| 33 |
{ |
| 34 |
char* path; |
| 35 |
faidx_t* index; |
| 36 |
} Faidx ; |
| 37 |
|
| 38 |
|
| 39 |
SV* new(const char * classname, const char * path) |
| 40 |
{ |
| 41 |
Faidx * faidx; |
| 42 |
SV * obj; |
| 43 |
SV * obj_ref; |
| 44 |
faidx_t * fai; |
| 45 |
|
| 46 |
Newx(faidx, 1, Faidx); |
| 47 |
|
| 48 |
fai = fai_load(path); |
| 49 |
faidx->path = savepv(path); |
| 50 |
faidx->index = fai; |
| 51 |
obj = newSViv((IV)faidx); |
| 52 |
obj_ref = newRV_noinc(obj); |
| 53 |
sv_bless(obj_ref, gv_stashpv(classname, GV_ADD)); |
| 54 |
SvREADONLY_on(obj); |
| 55 |
|
| 56 |
return obj_ref; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
void get_sequence(SV* obj, SV* location, SV** seq, int* seq_len) |
| 61 |
{ |
| 62 |
faidx_t *fai; |
| 63 |
char* char_seq; |
| 64 |
|
| 65 |
*seq = newSVpvn("",0); |
| 66 |
*seq_len = 0; |
| 67 |
|
| 68 |
fai = ((Faidx*)SvIV(SvRV(obj)))->index; |
| 69 |
//Fetch sequence |
| 70 |
char_seq = fai_fetch(fai, SvPV(location, PL_na), seq_len); |
| 71 |
|
| 72 |
//Push into a SV |
| 73 |
sv_catpv(*seq, char_seq); |
| 74 |
sv_2mortal(*seq); |
| 75 |
//Free the buffer created by faidx |
| 76 |
free(char_seq); |
| 77 |
} |
| 78 |
|
| 79 |
void get_sequence2(SV* obj, SV* seq_id, int start, int end, SV** seq, int* seq_len) |
| 80 |
{ |
| 81 |
faidx_t *fai; |
| 82 |
char* char_seq; |
| 83 |
|
| 84 |
*seq = newSVpvn("",0); |
| 85 |
*seq_len = 0; |
| 86 |
|
| 87 |
fai = ((Faidx*)SvIV(SvRV(obj)))->index; |
| 88 |
//Fetch sequence |
| 89 |
char_seq = faidx_fetch_seq(fai, SvPV(seq_id, PL_na), start, end, seq_len); |
| 90 |
|
| 91 |
//Push into a SV |
| 92 |
sv_catpv(*seq, char_seq); |
| 93 |
sv_2mortal(*seq); |
| 94 |
//Free the buffer created by faidx |
| 95 |
free(char_seq); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
int has_sequence(SV* obj, SV* seq_id) |
| 100 |
{ |
| 101 |
int has_seq=-1 ; |
| 102 |
has_seq = faidx_has_seq(((Faidx*)SvIV(SvRV(obj)))->index, SvPV(seq_id, PL_na)); |
| 103 |
return has_seq; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
int length(SV* obj, char* seq_id) |
| 108 |
{ |
| 109 |
int length = 0 ; |
| 110 |
faidx_t *fai = ((Faidx*)SvIV(SvRV(obj)))->index ; |
| 111 |
length = faidx_seq_len(fai, seq_id) ; |
| 112 |
return length ; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
void DESTROY(SV* obj) |
| 117 |
{ |
| 118 |
Faidx* faidx = (Faidx*)SvIV(SvRV(obj)); |
| 119 |
Safefree(faidx->path); |
| 120 |
fai_destroy(faidx->index); |
| 121 |
Safefree(faidx); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
MODULE = Bio::DB::HTS::Faidx PACKAGE = Bio::DB::HTS::Faidx |
| 126 |
PROTOTYPES: ENABLE |
| 127 |
|
| 128 |
|
| 129 |
SV* |
| 130 |
new(classname, path) |
| 131 |
char* classname |
| 132 |
char* path |
| 133 |
|
| 134 |
|
| 135 |
void |
| 136 |
get_sequence(obj, location, OUTLIST seq, OUTLIST length) |
| 137 |
SV* obj |
| 138 |
SV* location |
| 139 |
SV* seq |
| 140 |
int length = NO_INIT |
| 141 |
CODE: |
| 142 |
get_sequence(obj, location, &seq, &length) ; |
| 143 |
|
| 144 |
|
| 145 |
void |
| 146 |
get_sequence_no_length(obj, location, OUTLIST seq) |
| 147 |
SV* obj |
| 148 |
SV* location |
| 149 |
SV* seq |
| 150 |
CODE: |
| 151 |
int seq_len=0 ; |
| 152 |
get_sequence(obj, location, &seq, &seq_len) ; |
| 153 |
|
| 154 |
|
| 155 |
void |
| 156 |
get_sequence2(obj, seq_id, start, end, OUTLIST seq, OUTLIST length) |
| 157 |
SV* obj |
| 158 |
SV* seq_id |
| 159 |
int start |
| 160 |
int end |
| 161 |
SV* seq |
| 162 |
int length = NO_INIT |
| 163 |
|
| 164 |
|
| 165 |
void |
| 166 |
get_sequence2_no_length(obj, seq_id, start, end, OUTLIST seq) |
| 167 |
SV* obj |
| 168 |
SV* seq_id |
| 169 |
int start |
| 170 |
int end |
| 171 |
SV* seq |
| 172 |
CODE: |
| 173 |
int seq_len=0 ; |
| 174 |
get_sequence2(obj, seq_id, start, end, &seq, &seq_len) ; |
| 175 |
|
| 176 |
|
| 177 |
int |
| 178 |
has_sequence(obj, seq_id) |
| 179 |
SV* obj |
| 180 |
SV* seq_id |
| 181 |
|
| 182 |
|
| 183 |
int |
| 184 |
length(obj, seq_id) |
| 185 |
SV* obj |
| 186 |
char* seq_id |
| 187 |
|
| 188 |
|
| 189 |
void |
| 190 |
get_all_sequence_ids(obj) |
| 191 |
SV* obj |
| 192 |
INIT: |
| 193 |
int num_seqs ; |
| 194 |
int i ; |
| 195 |
const char* faidx_name ; |
| 196 |
PPCODE: |
| 197 |
num_seqs = 0 ; |
| 198 |
faidx_t *fai = ((Faidx*)SvIV(SvRV(obj)))->index ; |
| 199 |
num_seqs = faidx_nseq(fai) ; |
| 200 |
EXTEND(SP,num_seqs); |
| 201 |
for( i=0 ; i<num_seqs ; i++ ) |
| 202 |
{ |
| 203 |
faidx_name = faidx_iseq(fai,i) ; |
| 204 |
PUSHs(sv_2mortal(newSVpv(faidx_name,0))) ; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
void |
| 209 |
DESTROY(obj) |
| 210 |
SV* obj |