<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _add_gainloss_to_output(out, data):
 """Add gainloss based on genes, helpful for identifying changes in smaller genes. """

    out_file = "%s-gainloss.txt" % os.path.splitext(out["cns"])[0]
    if not utils.file_exists(out_file):
        with file_transaction(data, out_file) as tx_out_file:
            cmd = [os.path.join(os.path.dirname(sys.executable), "cnvkit.py"), "gainloss",
                   "-s", out["cns"], "-o", tx_out_file, out["cnr"]]
            gender = _get_batch_gender([data])
            if gender:
                cmd += ["--sample-sex", gender]
            do.run(cmd, "CNVkit gainloss")
    out["gainloss"] = out_file
    return out