mirror of
https://github.com/K-Dense-AI/claude-scientific-skills.git
synced 2026-01-26 16:58:56 +08:00
Merge pull request #11 from aledlie/main
refactor: replace manual argv parsing with argparse
This commit is contained in:
@@ -22,6 +22,7 @@ Usage examples:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
from typing import List, Dict, Optional, Generator
|
from typing import List, Dict, Optional, Generator
|
||||||
@@ -229,28 +230,112 @@ def get_id_mapping_databases() -> Dict:
|
|||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
# Example usage
|
def main():
|
||||||
if __name__ == "__main__":
|
"""Command-line interface for UniProt database queries."""
|
||||||
# Example 1: Search for human insulin proteins
|
import argparse
|
||||||
print("Searching for human insulin proteins...")
|
|
||||||
results = search_proteins(
|
|
||||||
"insulin AND organism_name:human AND reviewed:true",
|
|
||||||
format="json",
|
|
||||||
fields=["accession", "id", "gene_names", "protein_name"],
|
|
||||||
size=5
|
|
||||||
)
|
|
||||||
print(json.dumps(results, indent=2))
|
|
||||||
|
|
||||||
# Example 2: Get a specific protein in FASTA format
|
parser = argparse.ArgumentParser(
|
||||||
print("\nRetrieving protein P01308 (human insulin)...")
|
description='Query UniProt database using REST API',
|
||||||
protein = get_protein("P01308", format="fasta")
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||||
|
epilog="""
|
||||||
|
Examples:
|
||||||
|
# Search for proteins
|
||||||
|
%(prog)s --search "insulin AND organism_name:human" --format json
|
||||||
|
|
||||||
|
# Get a specific protein
|
||||||
|
%(prog)s --get P01308 --format fasta
|
||||||
|
|
||||||
|
# Map IDs from UniProt to PDB
|
||||||
|
%(prog)s --map P01308,P04637 --from UniProtKB_AC-ID --to PDB
|
||||||
|
|
||||||
|
# Stream large results
|
||||||
|
%(prog)s --stream "taxonomy_id:9606 AND reviewed:true" --format fasta
|
||||||
|
|
||||||
|
# List available fields
|
||||||
|
%(prog)s --list-fields
|
||||||
|
|
||||||
|
# List mapping databases
|
||||||
|
%(prog)s --list-databases
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
# Main operation arguments (mutually exclusive)
|
||||||
|
group = parser.add_mutually_exclusive_group(required=True)
|
||||||
|
group.add_argument('--search', '-s', help='Search query string')
|
||||||
|
group.add_argument('--get', '-g', help='Get protein by accession number')
|
||||||
|
group.add_argument('--map', '-m', help='Map IDs (comma-separated)')
|
||||||
|
group.add_argument('--stream', help='Stream large result sets')
|
||||||
|
group.add_argument('--list-fields', action='store_true',
|
||||||
|
help='List all available query fields')
|
||||||
|
group.add_argument('--list-databases', action='store_true',
|
||||||
|
help='List all ID mapping databases')
|
||||||
|
|
||||||
|
# Format options
|
||||||
|
parser.add_argument('--format', '-f', default='json',
|
||||||
|
help='Output format (json, tsv, xlsx, xml, fasta, txt, rdf)')
|
||||||
|
|
||||||
|
# Search-specific options
|
||||||
|
parser.add_argument('--fields', help='Comma-separated list of fields to return')
|
||||||
|
parser.add_argument('--size', type=int, default=25,
|
||||||
|
help='Number of results (default: 25, max: 500)')
|
||||||
|
|
||||||
|
# Mapping-specific options
|
||||||
|
parser.add_argument('--from', dest='from_db',
|
||||||
|
help='Source database for ID mapping')
|
||||||
|
parser.add_argument('--to', dest='to_db',
|
||||||
|
help='Target database for ID mapping')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
try:
|
||||||
|
if args.list_fields:
|
||||||
|
fields = get_available_fields()
|
||||||
|
print(json.dumps(fields, indent=2))
|
||||||
|
|
||||||
|
elif args.list_databases:
|
||||||
|
databases = get_id_mapping_databases()
|
||||||
|
print(json.dumps(databases, indent=2))
|
||||||
|
|
||||||
|
elif args.search:
|
||||||
|
fields_list = args.fields.split(',') if args.fields else None
|
||||||
|
results = search_proteins(
|
||||||
|
args.search,
|
||||||
|
format=args.format,
|
||||||
|
fields=fields_list,
|
||||||
|
size=args.size
|
||||||
|
)
|
||||||
|
if args.format == 'json':
|
||||||
|
print(json.dumps(results, indent=2))
|
||||||
|
else:
|
||||||
|
print(results)
|
||||||
|
|
||||||
|
elif args.get:
|
||||||
|
protein = get_protein(args.get, format=args.format)
|
||||||
|
if args.format == 'json':
|
||||||
|
print(json.dumps(protein, indent=2))
|
||||||
|
else:
|
||||||
print(protein)
|
print(protein)
|
||||||
|
|
||||||
# Example 3: Map UniProt IDs to PDB IDs
|
elif args.map:
|
||||||
print("\nMapping UniProt IDs to PDB...")
|
if not args.from_db or not args.to_db:
|
||||||
mapping = map_ids(
|
parser.error("--map requires --from and --to arguments")
|
||||||
["P01308", "P04637"],
|
|
||||||
from_db="UniProtKB_AC-ID",
|
ids = [id.strip() for id in args.map.split(',')]
|
||||||
to_db="PDB"
|
mapping = map_ids(ids, args.from_db, args.to_db, format=args.format)
|
||||||
)
|
if args.format == 'json':
|
||||||
print(json.dumps(mapping, indent=2))
|
print(json.dumps(mapping, indent=2))
|
||||||
|
else:
|
||||||
|
print(mapping)
|
||||||
|
|
||||||
|
elif args.stream:
|
||||||
|
fields_list = args.fields.split(',') if args.fields else None
|
||||||
|
for chunk in stream_results(args.stream, format=args.format, fields=fields_list):
|
||||||
|
print(chunk, end='')
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|||||||
@@ -213,30 +213,62 @@ class PEDSHelper:
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Command-line interface for PEDS data."""
|
"""Command-line interface for PEDS data."""
|
||||||
if len(sys.argv) < 2:
|
import argparse
|
||||||
print("Usage:")
|
|
||||||
print(" python peds_client.py <application_number>")
|
parser = argparse.ArgumentParser(
|
||||||
print(" python peds_client.py --patent <patent_number>")
|
description='Query USPTO Patent Examination Data System (PEDS)',
|
||||||
print(" python peds_client.py --status <application_number>")
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||||
print(" python peds_client.py --analyze <application_number>")
|
epilog="""
|
||||||
sys.exit(1)
|
Examples:
|
||||||
|
# Get application data by application number
|
||||||
|
%(prog)s --application 16123456
|
||||||
|
|
||||||
|
# Get patent data by patent number
|
||||||
|
%(prog)s --patent 11234567
|
||||||
|
|
||||||
|
# Get status summary
|
||||||
|
%(prog)s --status 16123456
|
||||||
|
|
||||||
|
# Analyze prosecution history
|
||||||
|
%(prog)s --analyze 16123456
|
||||||
|
|
||||||
|
# Get transaction history
|
||||||
|
%(prog)s --transactions 16123456
|
||||||
|
|
||||||
|
# Get office actions
|
||||||
|
%(prog)s --office-actions 16123456
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
if not HAS_USPTO_LIB:
|
if not HAS_USPTO_LIB:
|
||||||
print("Error: uspto-opendata-python library not installed")
|
parser.error("uspto-opendata-python library not installed. Install with: pip install uspto-opendata-python")
|
||||||
print("Install with: pip install uspto-opendata-python")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
helper = PEDSHelper()
|
# Main operation arguments (mutually exclusive)
|
||||||
|
group = parser.add_mutually_exclusive_group(required=True)
|
||||||
|
group.add_argument('--application', '-a', help='Get application by application number')
|
||||||
|
group.add_argument('--patent', '-p', help='Get patent by patent number')
|
||||||
|
group.add_argument('--status', '-s', help='Get status summary for application')
|
||||||
|
group.add_argument('--analyze', help='Analyze prosecution history for application')
|
||||||
|
group.add_argument('--transactions', '-t', help='Get transaction history for application')
|
||||||
|
group.add_argument('--office-actions', '-o', help='Get office actions for application')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if sys.argv[1] == "--patent":
|
helper = PEDSHelper()
|
||||||
result = helper.get_patent(sys.argv[2])
|
|
||||||
elif sys.argv[1] == "--status":
|
if args.application:
|
||||||
result = helper.get_status_summary(sys.argv[2])
|
result = helper.get_application(args.application)
|
||||||
elif sys.argv[1] == "--analyze":
|
elif args.patent:
|
||||||
result = helper.analyze_prosecution(sys.argv[2])
|
result = helper.get_patent(args.patent)
|
||||||
else:
|
elif args.status:
|
||||||
result = helper.get_application(sys.argv[1])
|
result = helper.get_status_summary(args.status)
|
||||||
|
elif args.analyze:
|
||||||
|
result = helper.analyze_prosecution(args.analyze)
|
||||||
|
elif args.transactions:
|
||||||
|
result = helper.get_transaction_history(args.transactions)
|
||||||
|
elif args.office_actions:
|
||||||
|
result = helper.get_office_actions(args.office_actions)
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
print(json.dumps(result, indent=2))
|
print(json.dumps(result, indent=2))
|
||||||
|
|||||||
@@ -225,35 +225,83 @@ class TrademarkClient:
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Command-line interface for trademark search."""
|
"""Command-line interface for trademark search."""
|
||||||
if len(sys.argv) < 2:
|
import argparse
|
||||||
print("Usage:")
|
|
||||||
print(" python trademark_client.py <serial_or_registration_number>")
|
|
||||||
print(" python trademark_client.py --status <number>")
|
|
||||||
print(" python trademark_client.py --health <number>")
|
|
||||||
print(" python trademark_client.py --goods <number>")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
client = TrademarkClient()
|
parser = argparse.ArgumentParser(
|
||||||
|
description='Query USPTO Trademark Status & Document Retrieval (TSDR) API',
|
||||||
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||||
|
epilog="""
|
||||||
|
Examples:
|
||||||
|
# Get trademark by serial number
|
||||||
|
%(prog)s --serial 87654321
|
||||||
|
|
||||||
|
# Get trademark by registration number
|
||||||
|
%(prog)s --registration 5678901
|
||||||
|
|
||||||
|
# Get status summary
|
||||||
|
%(prog)s --status 87654321
|
||||||
|
|
||||||
|
# Check trademark health
|
||||||
|
%(prog)s --health 87654321
|
||||||
|
|
||||||
|
# Get goods and services
|
||||||
|
%(prog)s --goods 87654321
|
||||||
|
|
||||||
|
# Get owner information
|
||||||
|
%(prog)s --owner 87654321
|
||||||
|
|
||||||
|
# Get prosecution history
|
||||||
|
%(prog)s --prosecution 87654321
|
||||||
|
|
||||||
|
Environment:
|
||||||
|
Set USPTO_API_KEY environment variable with your API key from:
|
||||||
|
https://account.uspto.gov/api-manager/
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
# Main operation arguments (mutually exclusive)
|
||||||
|
group = parser.add_mutually_exclusive_group(required=True)
|
||||||
|
group.add_argument('--serial', '-s', help='Get trademark by serial number')
|
||||||
|
group.add_argument('--registration', '-r', help='Get trademark by registration number')
|
||||||
|
group.add_argument('--status', help='Get status summary (serial or registration number)')
|
||||||
|
group.add_argument('--health', help='Check trademark health (serial or registration number)')
|
||||||
|
group.add_argument('--goods', '-g', help='Get goods and services (serial or registration number)')
|
||||||
|
group.add_argument('--owner', '-o', help='Get owner information (serial or registration number)')
|
||||||
|
group.add_argument('--prosecution', '-p', help='Get prosecution history (serial or registration number)')
|
||||||
|
|
||||||
|
# API key option
|
||||||
|
parser.add_argument('--api-key', '-k', help='USPTO API key (overrides USPTO_API_KEY env var)')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if sys.argv[1] == "--status":
|
client = TrademarkClient(api_key=args.api_key)
|
||||||
result = client.get_trademark_status(sys.argv[2])
|
|
||||||
elif sys.argv[1] == "--health":
|
if args.serial:
|
||||||
result = client.check_trademark_health(sys.argv[2])
|
result = client.get_trademark_by_serial(args.serial)
|
||||||
elif sys.argv[1] == "--goods":
|
elif args.registration:
|
||||||
result = client.get_goods_and_services(sys.argv[2])
|
result = client.get_trademark_by_registration(args.registration)
|
||||||
else:
|
elif args.status:
|
||||||
# Get full trademark data
|
result = client.get_trademark_status(args.status)
|
||||||
result = client.get_trademark_by_serial(sys.argv[1])
|
elif args.health:
|
||||||
if not result:
|
result = client.check_trademark_health(args.health)
|
||||||
result = client.get_trademark_by_registration(sys.argv[1])
|
elif args.goods:
|
||||||
|
result = client.get_goods_and_services(args.goods)
|
||||||
|
elif args.owner:
|
||||||
|
result = client.get_owner_info(args.owner)
|
||||||
|
elif args.prosecution:
|
||||||
|
result = client.get_prosecution_history(args.prosecution)
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
print(json.dumps(result, indent=2))
|
print(json.dumps(result, indent=2))
|
||||||
else:
|
else:
|
||||||
print(f"Trademark {sys.argv[1]} not found", file=sys.stderr)
|
number = (args.serial or args.registration or args.status or
|
||||||
|
args.health or args.goods or args.owner or args.prosecution)
|
||||||
|
print(f"Trademark {number} not found", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
except ValueError as e:
|
||||||
|
parser.error(str(e))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error: {e}", file=sys.stderr)
|
print(f"Error: {e}", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user