Skip to content

Commit

Permalink
Add summary export
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Jun 9, 2020
1 parent 5fae793 commit 7a6cb75
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
5 changes: 0 additions & 5 deletions docs/summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@
<td>21</td>
</tr>

<tr>
<td>?</td>
<td>20</td>
</tr>

<tr>
<td>Protein Complex</td>
<td>20</td>
Expand Down
Binary file added docs/summary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 31 additions & 5 deletions src/conso/export/html/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"""Export CONSO to HTML."""

import os
from collections import defaultdict
from collections import Counter, defaultdict
from typing import Optional

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from jinja2 import Environment, FileSystemLoader

#: Path to this directory
Expand Down Expand Up @@ -44,17 +46,20 @@ def main(directory: Optional[str] = None, debug_links: bool = False) -> None:

terms_df['author_name'] = terms_df['Author'].map(authors.get)

synonyms_df = pd.read_csv(SYNONYMS_PATH, sep='\t')
synonyms = defaultdict(list)
for _, row in pd.read_csv(SYNONYMS_PATH, sep='\t').iterrows():
for _, row in synonyms_df.iterrows():
synonyms[row.identifier].append((row.synonym, row.reference, row.specificity))

xrefs_df = pd.read_csv(XREFS_PATH, sep='\t')
xrefs = defaultdict(list)
for _, row in pd.read_csv(XREFS_PATH, sep='\t').iterrows():
for _, row in xrefs_df.iterrows():
xrefs[row.identifier].append((row.database, row.database_identifier))

relations_df = pd.read_csv(RELATIONS_PATH, sep='\t')
incoming_relations = defaultdict(list)
outgoing_relations = defaultdict(list)
for _, row in pd.read_csv(RELATIONS_PATH, sep='\t').iterrows():
for _, row in relations_df.iterrows():
if row['Source Namespace'] == CONSO:
outgoing_relations[row['Source Identifier']].append((
row['Relation'],
Expand Down Expand Up @@ -86,8 +91,11 @@ def main(directory: Optional[str] = None, debug_links: bool = False) -> None:
with open(os.path.join(directory, 'index.html'), 'w') as file:
print(index_html, file=file)

summary_df = terms_df.groupby('Type').count().sort_values('Identifier', ascending=False)['Identifier'].reset_index()
summary_df['Type'] = summary_df['Type'].map(str.title)
summary_df = summary_df[summary_df['Type'] != '?']
summary_html = summary_template.render(
terms_df=terms_df,
summary_df=summary_df,
)
with open(os.path.join(directory, 'summary.html'), 'w') as file:
print(summary_html, file=file)
Expand All @@ -106,6 +114,24 @@ def main(directory: Optional[str] = None, debug_links: bool = False) -> None:
with open(os.path.join(subdirectory, 'index.html'), 'w') as file:
print(html, file=file)

# Make some plots
fig, (lax, rax) = plt.subplots(ncols=2, figsize=(12, 5))
sns.barplot(data=summary_df, y='Type', x='Identifier', ax=lax)
lax.set_xlabel('Count')
lax.set_ylabel('')
lax.set_title(f'Entries ({len(terms_df.index)})')

relations = Counter(relations_df['Relation'].map(lambda s: s.replace('_', ' ').title()))
relations['Has Synonym'] = len(synonyms_df.index)
relations['Has Xref'] = len(xrefs_df.index)
relations_summary_df = pd.DataFrame(relations.most_common(), columns=['Type', 'Count'])
sns.barplot(data=relations_summary_df, x='Count', y='Type', ax=rax)
rax.set_xscale('log')
rax.set_ylabel('')
rax.set_title(f'Relations ({sum(relations.values())})')
plt.tight_layout()
plt.savefig(os.path.join(OUTPUT_DIRECTORY, 'summary.png'), dpi=300)


if __name__ == '__main__':
main()
7 changes: 3 additions & 4 deletions src/conso/export/html/summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
</tr>
</thead>
<tbody>
{% for idx, row in terms_df.groupby('Type').count().sort_values('Identifier',
ascending=False).iterrows() %}
{% for idx, count in summary_df.values %}
<tr>
<td>{{ idx.title() }}</td>
<td>{{ row['Identifier'] }}</td>
<td>{{ idx }}</td>
<td>{{ count }}</td>
</tr>
{% endfor %}
</tbody>
Expand Down

0 comments on commit 7a6cb75

Please sign in to comment.