immuneML.encodings.sequence_length_encoding package

Submodules

immuneML.encodings.sequence_length_encoding.SequenceLengthEncoder module

class immuneML.encodings.sequence_length_encoding.SequenceLengthEncoder.SequenceLengthEncoder(region_type: RegionType, sequence_type: SequenceType = SequenceType.AMINO_ACID, scale_to_zero_mean: bool = False, scale_to_unit_variance: bool = False, name: str = None)[source]

Bases: DatasetEncoder

Encodes a dataset based on the length of each receptor sequence in the specified region.

Each sequence (or chain, in the case of a ReceptorDataset) is encoded as a single integer feature representing its length.

For SequenceDatasets each sequence is one example with one feature: its length, giving output shape [n_sequences, 1].

For ReceptorDatasets the two chains of each receptor are paired into a single example with two features (one length per chain), giving output shape [n_receptors, 2]. The feature names are <locus>_length for each locus, ordered alphabetically (e.g. alpha_length, beta_length).

Dataset type:

  • SequenceDatasets

  • ReceptorDatasets

Specification arguments:

  • region_type (str): Which part of the receptor sequence to measure (e.g. imgt_cdr3).

  • sequence_type (str): Whether to measure amino acid or nucleotide sequence length. Valid values: amino_acid, nucleotide. Defaults to amino_acid.

  • scale_to_zero_mean (bool): Whether to scale each feature to zero mean across examples after encoding. Defaults to True.

  • scale_to_unit_variance (bool): Whether to scale each feature to unit variance across examples after encoding. Defaults to True.

YAML specification:

definitions:
    encodings:
        my_length_encoder:
            SequenceLength:
                region_type: imgt_cdr3
                sequence_type: amino_acid
                scale_to_zero_mean: True
                scale_to_unit_variance: True
static build_object(dataset: Dataset, **params)[source]

Creates an instance of the relevant subclass of the DatasetEncoder class using the given parameters. This method will be called during parsing time (early in the immuneML run), such that parameters and dataset type can be tested here.

The build_object method should do the following:

  1. Check parameters: immuneML should crash if wrong user parameters are specified. The ParameterValidator utility class may be used for parameter testing.

  2. Check the dataset type: immuneML should crash if the wrong dataset type is specified for this encoder. For example, DeepRCEncoder should only work for RepertoireDatasets and crash if the dataset is of another type.

  3. Create an instance of the correct Encoder class, using the given parameters. Return this object. Some encoders have different subclasses depending on the dataset type. Make sure to return an instance of the correct subclass. For instance: KmerFrequencyEncoder has different subclasses for each dataset type. When the dataset is a Repertoire dataset, KmerFreqRepertoireEncoder should be returned.

Parameters:
  • dataset – Dataset object of the same class as the dataset to be encoded later; in case there are multiple dataset types supported by the encoder, the dataset should be of one of these types and the correct subclass of the encoder should be returned

  • **params – keyword arguments that will be provided by users in the specification (if immuneML is used as a command line tool) or in the dictionary when calling the method from the code, and which should be used to create the Encoder object

Returns:

the object of the appropriate Encoder class

encode(dataset: Dataset, params: EncoderParams) Dataset[source]

This is the main encoding method of the Encoder. It takes in a given dataset, computes an EncodedData object, and returns a copy of the dataset with the attached EncodedData object.

Parameters:
  • dataset – A dataset object (Sequence, Receptor or RepertoireDataset)

  • params – An EncoderParams object containing few utility parameters which may be used during encoding (e.g., number of parallel processes to use).

Returns:

A copy of the original dataset, with an EncodedData object added to the dataset.encoded_data field.

Module contents