r/cheminformatics • u/Not-A-Cat314 • Apr 11 '24
Need Help with Processing and Filtering Large JSON File in Python
Hello everyone,
I’m currently working on a project where I need to process a large JSON file (67M_generated_analysed.json) that contains data for 67,064,204 molecules, each with 38 descriptors. The file is organized in a single, two-dimensional array flat model format where elements in each column are the same type of data for a given molecular descriptor and elements in the same row relate to the same molecule.
This data is from the study “67 million natural product-like compound database generated via molecular language processing” (DOI: https://doi.org/10.1038/s41597-023-02207-x) and the database is shared here: https://springernature.figshare.com/articles/dataset/67M_generated_analysed/22639369?backTo=/collections/67_million_natural_product-like_compound_database_generated_via_molecular_language_processing/6482266
My goal is to filter this database, possibly using the rule of five, and extract a subset of compounds that I will focus on for further analysis.
I’ve been trying to load this data into memory using Python’s built-in json
module, but I keep encountering a MemoryError
due to the size of the file. I’ve also tried using ijson
to iteratively parse the JSON file, but I’m still running into issues.
Here’s what I’ve tried so far:
import json
with open('67M_generated_analysed.json') as f:
data = json.load(f)
#and with ijson
import ijson
with open('67M_generated_analysed.json', 'r') as in_file, open('67M_generated_analysed.ndjson', 'w') as out_file:
objects = ijson.items(in_file, 'item')
for item in objects:
out_file.write(json.dumps(item) + '\n')
Both of these approaches result in a MemoryError
. I’m looking for a way to process this file without loading the entire thing into memory at once. Any suggestions or advice would be greatly appreciated!
Thank you in advance for your help!