<jupyter_start><jupyter_text>Extract-Transform-Load ScriptExtract-Transform-Load Scripts (ETLS) are common tools in data management. The purpose of ETLS is to gather relevant data (both direct and inferred) from public databases and capture important features in a possibly different data structure schema for specific analysis. PubMed Central ETLS ExampleThis script will Extract data from the CSV files provided to us by Stanford, Transform the data into a format usable by GeneDive, and then Load the data into the GeneDive sqlite database.Whenever new data is obtained for GeneDive, this process should be run against that dataset.<jupyter_code>import re
import sqlite3
INTERACTIONS_FILE = "pmc.csv"
DELIMITER = "\t"
DATABASE = "data.sqlite"<jupyter_output><empty_output><jupyter_text>If write is false, the script will run but not write anything to the database. This keeps it safe while you're nosing around, and can also be useful if you need to re-generate the complete typeahead/adjacency files.<jupyter_code>WRITE = True
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()<jupyter_output><empty_output><jupyter_text>Map the columns as they appear in the file to the correct values.<jupyter_code>interactions = []

with open(INTERACTIONS_FILE) as file:
    for line in file:
        line = line[:-1]
        line = line.split(DELIMITER)
        
        interaction = {
          "journal": line[0],
          "article_id": line[1],
          "pubmed_id": line[2],
          "sentence_id": line[3],
          "mention1_offset": line[5],
          "mention2_offset": line[4],
          "mention1": line[6],
          "mention2": line[7],
          "geneids1": line[8],
          "geneids2": line[9],
          "probability": line[10],
          "excerpt": line[11]    
        }
        
        interactions.append(interaction)<jupyter_output><empty_output><jupyter_text>Remove any interactions for which the a gene traces to multiple IDs.<jupyter_code>interactions = [i for i in interactions if ( ';' not in i['geneids1'] and ';' not in i['geneids2'] ) ]<jupyter_output><empty_output><jupyter_text>GeneDive expects the target genes in the excerpt to be wrapped in pound signs. This is important because a sentence may mention the target gene multiple times, so we need to use the offset data her to make sure we tag the right mention.<jupyter_code>for i in interactions:
    excerpt = i['excerpt']
    
    excerpt = re.sub('"', '', excerpt)
    tokens = excerpt.split(" ")
    offset1 = int(i['mention1_offset'])
    offset2 = int(i['mention2_offset'])

    tokens[offset1] = "".join(["#",tokens[offset1],"#"])
    tokens[offset2] = "".join(["#",tokens[offset2],"#"])
    
    i['excerpt'] = " ".join(tokens)<jupyter_output><empty_output><jupyter_text>**Specific for PMC Data**We didn't get Journal Data - we need to extract it from the article titles. Comment out the next section if journal titles were included.<jupyter_code>for i in interactions:
    journal = i['article_id'].split("_")
    x = 0

    while x < len(journal):
        if journal[x][:2] == "19" or journal[x][:2] == "20":
            journal = " ".join(journal[:x])
            break
            
        x+= 1

    i['journal'] = journal<jupyter_output><empty_output><jupyter_text>Our insert statement - probably don't need to touch this<jupyter_code>INTERACTIONS_WRITE = '''insert into interactions ( journal, article_id, pubmed_id, sentence_id, mention1_offset, mention2_offset, mention1, mention2, geneids1, geneids2, probability, context, section, reactome ) values ( "{}", "{}", "{}", "{}", "{}", "{}", "{}", "{}", "{}", "{}", "{}", "{}", "{}", "{}" );'''
for interaction in interactions:
    statement = INTERACTIONS_WRITE.format(
        interaction['journal'],
        interaction['article_id'],
        interaction['pubmed_id'],
        interaction['sentence_id'],
        interaction['mention1_offset'],
        interaction['mention2_offset'],
        interaction['mention1'],
        interaction['mention2'],
        interaction['geneids1'],
        interaction['geneids2'],
        interaction['probability'],
        interaction['excerpt'],
        "Unknown",
        0
    )
    
    cursor.execute(statement)

if WRITE:
    conn.commit()

conn.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1256422232601588, 'fraction_numerical': 0.012610929472209247, 'mean_word_length': 3.691128148959474, 'pattern_counts': {'":': 13, '<': 30, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '261717', 'license': 'MIT', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1274, 'n_words': 378, 'path': 'preprocessing/AddData.ipynb', 'repo_name': 'brookthomas/GeneDive', 'size': '4282'}