Parsing of Newick tree format files

Dear all,

I have >5000 files in newick tree format (which generally used for representing phylogenetic relationships) something like below.

(apple_OJJ_1:0.1,banana_OJJ_1:0.4,(((cat_OJJ_1:0.1,(dog_EHA_1:0.2,(elephant_OJJ_1:0.03,fish_CBF_1:0.032):0.088):0.24):0.123,(goat_EIT_1:0.00,hen_EED_1:0.00):0.31):0.05,
((ink_EAU_1:0.16,(jug_OJJ_1:0.9,(kite_OOG0_1:0.08,(lion_OJJ7_1:0.0,(monkey_OJI_1:0.00,(nest_OJ_1:0.000,owl_GAA_1:0.00):0.00):0.05):0.084):0.09):0.0484):0.0179,
(parrot_EAW_1:0.195,(queen_RT92_1:0.0291,rat_EAW_1:0.0156):0.1430):0.083):0.03243):0.0951);

I want to format the files in following format

(apple_OJJ_1:0.1,banana_OJJ_1:0.4,(((cat_OJJ_1:0.1,(dog_EHA_1:0.2,(elephant_OJJ_1:0.03,fish_CBF_1:0.032):0.088):0.24):0.123,(goat_EIT_1:0.00,hen_EED_1:0.00):0.31):0.05,
((ink_EAU_1:0.16,(jug_OJJ_1:0.9,(kite_OOG0_1:0.08,(lion_OJJ7_1:0.0,(monkey_OJI_1:0.00,(nest_OJ_1:0.000,owl_GAA_1:0.00):0.00):0.05):0.084):0.09):0.0484):0.0179,
(parrot_EAW_1:0.195,(queen_RT92_1:0.0291,rat_EAW_1:0.0156)#1:0.1430):0.083):0.03243):0.0951);

Only addition in the result file is addition of #1, shown by bold red. I want to add this #1 in all the files. I am basically interested in the parentheses ( ) in which the queen_xyz exists.
The task is to find queen_xvz id which can be paired up with any other fruit/object/animal in other files and once queen is found then need to look in which parentheses it exists and
then adding #1 after the first close parentheses ) of queen containing ( ) as shown in above dummy file.
Sorry for the long dummy file but this reflect the original files.

sed 's@\(queen_[^)]*\)\([)]\)@\1\2#1@' file
1 Like

Thanks this working. Could you please also suggest the change if I want to print #1 as

(queen_RT92_1#1:0.0291,rat_EAW_1:0.0156)

not like I asked before. Thanks for your support.

sed 's@\(queen_[^:]*\)\([:]\)@\1#1\2@' file