#!/usr/bin/perl -w

#
#Source data :  ftp://nlmpubs.nlm.nih.gov/online/mesh/.meshtrees/mtrees2011.bin
#  Body Regions;A01
#  Abdomen;A01.047
#  Abdominal Cavity;A01.047.025
#  Peritoneum;A01.047.025.600
#  Douglas' Pouch;A01.047.025.600.225

#output:        eprints subjects
#subjects:MESH subject tree:ROOT:0
#A01:Body Regions:subjects:1
#A01.047:Abdomen:A01:1
#A01.047.025:Abdominal Cavity:A01.047:1

$ROOT ='subjects';
$levels = 999;
print "$ROOT:MESH subject tree:ROOT:0\n";

while(<>){
    chomp;
    ($caption,$code) =split(/;/);
     (@codelist=split(/\./,$code));# ||( @codelist=($code));
    # print STDERR "codelist length: $#codelist\n";
    chop($code);
    if($#codelist==0){
	$parent=$ROOT;
	print "$code:[$code] $caption:$parent:1\n";
    }
    elsif($#codelist < $levels){
	pop @codelist;
	$parent=join('.',@codelist);
	print "$code:[$code] $caption:$parent:1\n";
    }
}

