<p>One potential solution using awk:</p>
<pre><code>awk 'BEGIN {
    OFS = &quot;\t&quot;
    print &quot;Parent&quot;, &quot;Child&quot;, &quot;Relationship&quot;
}

/GO:[[:digit:]]{1,}/ {
    if (<span class="math-container">$1 ~ /^id:/) {
    parent_id = $2
} else if ($</span>1 ~ /^is_a:/) {
        gsub(&quot;:&quot;, &quot;&quot;, <span class="math-container">$1)
    print parent_id, $</span>2, <span class="math-container">$1
} else if ($</span>1 ~ /^relationship:/) {
        print parent_id, <span class="math-container">$3, $</span>2
    }
}' test.txt
Parent      Child       Relationship
GO:0000001  GO:0048308  is_a
GO:0000001  GO:0048311  is_a
GO:0000015  GO:1902494  is_a
GO:0000015  GO:0005829  part_of
GO:0000122  GO:0006357  is_a
GO:0000122  GO:0045892  is_a
GO:0000122  GO:0006366  negatively_regulates
</code></pre>
<hr />
<p>Including comments:</p>
<pre><code>awk 'BEGIN {                      # before processing the first line
    OFS = &quot;\t&quot;                    # set the output field separator to tab
    print &quot;Parent&quot;, &quot;Child&quot;, &quot;Relationship&quot;    # and print the header
}

/GO:[[:digit:]]{1,}/ {    # for any line that contains a GO term
    if (<span class="math-container">$1 ~ /^id:/) {            # if the line starts with id:
    parent_id = $1                # set the parent id
} else if ($</span>1 ~ /^is_a/) {        # then, for lines that start with is_a 
        gsub(&quot;:&quot;, &quot;&quot;, <span class="math-container">$1)         # remove the colon
    print parent_id, $</span>2, <span class="math-container">$1       # and print the parent, child and relationship
} else if ($</span>1 ~ /^relationship:/) {  # then, for lines that start with relationship
        print parent_id, <span class="math-container">$3, $</span>2   # print the parent, child, and relationship
    }
}' test.txt
</code></pre>

CC BY-SA 4.0
2023-07-12T23:21:42.063Z
You're welcome - I'm glad you solved your problem @ojikj2023 - but comments are not the right place to say 'thank you'; please see [What should I do when someone answers my question?](https://bioinformatics.stackexchange.com/help/someone-answers).
jared_mamrot
CC BY-SA 4.0
2023-07-13T20:52:25.440Z
It's my first time to post a question and get help. I've just done voting following your comment!
ojikj2023
CC BY-SA 4.0
2023-07-12T02:17:46.353Z
2023-07-12T03:56:19.827Z
2023-07-12T03:56:19.827Z
jared_mamrot
<p>I have a gene ontology obo file, and would like to rearrange the obo format file in a single column to a text file in three columns comprised of 'parent (id:GO:xxxxxxx)', 'child (is_a: GO:xxxxxxx, part_of GO:xxxxxxx, or positively/negatively_regulates GO:xxxxxxx. For example...</p>
<p>Thank you for your help in advance!</p>
<p>Original file:</p>
<pre><code>[Term]
id: GO:0000001 # parent
name: mitochondrion inheritance
namespace: biological_process
def: &quot;The distribution of mitochondria, including the mitochondrial genome, into daughter cells after mitosis or meiosis, mediated by interactions between mitochondria and the cytoskeleton.&quot; [GOC:mcc, PMID:10873824, PMID:11389764]
synonym: &quot;mitochondrial inheritance&quot; EXACT []
is_a: GO:0048308 ! organelle inheritance # child
is_a: GO:0048311 ! mitochondrion distribution # child

[Term]
id: GO:0000015 # parent
name: phosphopyruvate hydratase complex
namespace: cellular_component
def: &quot;A multimeric enzyme complex, usually a dimer or an octamer, that catalyzes the conversion of 2-phospho-D-glycerate to phosphoenolpyruvate and water.&quot; [GOC:jl, ISBN:0198506732]
subset: goslim_metagenomics
synonym: &quot;enolase complex&quot; EXACT []
is_a: GO:1902494 ! catalytic complex # child
relationship: part_of GO:0005829 ! cytosol # child

[Term]
id: GO:0000122 # parent
name: negative regulation of transcription by RNA polymerase II
namespace: biological_process
alt_id: GO:0010553
alt_id: GO:0045816
def: &quot;Any process that stops, prevents, or reduces the frequency, rate or extent of transcription mediated by RNA polymerase II.&quot; [GOC:go_curators, GOC:txnOH]
synonym: &quot;down regulation of global transcription from RNA polymerase II promoter&quot; RELATED []
synonym: &quot;down regulation of transcription from RNA polymerase II promoter&quot; EXACT []
synonym: &quot;down-regulation of global transcription from RNA polymerase II promoter&quot; RELATED []
synonym: &quot;down-regulation of transcription from RNA polymerase II promoter&quot; EXACT []
synonym: &quot;downregulation of global transcription from RNA polymerase II promoter&quot; RELATED []
synonym: &quot;downregulation of transcription from RNA polymerase II promoter&quot; EXACT []
synonym: &quot;inhibition of global transcription from RNA polymerase II promoter&quot; RELATED []
synonym: &quot;inhibition of transcription from RNA polymerase II promoter&quot; EXACT []
synonym: &quot;negative regulation of gene-specific transcription from RNA polymerase II promoter&quot; RELATED []
synonym: &quot;negative regulation of global transcription from Pol II promoter&quot; RELATED []
synonym: &quot;negative regulation of transcription from Pol II promoter&quot; EXACT []
synonym: &quot;negative regulation of transcription from RNA polymerase II promoter&quot; EXACT []
synonym: &quot;negative regulation of transcription from RNA polymerase II promoter, global&quot; RELATED []
is_a: GO:0006357 ! regulation of transcription by RNA polymerase II # child
is_a: GO:0045892 ! negative regulation of DNA-templated transcription # child
relationship: negatively_regulates GO:0006366 ! transcription by RNA polymerase II # child
</code></pre>
<p>Desired format:</p>
<pre><code>Parent       Child       Relationship
GO:0000001   GO:0048308  is_a
GO:0000001   GO:0048311  is_a
GO:0000015   GO:1902494  is_a
GO:0000015   GO:0005829  part_of
GO:0000122   GO:0006357  is_a
GO:0000122   GO:0045892  is_a
GO:0000122   GO:0006366  negatively_regulates
</code></pre>

CC BY-SA 4.0
2023-07-11T15:40:46.770Z
Thank you for the advice. I've just reformatted the example file.
ojikj2023
<gene><gene-ontology>
How to rearrange gene ontology obo file?
# How to rearrange gene ontology obo file?

(Asked by: username_0 on 2023-07-11)

I have a gene ontology obo file, and would like to rearrange the obo format file in a single column to a text file in three columns comprised of 'parent (id:GO:xxxxxxx)', 'child (is_a: GO:xxxxxxx, part_of GO:xxxxxxx, or positively/negatively_regulates GO:xxxxxxx. For example...
Thank you for your help in advance!
Original file:
[Term]
id: GO:0000001 # parent
name: mitochondrion inheritance
namespace: biological_process
def: "The distribution of mitochondria, including the mitochondrial genome, into daughter cells after mitosis or meiosis, mediated by interactions between mitochondria and the cytoskeleton." [GOC:mcc, PMID:10873824, PMID:11389764]
synonym: "mitochondrial inheritance" EXACT []
is_a: GO:0048308 ! organelle inheritance # child
is_a: GO:0048311 ! mitochondrion distribution # child

[Term]
id: GO:0000015 # parent
name: phosphopyruvate hydratase complex
namespace: cellular_component
def: "A multimeric enzyme complex, usually a dimer or an octamer, that catalyzes the conversion of 2-phospho-D-glycerate to phosphoenolpyruvate and water." [GOC:jl, ISBN:0198506732]
subset: goslim_metagenomics
synonym: "enolase complex" EXACT []
is_a: GO:1902494 ! catalytic complex # child
relationship: part_of GO:0005829 ! cytosol # child

[Term]
id: GO:0000122 # parent
name: negative regulation of transcription by RNA polymerase II
namespace: biological_process
alt_id: GO:0010553
alt_id: GO:0045816
def: "Any process that stops, prevents, or reduces the frequency, rate or extent of transcription mediated by RNA polymerase II." [GOC:go_curators, GOC:txnOH]
synonym: "down regulation of global transcription from RNA polymerase II promoter" RELATED []
synonym: "down regulation of transcription from RNA polymerase II promoter" EXACT []
synonym: "down-regulation of global transcription from RNA polymerase II promoter" RELATED []
synonym: "down-regulation of transcription from RNA polymerase II promoter" EXACT []
synonym: "downregulation of global transcription from RNA polymerase II promoter" RELATED []
synonym: "downregulation of transcription from RNA polymerase II promoter" EXACT []
synonym: "inhibition of global transcription from RNA polymerase II promoter" RELATED []
synonym: "inhibition of transcription from RNA polymerase II promoter" EXACT []
synonym: "negative regulation of gene-specific transcription from RNA polymerase II promoter" RELATED []
synonym: "negative regulation of global transcription from Pol II promoter" RELATED []
synonym: "negative regulation of transcription from Pol II promoter" EXACT []
synonym: "negative regulation of transcription from RNA polymerase II promoter" EXACT []
synonym: "negative regulation of transcription from RNA polymerase II promoter, global" RELATED []
is_a: GO:0006357 ! regulation of transcription by RNA polymerase II # child
is_a: GO:0045892 ! negative regulation of DNA-templated transcription # child
relationship: negatively_regulates GO:0006366 ! transcription by RNA polymerase II # child

Desired format:
Parent       Child       Relationship
GO:0000001   GO:0048308  is_a
GO:0000001   GO:0048311  is_a
GO:0000015   GO:1902494  is_a
GO:0000015   GO:0005829  part_of
GO:0000122   GO:0006357  is_a
GO:0000122   GO:0045892  is_a
GO:0000122   GO:0006366  negatively_regulates

**Tags:** gene, gene-ontology
> **Comments:**
> **username_0**: Thank you for the advice. I've just reformatted the example file.  

---

## 1 answers

### Answer by username_1 on 2023-07-12. Score: 2

One potential solution using awk:
awk 'BEGIN {
    OFS = "\t"
    print "Parent", "Child", "Relationship"
}

/GO:[[:digit:]]{1,}/ {
    if ($1 ~ /^id:/) {
    parent_id = $2
} else if ($1 ~ /^is_a:/) {
        gsub(":", "", $1)
    print parent_id, $2, $1
} else if ($1 ~ /^relationship:/) {
        print parent_id, $3, $2
    }
}' test.txt
Parent      Child       Relationship
GO:0000001  GO:0048308  is_a
GO:0000001  GO:0048311  is_a
GO:0000015  GO:1902494  is_a
GO:0000015  GO:0005829  part_of
GO:0000122  GO:0006357  is_a
GO:0000122  GO:0045892  is_a
GO:0000122  GO:0006366  negatively_regulates


Including comments:
awk 'BEGIN {                      # before processing the first line
    OFS = "\t"                    # set the output field separator to tab
    print "Parent", "Child", "Relationship"    # and print the header
}

/GO:[[:digit:]]{1,}/ {    # for any line that contains a GO term
    if ($1 ~ /^id:/) {            # if the line starts with id:
    parent_id = $1                # set the parent id
} else if ($1 ~ /^is_a/) {        # then, for lines that start with is_a 
        gsub(":", "", $1)         # remove the colon
    print parent_id, $2, $1       # and print the parent, child and relationship
} else if ($1 ~ /^relationship:/) {  # then, for lines that start with relationship
        print parent_id, $3, $2   # print the parent, child, and relationship
    }
}' test.txt

> **Comments:**
> **username_1**: You're welcome - I'm glad you solved your problem @ojikj2023 - but comments are not the right place to say 'thank you'; please see [What should I do when someone answers my question?](https://bioinformatics.stackexchange.com/help/someone-answers).  
> **username_0**: It's my first time to post a question and get help. I've just done voting following your comment!  

---

