Skip to main content

A Python package to interact with ClinicalTrials.gov API v2

Project description

Clinical Trials Interact

A Python package for interacting with and analyzing clinical trial data from ClinicalTrials.gov API.

Features

  • Search clinical trials using flexible search expressions
  • Build similarity graphs between related clinical trials
  • Navigate through clinical trial networks using graph traversal
  • Extract detailed trial information and metadata
  • Analyze relationships between trials based on various criteria

Installation

Install using pip:

pip install clinicaltrials_interact

Note: For an isolated environment, consider using a virtual environment or Conda:

conda create -n ct_interact_env python=3.10
conda activate ct_interact_env
pip install clinicaltrials_interact

Requirements

  • Python 3.7+
  • requests
  • networkx
  • pandas
  • matplotlib
  • sentence-transformers
  • scikit-learn
  • seaborn
  • plotly

Quick Start

from clinicaltrials_interact import ClinicalTrialsAPI

# Initialize the API client
ct_api = ClinicalTrialsAPI()

# Search for clinical trials related to "breast cancer"
trials = ct_api.query_studies("breast cancer", max_results=100)

# Get detailed information about a specific trial
trial_detail = ct_api.get_study_details("NCT04303169")

Examples

Example 1: Analyzing COVID-19 Vaccine Trials

from clinicaltrials_interact import ClinicalTrialsNavigator
import matplotlib.pyplot as plt

# 1. Define the navigator
navigator = ClinicalTrialsNavigator()
    
# 2. Search for studies and build a similarity graph in one step
print("\nSearching for diabetes studies and building graph...")
graph = navigator.search_and_build_graph(
    search_expr="diabetes type 2", 
    max_studies=200,  # Limit to 50 studies for this example
    similarity_threshold=0.5,  # Lower threshold to get more connections
    max_edges_per_node=20
)

# 3. Perform breadth-first search traversal
print("\nPerforming Breadth-First Search traversal:")
bfs_results = navigator.breadth_first_search(
    start_id=first_trial_id,
    max_depth=2,
    visited_limit=10
)

print(f"BFS found {len(bfs_results)} trials:")
for i, result in enumerate(bfs_results[:5]):  # Show first 5 results
    print(f"{i+1}. {result['NCTId']} (depth {result['depth']}): {result['title'][:50]}...")

# 4. Perform depth-first search traversal
print("\nPerforming Depth-First Search traversal:")
dfs_results = navigator.depth_first_search(
    start_id=first_trial_id,
    max_depth=2,
    visited_limit=10
)

print(f"DFS found {len(dfs_results)} trials:")
for i, result in enumerate(dfs_results[:5]):  # Show first 5 results
    print(f"{i+1}. {result['NCTId']} (depth {result['depth']}): {result['title'][:50]}...")

# 5. Find a path between two trials
if len(trial_ids) >= 2:
    target_id = trial_ids[5]  # Pick the 6th trial as the target
    print(f"\nFinding path from {first_trial_id} to {target_id}:")
    path = navigator.find_path(first_trial_id, target_id)
    
    if path:
        print(f"Path found with {len(path)} nodes:")
        for i, node_id in enumerate(path):
            node_data = graph.nodes[node_id]
            print(f"{i+1}. {node_id}: {node_data.get('title', '')[:50]}...")
    else:
        print("No path found.")

# 6. Find connected component
print(f"\nFinding connected component for trial {first_trial_id}:")
connected = navigator.get_connected_component(first_trial_id)
print(f"Connected component has {len(connected)} trials.")

# 7. Visualize the graph with highlighted path and nodes
print("\nVisualizing the graph (a plot window should appear)...")
if len(trial_ids) >= 2:
    navigator.visualize_graph(
        highlight_nodes=[first_trial_id, target_id],
        highlight_path=path,
        figsize=(10, 8)
    )
else:
    navigator.visualize_graph(highlight_nodes=[first_trial_id])

Example 2: Finding Similar Trials

import clinicaltrials_interact
from clinicaltrials_interact import ClinicalTrialsAPI
import clinicaltrials_interact.clustering as clustering 

ct_api = ClinicalTrialsAPI()

# 1a. Retrieve candidate studies related to "leukemia" and compute embeddings. 
df, embeddings, model = clustering.get_candidate_embeddings("leukemia", max_studies=100)

# 1b. Plot the cosine similarity matrix for a subset (e.g., 50 studies) of the candidate pool.
# Saved to figures directory
clustering.plot_similarity_matrix(embeddings, subset_size=50)

# 1c. Output the index to id mapping to understand which studies are most to each other. 
clustering.plot_index_to_id_mapping(df, subset_size=50)

# 1d. Perform spectral clustering on the embeddings.
labels, sim_matrix = clustering.perform_spectral_clustering(embeddings, n_clusters=3)
    
# 1e. Visualize the clusters with a t-SNE plot.
clustering.plot_clusters(embeddings, labels, filename="leukemia_clusters.png", keyword = "leukemia")

# 2a. For a given query, return the IDs of the top 10 most similar studies.
query = "A study about heart attacks"
top_similar_ids = clustering.get_top_similar_ids(query, "", candidate_pool_size=500, top_n=10)
print(f"Top similar study IDs related to your query: {top_similar_ids}\n")

# 3. Perform general clustering and extract keywords for each cluster. Takes the top 500 or so studies and clusters them into 5 clusters. Returns the keywords for each cluster
clustering.print_cluster_keywords()

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Citation

If you use this package in your research, please cite:

KeyVuLee Innovations. (2025). Clinical Trials Interact: A Python package for analyzing clinical trial data.
GitHub: https://github.com/hsph-bst236/midterm-project-keyvulee-innovations

Contact

For questions and support, please open an issue or contact the maintainer at contact@keyvulee-innovations.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clinicaltrials_interact-0.1.5.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

clinicaltrials_interact-0.1.5-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file clinicaltrials_interact-0.1.5.tar.gz.

File metadata

  • Download URL: clinicaltrials_interact-0.1.5.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.0

File hashes

Hashes for clinicaltrials_interact-0.1.5.tar.gz
Algorithm Hash digest
SHA256 4c26d798e28f5450b2bc53828390aaf503189fd7a393ea35c1ccf4809d3111d3
MD5 df752d1c76f94839507a25bb581b1074
BLAKE2b-256 b345e19095bda06d8847d9a8cf712f72210c7dcf8936f5279c95adaada722d88

See more details on using hashes here.

File details

Details for the file clinicaltrials_interact-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for clinicaltrials_interact-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 77f9b9ef76e3045cc432d8014dfc8d3745ca6153be691c1d1652a7b3157a8582
MD5 0d98e252eb5601356d6c6a6eab2f704d
BLAKE2b-256 29cc27eaa469e738729d3a92c551f72b31877309c8a861d0e8386e4144d653f6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page