<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_brackets(docgraph, output_file, layer='mmax'):
 """ converts a document graph into a plain text file with brackets. Parameters layer : str or None The layer from which the pointing chains/relations (i.e. coreference relations) should be extracted. If no layer is selected, all pointing relations will be considered. (This might lead to errors, e.g. when the document contains Tiger syntax trees with secondary edges.) """

    bracketed_str = gen_bracketed_output(docgraph, layer=layer)
    assert isinstance(output_file, (str, file))
    if isinstance(output_file, str):
        path_to_file = os.path.dirname(output_file)
        if not os.path.isdir(path_to_file):
            create_dir(path_to_file)
        with codecs.open(output_file, 'w', 'utf-8') as outfile:
            outfile.write(bracketed_str)

    else:  # output_file is a file object
        output_file.write(bracketed_str)