# Path: bioframe/core/construction.py
# def make_viewframe(
#     regions,
#     check_bounds=None,
#     name_style=None,
#     view_name_col="name",
#     cols=None,
# ):
#     """
#     Makes and validates a dataframe `view_df` out of regions.
# 
#     Parameters
#     ----------
#     regions : supported input type
#         Currently supported input types:
# 
#             - a dictionary where keys are strings and values are integers {str:int}, specifying regions (chrom, 0, end, chrom)
#             - a pandas series of chromosomes lengths with index specifying region names
#             - a list of tuples [(chrom,start,end), ...] or [(chrom,start,end,name), ...]
#             - a pandas DataFrame, skips to validation step
# 
#     name_style : None or "ucsc"
#         If None and no column view_name_col, propagate values from cols[0]
#         If "ucsc" and no column view_name_col, create UCSC style names
# 
#     check_bounds : None, or chromosome sizes provided as any of valid formats above
#         Optional, if provided checks if regions in the view are contained by regions
#         supplied in check_bounds, typically provided as a series of chromosome sizes.
#         Default None.
# 
#     view_name_col : str
#         Specifies column name of the view regions. Default 'name'.
# 
#     cols : (str, str, str) or None
#         The names of columns containing the chromosome, start and end of the
#         genomic intervals, provided separately for each set. The default
#         values are 'chrom', 'start', 'end'.
# 
#     Returns
#     -------
#     view_df:dataframe satisfying properties of a view
# 
#     """
#     ck1, sk1, ek1 = _get_default_colnames() if cols is None else cols
# 
#     view_df = from_any(regions, name_col=view_name_col, cols=cols)
# 
#     if check_bounds is not None:
#         bounds_df = from_any(check_bounds, name_col="bounds", cols=cols)
#         if not checks.is_contained(
#             view_df,
#             bounds_df,
#             df_view_col=None,
#             view_name_col="bounds",
#             cols=cols,
#         ):
#             raise ValueError(
#                 "Invalid input to make a viewFrame, regions not contained by bounds"
#             )
# 
#     if not view_name_col in view_df.columns:
#         if name_style is None:
#             view_df[view_name_col] = view_df[ck1].values
#         elif name_style.lower() == "ucsc":
#             view_df = add_ucsc_name_column(view_df, name_col=view_name_col, cols=cols)
#         else:
#             raise ValueError("unknown value for name_style")
# 
#     if checks.is_viewframe(
#         view_df, view_name_col=view_name_col, cols=cols, raise_errors=True
#     ):
#         return view_df
#     else:
#         raise ValueError("could not make valid viewFrame, retry with new input")


from io import StringIO
from bioframe.core.construction import make_viewframe
import pandas as pd
import numpy as np
import pytest
import bioframe
import bioframe.core.checks as checks
    assert bioframe.merge(df1)["end"].iloc[0] == 12
    assert bioframe.merge(df1, on=["strand"]).shape[0] == df1.shape[0]
    assert bioframe.merge(df1, on=["animal"]).shape[0] == df1.shape[0]
    assert bioframe.merge(df1, on=["animal"]).shape[1] == df1.shape[1] + 1
    assert checks.is_bedframe(bioframe.merge(df1, on=["strand", "animal"]))


def test_complement():
    ### complementing a df with no intervals in chrX by a view with chrX should return entire chrX region
    df1 = pd.DataFrame(
        [["chr1", 1, 5], ["chr1", 3, 8], ["chr1", 8, 10], ["chr1", 12, 14]],
        columns=["chrom", "start", "end"],
    )
    df1_chromsizes = {"chr1": 100, "chrX": 100}

    df1_complement = pd.DataFrame(
        [
            ["chr1", 0, 1, "chr1"],
            ["chr1", 10, 12, "chr1"],
            ["chr1", 14, 100, "chr1"],
            ["chrX", 0, 100, "chrX"],
        ],
        columns=["chrom", "start", "end", "view_region"],
    )

    pd.testing.assert_frame_equal(
        bioframe.complement(df1, view_df=df1_chromsizes), df1_complement
    )

    # non-default columns in view

# Path: bioframe/core/construction.py
# def make_viewframe(
#     regions,
#     check_bounds=None,
#     name_style=None,
#     view_name_col="name",
#     cols=None,
# ):
#     """
#     Makes and validates a dataframe `view_df` out of regions.
# 
#     Parameters
#     ----------
#     regions : supported input type
#         Currently supported input types:
# 
#             - a dictionary where keys are strings and values are integers {str:int}, specifying regions (chrom, 0, end, chrom)
#             - a pandas series of chromosomes lengths with index specifying region names
#             - a list of tuples [(chrom,start,end), ...] or [(chrom,start,end,name), ...]
#             - a pandas DataFrame, skips to validation step
# 
#     name_style : None or "ucsc"
#         If None and no column view_name_col, propagate values from cols[0]
#         If "ucsc" and no column view_name_col, create UCSC style names
# 
#     check_bounds : None, or chromosome sizes provided as any of valid formats above
#         Optional, if provided checks if regions in the view are contained by regions
#         supplied in check_bounds, typically provided as a series of chromosome sizes.
#         Default None.
# 
#     view_name_col : str
#         Specifies column name of the view regions. Default 'name'.
# 
#     cols : (str, str, str) or None
#         The names of columns containing the chromosome, start and end of the
#         genomic intervals, provided separately for each set. The default
#         values are 'chrom', 'start', 'end'.
# 
#     Returns
#     -------
#     view_df:dataframe satisfying properties of a view
# 
#     """
#     ck1, sk1, ek1 = _get_default_colnames() if cols is None else cols
# 
#     view_df = from_any(regions, name_col=view_name_col, cols=cols)
# 
#     if check_bounds is not None:
#         bounds_df = from_any(check_bounds, name_col="bounds", cols=cols)
#         if not checks.is_contained(
#             view_df,
#             bounds_df,
#             df_view_col=None,
#             view_name_col="bounds",
#             cols=cols,
#         ):
#             raise ValueError(
#                 "Invalid input to make a viewFrame, regions not contained by bounds"
#             )
# 
#     if not view_name_col in view_df.columns:
#         if name_style is None:
#             view_df[view_name_col] = view_df[ck1].values
#         elif name_style.lower() == "ucsc":
#             view_df = add_ucsc_name_column(view_df, name_col=view_name_col, cols=cols)
#         else:
#             raise ValueError("unknown value for name_style")
# 
#     if checks.is_viewframe(
#         view_df, view_name_col=view_name_col, cols=cols, raise_errors=True
#     ):
#         return view_df
#     else:
#         raise ValueError("could not make valid viewFrame, retry with new input")


# Path: tests/test_ops.py
from io import StringIO
from bioframe.core.construction import make_viewframe
import pandas as pd
import numpy as np
import pytest
import bioframe
import bioframe.core.checks as checks

    assert bioframe.merge(df1)["end"].iloc[0] == 12
    assert bioframe.merge(df1, on=["strand"]).shape[0] == df1.shape[0]
    assert bioframe.merge(df1, on=["animal"]).shape[0] == df1.shape[0]
    assert bioframe.merge(df1, on=["animal"]).shape[1] == df1.shape[1] + 1
    assert checks.is_bedframe(bioframe.merge(df1, on=["strand", "animal"]))


def test_complement():
    ### complementing a df with no intervals in chrX by a view with chrX should return entire chrX region
    df1 = pd.DataFrame(
        [["chr1", 1, 5], ["chr1", 3, 8], ["chr1", 8, 10], ["chr1", 12, 14]],
        columns=["chrom", "start", "end"],
    )
    df1_chromsizes = {"chr1": 100, "chrX": 100}

    df1_complement = pd.DataFrame(
        [
            ["chr1", 0, 1, "chr1"],
            ["chr1", 10, 12, "chr1"],
            ["chr1", 14, 100, "chr1"],
            ["chrX", 0, 100, "chrX"],
        ],
        columns=["chrom", "start", "end", "view_region"],
    )

    pd.testing.assert_frame_equal(
        bioframe.complement(df1, view_df=df1_chromsizes), df1_complement
    )

    # non-default columns in view

    funky_view = make_viewframe(df1_chromsizes, cols=["chr", "start", "end"])