def gene_list(list_id=None):
    all_case_ids = [case.case_id for case in app.db.cases()]
    if list_id:
        genelist_obj = app.db.gene_list(list_id)
        case_ids = [case.case_id for case in app.db.cases()
                    if case not in genelist_obj.cases]
        if genelist_obj is None:
            return abort(404, "gene list not found: {}".format(list_id))
    if 'download' in request.args:
        response = make_response('\n'.join(genelist_obj.gene_ids))
        filename = secure_filename("{}.txt".format(genelist_obj.list_id))
        header = "attachment; filename={}".format(filename)
        response.headers['Content-Disposition'] = header
        return response
    if request.method == 'POST':
        if list_id:
            case_ids = request.form.getlist('case_id')
            for case_id in case_ids:
                case_obj = app.db.case(case_id)
                if case_obj not in genelist_obj.cases:
                    genelist_obj.cases.append(case_obj)
                    app.db.save()
        else:
            req_file = request.files['file']
            new_listid = (request.form['list_id'] or
                          secure_filename(req_file.filename))
            if app.db.gene_list(new_listid):
                return abort(500, 'Please provide a unique list name')
            if not req_file:
                return abort(500, 'Please provide a file for upload')
            gene_ids = [line for line in req_file.stream
                        if not line.startswith('
            genelist_obj = app.db.add_genelist(new_listid, gene_ids)
            case_ids = all_case_ids
    return render_template('gene_list.html', gene_list=genelist_obj,
                           case_ids=case_ids)