### Install BitBIRCH Package Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Installs the bitbirch package from GitHub using git clone and pip install -e. Requires a terminal environment. ```shell git clone https://github.com/mqcomplab/bitbirch.git cd bitbirch pip install -e . ``` -------------------------------- ### Install BitBirch Source: https://github.com/mqcomplab/bitbirch/blob/main/README.md Clones the BitBirch repository from GitHub and installs it using pip. This is the standard procedure for setting up the project on your local machine. ```bash git clone https://github.com/mqcomplab/bitbirch.git cd bitbirch pip install -e . ``` -------------------------------- ### Generate Molecular Fingerprints with RDKit Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Converts SMILES strings to RDKit molecules and then generates binary fingerprints. Requires RDKit to be installed. ```python mols = [Chem.MolFromSmiles(S) for S in smiles] fps = np.array([Chem.RDKFingerprint(mol) for mol in mols]) ``` -------------------------------- ### Example Usage of population_plot Function in Python Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Demonstrates how to call the `population_plot` function with pre-sorted `clustered_ids` and `smiles` data, specifying the number of top clusters to visualize. This snippet shows a practical application of the plotting function. ```python population_plot(clustered_ids, smiles, top_clusters=20) ``` -------------------------------- ### Get Number of Clusters and Clusters with More Than 10 Molecules in Python Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Calculates and prints the total number of identified clusters and counts how many of these clusters contain more than 10 molecules. This provides a quick statistical overview of the clustering results. ```python print(f'Number of clusters: {len(clustered_ids)}') print(f'number of clusters with more than 10 molecules: {len([cluster for cluster in clustered_ids if len(cluster) > 10])}') ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Imports core libraries for data manipulation, molecular processing, and BitBIRCH functionality. Requires numpy, rdkit, and bitbirch. ```python import numpy as np from rdkit import Chem import bitbirch.bitbirch as bb import bitbirch.plotting_utils as plotting_utils import pandas as pd ``` -------------------------------- ### Initialize BitBirch Clustering Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Sets the merging strategy to 'diameter' and initializes a BitBirch object with specified threshold and branching factor, then fits it to the fingerprints. ```python branching_factor = 50 threshold = 0.65 bb.set_merge('diameter') brc = bb.BitBirch(threshold=threshold, branching_factor=branching_factor) brc.fit(fps) ``` -------------------------------- ### Refine Clustering with Tolerance Merging Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Prepares data for refinement by splitting into well-defined and large clusters, then refits using 'tolerance' merging with a higher threshold. Uses fit_BFs for incremental fitting. ```python # Prepare the data from the first tree to refit rest, big = brc.prepare_data_BFs(fps) # Delete the initial tree to save memory #del brc # Create a new tree, this time with the data from the first tree and using tolerance merging and a higher threshold bb.set_merge('tolerance', 0.00) threshold = 0.70 brc_refined = bb.BitBirch(threshold=threshold, branching_factor=branching_factor) # Fit the new tree, notice that for this we are using fit_BFs, since we took the BitFeatures from the first tree brc_refined.fit_BFs(rest) brc_refined.fit_BFs(big) ``` -------------------------------- ### Save Metrics and Initialize Plotting Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Saves clustering metrics to a CSV file and initializes plotting for the 'diameter' method. Requires the bitbirch.plotting_utils module. ```python plotting_utils.save_metrics_to_csv(brc, fps, smiles, method='diameter') plotting_utils.init_plot('diameter') ``` -------------------------------- ### Save Refined Metrics and Initialize Plotting Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Saves clustering metrics for the refined results to a CSV file and initializes plotting for the 'refined' method. Requires the bitbirch.plotting_utils module. ```python plotting_utils.save_metrics_to_csv(brc_refined, fps, smiles, method='refined') plotting_utils.init_plot('refined') ``` -------------------------------- ### Analyze Cluster Sizes Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Retrieves molecule IDs for each cluster and counts the total number of clusters and the number of clusters containing more than 10 molecules. ```python mol_ids = brc_refined.get_cluster_mol_ids() print("Number of clusters: ", len(mol_ids)) # Get the clusters with more than 10 molecules clusters = [cluster for cluster in mol_ids if len(cluster) > 10] print("Number of clusters with more than 10 molecules: ", len(clusters)) ``` -------------------------------- ### Load Pre-computed Fingerprints Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Loads pre-computed molecular fingerprints from a .npy file using memory-mapping. Assumes the file path is correct. ```python fps = np.load('/home/kenneth/Documents/birch/bitbirch_sandbox/data/chembl_33_np.npy', mmap_mode='r') ``` -------------------------------- ### Load Clustered IDs Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Loads the clustered IDs from a pickle file named 'clustered_ids_parallel.pkl'. Requires the 'pickle' library. ```python import pickle with open('clustered_ids_parallel.pkl', 'rb') as f: clustered_ids = pickle.load(f) ``` -------------------------------- ### Split and Save Fingerprints Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Splits the generated fingerprints and their corresponding indices into a specified number of batches. Saves each batch as a .npy file in the 'fps' directory and records the index ranges in 'fps_splits.txt'. Requires 'numpy' and 'os'. ```python import numpy as np import os n_splits = 5 fps_splits = np.array_split(fps, n_splits) index_splits = np.array_split(np.arange(len(fps)), n_splits) os.makedirs('fps', exist_ok=True) with open('fps_splits.txt', 'w') as f: for i, (index_split, fps_split) in enumerate(zip(index_splits, fps_splits)): initial_index = index_split[0] final_index = index_split[-1] f.write(f'{initial_index}_{final_index}\n') np.save(f'fps/fps_{initial_index}_{final_index}.npy', fps_split) ``` -------------------------------- ### Visualize Molecules in a Specific Cluster Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Generates images to visually inspect molecules within a specified cluster. The function splits molecules into multiple images for better readability. Requires the bitbirch.plotting_utils module. ```python plotting_utils.mol_visualization(smiles, brc_refined, cluster=1243) ``` -------------------------------- ### Load Fingerprint Batches Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Reads all .npy files from the 'fps/' directory and concatenates them into a single NumPy array. Requires 'glob' and 'numpy'. ```python import glob import numpy as np fps = [] for file in glob.glob('fps/*.npy'): fps.extend(np.load(file)) fps = np.array(fps) ``` -------------------------------- ### Generate and Save Final Assignments Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Generates cluster assignments for all molecules and saves them along with their SMILES strings to a CSV file named 'smiles_assignments.csv'. ```python assignments = brc_refined.get_assignments(n_mols=n_mols) # Save assignments to our smiles df = pd.DataFrame({'smiles': smiles, 'assignments': assignments}) df.to_csv('smiles_assignments.csv', index=False) ``` -------------------------------- ### Execute Parallel Clustering Script Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Executes an external shell script ('sub_parallel.sh') to perform parallel clustering on the generated fingerprint batches. This is typically run from the command line. ```shell os.system('./sub_parallel.sh') ``` -------------------------------- ### Read SMILES Strings from File Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Reads SMILES strings from a .smi file into a NumPy array. Assumes the file path is correct and contains one SMILES per line. ```python with open('../../data/chembl_33_np.smi', 'r') as f: smiles = np.array([line.strip() for line in f]) print("Number of smiles: ", smiles.shape[0]) ``` -------------------------------- ### Generate t-SNE Plot for Spatial Visualization Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Creates a t-SNE plot to visualize the spatial distribution of the clustered data. Requires the bitbirch.plotting_utils module. ```python plotting_utils.tsne_plot(brc_refined, fps, "refined", title="refined") ``` -------------------------------- ### Generate Molecule Images from a Specific Cluster in Python Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Generates and saves images of molecules belonging to a specified cluster. It uses RDKit's `Chem.MolFromSmiles` to create molecule objects and `Draw.MolsToGridImage` to arrange them into a grid. The code iterates through molecules in a cluster, saving images in batches. ```python label = 0 mols = [Chem.MolFromSmiles(smiles[i]) for i in clustered_ids[label]] print('Number of molecules:', len(mols)) for i in range(0, len(mols), 30): img_1 = Draw.MolsToGridImage(mols[i:i+30], molsPerRow=5) # Save the images from the .data attribute with open(f'cluster_{label}_{i}.png', 'wb') as f: f.write(img_1.data) ``` -------------------------------- ### Check Fingerprint Quality Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/simple_birch/birch_tutorial.ipynb Verifies that the molecular fingerprints are binary (contain only 0s and 1s) and have the required int64 or float64 data type. ```python print(f'Bits are binary: {np.all(np.isin(fps, [0, 1]))}') print(f'Data type is int64: {fps.dtype == np.int64 or fps.dtype == np.float64}') ``` -------------------------------- ### Generate RDKit Fingerprints Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Converts SMILES strings to RDKit molecule objects and then generates RDKit fingerprints. Requires the 'rdkit' and 'numpy' libraries. Handles potential errors if SMILES are invalid. ```python from rdkit import Chem import numpy as np mols = [Chem.MolFromSmiles(smi) for smi in smiles] fps = np.array([Chem.RDKFingerprint(mol) for mol in mols]) ``` -------------------------------- ### Execute Final Clustering Script Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Executes an external shell script ('sub_final_clustering.sh') to perform the final clustering step on the results of the parallel jobs. This script is intended to be run from the command line, potentially with arguments like '-t 0.675'. ```shell os.system('./sub_final_clustering.sh') ``` -------------------------------- ### Define and Plot Cluster Population and Unique Scaffolds in Python Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Defines a Python function `population_plot` that sorts molecular clusters by size, plots the population of the top clusters, and visualizes the number of unique scaffolds within each cluster. It utilizes `matplotlib` for plotting and `rdkit` for scaffold analysis. The function takes sorted cluster IDs, SMILES strings, and the number of top clusters to display as input. ```python def population_plot(clustered_ids, smiles, top_clusters=20): # Sort BFs by size clustered_ids = sorted(clustered_ids, key=lambda x: len(x), reverse=True) # Get the BFs of interest mol_indices = clustered_ids[:top_clusters] # Plot the population plt.figure(figsize=(10, 10)) plt.bar(range(top_clusters), [len(x) for x in mol_indices], color = 'blue', label='Population') # Anotate the number of molecules in each cluster above the bars for i, mol in enumerate(mol_indices): plt.text(i, len(mol), len(mol), ha='center', va='bottom') # Get the number of unique scaffolds in each cluster unique_scaffolds = [] for cluster_of_interest in mol_indices: # Get the smiles in the cluster of interest smiles_interest = [smiles[i] for i in cluster_of_interest] # Get the Scaffols of the molecules in the cluster of interest scaffolds = [MurckoScaffold.MurckoScaffoldSmilesFromSmiles(smile) for smile in smiles_interest] # Get the unique scaffolds unique_scaffolds.append(len(list(set(scaffolds)))) # Plot the number of unique scaffolds in each cluster plt.bar(range(top_clusters), unique_scaffolds, color = 'orange', label='Unique Scaffolds') # Anotate the number of unique scaffolds in each cluster above the bars for i, mol in enumerate(unique_scaffolds): plt.text(i, mol, mol, ha='center', va='bottom', color='white') plt.xticks(range(top_clusters), rotation=45) plt.show() ``` -------------------------------- ### Read SMILES Input File Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Reads SMILES strings from a specified file into a NumPy array. Assumes each line in the file contains a valid SMILES string. Requires the 'numpy' library. ```python import numpy as np with open('chembl_33_np.smi', 'r') as f: smiles = np.array(f.readlines()) ``` -------------------------------- ### Check Fingerprint Data Type and Format Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Verifies if the generated fingerprints are binary (0 or 1) and checks their data type (e.g., int64 or float64). Uses 'numpy' for array operations and checks. ```python import numpy as np print(f'Bits are binary: {np.all(np.isin(fps, [0, 1]))}') print(f'Data type is int64: {fps.dtype == np.int64 or fps.dtype == np.float64}') ``` -------------------------------- ### Check Fingerprint Data Size Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Reports the number of fingerprints generated, the number of bits per fingerprint, and confirms consistency with the number of input SMILES strings. Uses 'numpy' for shape attributes. ```python import numpy as np print(f'Number of fingerprints: {fps.shape[0]}') print(f'Number of bits: {fps.shape[1]}') print(f'Number of fingerprints mathces the number of smiles: {fps.shape[0] == len(smiles)}') ``` -------------------------------- ### Delete Molecules to Save Memory Source: https://github.com/mqcomplab/bitbirch/blob/main/examples/parallel_birch/parallel_example.ipynb Frees up memory by deleting the list of RDKit molecule objects after fingerprint generation. This is a memory management step. ```python # Delete the mols to save memory del mols ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.