<jupyter_start><jupyter_text>NSF COA author/affiliation toolInspired by [this awesome tool](https://github.com/ejfertig/NSFBiosketch) from Dr. <NAME>, but unable to get it to run in time due to a java install problem with the xlsx package in my perpetually infuriating R environment, I whipped up something similar for the Pythonistas. This tool will take a list of PMIDs and return the list of authors and affiliations, along with most recent authorship date.<jupyter_code>import pandas as pd
from pymed import PubMed
from time import sleep<jupyter_output><empty_output><jupyter_text>Import papersImport a list of your publication PMIDs, one per line in a plaintext file<jupyter_code>pmids = []
with open('PMID-export.txt', 'r') as f:
    for line in f:
        pmids.append(line.strip())
pmids<jupyter_output><empty_output><jupyter_text>We'll sort them in chronological order, to ensure we get the most recent conflict dates per author<jupyter_code>pmids.sort()
# Create a PubMed object that GraphQL can use to query
# Note that the parameters are not required but kindly requested by PubMed Central
# https://www.ncbi.nlm.nih.gov/pmc/tools/developers/

pubmed = PubMed(tool="BioSketchify", email="my<EMAIL>")<jupyter_output><empty_output><jupyter_text>Retrieve and parse PubMed entriesQuery PubMed one publication at a time, and parse the author and affiliation list.Due to API limits, we have to limit the rate at which we query.<jupyter_code>authors = {}

for pmid in pmids:
    results = pubmed.query(pmid, max_results=1)
    for article in results:
        for author in article.authors:
            name = '%s, %s' % (author['lastname'], author['firstname'])
            year = article.publication_date.year
            affiliation = author['affiliation']
            authors[name] = (year, affiliation)
    print(article.title)
    sleep(1)<jupyter_output>Cephaloticoccus gen. nov., a new genus of 'Verrucomicrobia' containing two novel species isolated from Cephalotes ant guts.
Dissecting host-associated communities with DNA barcodes.
Gut microbiota of dung beetles correspond to dietary specializations of adults and larvae.
By their own devices: invasive Argentine ants have shifted diet without clear aid from symbiotic microbes.
Unraveling the processes shaping mammalian gut microbiomes over evolutionary time.
Corrigendum: Cephaloticoccus gen. nov., a new genus of 'Verrucomicrobia' containing two novel species isolated from Cephalotes ant guts.
The structured diversity of specialized gut symbionts of the New World army ants.
Ant-plant mutualism: a dietary by-product of a tropical ant's macronutrient requirements.
Dramatic Differences in Gut Bacterial Densities Correlate with Diet and Habitat in Rainforest Ants.
A communal catalogue reveals Earth's multiscale microbial diversity.
The human microbiome in evolution.
Improving saliva shotgun[...]<jupyter_text>Make an author dataframe, with blank columns for "Organization" and "Department"<jupyter_code>author_df = pd.DataFrame.from_dict(authors, orient='index', columns=['year','affiliation'])
author_df['Organization'] = ''
author_df['Department'] = ''

author_df.head()<jupyter_output><empty_output><jupyter_text>Split affiliation into department and organizationThis might be optional, but PubMed stores affiliation in a single column, and NSF requests 'Organization' be in its own column. This function will loop over the author dataframe, and present each comma-separated element of the 'affiliation' value to you and prompt for input. Press 1 to store that chunk to the 'Department' column, 2 to store that chunk to the 'Organization' column, and any other key to move to the next author.It will only parse authors that have no entry for the required 'Organization' column, so if you miss that and re-run this cell it will pick up where you left off.<jupyter_code>print("Enter 1 for Department, 2 for Organization, or nothing to skip rest")

for i, author in author_df.iterrows():
    if author['Organization'] != '':
        continue
    try:
        for bit in author['affiliation'].split(','):

            print(bit)
            choice = input("Input:")
            if choice == '1':
                author_df.loc[i, 'Department'] = author_df.loc[i, 'Department'] + bit
            elif choice == '2':
                author_df.loc[i, 'Organization'] = author_df.loc[i, 'Organization'] + bit
            else:
                break
    except:
        continue
    
author_df.head()<jupyter_output><empty_output><jupyter_text>Export author dataframe to CSV fileYou can now open this in your favorite spreadsheet column to clean it up and add to the NSF workbook.<jupyter_code>author_df.to_csv('authors_with_affiliations.csv')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup