This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Order contig by size

Hi there !

Does anyone has a easy solution to order contig from an assembly by size ? For example, I want that contig1 is the largest, contig 2 is the second largest etc...

I can't find a convenient solution (maybe I'm not looking good enough..)

Thanks for your help !

Cheers,

Roxane

assembly script

Sorry didn't found it... Should I delete my post or marked it as answered ?

oh right! didn't realize this. But it seems that OP wants rename contigs too.

1 answer

Since you ask an easy solution and you seem not a beginner. I introduce a fast and easy solution using seqkit, a handy FASTA/Q toolkits, link.

seqkit provides executable binaries for Linux/OS X/Windows, you just need download, decompress and immediately run.

Sample data:

$ cat contigs.fasta 
>contig1
aaaa
cccc
>contig2
cccccc
tttttt
>contigs
ggggg
ccc

Sorting using seqkit sort, contig sequences can be multi-line.

$ seqkit sort --by-length --reverse contigs.fasta
>contig2
cccccctttttt
>contig1
aaaacccc
>contigs
gggggccc

If you want to rename the FASTA header, then use seqkit replace:

$ seqkit sort --by-length --reverse contigs.fasta | seqkit replace --pattern '.+' --replacement 'Contig_{nr}'
>Contig_1
cccccctttttt
>Contig_2
aaaacccc
>Contig_3
gggggccc

Where {nr} is the number of record.

I actually like your solution ! awk solution was working but in ascending order, I wanted decreasing order. And adding just a --reverse on the sort command didn't solved it so I was running out of solution.. I'm going to try seqkit thanks !

sort can be in reversed order: sort -k1,1nr

Thanks ! I was trying -rk1,1n

man sort is your friend.

Actually, I have checked the man.. Thanks for the acid comment

That was not an acid comment. It was a genuine suggestion (in case you did not know about man inline help for unix commands). It was in light of your comment above which indicated that you were making a minor formatting error for the sort command option.

Sorry, perceived that as ironic, my bad!

It worked perfectly fine using your tool, thanks !

Log in to answer this question.