Dbase.Pubmed.PubmedUtils

Package Dbase :: Package Pubmed :: Module PubmedUtils |
[hide private]
[frames] | no frames]

# Source Code for Module Dbase.Pubmed.PubmedUtils

      1  # $Id: PubmedUtils.py 2 2006-05-06 22:54:39Z glandrum $
      2  #
      3  #  Copyright (C) 2003-2006  Rational Discovery LLC
      4  #
      5  #   @@ All Rights Reserved  @@
      6  #
      7  import re,urllib
      8  import QueryParams
      9
     10
     11 -def FillParagraph(text,width=70,indentLinesBy=0):
     12    """
     13
     14    >>> txt = "123 567 90 123\\n456 89\\n"
     15    >>> print FillParagraph(txt,width=10)
     16    123 567 90
     17    123 456 89
     18
     19    >>> print FillParagraph(txt,width=10,indentLinesBy=3)
     20    123 567 90
     21       123 456
     22       89
     23    >>> txt = '123 567 \\n'
     24    >>> print FillParagraph(txt,width=10)
     25    123 567
     26
     27    """
     28    expr = re.compile('[\ \t\n\r]+')
     29    words = expr.split(text)
     30    res = []
     31    line = ''
     32    for word in words:
     33      if word:
     34        if len(line)+len(word)>=width:
     35          res.append(line)
     36          line = ' '*indentLinesBy
     37          line += word
     38        else:
     39          if line:
     40            line += ' %s'%word
     41          else:
     42            line = word
     43    if line:
     44      res.append(line)
     45    return '\n'.join(res)
     46
     47 -def RecordsToPubmedText(records):
     48    """  Exports a sequence of JournalArticleRecords to a Medline text
     49    format (not XML) that can be imported by EndNote
     50
     51    """
     52    res = []
     53    for i in range(len(records)):
     54      rec = records[i]
     55      res.append('UI  - %d'%i)
     56      res.append('PMID- %s'%rec.PubMedId)
     57      for auth in rec.authors:
     58        res.append('AU  - %s %s'%(auth[0],auth[2]))
     59      res.append('TI  - %s'%FillParagraph(rec.Title,indentLinesBy=6))
     60      res.append('TA  - %s'%rec.Source)
     61      res.append('DP  - %s'%rec.PubYear)
     62      res.append('PG  - %s'%rec.Pages)
     63      res.append('VI  - %s'%rec.Volume)
     64      res.append('IP  - %s'%rec.Issue)
     65      res.append('AB  - %s'%FillParagraph(rec.Abstract,indentLinesBy=6))
     66
     67      for keyword in rec.keywords:
     68        res.append('MH  - %s'%(keyword))
     69
     70    return '\n'.join(res)
     71
     72
     73
     74 -def GetSearchURL(term,db="PubMed"):
     75    queryParams = {'CMD':'search',
     76                   'DB':db,
     77                   'term':term}
     78    url = '%s?%s'%(QueryParams.queryBase,
     79                   urllib.urlencode(queryParams))
     80    return url
     81
     82
     83
     84  #------------------------------------
     85  #
     86  #  doctest boilerplate
     87  #
     88 -def _test():
     89    import doctest,sys
     90    return doctest.testmod(sys.modules["__main__"])
     91
     92  if __name__ == '__main__':
     93    import sys,os.path
     94    failed,tried = _test()
     95    sys.exit(failed)
     96
     97    import Searches
     98    recs = Searches.GetRecords(['11960484','10893315'],
     99                               conn=open('test_data/records.xml','r'))
    100    res = RecordsToPubmedText(recs)
    101    print res
    102