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