This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to reduce the length of Ns in a fasta sequence

Hello all, I have a multi fasta file in which many sequences contain long runs of Ns. I want to reduce the length of Ns to a maximum of 150 any suggestions? Thank you,

The example sequence below contains a run of 215 Ns.

Seq1 TAGCAGCAGCAATAGCACTAGCAGTACGAGTAGCGGTAGCAGCAggtagtagcagtagcagcagtagtagaagtagtagtagtagtagtagtagtagtactagt agtactagtagtagtagtagcagcagcagcagcagcagcagtagcagcagccgCAGGGGGAGACAAANNNNNNNNNNNNNNNNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNTAGTAGAATGGGTGGTATaagtagcagcagcagcagcagcagcagtagcagcagcagcagctgTAGCAATAACAGCAGCA GCACCAGCAGTAGctgtagcagcagcagtagcagcagcaagAGTAGCAGGAGGAGTAGGAGGAGTAGGAGNNNNNNNNNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNNNNNNGGCAacagtagtagcagcagcagcagtagcagcaggagtagcagtagcagtagtagtagc

sequence

2 answers

Seqkit has quite good fasta manupulation functions especially seqkit replace in this case https://bioinf.shenwei.me/seqkit/usage/#replace

it takes search patterns with -p argument which in this case is N repeated 150 or more times N{150,} and -r as replacement which can be N typed 150 times or -

seqkit replace -p '(N{150,})' -r $(printf "%0.sN" {1..150}) -s -i yourFastaFile.fa

linearize and sed

awk '/^>/ {printf("%s%s\t",(N>0?"\n":""),$0);N++;next;} {printf("%s",$0);} END {printf("\n");}' input.fa | \
tr "\t" "\n" |\
sed -r '/^[^>]/s/N{150,}/NN/g'

Thank you very much for the help. it worked.

validate+close the answer by clicking on the green mark on the left please.

Log in to answer this question.