<p>Ok. Answering my own question.</p>

<p>I did this way. And I got the output I want.</p>

<pre><code>install.packages("msigdbr")
library(msigdbr)

m_df = msigdbr(species = "Homo sapiens", category = "C5")
head(m_df)
</code></pre>

<p>Output:</p>

<pre><code>gs_name gs_id gs_cat gs_subcat human_gene_symb~ species_name entrez_gene
  &lt;chr&gt;   &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt;     &lt;chr&gt;            &lt;chr&gt;              &lt;int&gt;
1 GO_14_~ M184~ C5     MF        AANAT            Homo sapiens          15
2 GO_14_~ M184~ C5     MF        AKT1             Homo sapiens         207
3 GO_14_~ M184~ C5     MF        ARRB2            Homo sapiens         409
4 GO_14_~ M184~ C5     MF        BAD              Homo sapiens         572
5 GO_14_~ M184~ C5     MF        DAB2IP           Homo sapiens      153090
6 GO_14_~ M184~ C5     MF        DDIT4            Homo sapiens       54541
# ... with 2 more variables: gene_symbol &lt;chr&gt;, sources &lt;chr&gt;
</code></pre>

<p>found this solution from here <a href="https://cran.r-project.org/web/packages/msigdbr/vignettes/msigdbr-intro.html" rel="nofollow noreferrer">https://cran.r-project.org/web/packages/msigdbr/vignettes/msigdbr-intro.html</a></p>

CC BY-SA 4.0
2019-12-18T18:19:54.020Z
perfect! thanks.
Ahdee
CC BY-SA 4.0
2018-11-08T13:27:42.457Z
2018-11-08T13:45:13.603Z
2018-11-08T13:45:13.603Z
beginner
<p>You can use GSA.read.gmt function from <a href="https://cran.r-project.org/web/packages/GSA/GSA.pdf" rel="nofollow noreferrer">GSA</a> package. The following code can be used to convert the file to a dataframe. Just ignore the warnings.</p>

<p><strong>Original_response</strong></p>

<pre><code>library(GSA)
data &lt;-  GSA.read.gmt("c5.all.v6.2.symbols.gmt")
gene_names &lt;- unlist(data<span class="math-container">$genesets, use.names=FALSE)
your_dataframe &lt;- cbind(data$</span>geneset.names,gene_names)
colnames(your_dataframe) &lt;- c("Pathways","Genes")
mydataframe &lt;- as.data.frame(your_dataframe)
head(mydataframe)
                                               Pathway   Gene
1            GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION POLR2C
2                           GO_CARDIAC_CHAMBER_DEVELOPMENT POLR2J
3 GO_DNA_DEPENDENT_DNA_REPLICATION_MAINTENANCE_OF_FIDELITY  CTDP1
4                                      GO_CIRCADIAN_RHYTHM   RDBP
5              GO_PHOSPHATIDYLSERINE_ACYL_CHAIN_REMODELING COBRA1
6                               GO_SPINAL_CORD_DEVELOPMENT   RSF1
</code></pre>

<p><strong>Edited_response</strong></p>

<p>You can use the code given below to achieve your desired output:</p>

<pre><code>library(GSA)
data &lt;-  GSA.read.gmt("c5.all.v6.2.symbols.gmt")
len_vec=c()           # Now create a vector for containing the length of genes at each position
len_vec[1] = 3
for(i in 1:length(data<span class="math-container">$genesets)){len_vec[i] &lt;- c(length(data$genesets[[i]]))}
pathway_vec &lt;- unlist(Vectorize(rep.int)(data$</span>geneset.names, len_vec),use.names = FALSE) # Now create a vector for all the pathways in the data 
desired_df &lt;- as.data.frame(cbind(pathway_vec,unlist(data$genesets,use.names = FALSE))) # This gives your desired dataframe
head(desired_df)
                                    Pathway      Gene
1 GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION POLR2C
2 GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION POLR2J
3 GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION  CTDP1
4 GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION   RDBP
5 GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION COBRA1
6 GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION   RSF1
</code></pre>

CC BY-SA 4.0
2018-11-08T13:02:34.857Z
sorry, this is wrong. Please check the output I want in the above question. In your output Genes POLR2J, CTDP1, RDBP, COBRA1, RSF1 shows different pathways.
beginner
CC BY-SA 4.0
2018-11-08T12:14:50.920Z
2018-11-08T17:09:22.203Z
2018-11-08T17:09:22.203Z
Ammar Sabir Cheema
<p>You can import the csv file as a table. This does not require further libraries:</p>

<pre><code>#you're reading a csv file, using tab as field separator and considering the first line as the headers of the data table
data &lt;- read.csv(filename, sep="\t", header=TRUE)
</code></pre>

<p>then what you need are just two columns of your <strong>data</strong>:</p>

<pre><code>colnames(data) #will give you the names of the columns that can be accessed
#to access two columns and merge them into a selectedColumns table
selectedColumns &lt;- cbind(data<span class="math-container">$colName1, data$</span>colName2)
</code></pre>

CC BY-SA 4.0
2018-11-08T12:21:38.863Z
The question is tagged with R so it is an R data.frame.
llrs
CC BY-SA 4.0
2018-11-08T12:51:13.010Z
you're right! I missed it
gbt
CC BY-SA 4.0
2018-11-08T12:00:40.937Z
2018-11-08T14:15:16.120Z
2018-11-08T14:15:16.120Z
gbt
<p>I downloaded c5: gene ontology gene sets file from <a href="http://software.broadinstitute.org/gsea/downloads.jsp" rel="nofollow noreferrer">http://software.broadinstitute.org/gsea/downloads.jsp</a></p>

<p>I opened the "c5.all.v6.2.symbols.gmt" file in csv format and It looks like below:</p>

<p><a href="https://i.sstatic.net/3bkVE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/3bkVE.png" alt="enter image description here"></a></p>

<p>I want to convert the .gmt file into a dataframe. And it should look like below:</p>

<p><a href="https://i.sstatic.net/4f5qp.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/4f5qp.png" alt="enter image description here"></a></p>

<p>can anyone say how I can do that. Thanq</p>

CC BY-SA 4.0
2018-11-08T12:25:06.220Z
This might be an XY problem (you are asking about a problem, but you really have another one). Why do you need this data in data.frame format? Also consider that the GO data is a directed acyclic graph, which has a special structure that should be taken into account
llrs
CC BY-SA 4.0
2018-11-08T13:17:53.867Z
for some analysis I want the data to be looked like the output I posted.
beginner
CC BY-SA 4.0
2018-11-08T14:28:44.927Z
Also note that the GO annotations imported by MSigDB are quite old. If you are interested in associations between GO and genes (updated) you should use other sources
llrs
CC BY-SA 4.0
2018-11-08T14:47:46.167Z
other resources like?
beginner
CC BY-SA 4.0
2018-11-08T15:41:09.077Z
Like the [GO database](http://www.geneontology.org/) directly or [PANTHER](http://pantherdb.org/). Or to retrieve the results [biomart](https://www.ensembl.org/biomart/martview) or its package biomaRt
llrs
<r><file-formats><format-conversion><gsea><conversion>
How to convert data in gmt format to dataframe?
# How to convert data in gmt format to dataframe?

(Asked by: username_0 on 2018-11-08)

I downloaded c5: gene ontology gene sets file from <http://software.broadinstitute.org/gsea/downloads.jsp>

I opened the "c5.all.v6.2.symbols.gmt" file in csv format and It looks like below:

[![enter image description here](https://i.sstatic.net/3bkVE.png)](https://i.sstatic.net/3bkVE.png)

I want to convert the .gmt file into a dataframe. And it should look like below:

[![enter image description here](https://i.sstatic.net/4f5qp.png)](https://i.sstatic.net/4f5qp.png)

can anyone say how I can do that. Thanq

**Tags:** r, file-formats, format-conversion, gsea, conversion
> **Comments:**
> **username_1**: This might be an XY problem (you are asking about a problem, but you really have another one). Why do you need this data in data.frame format? Also consider that the GO data is a directed acyclic graph, which has a special structure that should be taken into account  
> **username_0**: for some analysis I want the data to be looked like the output I posted.  
> **username_1**: Also note that the GO annotations imported by MSigDB are quite old. If you are interested in associations between GO and genes (updated) you should use other sources  
> **username_0**: other resources like?  
> **username_1**: Like the [GO database](http://www.geneontology.org/) directly or [PANTHER](http://pantherdb.org/). Or to retrieve the results [biomart](https://www.ensembl.org/biomart/martview) or its package biomaRt  

---

## 3 answers

### Answer by username_0 on 2018-11-08. Score: 3

Ok. Answering my own question.
I did this way. And I got the output I want.
install.packages("msigdbr")
library(msigdbr)

m_df = msigdbr(species = "Homo sapiens", category = "C5")
head(m_df)

Output:
gs_name gs_id gs_cat gs_subcat human_gene_symb~ species_name entrez_gene
                                       
1 GO_14_~ M184~ C5     MF        AANAT            Homo sapiens          15
2 GO_14_~ M184~ C5     MF        AKT1             Homo sapiens         207
3 GO_14_~ M184~ C5     MF        ARRB2            Homo sapiens         409
4 GO_14_~ M184~ C5     MF        BAD              Homo sapiens         572
5 GO_14_~ M184~ C5     MF        DAB2IP           Homo sapiens      153090
6 GO_14_~ M184~ C5     MF        DDIT4            Homo sapiens       54541
# ... with 2 more variables: gene_symbol , sources 

found this solution from here https://cran.r-project.org/web/packages/msigdbr/vignettes/msigdbr-intro.html

> **Comments:**
> **username_2**: perfect! thanks.  

---

### Answer by username_3 on 2018-11-08. Score: 1

You can use GSA.read.gmt function from GSA package. The following code can be used to convert the file to a dataframe. Just ignore the warnings.
Original_response
library(GSA)
data <-  GSA.read.gmt("c5.all.v6.2.symbols.gmt")
gene_names <- unlist(data$genesets, use.names=FALSE)
your_dataframe <- cbind(data$geneset.names,gene_names)
colnames(your_dataframe) <- c("Pathways","Genes")
mydataframe <- as.data.frame(your_dataframe)
head(mydataframe)
                                               Pathway   Gene
1            GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION POLR2C
2                           GO_CARDIAC_CHAMBER_DEVELOPMENT POLR2J
3 GO_DNA_DEPENDENT_DNA_REPLICATION_MAINTENANCE_OF_FIDELITY  CTDP1
4                                      GO_CIRCADIAN_RHYTHM   RDBP
5              GO_PHOSPHATIDYLSERINE_ACYL_CHAIN_REMODELING COBRA1
6                               GO_SPINAL_CORD_DEVELOPMENT   RSF1

Edited_response
You can use the code given below to achieve your desired output:
library(GSA)
data <-  GSA.read.gmt("c5.all.v6.2.symbols.gmt")
len_vec=c()           # Now create a vector for containing the length of genes at each position
len_vec[1] = 3
for(i in 1:length(data$genesets)){len_vec[i] <- c(length(data$genesets[[i]]))}
pathway_vec <- unlist(Vectorize(rep.int)(data$geneset.names, len_vec),use.names = FALSE) # Now create a vector for all the pathways in the data 
desired_df <- as.data.frame(cbind(pathway_vec,unlist(data$genesets,use.names = FALSE))) # This gives your desired dataframe
head(desired_df)
                                    Pathway      Gene
1 GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION POLR2C
2 GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION POLR2J
3 GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION  CTDP1
4 GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION   RDBP
5 GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION COBRA1
6 GO_POSITIVE_REGULATION_OF_VIRAL_TRANSCRIPTION   RSF1

> **Comments:**
> **username_0**: sorry, this is wrong. Please check the output I want in the above question. In your output Genes POLR2J, CTDP1, RDBP, COBRA1, RSF1 shows different pathways.  

---

### Answer by username_4 on 2018-11-08. Score: 1

You can import the csv file as a table. This does not require further libraries:
#you're reading a csv file, using tab as field separator and considering the first line as the headers of the data table
data <- read.csv(filename, sep="\t", header=TRUE)

then what you need are just two columns of your data:
colnames(data) #will give you the names of the columns that can be accessed
#to access two columns and merge them into a selectedColumns table
selectedColumns <- cbind(data$colName1, data$colName2)

> **Comments:**
> **username_1**: The question is tagged with R so it is an R data.frame.  
> **username_4**: you're right! I missed it  

---

