File: Faidx.xs

package info (click to toggle)
libbio-db-hts-perl 3.01-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,152 kB
  • sloc: perl: 2,375; sh: 247; makefile: 9
file content (210 lines) | stat: -rw-r--r-- 4,062 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Copyright [2015-2018] EMBL-European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include <stdio.h>
#include <string.h>

#include "htslib/faidx.h"
#ifndef Newx
#  define Newx(v,n,t) New(0,v,n,t)
#endif


// Code is written to use a blessed int pointer to this strut as an object
// You cannot use Data::Dumper to inspect the Faidx object. Sorry
typedef struct
{
  char* path;
  faidx_t* index;
} Faidx ;


SV* new(const char * classname, const char * path)
{
  Faidx   * faidx;
  SV      * obj;
  SV      * obj_ref;
  faidx_t * fai;

  Newx(faidx, 1, Faidx);

  fai = fai_load(path);
  faidx->path = savepv(path);
  faidx->index = fai;
  obj = newSViv((IV)faidx);
  obj_ref = newRV_noinc(obj);
  sv_bless(obj_ref, gv_stashpv(classname, GV_ADD));
  SvREADONLY_on(obj);

  return obj_ref;
}


void get_sequence(SV* obj, SV* location, SV** seq, int* seq_len)
{
  faidx_t *fai;
  char* char_seq;

  *seq = newSVpvn("",0);
  *seq_len = 0;

  fai = ((Faidx*)SvIV(SvRV(obj)))->index;
  //Fetch sequence
  char_seq = fai_fetch(fai, SvPV(location, PL_na), seq_len);

  //Push into a SV
  sv_catpv(*seq, char_seq);
  sv_2mortal(*seq);
  //Free the buffer created by faidx
  free(char_seq);
}

void get_sequence2(SV* obj, SV* seq_id, int start, int end, SV** seq, int* seq_len)
{
  faidx_t *fai;
  char* char_seq;

  *seq = newSVpvn("",0);
  *seq_len = 0;

  fai = ((Faidx*)SvIV(SvRV(obj)))->index;
  //Fetch sequence
  char_seq = faidx_fetch_seq(fai, SvPV(seq_id, PL_na), start, end, seq_len);

  //Push into a SV
  sv_catpv(*seq, char_seq);
  sv_2mortal(*seq);
  //Free the buffer created by faidx
  free(char_seq);
}


int has_sequence(SV* obj, SV* seq_id)
{
  int has_seq=-1 ;
  has_seq = faidx_has_seq(((Faidx*)SvIV(SvRV(obj)))->index, SvPV(seq_id, PL_na));
  return has_seq;
}


int length(SV* obj, char* seq_id)
{
    int length = 0 ;
    faidx_t *fai = ((Faidx*)SvIV(SvRV(obj)))->index ;
    length = faidx_seq_len(fai, seq_id) ;
    return length ;
}


void DESTROY(SV* obj)
{
  Faidx* faidx = (Faidx*)SvIV(SvRV(obj));
  Safefree(faidx->path);
  fai_destroy(faidx->index);
  Safefree(faidx);
}


MODULE = Bio::DB::HTS::Faidx    PACKAGE = Bio::DB::HTS::Faidx
PROTOTYPES: ENABLE


SV*
new(classname, path)
  char* classname
  char* path


void
get_sequence(obj, location, OUTLIST seq, OUTLIST length)
  SV* obj
  SV* location
  SV* seq
  int length = NO_INIT
  CODE:
     get_sequence(obj, location, &seq, &length) ;


void
get_sequence_no_length(obj, location, OUTLIST seq)
  SV* obj
  SV* location
  SV* seq
CODE:
  int seq_len=0 ;
  get_sequence(obj, location, &seq, &seq_len) ;


void
get_sequence2(obj, seq_id, start, end, OUTLIST seq, OUTLIST length)
  SV* obj
  SV* seq_id
  int start
  int end
  SV* seq
  int length = NO_INIT


void
get_sequence2_no_length(obj, seq_id, start, end, OUTLIST seq)
  SV* obj
  SV* seq_id
  int start
  int end
  SV* seq
CODE:
  int seq_len=0 ;
  get_sequence2(obj, seq_id, start, end, &seq, &seq_len) ;


int
has_sequence(obj, seq_id)
  SV* obj
  SV* seq_id


int
length(obj, seq_id)
  SV* obj
  char* seq_id


void
get_all_sequence_ids(obj)
   SV* obj
INIT:
   int num_seqs ;
   int i ;
    const char* faidx_name ;
PPCODE:
    num_seqs = 0 ;
    faidx_t *fai = ((Faidx*)SvIV(SvRV(obj)))->index ;
    num_seqs = faidx_nseq(fai) ;
    EXTEND(SP,num_seqs);
    for( i=0 ; i<num_seqs ; i++ )
    {
      faidx_name = faidx_iseq(fai,i) ;
      PUSHs(sv_2mortal(newSVpv(faidx_name,0))) ;
    }


void
DESTROY(obj)
  SV* obj