Project: s41467-024-53455-3 | Function: encode_c (python)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Implements the core CHN encoding algorithm that converts binary data to composite DNA letters using hash functions and constraint algorithms for GC content and homopolymer control

**KEY TERMS:** {
  "vbits": "binary input data",
  "codetext": "encoded composite letters",
  "prev_bits": "previous k bits for hash input",
  "digest_code": "hash function output",
  "pattern_mask": "bit mask for sliding window"
}

**CONTEXT CODE:**         is_homopolymer = False
 
        alphabetletter = [[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 2, 0], [0, 0, 0, 2],
                          [1, 1, 0, 0], [0, 0, 1, 1], [1, 0, 1, 0], [0, 1, 0, 1]]
        

        if len(codetext) > max_runs-1:
            alphabet_to_delete = []
            for sub_related_alphabet in related_alphabet:
                tmp = []
                for item in codetext[-max_runs:]:
                    apb = [item.ratio_dict[x] for x in all_base[:4]]
                    tmp.append(apb in sub_related_alphabet)
                is_homopolymer = all(tmp)
                if is_homopolymer:
                    alphabet_to_delete = []
                    alphabet_to_delete = [alphabetletter.index(i) for i in sub_related_alphabet]
                    break
        alphabet_to_delete = alphabet_to_delete if is_homopolymer else []
        window = len(codetext) if len(codetext)<= max_window else max_window
        if len(codetext) > max_runs-1:
            alphabet_count_in_window = [0] * 8
            for code_idx in range(-window , 0):
                current_code_base_ratio = [codetext[code_idx].ratio_dict[x] for x in all_base[:4]]
                for _ in range(len(alphabetletter)):
                    alphabet_count_in_window[_] += 1 if current_code_base_ratio == alphabetletter[_] else 0
            
            gc_count = (alphabet_count_in_window[1]+alphabet_count_in_window[2])
            my_count = (alphabet_count_in_window[4]+alphabet_count_in_window[7])
            kr_count = (alphabet_count_in_window[5]+alphabet_count_in_window[6])

            if mingcratio *len(codetext) > gc_count and self.sigma == 4:
                alphabet_to_delete += [0,3]
            if maxgcratio *len(codetext) < gc_count and self.sigma == 4:
                alphabet_to_delete += [1,2]
            
            if my_count < kr_count:
                alphabet_to_delete += [5,6]
            elif my_count > kr_count:
                alphabet_to_delete += [4,7]
            
            alphabet = self.alphabet
            alphabet = [item for index, item in enumerate(alphabet) if index not in list(set(alphabet_to_delete))]
            mod = len(alphabet)
        else:
            alphabet = self.alphabet[:self.sigma]
            mod = len(alphabet)
        
        return mod, alphabet

    def encode_c(self, vbits):
        """
        编码
        if DEBUG0: msg为全0数据
        if DEBUG1: msg为全1数据
        :param msg: input 数据
        :return: composite hedges编码后letter
        """
        raise NotImplementedError("Function implementation has been masked")

    def decode_c(self, data: List[composite_letter]):
        """
        通过遍历每一步的全部可能性，计算可能生成的当前letter可能性与data比较，并返回概率最大的数据
        :param data: letter list
        :return: decode后的二进制数据及其的得分
        """
        raise NotImplementedError("Function implementation has been masked")

    def hypo_backtrack(self, hypotree_list):
        min_penalty = np.inf
        chosen = []
        res = []

        for hypo in hypotree_list:
            if hypo.penalty < min_penalty:
                min_penalty = hypo.penalty
        for i in range(len(hypotree_list)):
            if hypotree_list[i].penalty == min_penalty:
                chosen.append(i)
        for i in range(len(chosen)):
            stack = []
            stack_n = []
            p = hypotree_list[chosen[i]]
            while p.parent != None:
                stack.append(p.bits)
                p = p.parent
            stack.reverse()
            for num in stack:
                if self.step != 1:
                    stack_n.append((num // (self.step)) % self.step)
                    stack_n.append(num % self.step)
                else:
                    stack_n.append(num)
            res.append(stack_n)
        return res, min_penalty

    def digest(self, bits, k: int, mod: int):
        """
        计算并返回对应的哈希值
        :param bits: 前k位二进制数据
        :param k: 当前二进制位的index
        :param mod: 模
        :return:
        """
        return int(self.ran_hash(bits + (k << self.pattern)) % mod)

    def to_combine(self, encode_res: List[dict]) -> str:
        tempstr = ''
        cnt = 0
        for item in encode_res:
            for key in code_pattern(self.resolution):
                if item.ratio_dict == code_pattern(self.resolution)[key]:
                    tempstr += key
                    cnt += 1
        return tempstr

    def combine_to_letters(self, string: str) -> List[composite_letter]:
        data = []
        for i in range(len(string)):
            data.append(
                composite_letter(code_pattern(self.resolution)[string[i]]))
        return data

    def js_dist(self, refer: List[float], input: List[float]):
        res = distance.jensenshannon(refer, input)
        return res
    
    def ran_hash(self, u):
        """
        哈希编码
        :param u: 编码值
        :return: 哈希值
        """
        v = u * 3935559000370003845 + 2691343689449507681
        v ^= v >> 21
        v ^= v << 37
        v ^= v >> 4
        v *= 4768777513237032717
        v ^= v << 20
        v ^= v >> 41
        v ^= v << 5
        return v

    def unpack_vbits(self, msg, pattern):
        """
        将msg转化成二进制数据, little-endian
        :param msg: 输入数据
        :param pattern: 进行哈希变换时的数据位
        :return: 二进制数据
        """


Complete the function implementation replacing "raise NotImplementedError".