0

I have 2 files, file 1 and file 2 with the same column numbers. Column one has the chromosome number, column two has the promoter start site and column three has the promoter stop site.

I want to match both files, if a match to all the three columns in file 1 is found in file 2. I want to generate an output file showing the exact three columns with a fourth one. The fourth one would show a match with a score of 1 and a mismatch with a score of 0.

File 1:

chr1 899 1098
chr3 677 900
chr3 844 1045

File 2:

chr3 844 1045
chr1 899 1098
chr4 500 789

Output File:

chr1 899 1098  1
chr3 677 900   0
chr3 844 1045  1
chr4 500 789   0

I found the answer to match the files and output a new file at the same forum:

awk 'FNR==NR{a[$1,$2,$3]=$0;next}{if(b=a[$1,$2,$3]){print b}}' file1 file2

But I want to include the match and mismatch score column in the output file too.

0

script

#!/bin/bash
filea=$1
fileb=$2
fout=$3
sed -i "/^$/d" $filea $fileb
cat $filea | while read a b c
  do
    grep "$a.*$b.*$c" $fileb &>/dev/null && l="$a   $b  $c  1" || l="$a $b  $c  0"
    grep "$l" $fout &>/dev/null || echo "$l" >> $fout
  done
cat $fileb | while read a b c
  do
    grep "$a.*$b.*$c" $filea $fout &>/dev/null && l="$a $b  $c  1" || l="$a $b  $c  0"
    grep "$l" $fout &>/dev/null || echo "$l" >> $fout
  done

stdout

younes@cbfcdf:~$ cat filea
chr1 899 1098
chr3 677 900
chr3 844 1045
younes@cbfcdf:~$ cat fileb
chr3 844 1045
chr1 899 1098
chr4 500 789
younes@cbfcdf:~$ ./script filea fileb new_file
younes@cbfcdf:~$ cat new_file 
chr1    899     1098    1
chr3    677     900     0
chr3    844     1045    1
chr4    500     789     0
younes@cbfcdf:~$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.