Skip to content

Instantly share code, notes, and snippets.

@fedarko
Last active September 26, 2019 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedarko/6135b39c859b2fcc7891dbe0fa3c4b64 to your computer and use it in GitHub Desktop.
Save fedarko/6135b39c859b2fcc7891dbe0fa3c4b64 to your computer and use it in GitHub Desktop.
Splits up a QIIME 2 metadata file into separate metadata files, such that there is one file per specified "run" column. This is useful if multiple samples from different runs share barcode sequences, which can make QIIME 2 angry.
# NOTE: Assumes that there's a SAMPLE_METADATA environment variable declared pointing to a metadata file
# NOTE: Assumes that this metadata file contains BarcodeSequence and seq_run_ord columns
import pandas as pd
import os
md = pd.read_csv(os.environ["SAMPLE_METADATA"], sep="\t", index_col=0)
print("There are {} unique barcode sequences in this metadata file.".format(len(md["BarcodeSequence"].unique())))
runs = tuple(md["seq_run_ord"].unique())
print("Also, the {} runs listed in this metadata file are {}.".format(len(runs), runs))
for run_id in runs:
md_subset = md[md["seq_run_ord"] == run_id]
md_subset_name = "metadata-{}.tsv".format(run_id)
md_subset.to_csv(md_subset_name, sep="\t")
print("Generated a subset of the metadata called {}".format(md_subset_name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment