def METHOD_NAME(args):
    assert args.output.endswith(".tsv")
    n_neg = args.n_neg_per_query
    require_pos_in_topk = args.require_pos_in_topk
    run = load_run(args.run_file, args.topk)
    qrels = load_qrels(args.qrel_file)
    n_not_in_topk, n_total = 0, len(qrels)
    with open_as_write(args.output) as fout:
        for n_processed, qid in tqdm(enumerate(qrels)):
            if qid not in run:
                continue
            top_k = run[qid]
            if require_pos_in_topk:
                pos_docids = [docid for docid in top_k if qrels[qid].get(docid, 0) > 0]
            else:
                pos_docids = [docid for docid in qrels[qid] if qrels[qid][docid] > 0]
            neg_docids = [docid for docid in top_k if qrels[qid].get(docid, 0) == 0]
            if len(pos_docids) == 0:
                n_not_in_topk += 1
            for pos_docid in pos_docids:
                sampled_neg_docids = random.choices(neg_docids, k=n_neg)
                lines = [f"{qid}\t{pos_docid}\t{neg_docid}\n" for neg_docid in sampled_neg_docids]
                fout.writelines(lines)
    print(f"Finished. {n_not_in_topk} out of {n_total} queries have no positive document in the runfile.")