Ok, I know this is not exactly what you need, but if you are using a linux machine this will give you an almost useful result:
join -t $'\t' GeneOntology.txt OntologyAncestors.txt -a1 -1 2 -2 1 --header
This relational operation called join. The result is
Ontology Gene Ancestor
TermA GeneA AncestorA
TermA GeneA AncestorB
TermA GeneB AncestorA
TermA GeneB AncestorB
TermB GeneA
TermC GeneC AncestorA
TermC GeneC AncestorD
Of course this isn't the view you wanted, but it's closer.
EDIT
I came up with a solution!
join -t $'\t' GeneOntology.txt OntologyAncestors.txt -a2 -1 2 -2 1 --header|sed 1d |cut --complement -f 1 -d $'\t' >> GeneOntology.txt
I'll explain step by step.
First you apply a right join
join -t $'\t' GeneOntology.txt OntologyAncestors.txt -a2 -1 2 -2 1 --header
This will leave you this output:
Ontology Gene Ancestor
TermA GeneA AncestorA
TermA GeneA AncestorB
TermA GeneB AncestorA
TermA GeneB AncestorB
TermC GeneC AncestorA
TermC GeneC AncestorD
Then you remove the headers with sed, ordering to (d)elete the line 1.
sed 1d
Then you remove the first column (the Ontology column):
cut --complement -f 1 -d $'\t'
The final result will be this
GeneA AncestorA
GeneA AncestorB
GeneB AncestorA
GeneB AncestorB
GeneC AncestorA
GeneC AncestorD
Then you just append this to the original GeneOntology.txt (probably you should consider backing up that file).
standard_output >> GeneOntology.txt
Your final file will be
Gene Ontology
GeneA TermA
GeneB TermA
GeneA TermB
GeneC TermC
GeneA AncestorA
GeneA AncestorB
GeneB AncestorA
GeneB AncestorB
GeneC AncestorA
GeneC AncestorD
Now, if you want to sort the file you can simply:
echo -e "Gene\tOntology"> sorted.txt
sed 1d GeneOntology.txt|sort>>sorted.txt
Final result
Gene Ontology
GeneA AncestorA
GeneA AncestorB
GeneA TermA
GeneA TermB
GeneB AncestorA
GeneB AncestorB
GeneB TermA
GeneC AncestorA
GeneC AncestorD
GeneC TermC
those tables are plaintext?
Yes, just two tab-separated columns as .txt for both.