<commit_before>import re
import numpy as np
from ..utils import DataLoader, Download
import logging
import pandas as pd

# setting up debugging messages
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)


class Antibody:
    """
    TODO: write description
    """

    def __init__(self, sequence='', name='', numbering=None, numbering_scheme='chothia'):
        self._raw_sequence = sequence.upper()
        self._sequence = self._raw_sequence.replace('-', '')
        self._name = name
        self.numbering = numbering
        self.hydrophobicity_matrix = np.array([])
        self._chain = ''
        self.mw = 0
        self.pI = 0
        self.cdr = [0, 0, 0]
        self.numbering_scheme = numbering_scheme

    def load(self):
        """
        Generates all the data:
        - Antibody Numbering
        - Hydrophobicity matrix
        - Molecular weight
        - pI

        All the data is then stored in its respective attributes

        :return:

        """
        try:
            if self.numbering is None or self._chain == '':
                self.numbering, self._chain = self.ab_numbering()
            if self.hydrophobicity_matrix.size == 0:
                self.hydrophobicity_matrix = self.ab_hydrophobicity_matrix()
            if self.mw == 0:
                self.mw = self.ab_molecular_weight()
            if self.pI == 0:
                self.pI = self.ab_pi()
            if sum(self.cdr) == 0:
                self.cdr = self.ab_regions()
        except ValueError:
            self.numbering = 'NA'
            self._chain = 'NA'
            self.hydrophobicity_matrix = 'NA'
            self.mw = 'NA'
            self.pI = 'NA'
            self.cdr = 'NA'

    def ab_numbering(self, server='abysis', numbering_scheme='chothia'):
        # type: (str, str) -> object

        available_numbering_schemes = ['chothia', 'chothia_ext', 'kabath']
        available_servers = ['abysis']

        assert numbering_scheme.lower() in available_numbering_schemes, \
            "Unknown Numbering scheme: {}. \
            Numbering schemes available: {}".format(numbering_scheme,
                                                    ', '.join(available_numbering_schemes))

        assert server in available_servers, "Unknown server: {}. \
            Available servers: {}".format(server, ' ,'.join(available_servers))

        # store the numbering scheme used for reference in other methods
        self.numbering_scheme = numbering_scheme

        numbering = get_ab_numbering(self._sequence, server, numbering_scheme)

        chain = ''

        if numbering == ['']:
            print('Could not apply numbering scheme on provided sequence')
            return 'NA', 'NA'

        elif numbering[0][0] == 'H':
            chain = 'heavy'
        elif numbering[0][0] == 'L':
            chain = 'light'

        return numbering, chain

    def ab_numbering_table(self, name='', only_array=False, replacement='-'):

        """

        :param name:
        :param only_array: if True returns numpy.array object, if False returns a pandas.DataFrame
        :param replacement: value to replace empty positions
        :return:
        """

        if len(name) == 0:
            name = self._name

        if self._chain == '':
            self.numbering, self._chain = self.ab_numbering()

        data_loader = DataLoader(data_type='NumberingSchemes',
                                 data=[self.numbering_scheme, self._chain])
        whole_sequence_dict = data_loader.get_data()

        whole_sequence = whole_sequence_dict['withCDR']

        if only_array:

            data = np.empty((len(whole_sequence)), dtype=str)
            for i, position in enumerate(whole_sequence):
                if position in self.numbering:
                    data[i] = self._sequence[self.numbering.index(position)]
                else:
                    data[i] = '-'

            return data

        else:

            data = pd.DataFrame(columns=whole_sequence, index=[name])

            for i, position in enumerate(self.numbering):
                data.ix[0, data.columns == position] = self._sequence[i]

            return data.fillna(value='-')

    def ab_hydrophobicity_matrix(self, hydrophobicity_scores='ew', include_cdr=True):

        # check if all the required parameters are in order
        available_hydrophobicity_scores = ['kd', 'ww', 'hh', 'mf', 'ew']

        if isinstance(hydrophobicity_scores, str):
            assert hydrophobicity_scores in available_hydrophobicity_scores, \
                "Chosen hydrophobicity scores ({}) not available. \
                Available hydrophobicity scores: {}".format(
                    hydrophobicity_scores, ' ,'.join(available_hydrophobicity_scores)
                )

        if self._chain == '':
            self._chain, self.numbering = self.ab_numbering()
        if self._chain == 'NA':
            raise ValueError("Could not determine chain type")

        data_loader = DataLoader(data_type='NumberingSchemes',
                                 data=[self.numbering_scheme, self._chain])
        whole_sequence_dict = data_loader.get_data()

        if include_cdr:
            whole_sequence = whole_sequence_dict['withCDR']
        else:
            whole_sequence = whole_sequence_dict['noCDR']

        # get the dictionary with the hydrophobicity scores
        data_loader = DataLoader(data_type='AminoAcidProperties',
                                 data=['hydrophobicity', hydrophobicity_scores + 'Hydrophobicity'])
        aa_hydrophobicity_scores = data_loader.get_data()<commit_msg>added replacement parameter for empty amino acid positions<commit_after>import re
import numpy as np
from ..utils import DataLoader, Download
import logging
import pandas as pd

# setting up debugging messages
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)


class Antibody:
    """
    TODO: write description
    """

    def __init__(self, sequence='', name='', numbering=None, numbering_scheme='chothia'):
        self._raw_sequence = sequence.upper()
        self._sequence = self._raw_sequence.replace('-', '')
        self._name = name
        self.numbering = numbering
        self.hydrophobicity_matrix = np.array([])
        self._chain = ''
        self.mw = 0
        self.pI = 0
        self.cdr = [0, 0, 0]
        self.numbering_scheme = numbering_scheme

    def load(self):
        """
        Generates all the data:
        - Antibody Numbering
        - Hydrophobicity matrix
        - Molecular weight
        - pI

        All the data is then stored in its respective attributes

        :return:

        """
        try:
            if self.numbering is None or self._chain == '':
                self.numbering, self._chain = self.ab_numbering()
            if self.hydrophobicity_matrix.size == 0:
                self.hydrophobicity_matrix = self.ab_hydrophobicity_matrix()
            if self.mw == 0:
                self.mw = self.ab_molecular_weight()
            if self.pI == 0:
                self.pI = self.ab_pi()
            if sum(self.cdr) == 0:
                self.cdr = self.ab_regions()
        except ValueError:
            self.numbering = 'NA'
            self._chain = 'NA'
            self.hydrophobicity_matrix = 'NA'
            self.mw = 'NA'
            self.pI = 'NA'
            self.cdr = 'NA'

    def ab_numbering(self, server='abysis', numbering_scheme='chothia'):
        # type: (str, str) -> object

        available_numbering_schemes = ['chothia', 'chothia_ext', 'kabath']
        available_servers = ['abysis']

        assert numbering_scheme.lower() in available_numbering_schemes, \
            "Unknown Numbering scheme: {}. \
            Numbering schemes available: {}".format(numbering_scheme,
                                                    ', '.join(available_numbering_schemes))

        assert server in available_servers, "Unknown server: {}. \
            Available servers: {}".format(server, ' ,'.join(available_servers))

        # store the numbering scheme used for reference in other methods
        self.numbering_scheme = numbering_scheme

        numbering = get_ab_numbering(self._sequence, server, numbering_scheme)

        chain = ''

        if numbering == ['']:
            print('Could not apply numbering scheme on provided sequence')
            return 'NA', 'NA'

        elif numbering[0][0] == 'H':
            chain = 'heavy'
        elif numbering[0][0] == 'L':
            chain = 'light'

        return numbering, chain

    def ab_numbering_table(self, name='', only_array=False, replacement='-'):

        """

        :param name:
        :param only_array: if True returns numpy.array object, if False returns a pandas.DataFrame
        :param replacement: value to replace empty positions
        :return:
        """

        if len(name) == 0:
            name = self._name

        if self._chain == '':
            self.numbering, self._chain = self.ab_numbering()

        data_loader = DataLoader(data_type='NumberingSchemes',
                                 data=[self.numbering_scheme, self._chain])
        whole_sequence_dict = data_loader.get_data()

        whole_sequence = whole_sequence_dict['withCDR']

        if only_array:

            data = np.empty((len(whole_sequence)), dtype=str)
            for i, position in enumerate(whole_sequence):
                if position in self.numbering:
                    data[i] = self._sequence[self.numbering.index(position)]
                else:
                    data[i] = replacement

            return data

        else:

            data = pd.DataFrame(columns=whole_sequence, index=[name])

            for i, position in enumerate(self.numbering):
                data.ix[0, data.columns == position] = self._sequence[i]

            return data.fillna(value=replacement)

    def ab_hydrophobicity_matrix(self, hydrophobicity_scores='ew', include_cdr=True):

        # check if all the required parameters are in order
        available_hydrophobicity_scores = ['kd', 'ww', 'hh', 'mf', 'ew']

        if isinstance(hydrophobicity_scores, str):
            assert hydrophobicity_scores in available_hydrophobicity_scores, \
                "Chosen hydrophobicity scores ({}) not available. \
                Available hydrophobicity scores: {}".format(
                    hydrophobicity_scores, ' ,'.join(available_hydrophobicity_scores)
                )

        if self._chain == '':
            self._chain, self.numbering = self.ab_numbering()
        if self._chain == 'NA':
            raise ValueError("Could not determine chain type")

        data_loader = DataLoader(data_type='NumberingSchemes',
                                 data=[self.numbering_scheme, self._chain])
        whole_sequence_dict = data_loader.get_data()

        if include_cdr:
            whole_sequence = whole_sequence_dict['withCDR']
        else:
            whole_sequence = whole_sequence_dict['noCDR']

        # get the dictionary with the hydrophobicity scores
        data_loader = DataLoader(data_type='AminoAcidProperties',
                                 data=['hydrophobicity', hydrophobicity_scores + 'Hydrophobicity'])
        aa_hydrophobicity_scores = data_loader.get_data()
{'fraction_non_alphanumeric': 0.10300731986947703, 'fraction_numerical': 0.002645735955551636, 'mean_word_length': 2.051668460710441, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6587744', 'n_tokens_mistral': 3261, 'n_tokens_neox': 3049, 'n_words': 727}