This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to remove lines with unmatched columns

Hi All, I have a bed file (annotation) with a bug throughout; the last 2 columns ARE NOT MATCHED in number of blocks ($5 has 4 blocks but $6 has 3) in some rows. How can I remove these lines having this unmatched columns. Thank you guys

  input:

  chr2   1627   4677   +     1,4,92,30    0,19,11
  chr2   2643   6698   +     10,42,9      0,14
  chr3   1327   4377   +     12,32        0,11
  chr4   4143   6698   +     64,43,23     0,24,51

  desired:

   chr3  1327   4377   +     12,32        0,11
   chr4  4143   6698   +     64,43,23     0,24,51
sequence rna-seq

Dear BehMah. Could you please share with a few more lines of the original file and another snipped with desired result after data is fixed (make it manually). So we can understand exactly what you are looking for. Thank you.

More explenation:

I want to extract sequences of the coordinates but as Exon sizes ($5) are different from exon offsets($6) in numbers, bedtools doesn't give me all the sequences

Thank you all 5heikki ,Petr, jmzeng1314 for your awesome codes

3 answers

perl -alne '{$tmp=tr/,//;print if $tmp %2==0}'  your.input >output
awk '{if(gsub(",","",$5)==gsub(",","",$6)){print $0}}' input.txt

gsub returns number of substitutions it made

gsub returns the number of substitutions, so:

awk 'BEGIN{OFS=FS="\t"}{if(gsub(",",",",$5) == gsub(",",",",$6)){print $0}}' inputFile

edit. Petr Ponomarenko suggested the same, however, at least with my gawk his solution deletes the commas from $5 and $6

Log in to answer this question.