Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LightMotif
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Larralde
LightMotif
Commits
453bdc99
Commit
453bdc99
authored
1 year ago
by
Martin Larralde
Browse files
Options
Downloads
Patches
Plain Diff
Update `Encode` trait to return an `EncodedSequence`
parent
5aab90ca
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lightmotif-py/lightmotif/lib.rs
+44
-12
44 additions, 12 deletions
lightmotif-py/lightmotif/lib.rs
lightmotif/src/pli/mod.rs
+7
-1
7 additions, 1 deletion
lightmotif/src/pli/mod.rs
with
51 additions
and
13 deletions
lightmotif-py/lightmotif/lib.rs
+
44
−
12
View file @
453bdc99
...
...
@@ -4,11 +4,14 @@ extern crate generic_array;
extern
crate
lightmotif
;
extern
crate
pyo3
;
use
std
::
sync
::
RwLock
;
use
lightmotif
::
abc
::
Alphabet
;
use
lightmotif
::
abc
::
Dna
;
use
lightmotif
::
abc
::
Symbol
;
use
lightmotif
::
dense
::
DenseMatrix
;
use
lightmotif
::
num
::
Unsigned
;
use
lightmotif
::
pli
::
dispatch
::
Dispatch
;
use
lightmotif
::
pli
::
platform
::
Backend
;
use
lightmotif
::
pli
::
Encode
;
use
lightmotif
::
pli
::
Maximum
;
...
...
@@ -19,6 +22,7 @@ use lightmotif::pli::Threshold;
use
pyo3
::
exceptions
::
PyBufferError
;
use
pyo3
::
exceptions
::
PyIndexError
;
use
pyo3
::
exceptions
::
PyRuntimeError
;
use
pyo3
::
exceptions
::
PyTypeError
;
use
pyo3
::
exceptions
::
PyValueError
;
use
pyo3
::
ffi
::
Py_ssize_t
;
...
...
@@ -46,6 +50,28 @@ type C = typenum::consts::U1;
// --- Helpers -----------------------------------------------------------------
static
PIPELINE
:
RwLock
<
Option
<
Pipeline
<
Dna
,
Dispatch
>>>
=
RwLock
::
new
(
None
);
fn
init_pipeline
()
->
PyResult
<
()
>
{
PIPELINE
.write
()
.map_err
(|
_
|
PyRuntimeError
::
new_err
(
"Failed to acquire global pipeline"
))
?
.replace
(
Pipeline
::
dispatch
());
Ok
(())
}
fn
with_pipeline
<
T
,
F
>
(
f
:
F
)
->
PyResult
<
T
>
where
F
:
FnOnce
(
&
Pipeline
<
Dna
,
Dispatch
>
)
->
T
,
{
PIPELINE
.read
()
.map_err
(|
_
|
PyRuntimeError
::
new_err
(
"Failed to acquire global lock"
))
?
.as_ref
()
.ok_or_else
(||
PyRuntimeError
::
new_err
(
"Global pipeline was not initialize"
))
.map
(
f
)
}
fn
dict_to_alphabet_array
<
'py
,
A
:
lightmotif
::
Alphabet
>
(
d
:
&
'py
PyDict
,
)
->
PyResult
<
GenericArray
<
f32
,
A
::
K
>>
{
...
...
@@ -91,7 +117,7 @@ impl EncodedSequence {
.map_err
(|
lightmotif
::
err
::
InvalidSymbol
(
x
)|
{
PyValueError
::
new_err
(
format!
(
"Invalid symbol in input: {}"
,
x
))
})
?
;
Ok
(
EncodedSequence
::
from
(
data
)
.into
())
Ok
(
Self
::
from
(
data
)
.into
())
}
/// Create a copy of this sequence.
...
...
@@ -100,15 +126,14 @@ impl EncodedSequence {
}
/// Convert this sequence into a striped matrix.
pub
fn
stripe
(
&
self
)
->
StripedSequence
{
let
data
=
Pipeline
::
dispatch
()
.stripe
(
&
self
.data
);
StripedSequence
{
data
}
pub
fn
stripe
(
&
self
)
->
PyResult
<
StripedSequence
>
{
with_pipeline
(|
pli
|
StripedSequence
::
from
(
pli
.stripe
(
&
self
.data
)))
}
}
impl
From
<
Vec
<<
Dna
as
Alphabet
>
::
Symbol
>>
for
EncodedSequence
{
fn
from
(
v
:
Vec
<<
Dna
as
Alphabet
>
::
Symbol
>
)
->
Self
{
Self
{
data
:
v
.into
()
}
impl
From
<
lightmotif
::
seq
::
EncodedSequence
<
lightmotif
::
Dna
>>
for
EncodedSequence
{
fn
from
(
data
:
lightmotif
::
seq
::
EncodedSequence
<
lightmotif
::
Dna
>
)
->
Self
{
Self
{
data
}
}
}
...
...
@@ -121,6 +146,12 @@ pub struct StripedSequence {
data
:
lightmotif
::
seq
::
StripedSequence
<
lightmotif
::
Dna
,
C
>
,
}
impl
From
<
lightmotif
::
seq
::
StripedSequence
<
lightmotif
::
Dna
,
C
>>
for
StripedSequence
{
fn
from
(
data
:
lightmotif
::
seq
::
StripedSequence
<
lightmotif
::
Dna
,
C
>
)
->
Self
{
Self
{
data
}
}
}
// --- CountMatrix -------------------------------------------------------------
/// A matrix storing the count of a motif letters at each position.
...
...
@@ -312,7 +343,7 @@ impl ScoringMatrix {
let
scores
=
slf
.py
()
.allow_threads
(||
P
ipeline
::
dispatch
()
.score
(
seq
,
pssm
));
.allow_threads
(||
with_p
ipeline
(|
pli
|
pli
.score
(
seq
,
pssm
))
)
?
;
Ok
(
StripedScores
::
from
(
scores
))
}
...
...
@@ -398,7 +429,7 @@ impl StripedScores {
let
scores
=
&
slf
.scores
;
let
indices
=
slf
.py
()
.allow_threads
(||
P
ipeline
::
<
Dna
,
_
>
::
dispatch
()
.threshold
(
scores
,
threshold
));
.allow_threads
(||
with_p
ipeline
(|
pli
|
pli
.threshold
(
scores
,
threshold
))
)
?
;
Ok
(
indices
)
}
...
...
@@ -415,7 +446,7 @@ impl StripedScores {
let
scores
=
&
slf
.scores
;
let
indices
=
slf
.py
()
.allow_threads
(||
P
ipeline
::
<
Dna
,
_
>
::
dispatch
()
.max
(
scores
));
.allow_threads
(||
with_p
ipeline
(|
pli
|
pli
.max
(
scores
))
)
?
;
Ok
(
indices
)
}
...
...
@@ -432,7 +463,7 @@ impl StripedScores {
let
scores
=
&
slf
.scores
;
let
indices
=
slf
.py
()
.allow_threads
(||
P
ipeline
::
<
Dna
,
_
>
::
dispatch
()
.argmax
(
scores
));
.allow_threads
(||
with_p
ipeline
(|
pli
|
pli
.argmax
(
scores
))
)
?
;
Ok
(
indices
)
}
}
...
...
@@ -524,7 +555,7 @@ pub fn stripe<'py>(sequence: &'py PyAny) -> PyResult<StripedSequence> {
let
s
=
sequence
.extract
::
<&
PyString
>
()
?
;
let
encoded
=
EncodedSequence
::
__init__
(
s
)
.and_then
(|
e
|
Py
::
new
(
py
,
e
))
?
;
let
striped
=
encoded
.borrow
(
py
)
.stripe
();
Ok
(
striped
)
striped
}
/// PyO3 bindings to ``lightmotif``, a library for fast PWM motif scanning.
...
...
@@ -556,5 +587,6 @@ pub fn init(_py: Python, m: &PyModule) -> PyResult<()> {
m
.add_function
(
wrap_pyfunction!
(
create
,
m
)
?
)
?
;
m
.add_function
(
wrap_pyfunction!
(
stripe
,
m
)
?
)
?
;
init_pipeline
()
?
;
Ok
(())
}
This diff is collapsed.
Click to expand it.
lightmotif/src/pli/mod.rs
+
7
−
1
View file @
453bdc99
...
...
@@ -20,6 +20,7 @@ use super::err::InvalidSymbol;
use
super
::
err
::
UnsupportedBackend
;
use
super
::
num
::
StrictlyPositive
;
use
super
::
pwm
::
ScoringMatrix
;
use
super
::
seq
::
EncodedSequence
;
use
super
::
seq
::
StripedSequence
;
use
typenum
::
consts
::
U16
;
...
...
@@ -35,7 +36,7 @@ mod scores;
/// Used for encoding a sequence into rank-based encoding.
pub
trait
Encode
<
A
:
Alphabet
>
{
/// Encode the given sequence into a vector of symbols.
fn
encode
<
S
:
AsRef
<
[
u8
]
>>
(
&
self
,
seq
:
S
)
->
Result
<
Vec
<
A
::
Symbol
>
,
InvalidSymbol
>
{
fn
encode
_raw
<
S
:
AsRef
<
[
u8
]
>>
(
&
self
,
seq
:
S
)
->
Result
<
Vec
<
A
::
Symbol
>
,
InvalidSymbol
>
{
let
s
=
seq
.as_ref
();
let
mut
buffer
=
Vec
::
with_capacity
(
s
.len
());
unsafe
{
buffer
.set_len
(
s
.len
())
};
...
...
@@ -45,6 +46,11 @@ pub trait Encode<A: Alphabet> {
}
}
/// Encode the given sequence into an `EncodedSequence`.
fn
encode
<
S
:
AsRef
<
[
u8
]
>>
(
&
self
,
seq
:
S
)
->
Result
<
EncodedSequence
<
A
>
,
InvalidSymbol
>
{
self
.encode_raw
(
seq
)
.map
(
EncodedSequence
::
new
)
}
/// Encode the given sequence into a buffer of symbols.
///
/// The destination buffer is expected to be large enough to store the
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment