Turn your functions into composable apps¶
Just use the define_app
decorator! This generates a wrapper class that has a reference to your function and can then become part of a composed function.
You need two things:
your function
Type hints on the function first argument and the function return type.
Note
At present, your function can have only one required argument.
Supported cogent3 types¶
If you function takes or returns cogent3 types, you can use the existing type hints. To see what these are, use the defined_types()
function.
Note
You don’t have to use cogent3 types, you can also use standard python types.
A simple example¶
Let’s make an app that returns the elements of an alignment up to a specified index, with the index being a keyword argument. We now define a decorated function up_to
and import the type hints and the decorator.
We create an app instance for a specific value of index
The repr()
of the instance indicates the wrapped function and the module it’s in.
You use first4()
like all composable apps, e.g.
Renaming sequences¶
This time we wrap a method call on a SequenceCollection
(and the alignment sub-classes) for renaming sequences. We also illustrate here that to support both aligned and unaligned data types as input/output, we have to include these in the construction of the custom function.
Note
The SerialisableType
indicates the data has the ability to be converted to json
.
A user app with a different output type¶
In this example, we make a function that returns a DistanceMatrix
from an alignment.