Maybe not the most efficient for super huge files, but working version.
Input file file1:
Bin_37:_Pelotomaculum_sp._DTU098 GH3 GH57 GH15 GH18 GT2 GT4 GT28
Bin_45_1:_Thiopseudomonas_denitrificans GH3 GH57 GT2 GT9 CBM48
Bin_99:_to_make_sure_no_columns_is_ok
Script (/bin/sh or /bin/bash):
#!/bin/sh
F="file1";
COLS=$(cat "${F}"|sed 's/^[^\t]*//g;s/\t/\n/g'|sort|uniq|xargs);
# list of all available unique columns in SORTED order
echo "All avaiulable columns: [${COLS}]";
echo
# reading from the file line by line
cat "${F}"|while read L; do
# assign to A the first column
A=$(echo "${L}"|cut -d' ' -f1);
# if A is not empty
[ -n "${A}" ] &&
{
# take one by one all possible column values
for C in ${COLS}; do
# if the taken line has such column, add it to A,
# otherwise add to A NA
echo "${L} "|grep "\s${C}\s" >/dev/null &&
A="$A"$'\t'"${C}" ||
A="$A"$'\tNA';
done;
# print result line
echo "${A}";
};
done
Output:
All avaiulable columns: [CBM48 GH15 GH18 GH3 GH57 GT2 GT28 GT4 GT9]
Bin_37:_Pelotomaculum_sp._DTU098 NA GH15 GH18 GH3 GH57 GT2 GT28 GT4 NA
Bin_45_1:_Thiopseudomonas_denitrificans CBM48 NA NA GH3 GH57 GT2 NA NA GT9
Bin_99:_to_make_sure_no_columns_is_ok NA NA NA NA NA NA NA NA NA
The same (without list of available columns at the beginning) as one liner:
F="file1"; COLS=$(cat "${F}"|sed 's/^[^\t]*//g;s/\t/\n/g'|sort|uniq|xargs); cat "${F}"|while read L; do A=$(echo "${L}"|cut -d' ' -f1); [ -n "${A}" ] && { for C in ${COLS}; do echo "${L} "|grep "\s${C}\s" >/dev/null && A="$A"$'\t'"${C}" || A="$A"$'\tNA'; done; echo "${A}"; }; done
UPDATED. Optimized more efficient version, based on suggestions in comments (/bin/bash required):
F="file1"; IFS=$'\n'; COLS=($(sed 's/^[^\t]*//g;s/\t/\n/g' "${F}"|sort -u)); while read -r L; do A="${L%%$'\t'*}"; [ -n "${A}" ] && for C in ${COLS[@]}; do [[ "${L}"$'\t' == *$'\t'"${C}"$'\t'* ]] && A="$A"$'\t'"${C}" || A="$A"$'\tNA'; done && echo "${A}"; done <${F}; IFS=' '