# Path: pyem/star.py
# def parse_star(starfile, keep_index=False, augment=True, nrows=sys.maxsize):
#     tables = star_table_offsets(starfile)
#     dfs = {t: parse_star_table(starfile, offset=tables[t][0], nrows=min(tables[t][3], nrows), keep_index=keep_index)
#            for t in tables}
#     if Relion.OPTICDATA in dfs:
#         if Relion.PARTICLEDATA in dfs:
#             data_table = Relion.PARTICLEDATA
#         elif Relion.MICROGRAPHDATA in dfs:
#             data_table = Relion.MICROGRAPHDATA
#         elif Relion.IMAGEDATA in dfs:
#             data_table = Relion.IMAGEDATA
#         else:
#             data_table = None
#         if data_table is not None:
#             df = pd.merge(dfs[Relion.OPTICDATA], dfs[data_table], on=Relion.OPTICSGROUP)
#         else:
#             df = dfs[Relion.OPTICDATA]
#     else:
#         df = dfs[next(iter(dfs))]
#     df = check_defaults(df, inplace=True)
#     if augment:
#         augment_star_ucsf(df, inplace=True)
#     return df
# 
# Path: pyem/star.py
# def write_star(starfile, df, resort_fields=True, resort_records=False, simplify=True, optics=True):
#     if not starfile.endswith(".star"):
#         starfile += ".star"
#     if resort_records:
#         df = sort_records(df, inplace=True)
#     if simplify and len([c for c in df.columns if "ucsf" in c or "eman" in c]) > 0:
#         df = simplify_star_ucsf(df)
# 
#     if optics:
#         if Relion.OPTICSGROUP not in df:
#             df[Relion.OPTICSGROUP] = 1
#         gb = df.groupby(Relion.OPTICSGROUP)
#         df_optics = gb[df.columns.intersection(Relion.OPTICSGROUPTABLE)].first().reset_index(drop=False)
#         df = df.drop(columns=Relion.OPTICSGROUPTABLE, errors="ignore")
#         data_table = Relion.PARTICLEDATA if is_particle_star(df) else Relion.MICROGRAPHDATA
#         dfs = {Relion.OPTICDATA: df_optics, data_table: df}
#         write_star_tables(starfile, dfs, resort_fields=resort_fields)
#     else:
#         write_star_table(starfile, df, table=Relion.IMAGEDATA, resort_fields=resort_fields)


import numpy as np
import sys
    import argparse
from pyem.star import parse_star
from pyem.star import write_star
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


def main(args):
    df = parse_star(args.input, keep_index=False)

    if args.cls is not None:
        clsfields = [f for f in df.columns if "ClassNumber" in f]
        if len(clsfields) == 0:
            print("No class labels found")
            return 1
        ind = df[clsfields[0]].isin(args.cls)
        if not np.any(ind):
            print("Specified class has no members")
            return 1
        df = df.loc[ind]

    if args.max_astigmatism is not None:
        astigmatism = df["rlnDefocusU"] - df["rlnDefocusV"]
        ind = astigmatism <= args.max_astigmatism
        df = df.loc[ind]

    if args.max_resolution is not None:
        if "rlnFinalResolution" in df.columns:
            ind = df["rlnFinalResolution"] <= args.max_resolution
        elif "rlnCtfMaxResolution" in df.columns:
            ind = df["rlnCtfMaxResolution"] <= args.max_resolution
        else:
            print("No CTF resolution field found in input")

# Path: pyem/star.py
# def parse_star(starfile, keep_index=False, augment=True, nrows=sys.maxsize):
#     tables = star_table_offsets(starfile)
#     dfs = {t: parse_star_table(starfile, offset=tables[t][0], nrows=min(tables[t][3], nrows), keep_index=keep_index)
#            for t in tables}
#     if Relion.OPTICDATA in dfs:
#         if Relion.PARTICLEDATA in dfs:
#             data_table = Relion.PARTICLEDATA
#         elif Relion.MICROGRAPHDATA in dfs:
#             data_table = Relion.MICROGRAPHDATA
#         elif Relion.IMAGEDATA in dfs:
#             data_table = Relion.IMAGEDATA
#         else:
#             data_table = None
#         if data_table is not None:
#             df = pd.merge(dfs[Relion.OPTICDATA], dfs[data_table], on=Relion.OPTICSGROUP)
#         else:
#             df = dfs[Relion.OPTICDATA]
#     else:
#         df = dfs[next(iter(dfs))]
#     df = check_defaults(df, inplace=True)
#     if augment:
#         augment_star_ucsf(df, inplace=True)
#     return df
# 
# Path: pyem/star.py
# def write_star(starfile, df, resort_fields=True, resort_records=False, simplify=True, optics=True):
#     if not starfile.endswith(".star"):
#         starfile += ".star"
#     if resort_records:
#         df = sort_records(df, inplace=True)
#     if simplify and len([c for c in df.columns if "ucsf" in c or "eman" in c]) > 0:
#         df = simplify_star_ucsf(df)
# 
#     if optics:
#         if Relion.OPTICSGROUP not in df:
#             df[Relion.OPTICSGROUP] = 1
#         gb = df.groupby(Relion.OPTICSGROUP)
#         df_optics = gb[df.columns.intersection(Relion.OPTICSGROUPTABLE)].first().reset_index(drop=False)
#         df = df.drop(columns=Relion.OPTICSGROUPTABLE, errors="ignore")
#         data_table = Relion.PARTICLEDATA if is_particle_star(df) else Relion.MICROGRAPHDATA
#         dfs = {Relion.OPTICDATA: df_optics, data_table: df}
#         write_star_tables(starfile, dfs, resort_fields=resort_fields)
#     else:
#         write_star_table(starfile, df, table=Relion.IMAGEDATA, resort_fields=resort_fields)


# Path: subset.py
import numpy as np
import sys
    import argparse
from pyem.star import parse_star
from pyem.star import write_star

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


def main(args):
    df = parse_star(args.input, keep_index=False)

    if args.cls is not None:
        clsfields = [f for f in df.columns if "ClassNumber" in f]
        if len(clsfields) == 0:
            print("No class labels found")
            return 1
        ind = df[clsfields[0]].isin(args.cls)
        if not np.any(ind):
            print("Specified class has no members")
            return 1
        df = df.loc[ind]

    if args.max_astigmatism is not None:
        astigmatism = df["rlnDefocusU"] - df["rlnDefocusV"]
        ind = astigmatism <= args.max_astigmatism
        df = df.loc[ind]

    if args.max_resolution is not None:
        if "rlnFinalResolution" in df.columns:
            ind = df["rlnFinalResolution"] <= args.max_resolution
        elif "rlnCtfMaxResolution" in df.columns:
            ind = df["rlnCtfMaxResolution"] <= args.max_resolution
        else:
            print("No CTF resolution field found in input")

            return 1