def write_output_to_file(pathway_table, currenttarmet, cutoff,
    cyclic_pathways, folder_to_create, namemap, source_metabolites, G):
    """
    This function writes the pathways of sizes less than or equal to the
    cutoff from source to the target and seed metabolites to target.
    This function also writes cyclic pathways of sizes less than or equal to
    cutoff from the source to target.

    Parameters
    ----------
    pathway_table : dict
        Dictionary of dictionary containing the pathways of different sizes
        identified for every metabolite. This will have only the acyclic/
        branched pathways.
    currenttarmet : str
        Current target metabolite
    cutoff : int
        Maximum pathway length cutoff
    cyclic_pathways : dict
        Dictionary of dictionary containing cyclic pathways of different sizes
        identified for every metabolite.
    folder_to_create : str
        Name of the folder where results have to be written
    namemap : dict
        Dictionary mapping the adhoc reaction names to reaction names in
        the model
    source_metabolites : list
        List of source metabolites
    G : NetworkX DiGraph Object
        Bipartite graph of the metabolic network


    Returns
    -------
    None
    """
    pred = G.predecessors
    succ = G.successors
    all_pathways_count = []
    path_count = []
    cyclic_pathway_count = []
    if currenttarmet in pathway_table:
        for plen in pathway_table[currenttarmet]:
            all_pathways_count.append(len(pathway_table[currenttarmet][plen]))
            if plen <= int(cutoff):
                path_count.append(len(pathway_table[currenttarmet][plen]))
        if currenttarmet in cyclic_pathways:
            for plen in cyclic_pathways[currenttarmet]:
                if plen <= int(cutoff):
                    cyclic_pathway_count.append(len(cyclic_pathways[
                        currenttarmet][plen]))
            cycfname = (folder_to_create + 'cyclic_pathways_' +
                currenttarmet.replace(' ', '') + '_' + 'leq_plen_' + str(
                cutoff) + '.txt')
            pathnumcount = 0
            with open(cycfname, 'w') as filetowrite:
                print('\nWriting cyclic pathways to a file')
                for idx in cyclic_pathways[currenttarmet]:
                    if idx <= int(cutoff):
                        filetowrite.write('Path length ' + str(idx) + '\n')
                    for items in cyclic_pathways[currenttarmet][idx]:
                        pathnumcount += 1
                        filetowrite.write(str(pathnumcount) + '\n')
                        for entities in list(items):
                            filetowrite.write(namemap[entities] + '\t' +
                                ' + '.join(pred(entities)) + '->' + ' + '.
                                join(succ(entities)))
                            filetowrite.write('\n')
                        filetowrite.write('--------------------\n')
        if int(cutoff) in pathway_table[currenttarmet]:
            for sourcemets in source_metabolites:
                seedfname = (folder_to_create +
                    'branched_pathways_from_seed_' + currenttarmet.replace(
                    ' ', '') + '_' + 'leq_plen_' + str(cutoff) + '.txt')
                pathnumcount = 0
                with open(seedfname, 'w') as filetowrite:
                    print('Writing branched pathways (from seed) to a file')
                    for idx in pathway_table[currenttarmet]:
                        if idx <= int(cutoff):
                            filetowrite.write('Path length ' + str(idx) + '\n')
                            for items in pathway_table[currenttarmet][idx]:
                                pathnumcount += 1
                                filetowrite.write(str(pathnumcount) + '\n')
                                for entities in list(items):
                                    filetowrite.write(namemap[entities] +
                                        '\t' + ' + '.join(pred(entities)) +
                                        '->' + ' + '.join(succ(entities)))
                                    filetowrite.write('\n')
                                filetowrite.write('--------------------\n')
                            filetowrite.write('--------------------\n')
                only_source_to_target = []
                for sourcemets in source_metabolites:
                    for idx in pathway_table[currenttarmet]:
                        if idx <= int(cutoff):
                            for items in pathway_table[currenttarmet][idx]:
                                if set(succ(sourcemets)).intersection(items):
                                    only_source_to_target.append(list(items))
                    if only_source_to_target:
                        sourcefname = (folder_to_create +
                            'branched_pathways_from_source_' +
                            currenttarmet.replace(' ', '') + '_' +
                            'leq_plen_' + str(cutoff) + '.txt')
                        with open(sourcefname, 'w') as filetowrite:
                            print(
                                'Writing branched pathways (from source) to a file \n'
                                )
                            for currentidx, listentries in enumerate(
                                only_source_to_target):
                                filetowrite.write(str(currentidx + 1) + '\n')
                                filetowrite.write('Path length ' + str(len(
                                    listentries)) + '\n')
                                for entities in listentries:
                                    filetowrite.write(namemap[entities] +
                                        '\t' + ' + '.join(pred(entities)) +
                                        '->' + ' + '.join(succ(entities)))
                                    filetowrite.write('\n')
                                filetowrite.write('--------------------\n')
    else:
        print(currenttarmet, ': Target could not be found.')
        print('Consider changing the cut-off or the seed metabolite set')
