{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# converting the ehrlink pdf data supplement to a tab seperated text\n", "\n", "This notebook converts the [pdf of indications](http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3422843/bin/amiajnl-2012-000852-s1.pdf) from:\n", "\n", "> McCoy et al. (2012) **Development and evaluation of a crowdsourcing methodology for knowledge base construction: identifying relationships between clinical problems and medications**. *Journal of the American Medical Informatics Association* [doi:10.1136/amiajnl-2012-000852](//dx.doi.org/10.1136/amiajnl-2012-000852)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import csv" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# convert pdf to text\n", "!pdftotext download/amiajnl-2012-000852-s1.pdf" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def generate_lines(path):\n", " with open(path) as read_file:\n", " for line in read_file:\n", " line = line.lstrip('\\x0c')\n", " if line.startswith('file://'):\n", " continue\n", " if not line.strip():\n", " continue\n", " yield line" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n_lines = 0\n", "with open('download/amiajnl-2012-000852-s1.tsv', 'w') as write_file:\n", " writer = csv.writer(write_file, delimiter='\\t')\n", " line_gen = generate_lines('download/amiajnl-2012-000852-s1.txt')\n", " reader = csv.reader(line_gen, delimiter='|', quotechar='\"', quoting=csv.QUOTE_ALL)\n", " for row in reader:\n", " row = [elem.replace('\\n', ' ') for elem in row]\n", " writer.writerow(row)\n", " n_lines += 1\n", "\n", "assert n_lines == 11166 + 1 # 1 for header" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | medication_definition_id | \n", "medication | \n", "problem_definition_id | \n", "problem | \n", "patient_link_frequency | \n", "link_ratio | \n", "
---|---|---|---|---|---|---|
0 | \n", "70 | \n", "Albuterol Sulfate (5 MG/ML) 0.5% Inhalation Ne... | \n", "64181 | \n", "Asthma | \n", "6 | \n", "0.600000 | \n", "
1 | \n", "70 | \n", "Albuterol Sulfate (5 MG/ML) 0.5% Inhalation Ne... | \n", "64205 | \n", "Chronic Obstructive Pulmonary Disease | \n", "3 | \n", "0.428571 | \n", "
2 | \n", "72 | \n", "Albuterol Sulfate 2 MG/5ML Oral Syrup | \n", "77891 | \n", "Acute Upper Respiratory Infection | \n", "2 | \n", "0.666667 | \n", "