1

I am working in bioinformatics, and I need to count patterns such as GATTACCA in large files that look something like:
"ATTTCCCGATCCGAG GATT (/n)
ACCA CGTAGATGATACACGT (etc)" Is there a way to get Grep to ignore the /n new line character? Thanks for any help!

2
  • do you just need the count of GATT\nACCA occurrence ? and how large the file is?
    – Kent
    Jun 20, 2014 at 11:06
  • How big are the files? Secondly - grep recognizes by newline so it can't match it. But you may be able to use "tr" to replace newline character with space, but again, how big are the files?
    – Dane Balia
    Jun 20, 2014 at 11:07

4 Answers 4

1

I think this may do what you want:

tr -d '\n' < file | grep -o GATTACCA

It (temporarily) removes the linefeeds from your file (using tr and its -d option to delete) before passing it into grep.

1
  • Thanks! this works easiest so far... Now... Any ideas how to parse it into python? I know how to use simple linux commands in python, but not how to pipe...
    – MrJanx
    Jun 20, 2014 at 19:16
1

Using sed and grep:

sed -n 'H;x;s/\n//g;/GATTACCA/p' input | grep -o GATTACCA
2
  • Why the grep? Your sed command already does its job. Am I missing something?
    – terdon
    Jun 20, 2014 at 11:21
  • there may be multiple occurrences of the pattern.
    – perreal
    Jun 20, 2014 at 11:25
1

You could do this through awk and grep to count the number of occurrences of the word GATTACCA in your file,

awk -v RS="\0" '{gsub (/\n/,""); print}' file | grep -o 'GATTACCA' | wc -l

Explanation:

RS="\0"            #  Turns the input file into a single record.

gsub (/\n/,"")     #  Removes all the \n character.

grep -o 'GATTACCA' # From the awk output, it fetches the string GATTACCA and prints every match in a new line.

wc -l              #   To count the number of lines
4
  • You can use grep -c to count instead of wc.
    – terdon
    Jun 20, 2014 at 11:18
  • @terdon it displays different result. Jun 20, 2014 at 11:26
  • I want number of occurences, not number of lines... so -c would probably be better?
    – MrJanx
    Jun 20, 2014 at 19:54
  • It displays the number of occurances.did you try this? You may check the output mycode with others. Jun 21, 2014 at 0:41
0

You already have two good general answers. Another approach is to use sed:

perl -pe 's/\n//' file | grep -o GATACA

However, if you're working on fasta files, this can be interesting:

#! /bin/sh
gawk '{
        if (substr($1,1,1)==">")
        if (NR>1)
                    printf "\n%s\t", substr($0,2,length($0)-1)
        else 
            printf "%s\t", substr($0,2,length($0)-1)
        else 
                printf "%s", $0
}END{printf "\n"}'  "$@"

The script above changes fasta format to tbl (seq IDsequence, all on the same line). I very often use it for grepping:

FastaToTbl foo.fa | grep GATTACA 

I also have a TblToFasta to restore the original:

#! /bin/sh
# tbl-to-fasta.awk transforms a tbl file into a fasta file, 60 columns per record
# usage=gawk -f tbl-to-fasta TBL_FILE 


gawk '{
  sequence=$NF

  ls = length(sequence)
  is = 1
  fld  = 1

  while (fld < NF)
  {
     if (fld == 1){printf ">"}
     printf "%s " , $fld

     if (fld == NF-1)
      {
        printf "\n"
      }
      fld = fld+1
  }

  while (is <= ls)
  {
    printf "%s\n", substr(sequence,is,60)
    is=is+60
  }
}' "$@"
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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