with gzip.open(fn, 'w') as ofile:
                cPickle.dump(data, ofile)
            print('Data saved to file:', os.path.abspath(fn))
        except IOError:
            print('Could not save data to file:', os.path.abspath(fn))
    else:
        with gzip.open(fn) as ifile:
            data = cPickle.load(ifile)

    # Check whether all regions are present
    if not all([region in data['mut_rate'] for region in regions]):
        print("data loading failed or data doesn't match specified regions:",
              regions, ' got:',data['mut_rate'].keys())

    # Average, annotate, and process allele frequencies
    av = process_average_allele_frequencies(data, genes, nbootstraps=0,
                                            synnonsyn=True)
    combined_af = av['combined_af']
    combined_entropy = av['combined_entropy']
    minor_af = av['minor_af']
    synnonsyn = av['synnonsyn']
    synnonsyn_unconstrained = av['synnonsyn_unconstrained']
    av = process_average_allele_frequencies(data, ['genomewide'], nbootstraps=0,
                                            synnonsyn=False)
    combined_af.update(av['combined_af'])
    combined_entropy.update(av['combined_entropy'])
    minor_af.update(av['minor_af'])
    synnonsyn['genomewide'] = np.ones_like(minor_af['genomewide'], dtype=bool)
    synnonsyn_unconstrained['genomewide'] = np.ones_like(minor_af['genomewide'], dtype=bool)
    for gene in genes:
        pos = [x for x in reference.annotation[gene]]
        synnonsyn_unconstrained['genomewide'][pos] = synnonsyn_unconstrained[gene]