Skip to content

Commit

Permalink
Shakedown (#2)
Browse files Browse the repository at this point in the history
* Correct some documentation
* Skip pointless allocations where there are no alignments
* Fix bug in subscripting of records when collating
  • Loading branch information
cjw85 authored Jul 9, 2017
1 parent d720e92 commit d8904ad
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ makes some amendments to the bwa/Makefile. To build and install the package one
therefore run:

git clone --recursive https://git/research/bwapy.git
make libbwa.a
make bwa/libbwa.a
python setup.py install


Expand All @@ -36,9 +36,12 @@ alignments of sequences given as strings:

```python
from bwapy import BwaAligner
aligner = BwaAligner(args.index)
index = 'path/to/index' # the path given to bwa index
seq = 'ACGATCGCGATCGA'

aligner = BwaAligner(index)
alignments = aligner.align_seq(seq)
print('Found {} alignments for input {}.'.format(len(alignments), i))
print('Found {} alignments.'.format(len(alignments))
for aln in alignments:
print(' ', aln)
```
Expand Down
2 changes: 1 addition & 1 deletion bwapy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '0.1.0'
__version__ = '0.1.1'

from bwapy.libbwa import BwaAligner
7 changes: 5 additions & 2 deletions bwapy/libbwa.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,11 @@ def align_seq(self, seq:str):
:returns: tuple of :class:`Alignment`
"""
alns = libbwa.align(self.index, seq.encode())
alignments = tuple(self._build_alignment(alns.aln[i]) for i in range(alns.n))
libbwa.free_mem_aln_v(alns)
if alns == ffi.NULL:
alignments = tuple()
else:
alignments = tuple(self._build_alignment(alns.aln[i]) for i in range(alns.n))
libbwa.free_mem_aln_v(alns)
return alignments


Expand Down
26 changes: 17 additions & 9 deletions bwapy/libbwapy.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,24 @@ mem_aln_v *align(bwaidx_t * idx, char * seq) {
if (ar.a[i].secondary >= 0) continue;
primary++;
}
mem_aln_v *alns = new_mem_aln_v(primary);
alns->n = primary;

for (size_t i = 0; i < ar.n; ++i) {
if (ar.a[i].secondary >= 0) continue;
alns->aln[i] = mem_reg2aln(opt, idx->bns, idx->pac, seq_len, seq, &ar.a[i]);
}
if(primary == 0){
free(ar.a);
free(opt);
return NULL;
} else {
mem_aln_v *alns = new_mem_aln_v(primary);
alns->n = primary;

primary = 0;
for (size_t i = 0; i < ar.n; ++i) {
if (ar.a[i].secondary >= 0) continue;
alns->aln[primary++] = mem_reg2aln(opt, idx->bns, idx->pac, seq_len, seq, &ar.a[i]);
}

free(ar.a);
free(opt);
free(ar.a);
free(opt);

return alns;
return alns;
}
}
9 changes: 6 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ therefore run:
.. code-block:: bash
git clone --recursive https://git/research/bwapy.git
make libbwa.a
make bwa/libbwa.a
python setup.py install
Expand All @@ -35,9 +35,12 @@ alignments of sequences given as strings:
.. code-block:: python
from bwapy import BwaAligner
aligner = BwaAligner(args.index)
index = 'path/to/index' # the path given to bwa index
seq = 'ACGATCGCGATCGA'
aligner = BwaAligner(index)
alignments = aligner.align_seq(seq)
print('Found {} alignments for input {}.'.format(len(alignments), i))
print('Found {} alignments.'.format(len(alignments))
for aln in alignments:
print(' ', aln)
Expand Down

0 comments on commit d8904ad

Please sign in to comment.