File size: 26,807 Bytes
c567880 cc69c66 c567880 e1dd4c7 cc69c66 c567880 cc69c66 c567880 e1dd4c7 c567880 e1dd4c7 c567880 e1dd4c7 c567880 e1dd4c7 c567880 cc69c66 c567880 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 |
import csv
import os
from io import BytesIO
from string import Template
import pandas as pd
import matplotlib.pyplot as plt
import networkx as nx
from PIL import Image
MAX_INPUT_TOKEN_LENGTH = 1024
TEMPERATURE = 0.1
MAX_NEW_TOKENS = 8192
def remove_chars_loop(text, chars_to_remove):
for char in chars_to_remove:
text = text.replace(char, "")
return text
def get_prompt_with_files_uploaded(filepaths: list[str] = None) -> str:
if not filepaths:
return "No files uploaded yet."
prompt_begin = """
You are a data structuring expert tasked with analyzing data files (CSV, TXT, TSV, XML) to identify their schema and
generate output in the Gen3 Data Dictionary format (JSON). Review the data files for column names, data types,
and relationships, and if a data dictionary is provided, ensure strict alignment with its metadata.
Column names may have embedded information to infer the type and/or units from.
Follow these steps:
- Examine each data file to define the schema
- Cross-reference with the data dictionary, if available, to match all column definitions and metadata exactly
- Generate an output schema that mirrors the provided data structure WITHOUT adding any new entities or attributes
- Limit your output to the smallest amount possible of JSON to capture the necessary information. DO NOT BE VERBOSE
The output must include nodes, properties of those nodes, descriptions of those properties, and links to other nodes.
The ouput must format as ONLY JSON, do not include additional text and please be concise. Limit your output to only what's
necessary (nodes, properties, descriptions, relationships / links).
"""
file_template = Template(
"""
File name: `$file_name`
File contents:
```
$file_contents```
"""
)
prompt_end = """
Please generate the Gen3 Data Dictionary in JSON format:
"""
# Start prompt
prompt = prompt_begin
for path in filepaths:
file_name = os.path.basename(path)
with open(path, "r", encoding="utf-8") as f:
reader = csv.DictReader(f, delimiter="\t")
file_contents = "\t".join(reader.fieldnames)
prompt += file_template.substitute(
file_name=file_name, file_contents=file_contents
)
prompt += prompt_end
print(f"prompt: {prompt}")
return prompt
def create_graph_image_from_json(json_response):
adj_dict = {}
if isinstance(json_response, dict) and "nodes" in json_response:
for node in json_response.get("nodes"):
adj_dict[node["name"]] = node["links"]
G = nx.from_dict_of_lists(adj_dict)
fig, ax = plt.subplots()
nx.draw_networkx(G, with_labels=True, node_color="lightblue", ax=ax)
buf = BytesIO()
fig.savefig(buf, format="png")
plt.close(fig)
buf.seek(0)
pil_img = Image.open(buf)
return pil_img
def create_summary_tables(json_response):
node_descriptions = {}
node_property_descriptions = {}
for node in json_response.get("nodes", []):
node_descriptions[node.get("name", "N/A")] = node.get("description", "N/A")
for prop in node.get("properties", []):
node_property_descriptions[
f"{node.get('name', 'N/A')}.{prop.get('name', 'N/A')}"
] = [prop.get("type", "N/A"), prop.get("description", "N/A")]
node_descriptions_df = pd.DataFrame.from_dict(
node_descriptions, orient="index"
).reset_index()
node_descriptions_df.rename(
columns={"index": "Node Name", 0: "Generated Description"}, inplace=True
)
node_property_descriptions_df = pd.DataFrame.from_dict(
node_property_descriptions, orient="index"
).reset_index()
node_property_descriptions_df.rename(
columns={
"index": "Node.Property Name",
0: "Generated Data Type",
1: "Generated Description",
},
inplace=True,
)
return node_descriptions_df, node_property_descriptions_df
def get_example_ai_model_output_simple():
return """
{
"nodes": [
{
"name": "project",
"description": "Any specifically defined piece of work that is undertaken or attempted to meet a single requirement. (NCIt C47885)",
"links": [],
"required": [
"dbgap_accession_number",
"project.id"
],
"properties": [
{
"name": "awg_review",
"description": "Indicates that the project is an AWG project.",
"type": "boolean"
},
{
"name": "data_citation",
"description": "The citation for the published dataset.",
"type": "string"
},
{
"name": "data_contributor",
"description": "The name of the organization or individual that the contributed dataset belongs to.",
"type": "string"
},
{
"name": "data_description",
"description": "A brief, free-text description of the data files and associated metadata provided for this dataset.",
"type": "string"
},
{
"name": "dbgap_accession_number",
"description": "The dbgap accession number provided for the project.",
"type": "string"
},
{
"name": "in_review",
"description": "Indicates that the project is under review by the submitter. Upload and data modification is disabled.",
"type": "boolean"
},
{
"name": "intended_release_date",
"description": "Tracks a Project's intended release date.",
"type": "string"
},
{
"name": "project.id",
"description": "A unique identifier for records in this 'project' table.",
"type": "string"
},
{
"name": "protocol_number",
"description": "The project's protocol number or similar amount.",
"type": "string"
},
{
"name": "releasable",
"description": "A project can only be released by the user when `releasable` is true.",
"type": "boolean"
},
{
"name": "request_submission",
"description": "Indicates that the user has requested submission to the GDC for harmonization.",
"type": "boolean"
},
{
"name": "research_design",
"description": "A summary of the goals of the research or a general description of the research's relationship to a clinical application.",
"type": "string"
},
{
"name": "research_objective",
"description": "The general objective of the research; what the researchers hope to discover or determine.",
"type": "string"
},
{
"name": "research_setup",
"description": "A high level description of the setup used to achieve the research objectives.",
"type": "string"
}
]
},
{
"name": "dataset",
"description": "A set of metadata and associated data file objects originating from single a research study, clinical trial or patient cohort.",
"links": [
"project"
],
"required": [
"dataset.id",
"project.id"
],
"properties": [
{
"name": "Class_of_Case_Desc",
"description": "The text term used to describe the kind of clinical condition that can be defined based on objective criteria or by including all patient information from the case.",
"type": "string"
},
{
"name": "data_citation",
"description": "The citation for the published dataset.",
"type": "string"
},
{
"name": "full_name",
"description": "The full name or title of the dataset or publication.",
"type": "string"
},
{
"name": "longitudinal",
"description": "Indicates whether the dataset has longitudinal or time-series data.",
"type": "boolean"
},
{
"name": "project.id",
"description": "Unique identifiers for records in the 'project' table that relate via this foreign key to records in this 'dataset' table.",
"type": "string"
},
{
"name": "dataset.id",
"description": "A unique identifier for records in this 'dataset' table.",
"type": "string"
}
]
},
{
"name": "subject",
"description": "The collection of all data related to a specific subject in the context of a specific experiment.",
"links": [
"project",
"dataset"
],
"required": [
"dataset.id",
"subject.id"
],
"properties": [
{
"name": "date_of_death",
"description": "The date of death of the subject in the context of a specific experiment.",
"type": "string"
},
{
"name": "dataset.id",
"description": "Unique identifiers for records in the 'dataset' table that relate via this foreign key to records in this'subject' table.",
"type": "string"
},
{
"name": "subject.id",
"description": "A unique identifier for records in this'subject' table.",
"type": "string"
},
{
"name": "CancerRegistry_PatientID",
"description": "The patient unique id in the case registry.",
"type": "string"
},
{
"name": "Ethnicity",
"description": "An individual's self-described social and cultural grouping, specifically whether an individual describes themselves as Hispanic or Latino. The provided values are based on the categories defined by the U.S. Office of Management and Business and used by the U.S. Census Bureau.",
"type": "string"
},
{
"name": "Last_Name",
"description": "The surname(s) of individual(s) in study, in the form used for cultural or ethnic reasons (e.g., Spanish surnames)",
"type": "string"
},
{
"name": "Sex_Desc",
"description": "The description of the individual's gender.",
"type": "string"
},
{
"name": "project.id",
"description": "Unique identifiers for records in the 'project' table that relate via this foreign key to records in this'subject' table.",
"type": "string"
}
]
},
{
"name": "sample",
"description": "Any material sample taken from a biological entity for testing, diagnostic, propagation, treatment or research purposes, including a sample obtained from a living organism or taken from the biological object after halting of all its life functions. Biospecimen can contain one or more components including but not limited to cellular molecules, cells, tissues, organs, body fluids, embryos, and body excretory products.",
"links": [
"subject"
],
"required": [
"sample.id",
"subject.id"
],
"properties": [
{
"name": "body_fluid_code",
"description": "The code for the body fluid from which the sample was taken.",
"type": "string"
},
{
"name": "procedure_date",
"description": "Year the sample was taken for analysis.",
"type": "integer"
},
{
"name": "subject.id",
"description": "Unique identifiers for records in the'subject' table that relate via this foreign key to records in this'sample' table.",
"type": "string"
},
{
"name": "sample.id",
"description": "A unique identifier for records in this'sample' table.",
"type": "string"
}
]
}
]
}
"""
def get_example_ai_model_output_many():
return """
{
"nodes": [
{
"name": "project",
"description": "Any specifically defined piece of work that is undertaken or attempted to meet a single requirement. (NCIt C47885)",
"links": [],
"required": [
"code",
"dbgap_accession_number",
"name",
"project.id"
],
"properties": [
{
"name": "category",
"description": "The nature of the investigation or investigational use for which clinical study information is being submitted.",
"type": "enum"
},
{
"name": "code",
"description": "Unique identifier for the project.",
"type": "string"
},
{
"name": "collaborators",
"description": "Other organizations (if any) providing support. Support may include funding, design, implementation, data analysis or reporting. The responsible party is responsible for confirming all collaborators before listing them.",
"type": "string"
},
{
"name": "dbgap_accession_number",
"description": "The dbgap accession number provided for the project.",
"type": "string"
},
{
"name": "investigator_name",
"description": "Name of the principal investigator for the project.",
"type": "string"
},
{
"name": "name",
"description": "Display name/brief description for the project.",
"type": "string"
},
{
"name": "publisher",
"description": "An entity responsible for making the resource available. Examples of a Publisher include a person, an organization, or a service. Typically, the name of a Publisher should be used to indicate the entity.",
"type": "string"
},
{
"name": "released",
"description": "To release a project is to tell the GDC to include all submitted entities in the next GDC index.",
"type": "boolean"
},
{
"name": "study_design_allocation",
"description": "The method by which participants are assigned to arms in a clinical trial.",
"type": "enum"
},
{
"name": "title",
"description": "The title of the clinical study, corresponding to the title of the protocol.",
"type": "string"
},
{
"name": "project.id",
"description": "A unique identifier for records in this 'project' table.",
"type": "string"
},
{
"name": "verification_date",
"description": "The date on which the responsible party last verified the clinical study information in the entire ClinicalTrials.gov record for the clinical study, even if no additional or updated information is being submitted.",
"type": "string"
}
]
},
{
"name": "center",
"description": "Genetic Research Center (GRC) or other clinical center at which research participants are recruited.",
"links": [
"project"
],
"required": [
"name",
"project.id",
"center.id"
],
"properties": [
{
"name": "name",
"description": "Name of center at which participants were recruited and/or at which data were collected.",
"type": "string"
},
{
"name": "project.id",
"description": "Unique identifiers for records in the 'project' table that relate via this foreign key to records in this 'center' table.",
"type": "string"
},
{
"name": "center.id",
"description": "A unique identifier for records in this 'center' table.",
"type": "string"
}
]
},
{
"name": "participant",
"description": "The collection of all data related to a specific subject in the context of a specific project.",
"links": [
"project",
"center"
],
"required": [
"participant.id",
"project.id"
],
"properties": [
{
"name": "initials",
"description": "The participant's initials.",
"type": "string"
},
{
"name": "project.id",
"description": "Unique identifiers for records in the 'project' table that relate via this foreign key to records in this 'participant' table.",
"type": "string"
},
{
"name": "participant.id",
"description": "A unique identifier for records in this 'participant' table.",
"type": "string"
},
{
"name": "consent_codes",
"description": "",
"type": "array"
},
{
"name": "consented_for_data_sharing",
"description": "The participant has consented to share their data.",
"type": "boolean"
},
{
"name": "consortium_id_of_affected_spouse",
"description": "TBD",
"type": "integer"
},
{
"name": "mothers_consortium_id",
"description": "TBD",
"type": "integer"
},
{
"name": "center.id",
"description": "Unique identifiers for records in the 'center' table that relate via this foreign key to records in this 'participant' table.",
"type": "string"
}
]
},
{
"name": "summary_file",
"description": "A summary of the data file, including the number of rows, columns, and data types.",
"links": [
"center"
],
"required": [
"data_format",
"file_size",
"center.id",
"summary_file.id"
],
"properties": [
{
"name": "data_format",
"description": "Format of the data files.",
"type": "enum"
},
{
"name": "file_size",
"description": "The size of the data file (object) in bytes.",
"type": "integer"
},
{
"name": "center.id",
"description": "Unique identifiers for records in the 'center' table that relate via this foreign key to records in this'summary_file' table.",
"type": "string"
},
{
"name": "summary_file.id",
"description": "A unique identifier for records in this'summary_file' table.",
"type": "string"
}
]
},
{
"name": "visit",
"description": "A visit by a patient or study participant to a medical professional. A clinical encounter that encompasses planned and unplanned trial interventions, procedures and assessments that may be performed on a participant. A visit has a start and an end, each described with a rule. The process by which information about the health status of an individual is obtained before and after a study has officially closed; an activity that continues something that has already begun or that repeats something that has already been done.",
"links": [
"participant"
],
"required": [
"visit.id",
"participant.id"
],
"properties": [
{
"name": "age_at_visit",
"description": "The study participant's age, in years, at the visit. If the age is greater than 89 years, use the age_at_visit_gt89 property instead.",
"type": "number"
},
{
"name": "bmi",
"description": "The body mass divided by the square of the body height expressed in units of kg/m^2.",
"type": "number"
},
{
"name": "days_to_follow_up",
"description": "Number of days between the date used for index and the date the patient was seen or contacted at follow-up.",
"type": "integer"
},
{
"name": "ever_transferred",
"description": "Participant ever transferred sites (changed ids)",
"type": "enum"
},
{
"name": "harmonized_visit_number",
"description": "The derived harmonized visit number for the studies MACS and WIHS.",
"type": "integer"
},
{
"name": "health_insurance",
"description": "Currently have any health insurance",
"type": "boolean"
},
{
"name": "review_yr",
"description": "Year in which the participant's visit was reviewed",
"type": "integer"
},
{
"name": "visit_date",
"description": "Year of the visit.",
"type": "integer"
},
{
"name": "visit_number",
"description": "Visit number",
"type": "integer"
},
{
"name": "visit_type",
"description": "Define if the visit is a follow-up or the baseline visit.",
"type": "enum"
},
{
"name": "weight",
"description": "The weight of the participant measured in grams.",
"type": "number"
},
{
"name": "participant.id",
"description": "Unique identifiers for records in the 'participant' table that relate via this foreign key to records in this 'visit' table.",
"type": "string"
},
{
"name": "visit.id",
"description": "A unique identifier for records in this 'visit' table.",
"type": "string"
}
]
},
{
"name": "alias",
"description": "An alias for the subject.",
"links": [
"participant"
],
"required": [
"participant.id",
"alias.id"
],
"properties": [
{
"name": "participant.id",
"description": "Unique identifiers for records in the 'participant' table that relate via this foreign key to records in this 'alias' table.",
"type": "string"
},
{
"name": "alias.id",
"description": "A unique identifier for records in this 'alias' table.",
"type": "string"
}
]
},
{
"name": "diagnosis",
"description": "Data from the investigation, analysis and recognition of the presence and nature of disease, condition, or injury from expressed signs and symptoms; also, the scientific determination of any kind; the concise results of such an investigation.",
"links": [
"visit"
],
"required": [
"visit.id",
"diagnosis.id"
],
"properties": [
{
"name": "age_at_diagnosis",
"description": "The age of the patient at the time of diagnosis.",
"type": "number"
},
{
"name": "age_at_diagnosis_gt89",
"description": "Indicates if the age at diagnosis is greater than 89 years.",
"type": "enum"
},
{
"name": "ibd_affection_status",
"description": "The IBD Affection Status of the patient.",
"type": "enum"
},
{
"name": "visit.id",
"description": "Unique identifiers for records in the 'visit' table that relate via this foreign key to records in this 'diagnosis' table.",
"type": "string"
},
{
"name": "diagnosis.id",
"description": "A unique identifier for records in this 'diagnosis' table.",
"type": "string"
}
]
},
{
"name": "exposure",
"description": "Data related to exposure information.",
"links": [
"visit"
],
"required": [
"visit.id",
"exposure.id"
],
"properties": [
{
"name": "nocigar_day_unknown",
"description": "Unknown",
"type": "enum"
},
{
"name": "smoking",
"description": "Smoking",
"type": "enum"
},
{
"name": "smoking_stop",
"description": "Smoking stop",
"type": "enum"
},
{
"name": "visit.id",
"description": "Unique identifiers for records in the 'visit' table that relate via this foreign key to records in this 'exposure' table.",
"type": "string"
},
{
"name": "exposure.id",
"description": "A unique identifier for records in this 'exposure' table.",
"type": "string"
}
]
}
]
}
"""
|