2018awk8回答例

練習問題①

1.awkで数字の絶対値を返す関数 absを作ってみる。

awk '
 function abs(x){
  if(x>0){
   return x
  }else{
   return -x
  }
 }
 BEGIN{
  print abs(5);
  print abs(-5);
 }
'

2.四捨五入する関数roundを作ってみる。(int()関数で切り捨ては出来る)

awk '
 function round(x){
  if(x>0){
   return int(x+0.5)
  }else{
   return int(x-0.5)
  }
 }
 BEGIN{
  print round(5.6);
  print round(-5.6);
 }
'

練習問題② GOアノテーション情報の整理

go.oboファイルをGene Ontology Consortiumのページからダウンロードする http://geneontology.org/page/download-ontology

wget http://purl.obolibrary.org/obo/go.obo

このファイルは、[Term]という行の直後に、GOのIDが記述され、その次の行に名前が記述されている。 さらに数行後に、isa: から始まる行がある(ないこともある)。 このisaの行は、GOのより大きな階層の情報(親のGOとでも呼んでおく)を示している。 例: GO:0000001 mitochondrion inheritance の場合、

is_a: GO:0048308 ! organelle inheritance
is_a: GO:0048311 ! mitochondrion distribution

とより広義な意味を持つGOにリンクされている。

1.該当レコードのGO IDを1次元目、親のGO IDを2次元目に格納せよ。

2.逆に、親のGO IDを1次元目、該当レコードのGO IDを2次元目に格納せよ。

3.

GO:0008150 (biological_process)
GO:0005575   (cellular_component)
GO:0003674   (molecular_function)

を直接の親とするGO:IDをそれぞれすべて表示せよ。

#まず"[Typedef]"以降を削除したファイルを作っておく
awk '{if($1=="[Typedef]"){exit}; print $0}' go.obo > go.obo.notypedef

#biological_processを表示させる場合
awk '
{
 if($1=="id:"){
  goid=$2
 }
 if($1=="name:"){
  name[goid]=$0
 }
 if($1=="is_a:"){
  child2parent[goid][$2]=1;
  parent2child[$2][goid]=1;
 }
}
END{
 for(i in parent2child["GO:0008150"]){
  print i,name[i];
 }
}
' go.obo.notypedef

4. 任意のGO:IDが与えられたら、その親、親の親、・・・とすべて表示せよ。親は1つだけとは限らない(1対1対応ではない)ことに注意。

awk '
function search_parent(x, space,  i){
 print space, x, name[x];
 if(length(child2parent[x])>0){
  for(i in child2parent[x]){
   search_parent(i, " "space)
  }
 }
}
{
 if($1=="id:"){
  goid=$2
 }
 if($1=="name:"){
  name[goid]=$0
 }
 if($1=="is_a:"){
  child2parent[goid][$2]=1;
  parent2child[$2][goid]=1;
 }
}
END{
 search_parent("GO:0000001");
}
' go.obo.notypedef

##出力結果:
 GO:0000001 name: mitochondrion inheritance
  GO:0048308 name: organelle inheritance
   GO:0006996 name: organelle organization
    GO:0016043 name: cellular component organization
     GO:0009987 name: cellular process
      GO:0008150 name: biological_process
     GO:0071840 name: cellular component organization or biogenesis
      GO:0008150 name: biological_process
  GO:0048311 name: mitochondrion distribution
   GO:0007005 name: mitochondrion organization
    GO:0006996 name: organelle organization
     GO:0016043 name: cellular component organization
      GO:0009987 name: cellular process
       GO:0008150 name: biological_process
      GO:0071840 name: cellular component organization or biogenesis
       GO:0008150 name: biological_process
   GO:0051646 name: mitochondrion localization
    GO:0051640 name: organelle localization
     GO:0051641 name: cellular localization
      GO:0051179 name: localization
       GO:0008150 name: biological_process

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 4096 bytes) in /home/webpark1634/www/yosh/lib/plugins/authplain/auth.php on line 441

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /home/webpark1634/www/yosh/inc/ErrorHandler.php on line 90