Package rdkit :: Package Dbase :: Package Pubmed :: Module PubmedUtils

[hide private]
[frames] | no frames]

# Source Code for Module rdkit.Dbase.Pubmed.PubmedUtils

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