Get mmol file from PDBe and return its content as a string. Write to file if outfile given.

    Parameters
    ----------
    code : str
        PDB code.
    mmol_number : int
        mmol number (biological assembly number) of file to download. Numbers from PDBe.
        If None, defaults to the preferred biological assembly listed for code on the PDBe.
    outfile : str
        Filepath. Writes returned value to this file.

    Returns
    -------
    mmol_string : str, or None
        Content of the mmol file as a string.
        None if there are no pdbe files to download, as determined by pdbe_status_code().
        None if unable to download the mmol_file from the pdbe site.

    Raises
    ------
    ValueError
        If the number of mmols for code is stored in mmols_numbers and if mmol_number is larger than this value.

def get_mmol(code, mmol_number=None, outfile=None):
    """ Get mmol file from PDBe and return its content as a string. Write to file if outfile given.

    Parameters
    ----------
    code : str
        PDB code.
    mmol_number : int
        mmol number (biological assembly number) of file to download. Numbers from PDBe.
        If None, defaults to the preferred biological assembly listed for code on the PDBe.
    outfile : str
        Filepath. Writes returned value to this file.

    Returns
    -------
    mmol_string : str, or None
        Content of the mmol file as a string.
        None if there are no pdbe files to download, as determined by pdbe_status_code().
        None if unable to download the mmol_file from the pdbe site.

    Raises
    ------
    ValueError
        If the number of mmols for code is stored in mmols_numbers and if mmol_number is larger than this value.
    """
    if not mmol_number:
        try:
            mmol_number = preferred_mmol(code=code)
        except (ValueError, TypeError, IOError):
            print("No mmols for {0}".format(code))
            return None
    # sanity check
    if mmols_numbers:
        if code in mmols_numbers.keys():
            num_mmols = mmols_numbers[code][0]
            if mmol_number > num_mmols:
                raise ValueError('There are only {0} mmols for code {1}. mmol_number {2} is too big'
                                 .format(num_mmols, code, mmol_number))
    # Download mmol file from the PDBE webserver.
    pdbe_url = "http://www.ebi.ac.uk/pdbe/entry-files/download/{0}_{1}.mmol".format(code, mmol_number)
    r = requests.get(pdbe_url)
    if r.status_code == 200:
        mmol_string = r.text
    else:
        # Download gz pdb file from the PDB.
        pdb_url = "http://www.rcsb.org/pdb/files/{0}.pdb{1}.gz".format(code.upper(), mmol_number)
        r = requests.get(pdb_url)
        if r.status_code == 200:
            temp_gz = tempfile.NamedTemporaryFile()
            temp_gz.write(r.content)
            with gzip.open(temp_gz.name, 'rb') as foo:
                mmol_string = foo.read().decode()
        else:
            print("Could not download mmol file for {0}.\n Got requests status_code {1}".format(code, r.status_code))
            return None
    # Write to file
    if outfile and mmol_string:
        with open(outfile, 'w') as foo:
            foo.write(mmol_string)

    return mmol_string