<s> '''<STR_LIT>''' <EOL> import sys <EOL> import re <EOL> import urllib2 <EOL> import urllib <EOL> import xml . etree . ElementTree as ET <EOL> import CGAT . Experiment as E <EOL> import CGAT . IOTools as IOTools <EOL> def main ( argv = None ) : <EOL> """<STR_LIT>""" <EOL> if not argv : <EOL> argv = sys . argv <EOL> parser = E . OptionParser ( version = "<STR_LIT>" , <EOL> usage = globals ( ) [ "<STR_LIT>" ] ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT:test>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> ( options , args ) = E . Start ( parser , argv = argv ) <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT:y>' , <EOL> } <EOL> params = urllib . urlencode ( params ) <EOL> query_filter = '<STR_LIT>' <EOL> query_retrieve = '<STR_LIT>' <EOL> query_fetch = '<STR_LIT>' <EOL> query_summary = '<STR_LIT>' <EOL> data = urllib2 . urlopen ( query_filter , params ) <EOL> etree = ET . parse ( data ) <EOL> root = etree . getroot ( ) <EOL> webenv = root . find ( "<STR_LIT>" ) . text <EOL> query_key = root . find ( "<STR_LIT>" ) . text <EOL> uids = [ x . text for x in root . findall ( "<STR_LIT>" ) ] <EOL> pubmedlist = [ ] <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> params = urllib . urlencode ( params ) <EOL> params += "<STR_LIT:&>" + "<STR_LIT:&>" . join ( [ "<STR_LIT>" % x for x in uids ] ) <EOL> data = urllib2 . urlopen ( query_retrieve , params ) <EOL> etree = ET . parse ( data ) <EOL> root = etree . getroot ( ) <EOL> map_uid2pmid = { } <EOL> for linkset in root . findall ( "<STR_LIT>" ) : <EOL> uid = linkset . find ( "<STR_LIT>" ) . text <EOL> try : <EOL> pmid = linkset . find ( "<STR_LIT>" ) . text <EOL> except AttributeError : <EOL> pmid = None <EOL> map_uid2pmid [ uid ] = pmid <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : "<STR_LIT:U+002C>" . join ( uids ) } <EOL> params = urllib . urlencode ( params ) <EOL> data = urllib2 . urlopen ( query_fetch , params ) . read ( ) <EOL> map_uid2accession = { } <EOL> map_uid2description = { } <EOL> map_pmid2accession = { } <EOL> for block in data . split ( "<STR_LIT>" ) : <EOL> uid = re . search ( "<STR_LIT>" , block ) . groups ( ) [ <NUM_LIT:0> ] <EOL> accession = re . search ( "<STR_LIT>" , block ) . groups ( ) [ <NUM_LIT:0> ] <EOL> map_uid2accession [ uid ] = accession <EOL> lines = block . split ( "<STR_LIT:\n>" ) <EOL> description = lines [ <NUM_LIT:0> ] <EOL> map_uid2description [ uid ] = description <EOL> map_pmid2accession [ map_uid2pmid [ uid ] ] = accession . encode ( <EOL> "<STR_LIT:ascii>" , "<STR_LIT:ignore>" ) <EOL> url_pmid = "<STR_LIT>" <EOL> url_geo = "<STR_LIT>" <EOL> outfile_uid = IOTools . openFile ( "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> outfile_uid . write ( "<STR_LIT>" ) <EOL> for uid in uids : <EOL> outfile_uid . write ( "<STR_LIT:\t>" . join ( map ( str , <EOL> ( uid , <EOL> map_uid2description [ uid ] , <EOL> url_pmid % map_uid2pmid . get ( uid , "<STR_LIT>" ) , <EOL> url_geo % map_uid2accession . get ( uid , "<STR_LIT>" ) ) ) ) + "<STR_LIT:\n>" ) <EOL> outfile_pmid = IOTools . openFile ( "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> outfile_pmid . write ( "<STR_LIT>" ) <EOL> E . info ( "<STR_LIT>" ) <EOL> for pmid in map_uid2pmid . values ( ) : <EOL> if pmid is None : <EOL> continue <EOL> print pmid <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : pmid , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> params = urllib . urlencode ( params ) <EOL> data = urllib2 . urlopen ( query_fetch , params ) <EOL> etree = ET . parse ( data ) <EOL> root = etree . getroot ( ) <EOL> article = root . find ( "<STR_LIT>" ) <EOL> assert article is not None <EOL> journal = article . find ( "<STR_LIT>" ) <EOL> assert journal is not None <EOL> year = journal . find ( "<STR_LIT>" ) . text <EOL> journal_title = journal . find ( "<STR_LIT>" ) . text <EOL> title = article . find ( "<STR_LIT>" ) . text . encode ( "<STR_LIT:ascii>" , "<STR_LIT:ignore>" ) <EOL> try : <EOL> abstract = article . find ( <EOL> "<STR_LIT>" ) . text . encode ( "<STR_LIT:ascii>" , "<STR_LIT:ignore>" ) <EOL> except AttributeError : <EOL> abstract = "<STR_LIT>" <EOL> outfile_pmid . write ( "<STR_LIT:\t>" . join ( map ( str , <EOL> ( url_pmid % pmid , <EOL> year , <EOL> journal_title , <EOL> title , <EOL> abstract , <EOL> url_geo % map_pmid2accession . get ( pmid , "<STR_LIT>" ) ) ) ) + "<STR_LIT:\n>" ) <EOL> E . Stop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
