Sequence properties are affected by the moltype you specify. Here we specify the DNA molecular type.
from cogent3 import make_seqmy_seq = make_seq("AGTACACTGGT", moltype="dna")my_seq
0
None
AGTACACTGGT
DnaSequence, length=11
Creating a RNA sequence from a string
from cogent3 import make_seqrnaseq = make_seq("ACGUACGUACGUACGU", moltype="rna")
Converting to FASTA format
from cogent3 import make_seqmy_seq = make_seq("AGTACACTGGT", moltype="dna")my_seq
0
None
AGTACACTGGT
DnaSequence, length=11
Convert a RNA sequence to FASTA format
from cogent3 import make_seqrnaseq = make_seq("ACGUACGUACGUACGU", moltype="rna")rnaseq
0
None
ACGUACGUACGUACGU
RnaSequence, length=16
Writing a sequence to file
from cogent3 import make_seqmy_seq = make_seq("AGTACACTGGT", name="seq1", moltype="dna")my_seq.write("my_seq.fasta")
/tmp/ipykernel_9995/2152054025.py:3: DeprecationWarning: function remove_files is discontinued and will be removed in version 2026.9
reason='use shutil instead'
remove_files(["my_seq.fasta"], error_on_missing=False)
Creating a named sequence
from cogent3 import make_seqmy_seq = make_seq("AGTACACTGGT", "my_gene", moltype="dna")my_seqtype(my_seq)
cogent3.core.sequence.DnaSequence
Setting or changing the name of a sequence
from cogent3 import make_seqmy_seq = make_seq("AGTACACTGGT", moltype="dna")my_seq.name ="my_gene"my_seq
0
my_gene
AGTACACTGGT
DnaSequence, length=11
Complementing a DNA sequence
from cogent3 import make_seqmy_seq = make_seq("AGTACACTGGT", moltype="dna")my_seq.complement()
0
None
TCATGTGACCA
DnaSequence, length=11
Reverse complementing a DNA sequence
my_seq.rc()
0
None
ACCAGTGTACT
DnaSequence, length=11
Translate a sequence to protein
from cogent3 import make_seqmy_seq = make_seq("GCTTGGGAAAGTCAAATGGAA", name="s1", moltype="dna")pep = my_seq.get_translation()type(pep)
cogent3.core.sequence.ProteinSequence
pep
0
s1
AWESQME
ProteinSequence, length=7
The default is to trim a terminating stop if it exists. If you set trim_stop=False and there is a terminating stop, an AlphabetError is raised.
from cogent3 import make_seqmy_seq = make_seq("ATGCACTGGTAA", name="my_gene", moltype="dna")my_seq.get_translation(trim_stop=False)
---------------------------------------------------------------------------AlphabetError Traceback (most recent call last)
CellIn[18], line 4 1from cogent3 import make_seq
2 3 my_seq = make_seq("ATGCACTGGTAA", name="my_gene", moltype="dna")
----> 4 my_seq.get_translation(trim_stop=False)
File ~/work/cogent3.github.io/cogent3.github.io/doc/cogent3/src/cogent3/core/sequence.py:2282, in NucleicAcidSequenceBase.get_translation(self, gc, incomplete_ok, include_stop, trim_stop) 2280ifnot include_stop and"*"in pep:
2281 msg = f"{self.name!r} has a stop codon in the translation"-> 2282raise c3_alphabet.AlphabetError(msg)
2284ifnot incomplete_ok and"X"in pep:
2285 msg = (
2286f"{self.name!r} has an incomplete codon or contains an ambiguity, set incomplete_ok=True to " 2287"allow translation" 2288 )
AlphabetError: 'my_gene' has a stop codon in the translation
my_seq.get_translation(gc="Vertebrate Mitochondrial") # or gc=2
0
my_gene
MHW
ProteinSequence, length=3
Translating a DNA sequence containing stop codons
By default, get_translation() will fail if there are any stop codons in frame in the sequence. You can allow translation in these cases by setting the optional argument include_stop=True.
from cogent3 import make_seqseq = make_seq("ATGTGATGGTAA", name="s1", moltype="dna")pep = seq.get_translation(include_stop=True)pep
0
s1
M*W
ProteinWithStopSequence, length=3
Converting a DNA sequence to RNA
from cogent3 import make_seqmy_seq = make_seq("ACGTACGTACGTACGT", moltype="dna")rnaseq = my_seq.to_rna()rnaseq
0
None
ACGUACGUACGUACGU
RnaSequence, length=16
Convert an RNA sequence to DNA
from cogent3 import make_seqrnaseq = make_seq("ACGUACGUACGUACGU", moltype="rna")dnaseq = rnaseq.to_dna()dnaseq
0
None
ACGTACGTACGTACGT
DnaSequence, length=16
Testing complementarity
from cogent3 import make_seqa = make_seq("AGTACACTGGT", moltype="dna")a.can_pair(a.complement())
Other arguments on the counts() method allow including ambiguous or gap characters in the result.
Counting k-mers
A k-mer is a word of size \(k\) and, as is the convention, its counts are derived from all possible positions (as distinct from non-overlapping words which is how motif counts are calculated).
from cogent3 import make_seqs = make_seq("ACCGTGACGA", moltype="dna")kcounts = s.count_kmers(k=2)kcounts
We support third-party plugins for k-mer counting. After installing one, they can be selected by specifying the package with the .count_kmers(k=2, use_hook="<package name>").