Ejemplo n.º 1
0
    def fetch_data(
        self
    ):  #TODO: MUST be splitted into several methods, it is too huge and ugly!
        """Queries Entrez EUtils to retrieve information on a reference."""
        if self.pmid:
            try:
                handle = Entrez.esummary(db="pubmed", id=self.pmid)
                r = Entrez.read(handle)
                # print r
                r = r[0]  #  reference.
                self.title = unicode(r['Title'])
                self.volume = r.get('Volume', None) or None
                self.issue = r.get('Issue', None) or None
                self.pages = r['Pages']
                self.authors = unicode('; '.join(r['AuthorList']))
                self.journal = r['FullJournalName']
                self.alternate_journal = r['Source']
                self.year = int(r['PubDate'].split(' ')[0])
                self.language = r['LangList'][0]
                self.doi = r.get('DOI', None)

                handle = Entrez.efetch(db="pubmed",
                                       id=self.pmid,
                                       rettype="medline",
                                       retmode="text")
                records = Medline.parse(handle)
                for record in records:
                    pass
                self.abstract = record.get('AB', None)
                s = record['EDAT']
                #print "; ".join(record.get('MH', ''))
                self.keywords = "; ".join(record.get('MH',
                                                     '')) or None  # MeSH terms
            except Exception as e:
                print "Failed fetching information"
                print e, self
            if not self.title:
                from denigma.library import Bibliography
                bib = Bibliography()
                r = bib.efetch(id=self.pmid)
                #print("datasets.Reference.fetch_data")
                self.__dict__.update(r.__dict__)

                # Transforming lists into strings:
                self.keywords = "; ".join(self.keywords)
                self.authors = "; ".join(self.authors)

            self.date = normalize_time(s)
Ejemplo n.º 2
0
    def save(self, update=False, *args, **kwargs):
        if not self.pk or update:
            # This code only happens if the objects is not in the database yet.
            # Otherwise it would have pk.
            try:
                #Reference._for_write = True
                if self.pmid or 'pmid' in kwargs:
                    print "Did not failed"
                    return Reference.objects.get(pmid=self.pmid)  #, False
                elif self.title or 'title' in kwargs:
                    #print self.title
                    handle = Entrez.esearch(db='pubmed', term=self.title)
                    print "Got handle"
                    record = Entrez.read(handle)
                    print "Got record", record
                    print record['Count'], type(record['Count'])
                    if record['Count'] == "1":
                        print "Record count is 1"
                        self.pmid = record['IdList'][0]
                        #print self.title, self.pmid
                        Reference.fetch_data(self)
                        print("Saving")
                        super(Reference, self).save(*args, **kwargs)
                        print("Saved")
                    else:
                        from denigma.library import Bibliography  # This statement at the top breaks Denigma for unknown reason.
                        #print("Trying it different. %s" % type(self.title))
                        # Google:
                        bib = Bibliography()
                        #print("googling")
                        r = bib.google(self.title)
                        if r:
                            r = r[0]
                            self.pmid = r.pmid
                            #print("Google successufull: %s" % self.pmid)
                        else:
                            #print("Google failed.")
                            #                           r = bib.find(self.title)[0]
                            #                           self.pmid = r.pmid
                            #                           print self.pmid
                            #print("Trying it different.")
                            r = bib.find(unicode(self.title))
                            if len(r) == 1:
                                r = r[0]
                                self.pmid = r.pmid
                                print self.pmid
                            elif len(r) > 1:
                                title = normalize_title(self.title)
                                for areference in r:
                                    if normalize_title(
                                            areference.title) == title:
                                        r = areference
                        print("datasets.Reference.save()")
                        self.__dict__.update(r.__dict__)
                        print r
                        print vars(r)
                        self.date = normalize_time(r.date)

                        print "# Transforming lists into strings:"
                        self.keywords = "; ".join(self.keywords)
                        self.authors = "; ".join(self.authors)
                        print "calling super"
                        print self.pmid
                        try:
                            super(Reference, self).save(
                                *args,
                                **kwargs)  # Just save the given information.
                        except Exception as e:
                            print e
                        print "called super"
                        # Raise Exception and state the the given information yielded more than one reference.
                else:
                    super(Reference, self).save(*args, **kwargs)
            except Reference.DoesNotExist as e:
                print "Error", e
                Reference.fetch_data(self)
                super(Reference, self).save(*args, **kwargs)
        else:
            super(Reference, self).save(*args, **kwargs)