This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Grep A Specific Contig From A List?

Hi

I have a list of contigs and want to extract one contig (by its title). What is the exact command for that?

Thanks!

contigs fasta

if your contig file is in fasta format, there are a lot of discussion already open. Have a search in the archives.

4 answers

This has already been addressed as Giovanni pointed out:

Please try at least a cursory search of the site before posting a new question!

Default grep can't find multiline patterns.

You can use pcregrep.

Check this topic from stackoverflow.

If you want to extract multiple contigs at different times, it is highly suggested that you build an index. Here some commands:

samtools faidx in.fasta #(create an index for the fasta file with all the contigs)
samtools faidx in.fasta contigname > contigname.fasta #(extract the single contig you need)
#to extract seqnames from MS blast result(seqtool) files:
grep contigname filename > out.txt

If you need more lines from contig add -A number after grep like: (for 3 lines)

grep -A 3 contigname filename > out.txt

This is impractical. A better solution would be to use a regex like >CONTIG_NAME[^>]+ but only if the engine supports multi-line matching (i.e., not the Unix grep tool).

I've used agrep for this (as in: agrep -d '^>' pattern contigs.fasta) but unfortunately it is unreliable, and has some ugly limitations. I also implemented my own tool (sgrep, part of the biohaskell stuff)

Log in to answer this question.