def main(args): log = logging.getLogger('root') hdlr = logging.StreamHandler(sys.stdout) log.addHandler(hdlr) log.setLevel(logging.getLevelName(args.loglevel.upper())) if args.input.endswith(".cs"): log.debug("Detected CryoSPARC 2+ .cs file") cs = np.load(args.input) try: df = metadata.parse_cryosparc_2_cs(cs, passthrough=args.passthrough, minphic=args.minphic) except (KeyError, ValueError) as e: log.error(e.message) log.error( "A passthrough file may be required (check inside the cryoSPARC 2+ job directory)" ) log.debug(e, exc_info=True) return 1 else: log.debug("Detected CryoSPARC 0.6.5 .csv file") meta = metadata.parse_cryosparc_065_csv( args.input) # Read cryosparc metadata file. df = metadata.cryosparc_065_csv2star(meta, args.minphic) if args.cls is not None: df = star.select_classes(df, args.cls) if args.copy_micrograph_coordinates is not None: coord_star = pd.concat( (star.parse_star(inp, keep_index=False) for inp in glob(args.copy_micrograph_coordinates)), join="inner") star.augment_star_ucsf(coord_star) star.augment_star_ucsf(df) key = star.merge_key(df, coord_star) log.debug("Coordinates merge key: %s" % key) if args.cached or key == star.Relion.IMAGE_NAME: fields = star.Relion.MICROGRAPH_COORDS else: fields = star.Relion.MICROGRAPH_COORDS + [ star.UCSF.IMAGE_INDEX, star.UCSF.IMAGE_PATH ] df = star.smart_merge(df, coord_star, fields=fields, key=key) star.simplify_star_ucsf(df) if args.micrograph_path is not None: df = star.replace_micrograph_path(df, args.micrograph_path, inplace=True) if args.transform is not None: r = np.array(json.loads(args.transform)) df = star.transform_star(df, r, inplace=True) # Write Relion .star file with correct headers. star.write_star(args.output, df, reindex=True) log.info("Output fields: %s" % ", ".join(df.columns)) return 0
def main(args): log = logging.getLogger('root') hdlr = logging.StreamHandler(sys.stdout) log.addHandler(hdlr) log.setLevel(logging.getLevelName(args.loglevel.upper())) if args.input[0].endswith(".cs"): log.debug("Detected CryoSPARC 2+ .cs file") cs = np.load(args.input[0]) try: df = metadata.parse_cryosparc_2_cs(cs, passthroughs=args.input[1:], minphic=args.minphic, boxsize=args.boxsize, swapxy=args.swapxy, invertx=args.invertx, inverty=args.inverty) except (KeyError, ValueError) as e: log.error(e, exc_info=True) log.error("Required fields could not be mapped. Are you using the right input file(s)?") return 1 else: log.debug("Detected CryoSPARC 0.6.5 .csv file") if len(args.input) > 1: log.error("Only one file at a time supported for CryoSPARC 0.6.5 .csv format") return 1 meta = metadata.parse_cryosparc_065_csv(args.input[0]) # Read cryosparc metadata file. df = metadata.cryosparc_065_csv2star(meta, args.minphic) if args.cls is not None: df = star.select_classes(df, args.cls) if args.copy_micrograph_coordinates is not None: df = star.augment_star_ucsf(df, inplace=True) coord_star = pd.concat( (star.parse_star(inp, keep_index=False, augment=True) for inp in glob(args.copy_micrograph_coordinates)), join="inner") key = star.merge_key(df, coord_star) log.debug("Coordinates merge key: %s" % key) if args.cached or key == star.Relion.IMAGE_NAME: fields = star.Relion.MICROGRAPH_COORDS else: fields = star.Relion.MICROGRAPH_COORDS + [star.UCSF.IMAGE_INDEX, star.UCSF.IMAGE_PATH] df = star.smart_merge(df, coord_star, fields=fields, key=key) star.simplify_star_ucsf(df) if args.micrograph_path is not None: df = star.replace_micrograph_path(df, args.micrograph_path, inplace=True) if args.transform is not None: r = np.array(json.loads(args.transform)) df = star.transform_star(df, r, inplace=True) df = star.check_defaults(df, inplace=True) if args.relion2: df = star.remove_new_relion31(df, inplace=True) star.write_star(args.output, df, resort_records=True, optics=False) else: df = star.remove_deprecated_relion2(df, inplace=True) star.write_star(args.output, df, resort_records=True, optics=True) log.info("Output fields: %s" % ", ".join(df.columns)) return 0
def main(args): if args.info: args.input.append(args.output) df = pd.concat( (star.parse_star(inp, augment=args.augment) for inp in args.input), join="inner") dfaux = None if args.cls is not None: df = star.select_classes(df, args.cls) if args.info: if star.is_particle_star(df) and star.Relion.CLASS in df.columns: c = df[star.Relion.CLASS].value_counts() print("%s particles in %d classes" % ("{:,}".format(df.shape[0]), len(c))) print(" ".join([ '%d: %s (%.2f %%)' % (i, "{:,}".format(s), 100. * s / c.sum()) for i, s in iteritems(c.sort_index()) ])) elif star.is_particle_star(df): print("%s particles" % "{:,}".format(df.shape[0])) if star.Relion.MICROGRAPH_NAME in df.columns: mgraphcnt = df[star.Relion.MICROGRAPH_NAME].value_counts() print( "%s micrographs, %s +/- %s particles per micrograph" % ("{:,}".format(len(mgraphcnt)), "{:,.3f}".format( np.mean(mgraphcnt)), "{:,.3f}".format(np.std(mgraphcnt)))) try: print("%f A/px (%sX magnification)" % (star.calculate_apix(df), "{:,.0f}".format( df[star.Relion.MAGNIFICATION][0]))) except KeyError: pass if len(df.columns.intersection(star.Relion.ORIGINS3D)) > 0: print("Largest shift is %f pixels" % np.max( np.abs(df[df.columns.intersection( star.Relion.ORIGINS3D)].values))) return 0 if args.drop_angles: df.drop(star.Relion.ANGLES, axis=1, inplace=True, errors="ignore") if args.drop_containing is not None: containing_fields = [ f for q in args.drop_containing for f in df.columns if q in f ] if args.invert: containing_fields = df.columns.difference(containing_fields) df.drop(containing_fields, axis=1, inplace=True, errors="ignore") if args.offset_group is not None: df[star.Relion.GROUPNUMBER] += args.offset_group if args.restack is not None: if not args.augment: star.augment_star_ucsf(df, inplace=True) star.set_original_fields(df, inplace=True) df[star.UCSF.IMAGE_PATH] = args.restack df[star.UCSF.IMAGE_INDEX] = np.arange(df.shape[0]) if args.subsample_micrographs is not None: if args.bootstrap is not None: print("Only particle sampling allows bootstrapping") return 1 mgraphs = df[star.Relion.MICROGRAPH_NAME].unique() if args.subsample_micrographs < 1: args.subsample_micrographs = np.int( max(np.round(args.subsample_micrographs * len(mgraphs)), 1)) else: args.subsample_micrographs = np.int(args.subsample_micrographs) ind = np.random.choice(len(mgraphs), size=args.subsample_micrographs, replace=False) mask = df[star.Relion.MICROGRAPH_NAME].isin(mgraphs[ind]) if args.auxout is not None: dfaux = df.loc[~mask] df = df.loc[mask] if args.subsample is not None and args.suffix == "": if args.subsample < 1: args.subsample = np.int( max(np.round(args.subsample * df.shape[0]), 1)) else: args.subsample = np.int(args.subsample) ind = np.random.choice(df.shape[0], size=args.subsample, replace=False) mask = df.index.isin(ind) if args.auxout is not None: dfaux = df.loc[~mask] df = df.loc[mask] if args.copy_angles is not None: angle_star = star.parse_star(args.copy_angles, augment=args.augment) df = star.smart_merge(df, angle_star, fields=star.Relion.ANGLES, key=args.merge_key) if args.copy_alignments is not None: align_star = star.parse_star(args.copy_alignments, augment=args.augment) df = star.smart_merge(df, align_star, fields=star.Relion.ALIGNMENTS, key=args.merge_key) if args.copy_reconstruct_images is not None: recon_star = star.parse_star(args.copy_reconstruct_images, augment=args.augment) df[star.Relion.RECONSTRUCT_IMAGE_NAME] = recon_star[ star.Relion.IMAGE_NAME] if args.transform is not None: if args.transform.count(",") == 2: r = geom.euler2rot( *np.deg2rad([np.double(s) for s in args.transform.split(",")])) else: r = np.array(json.loads(args.transform)) df = star.transform_star(df, r, inplace=True) if args.invert_hand: df = star.invert_hand(df, inplace=True) if args.copy_paths is not None: path_star = star.parse_star(args.copy_paths) star.set_original_fields(df, inplace=True) df[star.Relion.IMAGE_NAME] = path_star[star.Relion.IMAGE_NAME] if args.copy_ctf is not None: ctf_star = pd.concat((star.parse_star(inp, augment=args.augment) for inp in glob.glob(args.copy_ctf)), join="inner") df = star.smart_merge(df, ctf_star, star.Relion.CTF_PARAMS, key=args.merge_key) if args.copy_micrograph_coordinates is not None: coord_star = pd.concat( (star.parse_star(inp, augment=args.augment) for inp in glob.glob(args.copy_micrograph_coordinates)), join="inner") df = star.smart_merge(df, coord_star, fields=star.Relion.MICROGRAPH_COORDS, key=args.merge_key) if args.scale is not None: star.scale_coordinates(df, args.scale, inplace=True) star.scale_origins(df, args.scale, inplace=True) star.scale_magnification(df, args.scale, inplace=True) if args.scale_particles is not None: star.scale_origins(df, args.scale_particles, inplace=True) star.scale_magnification(df, args.scale_particles, inplace=True) if args.scale_coordinates is not None: star.scale_coordinates(df, args.scale_coordinates, inplace=True) if args.scale_origins is not None: star.scale_origins(df, args.scale_origins, inplace=True) if args.scale_magnification is not None: star.scale_magnification(df, args.scale_magnification, inplace=True) if args.scale_apix is not None: star.scale_apix(df, args.scale_apix, inplace=True) if args.recenter: df = star.recenter(df, inplace=True) if args.zero_origins: df = star.zero_origins(df, inplace=True) if args.pick: df.drop(df.columns.difference(star.Relion.PICK_PARAMS), axis=1, inplace=True, errors="ignore") if args.subsample is not None and args.suffix != "": if args.subsample < 1: print("Specific integer sample size") return 1 nsamplings = args.bootstrap if args.bootstrap is not None else df.shape[ 0] / np.int(args.subsample) inds = np.random.choice(df.shape[0], size=(nsamplings, np.int(args.subsample)), replace=args.bootstrap is not None) for i, ind in enumerate(inds): star.write_star( os.path.join( args.output, os.path.basename(args.input[0])[:-5] + args.suffix + "_%d" % (i + 1)), df.iloc[ind]) if args.to_micrographs: df = star.to_micrographs(df) if args.micrograph_range: df.set_index(star.Relion.MICROGRAPH_NAME, inplace=True) m, n = [int(tok) for tok in args.micrograph_range.split(",")] mg = df.index.unique().sort_values() outside = list(range(0, m)) + list(range(n, len(mg))) dfaux = df.loc[mg[outside]].reset_index() df = df.loc[mg[m:n]].reset_index() if args.micrograph_path is not None: df = star.replace_micrograph_path(df, args.micrograph_path, inplace=True) if args.min_separation is not None: gb = df.groupby(star.Relion.MICROGRAPH_NAME) dupes = [] for n, g in gb: nb = algo.query_connected( g[star.Relion.COORDS].values - g[star.Relion.ORIGINS], args.min_separation / star.calculate_apix(df)) dupes.extend(g.index[~np.isnan(nb)]) dfaux = df.loc[dupes] df.drop(dupes, inplace=True) if args.merge_source is not None: if args.merge_fields is not None: if "," in args.merge_fields: args.merge_fields = args.merge_fields.split(",") else: args.merge_fields = [args.merge_fields] else: print("Merge fields must be specified using --merge-fields") return 1 if args.merge_key is not None: if "," in args.merge_key: args.merge_key = args.merge_key.split(",") if args.by_original: args.by_original = star.original_field(args.merge_key) else: args.by_original = args.merge_key merge_star = star.parse_star(args.merge_source, augment=args.augment) df = star.smart_merge(df, merge_star, fields=args.merge_fields, key=args.merge_key, left_key=args.by_original) if args.revert_original: df = star.revert_original(df, inplace=True) if args.set_optics is not None: tok = args.set_optics.split(",") df = star.set_optics_groups(df, sep=tok[0], idx=int(tok[1]), inplace=True) df.dropna(axis=0, how="any", inplace=True) if args.drop_optics_group is not None: idx = df[star.Relion.OPTICSGROUP].isin(args.drop_optics_group) if not np.any(idx): idx = df[star.Relion.OPTICSGROUPNAME].isin(args.drop_optics_group) if not np.any(idx): print("No group found to drop") return 1 df = df.loc[~idx] if args.split_micrographs: dfs = star.split_micrographs(df) for mg in dfs: star.write_star( os.path.join(args.output, os.path.basename(mg)[:-4]) + args.suffix, dfs[mg]) return 0 if args.auxout is not None and dfaux is not None: if not args.relion2: df = star.remove_deprecated_relion2(dfaux, inplace=True) star.write_star(args.output, df, resort_records=args.sort, simplify=args.augment_output, optics=True) else: df = star.remove_new_relion31(dfaux, inplace=True) star.write_star(args.output, df, resort_records=args.sort, simplify=args.augment_output, optics=False) if args.output is not None: if not args.relion2: # Relion 3.1 style output. df = star.remove_deprecated_relion2(df, inplace=True) star.write_star(args.output, df, resort_records=args.sort, simplify=args.augment_output, optics=True) else: df = star.remove_new_relion31(df, inplace=True) star.write_star(args.output, df, resort_records=args.sort, simplify=args.augment_output, optics=False) return 0
def main(args): if args.info: args.input.append(args.output) df = pd.concat( (star.parse_star(inp, augment=args.augment) for inp in args.input), join="inner") dfaux = None if args.cls is not None: df = star.select_classes(df, args.cls) if args.info: if star.is_particle_star(df) and star.Relion.CLASS in df.columns: c = df[star.Relion.CLASS].value_counts() print("%s particles in %d classes" % ("{:,}".format(df.shape[0]), len(c))) print(" ".join([ '%d: %s (%.2f %%)' % (i, "{:,}".format(s), 100. * s / c.sum()) for i, s in c.sort_index().iteritems() ])) elif star.is_particle_star(df): print("%s particles" % "{:,}".format(df.shape[0])) if star.Relion.MICROGRAPH_NAME in df.columns: mgraphcnt = df[star.Relion.MICROGRAPH_NAME].value_counts() print( "%s micrographs, %s +/- %s particles per micrograph" % ("{:,}".format(len(mgraphcnt)), "{:,.3f}".format( np.mean(mgraphcnt)), "{:,.3f}".format(np.std(mgraphcnt)))) try: print("%f A/px (%sX magnification)" % (star.calculate_apix(df), "{:,.0f}".format( df[star.Relion.MAGNIFICATION][0]))) except KeyError: pass return 0 if args.drop_angles: df.drop(star.Relion.ANGLES, axis=1, inplace=True, errors="ignore") if args.drop_containing is not None: containing_fields = [ f for q in args.drop_containing for f in df.columns if q in f ] if args.invert: containing_fields = df.columns.difference(containing_fields) df.drop(containing_fields, axis=1, inplace=True, errors="ignore") if args.offset_group is not None: df[star.Relion.GROUPNUMBER] += args.offset_group if args.subsample_micrographs is not None: if args.bootstrap is not None: print("Only particle sampling allows bootstrapping") return 1 mgraphs = df[star.Relion.MICROGRAPH_NAME].unique() if args.subsample_micrographs < 1: args.subsample_micrographs = np.int( max(np.round(args.subsample_micrographs * len(mgraphs)), 1)) else: args.subsample_micrographs = np.int(args.subsample_micrographs) ind = np.random.choice(len(mgraphs), size=args.subsample_micrographs, replace=False) mask = df[star.Relion.MICROGRAPH_NAME].isin(mgraphs[ind]) if args.auxout is not None: dfaux = df.loc[~mask] df = df.loc[mask] if args.subsample is not None and args.suffix == "": if args.subsample < 1: args.subsample = np.int( max(np.round(args.subsample * df.shape[0]), 1)) else: args.subsample = np.int(args.subsample) ind = np.random.choice(df.shape[0], size=args.subsample, replace=False) mask = df.index.isin(ind) if args.auxout is not None: dfaux = df.loc[~mask] df = df.loc[mask] if args.copy_angles is not None: angle_star = star.parse_star(args.copy_angles, augment=args.augment) df = star.smart_merge(df, angle_star, fields=star.Relion.ANGLES) if args.transform is not None: if args.transform.count(",") == 2: r = star.euler2rot( *np.deg2rad([np.double(s) for s in args.transform.split(",")])) else: r = np.array(json.loads(args.transform)) df = star.transform_star(df, r, inplace=True) if args.invert_hand: df[star.Relion.ANGLEROT] = -df[star.Relion.ANGLEROT] df[star.Relion.ANGLETILT] = 180 - df[star.Relion.ANGLETILT] if args.copy_paths is not None: path_star = star.parse_star(args.copy_paths) df[star.Relion.IMAGE_NAME] = path_star[star.Relion.IMAGE_NAME] if args.copy_ctf is not None: ctf_star = pd.concat((star.parse_star(inp, augment=args.augment) for inp in glob.glob(args.copy_ctf)), join="inner") df = star.smart_merge(df, ctf_star, star.Relion.CTF_PARAMS) if args.copy_micrograph_coordinates is not None: coord_star = pd.concat( (star.parse_star(inp, augment=args.augment) for inp in glob.glob(args.copy_micrograph_coordinates)), join="inner") df = star.smart_merge(df, coord_star, fields=star.Relion.MICROGRAPH_COORDS) if args.scale is not None: star.scale_coordinates(df, args.scale, inplace=True) star.scale_origins(df, args.scale, inplace=True) star.scale_magnification(df, args.scale, inplace=True) if args.scale_particles is not None: star.scale_origins(df, args.scale, inplace=True) star.scale_magnification(df, args.scale, inplace=True) if args.scale_coordinates is not None: star.scale_coordinates(df, args.scale_coordinates, inplace=True) if args.scale_origins is not None: star.scale_origins(df, args.scale_origins, inplace=True) if args.scale_magnification is not None: star.scale_magnification(df, args.scale_magnfication, inplace=True) if args.recenter: df = star.recenter(df, inplace=True) if args.zero_origins: df = star.zero_origins(df, inplace=True) if args.pick: df.drop(df.columns.difference(star.Relion.PICK_PARAMS), axis=1, inplace=True, errors="ignore") if args.subsample is not None and args.suffix != "": if args.subsample < 1: print("Specific integer sample size") return 1 nsamplings = args.bootstrap if args.bootstrap is not None else df.shape[ 0] / np.int(args.subsample) inds = np.random.choice(df.shape[0], size=(nsamplings, np.int(args.subsample)), replace=args.bootstrap is not None) for i, ind in enumerate(inds): star.write_star( os.path.join( args.output, os.path.basename(args.input[0])[:-5] + args.suffix + "_%d" % (i + 1)), df.iloc[ind]) if args.to_micrographs: gb = df.groupby(star.Relion.MICROGRAPH_NAME) mu = gb.mean() df = mu[[ c for c in star.Relion.CTF_PARAMS + star.Relion.MICROSCOPE_PARAMS + [star.Relion.MICROGRAPH_NAME] if c in mu ]].reset_index() if args.micrograph_range: df.set_index(star.Relion.MICROGRAPH_NAME, inplace=True) m, n = [int(tok) for tok in args.micrograph_range.split(",")] mg = df.index.unique().sort_values() outside = list(range(0, m)) + list(range(n, len(mg))) dfaux = df.loc[mg[outside]].reset_index() df = df.loc[mg[m:n]].reset_index() if args.micrograph_path is not None: df = star.replace_micrograph_path(df, args.micrograph_path, inplace=True) if args.min_separation is not None: gb = df.groupby(star.Relion.MICROGRAPH_NAME) dupes = [] for n, g in gb: nb = algo.query_connected( g[star.Relion.COORDS], args.min_separation / star.calculate_apix(df)) dupes.extend(g.index[~np.isnan(nb)]) dfaux = df.loc[dupes] df.drop(dupes, inplace=True) if args.merge_source is not None: if args.merge_fields is not None: if "," in args.merge_fields: args.merge_fields = args.merge_fields.split(",") else: args.merge_fields = [args.merge_fields] else: print("Merge fields must be specified using --merge-fields") return 1 if args.merge_key is not None: if "," in args.merge_key: args.merge_key = args.merge_key.split(",") merge_star = star.parse_star(args.merge_source, augment=args.augment) df = star.smart_merge(df, merge_star, fields=args.merge_fields, key=args.merge_key) if args.split_micrographs: dfs = star.split_micrographs(df) for mg in dfs: star.write_star( os.path.join(args.output, os.path.basename(mg)[:-4]) + args.suffix, dfs[mg]) return 0 if args.auxout is not None and dfaux is not None: star.write_star(args.auxout, dfaux, simplify=args.augment) if args.output is not None: star.write_star(args.output, df, simplify=args.augment) return 0