Package Bio :: Package Medline
[hide private]
[frames] | no frames]

Source Code for Package Bio.Medline

  1  # Copyright 1999 by Jeffrey Chang.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  """Code to work with Medline from the NCBI. 
  7   
  8  Classes: 
  9   - Record           A dictionary holding Medline data. 
 10   
 11  Functions: 
 12   - read             Reads one Medline record 
 13   - parse            Allows you to iterate over a bunch of Medline records 
 14  """ 
 15   
 16   
17 -class Record(dict):
18 """A dictionary holding information from a Medline record. 19 20 All data are stored under the mnemonic appearing in the Medline 21 file. These mnemonics have the following interpretations: 22 23 ========= ============================== 24 Mnemonic Description 25 --------- ------------------------------ 26 AB Abstract 27 CI Copyright Information 28 AD Affiliation 29 IRAD Investigator Affiliation 30 AID Article Identifier 31 AU Author 32 FAU Full Author 33 CN Corporate Author 34 DCOM Date Completed 35 DA Date Created 36 LR Date Last Revised 37 DEP Date of Electronic Publication 38 DP Date of Publication 39 EDAT Entrez Date 40 GS Gene Symbol 41 GN General Note 42 GR Grant Number 43 IR Investigator Name 44 FIR Full Investigator Name 45 IS ISSN 46 IP Issue 47 TA Journal Title Abbreviation 48 JT Journal Title 49 LA Language 50 LID Location Identifier 51 MID Manuscript Identifier 52 MHDA MeSH Date 53 MH MeSH Terms 54 JID NLM Unique ID 55 RF Number of References 56 OAB Other Abstract 57 OCI Other Copyright Information 58 OID Other ID 59 OT Other Term 60 OTO Other Term Owner 61 OWN Owner 62 PG Pagination 63 PS Personal Name as Subject 64 FPS Full Personal Name as Subject 65 PL Place of Publication 66 PHST Publication History Status 67 PST Publication Status 68 PT Publication Type 69 PUBM Publishing Model 70 PMC PubMed Central Identifier 71 PMID PubMed Unique Identifier 72 RN Registry Number/EC Number 73 NM Substance Name 74 SI Secondary Source ID 75 SO Source 76 SFM Space Flight Mission 77 STAT Status 78 SB Subset 79 TI Title 80 TT Transliterated Title 81 VI Volume 82 CON Comment on 83 CIN Comment in 84 EIN Erratum in 85 EFR Erratum for 86 CRI Corrected and Republished in 87 CRF Corrected and Republished from 88 PRIN Partial retraction in 89 PROF Partial retraction of 90 RPI Republished in 91 RPF Republished from 92 RIN Retraction in 93 ROF Retraction of 94 UIN Update in 95 UOF Update of 96 SPIN Summary for patients in 97 ORI Original report in 98 ========= ============================== 99 """
100 101
102 -def parse(handle):
103 """Read Medline records one by one from the handle. 104 105 The handle is either is a Medline file, a file-like object, or a list 106 of lines describing one or more Medline records. 107 108 Typical usage:: 109 110 from Bio import Medline 111 with open("mymedlinefile") as handle: 112 records = Medline.parse(handle) 113 for record in records: 114 print(record['TI']) 115 116 """ 117 # TODO - Turn that into a working doctest 118 # These keys point to string values 119 textkeys = ("ID", "PMID", "SO", "RF", "NI", "JC", "TA", "IS", "CY", "TT", 120 "CA", "IP", "VI", "DP", "YR", "PG", "LID", "DA", "LR", "OWN", 121 "STAT", "DCOM", "PUBM", "DEP", "PL", "JID", "SB", "PMC", 122 "EDAT", "MHDA", "PST", "AB", "AD", "EA", "TI", "JT") 123 handle = iter(handle) 124 125 key = "" 126 record = Record() 127 for line in handle: 128 line = line.rstrip() 129 if line[:6] == " ": # continuation line 130 if key == "MH": 131 # Multi-line MESH term, want to append to last entry in list 132 record[key][-1] += line[5:] # including space using line[5:] 133 else: 134 record[key].append(line[6:]) 135 elif line: 136 key = line[:4].rstrip() 137 if key not in record: 138 record[key] = [] 139 record[key].append(line[6:]) 140 elif record: 141 # Join each list of strings into one string. 142 for key in record: 143 if key in textkeys: 144 record[key] = " ".join(record[key]) 145 yield record 146 record = Record() 147 if record: # catch last one 148 for key in record: 149 if key in textkeys: 150 record[key] = " ".join(record[key]) 151 yield record
152 153
154 -def read(handle):
155 """Read a single Medline record from the handle. 156 157 The handle is either is a Medline file, a file-like object, or a list 158 of lines describing a Medline record. 159 160 Typical usage: 161 162 >>> from Bio import Medline 163 >>> with open("mymedlinefile") as handle: 164 ... record = Medline.read(handle) 165 ... print(record['TI']) 166 167 """ 168 # TODO - Turn that into a working doctest 169 records = parse(handle) 170 return next(records)
171