Skip to content

Enhanced Repeat Relationship Extraction - Implementation Summary

โœ… Current System Capabilities

Your Graffold now has comprehensive repeat relationship extraction capabilities that are natively integrated into the main pipeline. Here's what has been implemented:

๐Ÿ”„ Native Integration with Main Pipeline

The enhanced relationship extraction is now automatically activated when running main.py:

# Enhanced relationship tracking runs automatically
python main.py --search-term "insulin diabetes" --max-results 20

What happens automatically: - โœ… Abstracts are fetched from PubMed - โœ… Enhanced KG processing captures full relationship contexts immediately
- โœ… Confidence scores calculated for each relationship as it's extracted - โœ… Sentence-level context stored with every relationship occurrence - โœ… Comprehensive quality report generated after processing - โœ… High-confidence relationships logged with evidence details

1. Advanced Relationship Tracking (relationship_counter.py)

The RelationshipCounter class now includes:

Original Features:

  • โœ… Frequency counting for duplicate relationships across documents
  • โœ… Source document aggregation and tracking
  • โœ… High-confidence relationship filtering based on frequency

Enhanced Features (NEW):

  • โœ… Full Context Storage: store_relationship_with_context() stores complete abstract text, sentence context, and metadata
  • โœ… Confidence Scoring: Each relationship gets a confidence score based on contextual factors
  • โœ… Rich Evidence Retrieval: get_relationship_evidence() returns all supporting evidence with full context
  • โœ… Pattern Analysis: analyze_relationship_patterns() identifies extraction method performance and conflicts
  • โœ… High-Confidence Filtering: Enhanced get_high_confidence_relationships() with confidence thresholds

2. Enhanced KG Pipeline (kg_pipeline.py)

The KGPipeline class now includes:

Enhanced Processing Methods:

  • โœ… Sentence Context Extraction: _extract_sentence_context() finds specific sentences mentioning both entities
  • โœ… Dynamic Confidence Calculation: _calculate_relationship_confidence() scores relationships based on:
  • Entity proximity in text
  • Presence of relationship keywords
  • Sentence-level co-occurrence
  • โœ… Enhanced Document Processing: process_documents_enhanced() combines standard extraction with rich context storage
  • โœ… Quality Reporting: get_relationship_quality_report() provides comprehensive extraction analytics

3. Relationship Occurrence Storage

The system now creates RelationshipOccurrence nodes that store: - ๐Ÿ”— Full Abstract Text: Complete source document content - ๐Ÿ“ Sentence Context: Specific sentence mentioning the relationship
- ๐Ÿ“Š Confidence Score: Algorithmic assessment of relationship strength - ๐Ÿ” Extraction Method: Track which method found the relationship - ๐Ÿ“‹ Rich Metadata: PMID, study type, chunk IDs, etc. - โฐ Temporal Tracking: When relationships were first/last seen

๐Ÿš€ How to Use the Enhanced System

The enhanced relationship extraction now runs automatically with the main pipeline:

# All enhanced features activate automatically
python main.py --search-term "protein disease association" --max-results 50

# Force re-processing of existing abstracts with enhanced tracking
python main.py --search-term "insulin diabetes" --max-results 20 --force

# Monitor the logs for relationship quality reports

Output you'll see:

INFO - Relationship Quality Summary:
INFO -   Total relationships: 245
INFO -   Total occurrences: 580
INFO -   Unique source documents: 45
INFO -   Overall avg confidence: 0.823
INFO - Found 67 high-confidence relationships:
INFO -   Insulin (Protein) -[ASSOCIATED_WITH]-> Diabetes Mellitus (Disease): confidence=0.945, occurrences=12
INFO -   TNF-alpha (Protein) -[REGULATES]-> Inflammation (Process): confidence=0.891, occurrences=8

Manual/Programmatic Usage

Manual/Programmatic Usage

For advanced use cases, you can still access all enhanced features programmatically:

from src.pipeline.kg_pipeline import KGPipeline
from src.processors.relationship_counter import RelationshipCounter

# Initialize enhanced pipeline
pipeline = KGPipeline(service="local", database="olink")

# Process documents with enhanced relationship tracking (happens automatically in main.py)
docs = await pipeline.process_documents_enhanced(documents, metadata={"source": "pubmed"})

# Get comprehensive quality report (happens automatically in main.py)
quality_report = pipeline.get_relationship_quality_report()

# Access rich relationship evidence
counter = RelationshipCounter(database="olink")
evidence = counter.get_relationship_evidence("P01308", "D003920", "ASSOCIATED_WITH")

# Analyze extraction patterns
patterns = counter.analyze_relationship_patterns()

# Get high-confidence relationships with confidence thresholds
high_conf = counter.get_high_confidence_relationships(
    min_occurrences=3, 
    min_avg_confidence=0.8
)

Enhanced Usage (LEGACY - now integrated)

from src.pipeline.kg_pipeline import KGPipeline
from src.processors.relationship_counter import RelationshipCounter

# Initialize enhanced pipeline
pipeline = KGPipeline(service="local", database="olink")

# Process documents with enhanced relationship tracking
docs = await pipeline.process_documents_enhanced(documents, metadata={"source": "pubmed"})

# Get comprehensive quality report
quality_report = pipeline.get_relationship_quality_report()

# Access rich relationship evidence
counter = RelationshipCounter(database="olink")
evidence = counter.get_relationship_evidence("P01308", "D003920", "ASSOCIATED_WITH")

# Analyze extraction patterns
patterns = counter.analyze_relationship_patterns()

# Get high-confidence relationships with confidence thresholds
high_conf = counter.get_high_confidence_relationships(
    min_occurrences=3, 
    min_avg_confidence=0.8
)

๐Ÿ“Š Example Output

Relationship Evidence

evidence = counter.get_relationship_evidence("P01308", "D003920", "ASSOCIATED_WITH")
# Returns:
[
    {
        "source_doc": "pubmed_12345",
        "abstract_text": "Insulin plays a crucial role in diabetes mellitus...",
        "sentence_context": "insulin resistance is strongly associated with type 2 diabetes",
        "confidence_score": 0.87,
        "extraction_method": "llm_enhanced",
        "metadata": {"pmid": "12345", "study_type": "clinical"},
        "extracted_at": "2024-01-01T10:00:00",
        "total_count": 5
    }
]

Quality Report

report = pipeline.get_relationship_quality_report()
# Returns:
{
    "summary": {
        "total_relationships": 150,
        "total_occurrences": 450,
        "unique_source_documents": 75,
        "overall_avg_confidence": 0.78
    },
    "high_confidence_relationships": [...],
    "pattern_analysis": {
        "extraction_methods": [
            {"method": "llm_enhanced", "count": 400},
            {"method": "rule_based", "count": 50}
        ],
        "confidence_distribution": [...],
        "conflicting_evidence": [...]
    },
    "quality_metrics": {
        "total_high_confidence_relationships": 45,
        "average_confidence_score": 0.823,
        "average_occurrences_per_relationship": 3.2
    }
}

๐Ÿงช Testing

The system includes comprehensive test coverage: - โœ… test_relationship_counter_enhanced.py - Tests all enhanced relationship tracking features - โœ… Integration with existing test suite - โœ… Mocked database operations for fast testing - โœ… Error handling and edge case coverage

๐ŸŽฏ Key Benefits

  1. Native Integration: Enhanced relationship extraction runs automatically with main pipeline - no code changes needed
  2. Immediate Processing: Relationship contexts are captured as each abstract is processed (no batch delays)
  3. Real-time Quality Reporting: Get comprehensive relationship statistics immediately after processing
  4. No Duplicate Files: All enhancements are integrated into existing files - no path updates needed
  5. Backward Compatibility: Existing functionality remains unchanged
  6. Rich Context: Full abstract text and sentence-level context for every relationship
  7. Quality Assurance: Confidence scoring and pattern analysis for relationship validation
  8. Comprehensive Evidence: Track all supporting documents with full metadata
  9. Performance Analytics: Understand which extraction methods work best

๐Ÿ“ˆ Use Cases

  1. Automated Scientific Literature Analysis: Simply run main.py to track protein-disease relationships across studies
  2. Real-time Evidence Strength Assessment: Identify relationships with strongest supporting evidence as they're processed
  3. Method Comparison: Compare performance of different extraction algorithms automatically
  4. Quality Control: Identify relationships with conflicting or low-confidence evidence in real-time
  5. Knowledge Validation: Cross-reference relationship claims across multiple papers immediately

๐Ÿ”„ Migration Path

No migration needed! Your existing code will continue to work unchanged. The enhanced relationship extraction is now:

  • โœ… Automatically activated when running main.py
  • โœ… Immediate processing - relationships are analyzed as abstracts are processed
  • โœ… Quality reporting - comprehensive statistics logged after each run
  • โœ… Backward compatible - existing RelationshipCounter methods still work

Just run your normal pipeline commands and you'll get enhanced relationship tracking automatically!