首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将PubMed数据导入Google Sheets

将PubMed数据导入Google Sheets
EN

Stack Overflow用户
提问于 2018-08-18 02:21:15
回答 1查看 248关注 0票数 0

我在找人帮忙。我正在尝试从PubMed抓取一个作者的出版物,并使用应用程序脚本将数据填充到Google Sheets中。我已经完成了下面的代码,现在被卡住了。

基本上,我所做的是首先从一个特定的作者那里提取所有的Pubmed ID,该作者的名字来自于工作表的名称。然后,我尝试创建一个循环来遍历每个Pubmed ID JSON摘要,并提取我想要的每个字段。我已经可以取消酒吧的约会了。我的想法是对PMID的每个字段执行一个循环,将其存储在一个数组中,然后将其返回到我的工作表中。然而,我现在在尝试获取第二个字段- title -以及所有后续字段(例如,作者、最后作者、第一作者等)时遇到了困难。

任何帮助都将不胜感激。

代码语言:javascript
复制
function IMPORTPMID(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];                   
  var author = sheet.getSheetName();

  var url = ("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=" + author + "[author]&retmode=json&retmax=1000");
  var response = UrlFetchApp.fetch(url);
  var AllAuthorPMID = JSON.parse(response.getContentText());

  var xpath = "esearchresult/idlist";
  var patharray = xpath.split("/");
  for (var i = 0; i < patharray.length; i++) {
     AllAuthorPMID = AllAuthorPMID[patharray[i]];
  }

  var PMID = AllAuthorPMID;
  var PDparsearray = [PMID.length];
  var titleparsearray = [PMID.length];
  for (var x = 0; x < PMID.length; x++) {
    var urlsum = ("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&retmode=json&rettype=abstract&id=" + PMID[x]);
    var ressum = UrlFetchApp.fetch(urlsum);
    var contentsum = ressum.getContentText();
    var jsonsum = JSON.parse(contentsum);

    var PDpath = "result/" + PMID[x] + "/pubdate";
    var titlepath = "result/" + PMID[x] + "/title";
    var PDpatharray = PDpath.split("/");
    var titlepatharray = titlepath.split("/");

    for (var j = 0; j < PDpatharray.length; j++) {
      var jsonsum = jsonsum[PDpatharray[j]];
    }  

    PDparsearray[x] = jsonsum;
  }

  var tempArr = [];
  for (var obj in AllAuthorPMID) {
    tempArr.push([obj, AllAuthorPMID[obj], PDparsearray[obj]]);
  }

  return tempArr;
}
EN

回答 1

Stack Overflow用户

发布于 2018-08-18 14:04:24

从给定PubMed ID的PubMed JSON响应中,您应该能够确定要包含在摘要报告中的字段名(以及它们的路径)。如果它们都在同一级别,则读取它们更容易实现,但如果其中一些是子字段的属性,如果您在设置中给出了正确的路径,您仍然可以访问它们。

考虑“源JSON":

代码语言:javascript
复制
[
  { "pubMedId": "1234",
    "name": "Jay Sahn",
    "publications": [
      { "pubId": "abcd",
        "issn": "A1B2C3",
        "title": "Dynamic JSON Parsing: A Journey into Madness",
        "authors": [
          { "pubMedId": "1234" },
          { "pubMedId": "2345" }
        ]
      },
      { "pubId": "efgh",
        ...
      },
      ...
    ],
    ...
  },
  ...
]

pubIdissn字段将处于同一级别,而publicationsauthors字段则不在同一级别。

通过1)硬编码字段访问,或者2)编写解析字段路径并提供字段路径的代码,您可以在同一个循环中检索pubMedIdpublications字段(以及您想要的其他字段)。

选项1可能会更快,但如果你突然想要获得一个新字段,那么灵活性就会差得多,因为你必须记住如何编写代码来访问那个字段,以及在哪里插入它,等等。如果API发生变化,上帝会拯救你。

选项2很难正确,但一旦正确,将(应该)适用于您(正确)指定的任何字段。获取新字段非常简单,只需在相关的配置变量中写入它的路径即可。可能有一些库可以帮你做到这一点。

要将上面的行转换为电子表格行(外部数组中的每个pubMedId一个,例如您查询其example code的in ),请考虑下面的API

代码语言:javascript
复制
function foo() {
  const sheet = /* get a sheet reference somehow */;
  const resp = UrlFetchApp.fetch(...).getContentText();
  const data = JSON.parse(resp);
  // paths relative to the outermost field, which for the imaginary source is an array of "author" objects
  const fields = ['pubMedId', 'name', 'publications/pubId', 'publications/title', 'publications/authors/pubMedId'];

  const output = data.map(function (author) {
    var row = fields.map(function (f) {
      var desiredField = f.split('/').reduce(delve_, author);
      return JSON.stringify(desiredField);
    });
    return row;
  });
  sheet.getRange(1, 1, output.length, output[0].length).setValues(output);
}

function delve_(parentObj, property, i, fullPath) {
  // Dive into the given object to get the path. If the parent is an array, access its elements.
  if (parentObj === undefined)
    return;

  // Simple case: parentObj is an Object, and property exists.
  const child = parentObj[property];
  if (child)
    return child;

  // Not a direct property / index, so perhaps a property on an object in an Array.
  if (parentObj.constructor === Array)
    return collate_(parentObj, fullPath.splice(i));

  console.warn({message: "Unhandled case / missing property", 
                args: {parent: parentObj, prop: property, index: i, pathArray: fullPath}});
  return; // property didn't exist, user error.
}
function collate_(arr, fields) {
  // Obtain the given property from all elements of the array.
  const results = arr.map(function (element) {
    return fields.slice().reduce(delve_, element);
  });
  return results;
}

执行此操作将在Stackdriver中生成以下输出:

显然,您可能需要一些不同的(即真实的)字段,并且可能对如何报告它们有其他想法,所以我将这部分留给读者。

欢迎任何对以上内容进行改进的人提交PR。

推荐阅读:

解析嵌套JSON的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51900981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档