Project: s41467-024-51864-y | Function: cull_redundant_hits (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 an algorithm to filter redundant BLAST hits by selecting the best non-overlapping hits based on quality scores. The function sorts hits by a composite quality metric and removes overlapping alignments to produce a non-redundant set.

**KEY TERMS:** {
  "blast_hits": "BLAST alignment results",
  "pcid": "sequence identity percentage",
  "score": "BLAST bit score",
  "qcovs": "query coverage",
  "overlapping": "redundant hit detection"
}

**CONTEXT CODE:**         self.typep = fields[18]                 #type of alleles: perfect or strict
        self.sseq_new = fields[19]              #subject sequence
        self.strand = fields[16]                #subject strand


def select_best_blast_hit(blast_file, output_file):
    blast_hits = [] #all_records
    with open(blast_file,"r") as fileobject:
        next(fileobject)
        for line in fileobject:
            blast_hits.append(BlastHit(line))


    # Clean up redundant hits so we only have one hit for each part of the genome.
    blast_hits = cull_redundant_hits(blast_hits)
    #return blast_hits
    f = open(output_file,"w")
    f.write(str("gene_id")+"\t"+\
                "contig_name" + "\t" + \
                "contig_start"+"\t"+\
                "contig_end" + "\t" +\
                "strand" + "\t" +\
                "gene_id_long" + "\t"+\
                "pcid" + "\t"+\
                "qcovs"+ "\t"+\
                "score" + "\t"+\
                "alignment_length" + "\t"+\
                "typep" + "\t"+\
                "sseq_new" + "\t"+\
                "\n")
    for blast_hit in blast_hits:
        f.write(str(blast_hit.gene_id)+"\t"+\
                blast_hit.contig_name + "\t" + \
                str(blast_hit.contig_start)+"\t"+\
                str(blast_hit.contig_end) + "\t" +\
                str(blast_hit.strand) + "\t" +\
                str(blast_hit.gene_id_long) + "\t"+\
                str(blast_hit.pcid) + "\t"+\
                str(blast_hit.qcovs) + "\t"+\
                str(blast_hit.score) + "\t"+\
                str(blast_hit.alignment_length) + "\t"+\
                str(blast_hit.typep) + "\t"+\
                str(blast_hit.sseq_new) + "\t"+\
                "\n")


    f.close()



def cull_redundant_hits(blast_hits):
    """
    Cull out redundant hits
    """
    raise NotImplementedError("Function implementation has been masked")


def overlapping(hit, existing_hits):
    # Only consider hits in the same reading frame.
    existing_hits = [h for h in existing_hits if
                     h.strand == hit.strand and
                     h.contig_name == hit.contig_name]

    for existing_hit in existing_hits:
        if hits_overlap(hit, existing_hit):
            return True

    return False


def hits_overlap(a, b):
    raise NotImplementedError("Function implementation has been masked")


if __name__ == "__main__":
    select_best_blast_hit(sys.argv[1],sys.argv[2])



Complete the function implementation replacing "raise NotImplementedError".