<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/gene-homology-part-2-creating-directed-networks-with-igraph/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://shiring.github.io/genome/2016/12/14/homologous_genes_part2_post"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In my <a target="_blank" rel="nofollow" href="https://shiring.github.io/genome/2016/12/11/homologous_genes_post">last post</a> I created a gene homology network for human genes. In this post I want to extend the network to include edges for other species.</p>
<p>First, I am loading biomaRt and extract a list of all available datasets.</p>
<div>
<pre><code><span>library</span><span>(</span><span>biomaRt</span><span>)</span><span>
</span><span>ensembl</span><span> </span><span>=</span><span> </span><span>useMart</span><span>(</span><span>&quot;ensembl&quot;</span><span>)</span><span>
</span><span>datasets</span><span> </span><span>&lt;-</span><span> </span><span>listDatasets</span><span>(</span><span>ensembl</span><span>)</span><span>
</span></code></pre>
</div>
<p>For this network I don’t want to include all species with available information in Biomart because it would get way too messy. So, I am only picking the 11 species below: Human, fruitfly, mouse, C. elegans, dog, zebrafish, chicken, chimp, rat, yeast and pig. For all of these species, I am going to use their respective org.db database to extract gene information.</p>
<p>In order to loop over all species’ org.dbs for installation and loading, I create a new column with the name of the library.</p>
<div>
<pre><code><span>datasets</span><span>$</span><span>orgDb</span><span> </span><span>&lt;-</span><span> </span><span>NA</span><span>
</span><span>datasets</span><span>[</span><span>grep</span><span>(</span><span>&quot;hsapiens&quot;</span><span>,</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>),</span><span> </span><span>&quot;orgDb&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;org.Hs.eg.db&quot;</span><span>
</span><span>datasets</span><span>[</span><span>grep</span><span>(</span><span>&quot;dmel&quot;</span><span>,</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>),</span><span> </span><span>&quot;orgDb&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;org.Dm.eg.db&quot;</span><span>
</span><span>datasets</span><span>[</span><span>grep</span><span>(</span><span>&quot;mmus&quot;</span><span>,</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>),</span><span> </span><span>&quot;orgDb&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;org.Mm.eg.db&quot;</span><span>
</span><span>datasets</span><span>[</span><span>grep</span><span>(</span><span>&quot;celegans&quot;</span><span>,</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>),</span><span> </span><span>&quot;orgDb&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;org.Ce.eg.db&quot;</span><span>
</span><span>datasets</span><span>[</span><span>grep</span><span>(</span><span>&quot;cfam&quot;</span><span>,</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>),</span><span> </span><span>&quot;orgDb&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;org.Cf.eg.db&quot;</span><span>
</span><span>datasets</span><span>[</span><span>grep</span><span>(</span><span>&quot;drerio&quot;</span><span>,</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>),</span><span> </span><span>&quot;orgDb&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;org.Dr.eg.db&quot;</span><span>
</span><span>datasets</span><span>[</span><span>grep</span><span>(</span><span>&quot;ggallus&quot;</span><span>,</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>),</span><span> </span><span>&quot;orgDb&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;org.Gg.eg.db&quot;</span><span>
</span><span>datasets</span><span>[</span><span>grep</span><span>(</span><span>&quot;ptrog&quot;</span><span>,</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>),</span><span> </span><span>&quot;orgDb&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;org.Pt.eg.db&quot;</span><span>
</span><span>datasets</span><span>[</span><span>grep</span><span>(</span><span>&quot;rnor&quot;</span><span>,</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>),</span><span> </span><span>&quot;orgDb&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;org.Rn.eg.db&quot;</span><span>
</span><span>datasets</span><span>[</span><span>grep</span><span>(</span><span>&quot;scer&quot;</span><span>,</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>),</span><span> </span><span>&quot;orgDb&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;org.Sc.sgd.db&quot;</span><span>
</span><span>datasets</span><span>[</span><span>grep</span><span>(</span><span>&quot;sscrofa&quot;</span><span>,</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>),</span><span> </span><span>&quot;orgDb&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;org.Ss.eg.db&quot;</span><span>
</span><span>datasets</span><span> </span><span>&lt;-</span><span> </span><span>datasets</span><span>[</span><span>!</span><span>is.na</span><span>(</span><span>datasets</span><span>$</span><span>orgDb</span><span>),</span><span> </span><span>]</span><span>
</span><span>datasets</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##                       dataset                              description
## 9    rnorvegicus_gene_ensembl                     Rat genes (Rnor_6.0)
## 13   scerevisiae_gene_ensembl Saccharomyces cerevisiae genes (R64-1-1)
## 14      celegans_gene_ensembl  Caenorhabditis elegans genes (WBcel235)
## 22  ptroglodytes_gene_ensembl            Chimpanzee genes (CHIMP2.1.4)
## 26       sscrofa_gene_ensembl                  Pig genes (Sscrofa10.2)
## 32      hsapiens_gene_ensembl                  Human genes (GRCh38.p7)
## 36       ggallus_gene_ensembl        Chicken genes (Gallus_gallus-5.0)
## 41        drerio_gene_ensembl                 Zebrafish genes (GRCz10)
## 53 dmelanogaster_gene_ensembl                   Fruitfly genes (BDGP6)
## 61     mmusculus_gene_ensembl                  Mouse genes (GRCm38.p5)
## 69   cfamiliaris_gene_ensembl                    Dog genes (CanFam3.1)
##              version         orgDb
## 9           Rnor_6.0  org.Rn.eg.db
## 13           R64-1-1 org.Sc.sgd.db
## 14          WBcel235  org.Ce.eg.db
## 22        CHIMP2.1.4  org.Pt.eg.db
## 26       Sscrofa10.2  org.Ss.eg.db
## 32         GRCh38.p7  org.Hs.eg.db
## 36 Gallus_gallus-5.0  org.Gg.eg.db
## 41            GRCz10  org.Dr.eg.db
## 53             BDGP6  org.Dm.eg.db
## 61         GRCm38.p5  org.Mm.eg.db
## 69         CanFam3.1  org.Cf.eg.db
</code></pre>
</div>
<p>Now I can load the Ensembl mart for each species.</p>
<div>
<pre><code><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>datasets</span><span>))</span><span> </span><span>{</span><span>
</span><span>ensembl</span><span> </span><span>&lt;-</span><span> </span><span>datasets</span><span>[</span><span>i</span><span>,</span><span> </span><span>1</span><span>]</span><span>
</span><span>assign</span><span>(</span><span>paste0</span><span>(</span><span>ensembl</span><span>),</span><span> </span><span>useMart</span><span>(</span><span>&quot;ensembl&quot;</span><span>,</span><span> </span><span>dataset</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>ensembl</span><span>)))</span><span>
</span><span>}</span><span>
</span><span>specieslist</span><span> </span><span>&lt;-</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>
</span></code></pre>
</div>
<p>And install all org.db libraries, if necessary.</p>
<div>
<pre><code><span>library</span><span>(</span><span>AnnotationDbi</span><span>)</span><span>
</span><span>load_orgDb</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>orgDb</span><span>){</span><span>
</span><span>if</span><span>(</span><span>!</span><span>orgDb</span><span> </span><span>%in%</span><span> </span><span>installed.packages</span><span>()[,</span><span>&quot;Package&quot;</span><span>]){</span><span>
</span><span>source</span><span>(</span><span>&quot;https://bioconductor.org/biocLite.R&quot;</span><span>)</span><span>
</span><span>biocLite</span><span>(</span><span>orgDb</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>sapply</span><span>(</span><span>datasets</span><span>$</span><span>orgDb</span><span>,</span><span> </span><span>load_orgDb</span><span>,</span><span> </span><span>simplify</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>USE.NAMES</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<p>Once they are all installed, I can load the libraries.</p>
<div>
<pre><code><span>lapply</span><span>(</span><span>datasets</span><span>$</span><span>orgDb</span><span>,</span><span> </span><span>require</span><span>,</span><span> </span><span>character.only</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<p>Because I want to exctract and compare information on homologus genes for all possible combinations of species, they need to have a common identifier. To find them, I first produce a list with all available keytypes and then ask for common elements using <em>rlist</em>’s <strong>list.common()</strong> function.</p>
<div>
<pre><code><span>keytypes_list</span><span> </span><span>&lt;-</span><span> </span><span>lapply</span><span>(</span><span>datasets</span><span>$</span><span>orgDb</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>NULL</span><span>)</span><span>
</span><span>names</span><span>(</span><span>keytypes_list</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>datasets</span><span>$</span><span>orgDb</span><span>)</span><span>
</span><span>for</span><span> </span><span>(</span><span>orgDb</span><span> </span><span>in</span><span> </span><span>datasets</span><span>$</span><span>orgDb</span><span>){</span><span>
</span><span>keytypes_list</span><span>[[</span><span>orgDb</span><span>]]</span><span> </span><span>&lt;-</span><span> </span><span>keytypes</span><span>(</span><span>get</span><span>(</span><span>orgDb</span><span>))</span><span>
</span><span>}</span><span>
</span><span>library</span><span>(</span><span>rlist</span><span>)</span><span>
</span><span>list.common</span><span>(</span><span>keytypes_list</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##  [1] &quot;ENTREZID&quot;    &quot;ENZYME&quot;      &quot;EVIDENCE&quot;    &quot;EVIDENCEALL&quot; &quot;GENENAME&quot;   
##  [6] &quot;GO&quot;          &quot;GOALL&quot;       &quot;ONTOLOGY&quot;    &quot;ONTOLOGYALL&quot; &quot;PATH&quot;       
## [11] &quot;PMID&quot;        &quot;REFSEQ&quot;      &quot;UNIPROT&quot;
</code></pre>
</div>
<p>Entrez IDs are available for all species, so these are the keys I am using as gene identifiers. I am first creating a table of homologous genes for each species with all other species separately by looping over the datasets table.</p>
<div>
<pre><code><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>datasets</span><span>)){</span><span>
</span><span>orgDbs</span><span> </span><span>&lt;-</span><span> </span><span>datasets</span><span>$</span><span>orgDb</span><span>[</span><span>i</span><span>]</span><span>
</span><span>values</span><span> </span><span>&lt;-</span><span> </span><span>keys</span><span>(</span><span>get</span><span>(</span><span>orgDbs</span><span>),</span><span> </span><span>keytype</span><span> </span><span>=</span><span> </span><span>&quot;ENTREZID&quot;</span><span>)</span><span>
</span><span>ds</span><span> </span><span>&lt;-</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>[</span><span>i</span><span>]</span><span>
</span><span>mart</span><span> </span><span>&lt;-</span><span> </span><span>useMart</span><span>(</span><span>&quot;ensembl&quot;</span><span>,</span><span> </span><span>dataset</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>ds</span><span>))</span><span>
</span><span>print</span><span>(</span><span>mart</span><span>)</span><span>
</span><span>if</span><span> </span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>listFilters</span><span>(</span><span>mart</span><span>)</span><span>$</span><span>name</span><span>[</span><span>grep</span><span>(</span><span>&quot;^entrezgene$&quot;</span><span>,</span><span> </span><span>listFilters</span><span>(</span><span>mart</span><span>)</span><span>$</span><span>name</span><span>)])){</span><span>
</span><span>if</span><span> </span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>listAttributes</span><span>(</span><span>mart</span><span>)</span><span>$</span><span>name</span><span>[</span><span>grep</span><span>(</span><span>&quot;^entrezgene$&quot;</span><span>,</span><span> </span><span>listAttributes</span><span>(</span><span>mart</span><span>)</span><span>$</span><span>name</span><span>)])){</span><span>
</span><span>print</span><span>(</span><span>&quot;TRUE&quot;</span><span>)</span><span>
</span><span>for</span><span> </span><span>(</span><span>species</span><span> </span><span>in</span><span> </span><span>specieslist</span><span>)</span><span> </span><span>{</span><span>
</span><span>print</span><span>(</span><span>species</span><span>)</span><span>
</span><span>if</span><span> </span><span>(</span><span>species</span><span> </span><span>!=</span><span> </span><span>ds</span><span>){</span><span>
</span><span>assign</span><span>(</span><span>paste</span><span>(</span><span>&quot;homologs&quot;</span><span>,</span><span> </span><span>orgDbs</span><span>,</span><span> </span><span>species</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>),</span><span> </span><span>getLDS</span><span>(</span><span>attributes</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;entrezgene&quot;</span><span>),</span><span>
</span><span>filters</span><span> </span><span>=</span><span> </span><span>&quot;entrezgene&quot;</span><span>,</span><span>
</span><span>values</span><span> </span><span>=</span><span> </span><span>values</span><span>,</span><span>
</span><span>mart</span><span> </span><span>=</span><span> </span><span>mart</span><span>,</span><span>
</span><span>attributesL</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;entrezgene&quot;</span><span>),</span><span>
</span><span>martL</span><span> </span><span>=</span><span> </span><span>get</span><span>(</span><span>species</span><span>)))</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<p>Now I sort and combine these tables into one big table and remove duplicate entries.</p>
<div>
<pre><code><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>datasets</span><span>)){</span><span>
</span><span>orgDbs</span><span> </span><span>&lt;-</span><span> </span><span>datasets</span><span>$</span><span>orgDb</span><span>[</span><span>i</span><span>]</span><span>
</span><span>values</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>GeneID</span><span> </span><span>=</span><span> </span><span>keys</span><span>(</span><span>get</span><span>(</span><span>orgDbs</span><span>),</span><span> </span><span>keytype</span><span> </span><span>=</span><span> </span><span>&quot;ENTREZID&quot;</span><span>))</span><span>
</span><span>values</span><span>$</span><span>GeneID</span><span> </span><span>&lt;-</span><span> </span><span>as.character</span><span>(</span><span>values</span><span>$</span><span>GeneID</span><span>)</span><span>
</span><span>ds</span><span> </span><span>&lt;-</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>[</span><span>i</span><span>]</span><span>
</span><span>for</span><span> </span><span>(</span><span>j</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>length</span><span>(</span><span>specieslist</span><span>)){</span><span>
</span><span>species</span><span> </span><span>&lt;-</span><span> </span><span>specieslist</span><span>[</span><span>j</span><span>]</span><span>
</span><span>if</span><span> </span><span>(</span><span>j</span><span> </span><span>==</span><span> </span><span>1</span><span>){</span><span>
</span><span>homologs_table</span><span> </span><span>&lt;-</span><span> </span><span>values</span><span>
</span><span>}</span><span>
</span><span>if</span><span> </span><span>(</span><span>species</span><span> </span><span>!=</span><span> </span><span>ds</span><span>){</span><span>
</span><span>homologs_species</span><span> </span><span>&lt;-</span><span> </span><span>get</span><span>(</span><span>paste</span><span>(</span><span>&quot;homologs&quot;</span><span>,</span><span> </span><span>orgDbs</span><span>,</span><span> </span><span>species</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>))</span><span>
</span><span>homologs_species</span><span>$</span><span>EntrezGene.ID</span><span> </span><span>&lt;-</span><span> </span><span>as.character</span><span>(</span><span>homologs_species</span><span>$</span><span>EntrezGene.ID</span><span>)</span><span>
</span><span>homologs</span><span> </span><span>&lt;-</span><span> </span><span>left_join</span><span>(</span><span>values</span><span>,</span><span> </span><span>homologs_species</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;GeneID&quot;</span><span> </span><span>=</span><span> </span><span>&quot;EntrezGene.ID&quot;</span><span>))</span><span>
</span><span>homologs</span><span> </span><span>&lt;-</span><span> </span><span>homologs</span><span>[</span><span>!</span><span>duplicated</span><span>(</span><span>homologs</span><span>$</span><span>GeneID</span><span>),</span><span> </span><span>]</span><span>
</span><span>colnames</span><span>(</span><span>homologs</span><span>)[</span><span>2</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>species</span><span>)</span><span>
</span><span>homologs_table</span><span> </span><span>&lt;-</span><span> </span><span>left_join</span><span>(</span><span>homologs_table</span><span>,</span><span> </span><span>homologs</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;GeneID&quot;</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>colnames</span><span>(</span><span>homologs_table</span><span>)[</span><span>1</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>ds</span><span>)</span><span>
</span><span>assign</span><span>(</span><span>paste</span><span>(</span><span>&quot;homologs_table&quot;</span><span>,</span><span> </span><span>ds</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>),</span><span> </span><span>homologs_table</span><span>)</span><span>
</span><span>}</span><span>
</span><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>datasets</span><span>)){</span><span>
</span><span>ds</span><span> </span><span>&lt;-</span><span> </span><span>datasets</span><span>$</span><span>dataset</span><span>[</span><span>i</span><span>]</span><span>
</span><span>if</span><span> </span><span>(</span><span>i</span><span> </span><span>==</span><span> </span><span>1</span><span>){</span><span>
</span><span>homologs_table_combined</span><span> </span><span>&lt;-</span><span> </span><span>get</span><span>(</span><span>paste</span><span>(</span><span>&quot;homologs_table&quot;</span><span>,</span><span> </span><span>ds</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>))</span><span>
</span><span>homologs_table_combined</span><span> </span><span>&lt;-</span><span> </span><span>homologs_table_combined</span><span>[,</span><span> </span><span>order</span><span>(</span><span>colnames</span><span>(</span><span>homologs_table_combined</span><span>))]</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>homologs_table_species</span><span> </span><span>&lt;-</span><span> </span><span>get</span><span>(</span><span>paste</span><span>(</span><span>&quot;homologs_table&quot;</span><span>,</span><span> </span><span>ds</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>))</span><span>
</span><span>homologs_table_species</span><span> </span><span>&lt;-</span><span> </span><span>homologs_table_species</span><span>[,</span><span> </span><span>order</span><span>(</span><span>colnames</span><span>(</span><span>homologs_table_species</span><span>))]</span><span>
</span><span>homologs_table_combined</span><span> </span><span>&lt;-</span><span> </span><span>rbind</span><span>(</span><span>homologs_table_combined</span><span>,</span><span> </span><span>homologs_table_species</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>homologs_table_combined</span><span> </span><span>&lt;-</span><span> </span><span>homologs_table_combined</span><span>[</span><span>!</span><span>duplicated</span><span>(</span><span>homologs_table_combined</span><span>),</span><span> </span><span>]</span><span>
</span></code></pre>
</div>
<p>Now each row in the table shows one gene and its homologs in all of the 11 species. Some genes have multiple homologs in other species (e.g. gene 44071 of the fruitfly has two homologs in zebrafish).</p>
<div>
<pre><code><span>head</span><span>(</span><span>homologs_table_combined</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##   celegans_gene_ensembl cfamiliaris_gene_ensembl
## 1                    NA                       NA
## 2                    NA                   477699
## 3                    NA                   477023
## 4                173979                   490207
## 5                179795                   607852
## 6                177055                       NA
##   dmelanogaster_gene_ensembl drerio_gene_ensembl ggallus_gene_ensembl
## 1                         NA                  NA                   NA
## 2                      44071                  NA                   NA
## 3                         NA              431754               770094
## 4                      38864              406283                   NA
## 5                      36760              794259               395373
## 6                    3772179              541489               421909
##   hsapiens_gene_ensembl mmusculus_gene_ensembl ptroglodytes_gene_ensembl
## 1                    NA                     NA                        NA
## 2                     2                 232345                    465372
## 3                    30                 235674                    460268
## 4                    34                  11364                    469356
## 5                    47                 104112                    454672
## 6                    52                  11431                    458990
##   rnorvegicus_gene_ensembl scerevisiae_gene_ensembl sscrofa_gene_ensembl
## 1                    24152                       NA                   NA
## 2                    24153                       NA               403166
## 3                    24157                   854646            100515577
## 4                    24158                       NA               397104
## 5                    24159                   854310                   NA
## 6                    24161                   856187            100737301
</code></pre>
</div>
<p>In order to create the cooccurrence matrix, I am converting gene names to “1” and NAs to “0” and multiply the matrix with its transposed self. This is now the basis for <em>igraph</em>’s <strong>graph_from_adjacency_matrix()</strong> function with which I’m creating a directed network. Before plotting the network, I am removing multiple entries and loops.</p>
<div>
<pre><code><span>homologs_table_combined_matrix</span><span> </span><span>&lt;-</span><span> </span><span>as.matrix</span><span>(</span><span>ifelse</span><span>(</span><span>is.na</span><span>(</span><span>homologs_table_combined</span><span>),</span><span> </span><span>0</span><span>,</span><span> </span><span>1</span><span>))</span><span>
</span><span>co_occurrence</span><span> </span><span>&lt;-</span><span> </span><span>t</span><span>(</span><span>as.matrix</span><span>(</span><span>homologs_table_combined_matrix</span><span>))</span><span> </span><span>%*%</span><span> </span><span>as.matrix</span><span>(</span><span>homologs_table_combined_matrix</span><span>)</span><span>
</span><span>library</span><span>(</span><span>igraph</span><span>)</span><span>
</span><span>g</span><span> </span><span>&lt;-</span><span> </span><span>graph_from_adjacency_matrix</span><span>(</span><span>co_occurrence</span><span>,</span><span>
</span><span>weighted</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>diag</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>mode</span><span> </span><span>=</span><span> </span><span>&quot;directed&quot;</span><span>)</span><span>
</span><span>g</span><span> </span><span>&lt;-</span><span> </span><span>simplify</span><span>(</span><span>g</span><span>,</span><span> </span><span>remove.multiple</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>remove.loops</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
<p>I am also preparing a node annotation table with colors for each species group and the count of unique gene identifiers for species.</p>
<div>
<pre><code><span>datasets</span><span>[,</span><span> </span><span>2</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot;(.*)( genes (.*))&quot;</span><span>,</span><span> </span><span>&quot;\\1&quot;</span><span>,</span><span> </span><span>datasets</span><span>[,</span><span> </span><span>2</span><span>])</span><span>
</span><span>datasets</span><span>$</span><span>description</span><span>[</span><span>grep</span><span>(</span><span>&quot;Saccharomyces&quot;</span><span>,</span><span> </span><span>datasets</span><span>$</span><span>description</span><span>)]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;Yeast&quot;</span><span>
</span><span>datasets</span><span>$</span><span>description</span><span>[</span><span>grep</span><span>(</span><span>&quot;elegans&quot;</span><span>,</span><span> </span><span>datasets</span><span>$</span><span>description</span><span>)]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;C. elegans&quot;</span><span>
</span><span>datasets</span><span>$</span><span>group</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>datasets</span><span>$</span><span>description</span><span> </span><span>==</span><span> </span><span>&quot;Yeast&quot;</span><span>,</span><span> </span><span>&quot;fungus&quot;</span><span>,</span><span>
</span><span>ifelse</span><span>(</span><span>datasets</span><span>$</span><span>description</span><span> </span><span>==</span><span> </span><span>&quot;C. elegans&quot;</span><span>,</span><span> </span><span>&quot;roundworm&quot;</span><span>,</span><span>
</span><span>ifelse</span><span>(</span><span>datasets</span><span>$</span><span>description</span><span> </span><span>==</span><span> </span><span>&quot;Chicken&quot;</span><span>,</span><span> </span><span>&quot;bird&quot;</span><span>,</span><span>
</span><span>ifelse</span><span>(</span><span>datasets</span><span>$</span><span>dataset</span><span> </span><span>==</span><span> </span><span>&quot;Zebrafish&quot;</span><span>,</span><span> </span><span>&quot;fish&quot;</span><span>,</span><span>
</span><span>ifelse</span><span>(</span><span>datasets</span><span>$</span><span>description</span><span> </span><span>==</span><span> </span><span>&quot;Fruitfly&quot;</span><span>,</span><span> </span><span>&quot;insect&quot;</span><span>,</span><span> </span><span>&quot;mammal&quot;</span><span>)))))</span><span>
</span><span>list</span><span> </span><span>&lt;-</span><span> </span><span>as.character</span><span>(</span><span>unique</span><span>(</span><span>datasets</span><span>$</span><span>dataset</span><span>))</span><span>
</span><span>library</span><span>(</span><span>RColorBrewer</span><span>)</span><span>
</span><span>set3</span><span> </span><span>&lt;-</span><span> </span><span>brewer.pal</span><span>(</span><span>length</span><span>(</span><span>list</span><span>),</span><span> </span><span>&quot;Set3&quot;</span><span>)</span><span>
</span><span>datasets</span><span>$</span><span>col</span><span> </span><span>&lt;-</span><span> </span><span>NA</span><span>
</span><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>length</span><span>(</span><span>list</span><span>)){</span><span>
</span><span>datasets</span><span>[</span><span>datasets</span><span>$</span><span>dataset</span><span> </span><span>==</span><span> </span><span>list</span><span>[</span><span>i</span><span>],</span><span> </span><span>&quot;col&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>set3</span><span>[</span><span>i</span><span>]</span><span>
</span><span>}</span><span>
</span><span>no_genes</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>apply</span><span>(</span><span>homologs_table_combined</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>length</span><span>(</span><span>unique</span><span>(</span><span>x</span><span>))))</span><span>
</span><span>no_genes</span><span>$</span><span>dataset</span><span> </span><span>&lt;-</span><span> </span><span>rownames</span><span>(</span><span>no_genes</span><span>)</span><span>
</span><span>colnames</span><span>(</span><span>no_genes</span><span>)[</span><span>1</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;no_genes&quot;</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>datasets</span><span> </span><span>&lt;-</span><span> </span><span>left_join</span><span>(</span><span>datasets</span><span>,</span><span> </span><span>no_genes</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;dataset&quot;</span><span>)</span><span>
</span><span>datasets</span><span> </span><span>&lt;-</span><span> </span><span>datasets</span><span>[</span><span>order</span><span>(</span><span>datasets</span><span>$</span><span>dataset</span><span>),</span><span> </span><span>]</span><span>
</span></code></pre>
</div>
<p>I also want to have the proportion of genes that have homologs in each of the respective other species as edge attributes. I am preparing an extra table for this of all possible combinations of nodes.</p>
<div>
<pre><code><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>ncol</span><span>(</span><span>homologs_table_combined</span><span>)){</span><span>
</span><span>input</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>na.omit</span><span>(</span><span>homologs_table_combined</span><span>[,</span><span> </span><span>i</span><span>]))</span><span>
</span><span>homologs_table_combined_subset</span><span> </span><span>&lt;-</span><span> </span><span>homologs_table_combined</span><span>[</span><span>!</span><span>is.na</span><span>(</span><span>homologs_table_combined</span><span>[,</span><span> </span><span>i</span><span>]),</span><span> </span><span>]</span><span>
</span><span>for</span><span> </span><span>(</span><span>j</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>ncol</span><span>(</span><span>homologs_table_combined</span><span>)){</span><span>
</span><span>if</span><span> </span><span>(</span><span>i</span><span> </span><span>!=</span><span> </span><span>j</span><span>){</span><span>
</span><span>output</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>na.omit</span><span>(</span><span>homologs_table_combined_subset</span><span>[,</span><span> </span><span>j</span><span>]))</span><span>
</span><span>if</span><span> </span><span>(</span><span>i</span><span> </span><span>==</span><span> </span><span>1</span><span> </span><span>&amp;</span><span> </span><span>j</span><span> </span><span>==</span><span> </span><span>2</span><span>){</span><span>
</span><span>edge_table</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>source</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>homologs_table_combined</span><span>)[</span><span>i</span><span>],</span><span> </span><span>target</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>homologs_table_combined</span><span>)[</span><span>j</span><span>],</span><span> </span><span>weight</span><span> </span><span>=</span><span> </span><span>length</span><span>(</span><span>output</span><span>)</span><span>/</span><span>length</span><span>(</span><span>input</span><span>))</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>table</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>source</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>homologs_table_combined</span><span>)[</span><span>i</span><span>],</span><span> </span><span>target</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>homologs_table_combined</span><span>)[</span><span>j</span><span>],</span><span> </span><span>weight</span><span> </span><span>=</span><span> </span><span>length</span><span>(</span><span>output</span><span>)</span><span>/</span><span>length</span><span>(</span><span>input</span><span>))</span><span>
</span><span>edge_table</span><span> </span><span>&lt;-</span><span> </span><span>rbind</span><span>(</span><span>edge_table</span><span>,</span><span> </span><span>table</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>list</span><span> </span><span>&lt;-</span><span> </span><span>as.character</span><span>(</span><span>unique</span><span>(</span><span>edge_table</span><span>$</span><span>source</span><span>))</span><span>
</span><span>edge_table</span><span>$</span><span>col</span><span> </span><span>&lt;-</span><span> </span><span>NA</span><span>
</span><span>for</span><span> </span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>length</span><span>(</span><span>list</span><span>)){</span><span>
</span><span>edge_table</span><span>[</span><span>edge_table</span><span>$</span><span>source</span><span> </span><span>==</span><span> </span><span>list</span><span>[</span><span>i</span><span>],</span><span> </span><span>&quot;col&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>set3</span><span>[</span><span>i</span><span>]</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<p>And finally I can produce the plot:</p>
<div>
<pre><code><span>V</span><span>(</span><span>g</span><span>)</span><span>$</span><span>color</span><span> </span><span>&lt;-</span><span> </span><span>datasets</span><span>$</span><span>col</span><span>
</span><span>V</span><span>(</span><span>g</span><span>)</span><span>$</span><span>label</span><span> </span><span>&lt;-</span><span> </span><span>datasets</span><span>$</span><span>description</span><span>
</span><span>V</span><span>(</span><span>g</span><span>)</span><span>$</span><span>size</span><span> </span><span>&lt;-</span><span> </span><span>datasets</span><span>$</span><span>no_genes</span><span>/</span><span>2000</span><span>
</span><span>E</span><span>(</span><span>g</span><span>)</span><span>$</span><span>arrow.size</span><span> </span><span>&lt;-</span><span> </span><span>3</span><span>
</span><span>E</span><span>(</span><span>g</span><span>)</span><span>$</span><span>width</span><span> </span><span>&lt;-</span><span> </span><span>edge_table</span><span>$</span><span>weight</span><span>*</span><span>25</span><span>
</span><span>plot</span><span>(</span><span>g</span><span>,</span><span>
</span><span>vertex.label.font</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span>
</span><span>vertex.shape</span><span> </span><span>=</span><span> </span><span>&quot;sphere&quot;</span><span>,</span><span>
</span><span>vertex.label.cex</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span>
</span><span>vertex.label.color</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>,</span><span>
</span><span>vertex.frame.color</span><span> </span><span>=</span><span> </span><span>NA</span><span>,</span><span>
</span><span>edge.curved</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>0.1</span><span>,</span><span> </span><span>ecount</span><span>(</span><span>g</span><span>)),</span><span>
</span><span>edge.color</span><span> </span><span>=</span><span> </span><span>edge_table</span><span>$</span><span>col</span><span>,</span><span>
</span><span>layout</span><span> </span><span>=</span><span> </span><span>layout_in_circle</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i2.wp.com/shiring.github.io/genome/2016/12/14/homologous_genes_part2_post_files/figure-markdown_github/unnamed-chunk-15-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Node size shows the total number of genes of each species and edge width shows the proportion of these genes with homologs in the respective other species. Edge and node colors show the 11 species and their outgoing edges.</p>
<p>Weirdly, the mouse seems to have more gene entries in the org.db library than human. If anyone knows why that is, please let me know!</p>
<hr>
<p></p>
<div>
<pre><code>## R version 3.3.2 (2016-10-31)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: macOS Sierra 10.12.1
## 
## locale:
## [1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8
## 
## attached base packages:
## [1] parallel  stats4    stats     graphics  grDevices utils     datasets 
## [8] methods   base     
## 
## other attached packages:
##  [1] dplyr_0.5.0          RColorBrewer_1.1-2   igraph_1.0.1        
##  [4] rlist_0.4.6.1        org.Cf.eg.db_3.4.0   org.Mm.eg.db_3.4.0  
##  [7] org.Dm.eg.db_3.4.0   org.Dr.eg.db_3.4.0   org.Gg.eg.db_3.4.0  
## [10] org.Hs.eg.db_3.4.0   org.Ss.eg.db_3.4.0   org.Pt.eg.db_3.4.0  
## [13] org.Ce.eg.db_3.4.0   org.Sc.sgd.db_3.4.0  org.Rn.eg.db_3.4.0  
## [16] AnnotationDbi_1.36.0 IRanges_2.8.1        S4Vectors_0.12.1    
## [19] Biobase_2.34.0       BiocGenerics_0.20.0  biomaRt_2.30.0      
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.8       bitops_1.0-6      tools_3.3.2      
##  [4] digest_0.6.10     tibble_1.2        RSQLite_1.1-1    
##  [7] evaluate_0.10     memoise_1.0.0     DBI_0.5-1        
## [10] yaml_2.1.14       stringr_1.1.0     knitr_1.15.1     
## [13] rprojroot_1.1     data.table_1.10.0 R6_2.2.0         
## [16] XML_3.98-1.5      rmarkdown_1.2     magrittr_1.5     
## [19] backports_1.0.4   htmltools_0.3.5   assertthat_0.1   
## [22] stringi_1.1.2     RCurl_1.95-4.8
</code></pre>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://shiring.github.io/genome/2016/12/14/homologous_genes_part2_post"> Shirin's playgRound</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/uTItAerDyl0"></a></div>