Problem with multiple grep in bash loop

Hello,
I am trying to create a matrix of 0's and 1's depending on whether a gene and sample name are found in the same line in a file called results.txt. An example of the results.txt file is (tab-delimited):

Sample1 Gene1 ## Gene2 ##
Sample2 Gene2 ## Gene 4 ##
Sample3 Gene3 ## Gene 6 ##

The matrix should look like this:

            Sample1  Sample 2    Sample3
Gene1        1               0           0
Gene2        0               1           0
Gene3        0               0           1

where 1 = 'match found for both gene and sample' and 0 for 'no match'.

My code reads in three files: GenesList.txt (gene names), SampleList.txt (Sample names), and the results.txt file. The output matrix should be Matrix.out.

The multiple grep condition on a single line does not seem to be working properly for me, or maybe I have a problem with my If statement??

For the resultant matrix I expect for any row to have both 0's and 1's, but I end up with an entire matrix of only 1's or only 0's. What am I missing from this bash script?

for Gene in `cat GenesList.txt`; #Read all the genes in the file
do
      echo -n -e $Gene '\t' >> FusionMatrix.out # Echo gene name (designates the row in my table)
        for Sample in `cat SampleList.txt`;
        do
    grep_output= grep $Sample result.txt | grep $Gene

        #if ["$grep_output" == ""] #my first attempt
        if [[ $grep_output ]]; # second attempt
        then echo -n -e "1" '\t' >> Matrix.out
        else echo -n -e "0" '\t' >> Matrix.out
        fi # Value of 1 if gene involved in that sample, else 0
        done

    echo "">> Matrix.out #Move to next line
done

Thanks for any advice!!

This code certainly is not portable, but if you're getting the 0's and 1's and heading correctly, the simple changes marked below in red may be enough to get this script to work on your system. Since I have no idea what any of your input files look like, I have no way of testing this suggestion.

for Gene in `cat GenesList.txt`; #Read all the genes in the file
do
      echo -n -e $Gene '\t' >> FusionMatrix.out # Echo gene name (designates the row in my table)
        for Sample in `cat SampleList.txt`;
        do
    grep_output=$(grep "$Sample" result.txt | grep "$Gene")

        #if ["$grep_output" == ""] #my first attempt
        #if [[ $grep_output ]]; # second attempt
        if [ "$grep_output" != "" ]
        then echo -n -e "1" '\t' >> Matrix.out
        else echo -n -e "0" '\t' >> Matrix.out
        fi # Value of 1 if gene involved in that sample, else 0
        done

    echo "">> Matrix.out #Move to next line
done

Note that the spaces before and after the opening square bracket and the space before the closing square bracket in the if statement are crucial.

This could be made much more efficient by getting rid of the calls to cat and by using grep -c for your last grep or by replacing most of this shell code with an awk script, but before I would suggest the changes needed to do that, I would need some actual data to verify that my suggested changes would work.

Good luck...

If I understand what you want to do, You can do something like the below:

$ cat t
Sample1 Gene1   ##      Gene2   ##
Sample2 Gene2   ##      Gene4   ##
Sample3 Gene3   ##      Gene6   ##

$ cat test.sh
s="Sample1"
g1="Gene1"
g2="Gene2"
grep -q "$s[[:cntrl:]]$g1\|$s[[:cntrl:]][[:alnum:]]*[[:cntrl:]]##[[:cntrl:]]$g1" t
if [ $? = 0 ]; then
  echo "Found First" 
else
  echo "Not Found First"
fi
grep -q "$s[[:cntrl:]]$g2\|$s[[:cntrl:]][[:alnum:]]*[[:cntrl:]]##[[:cntrl:]]$g2" t
if [ $? = 0 ]; then
  echo "Found second"
else
  echo "Not Found second"
fi

$ test.sh
Found First
Found second