1
$\begingroup$

kindly your comment highly appreciated

have more than 4k header sequences look like:

>LTR_retrotransposon100_Gypsy? 
cgtgcaccccaatgttcatagcagcactatttacaatagccaagacatggaaacaaccta aatgtcctttgacaggtgactggataaagaagctgtggtatatttatacaatggaatgct actcagccataaaaaagaataagataatgccatttacagcaacatggatagacctggaga atgttattctaagtgaagtaagccagaaagaaaaagaaaaataccatatgctatcactta

>LTR_retrotransposon10011_ERVK 
taatttgaaaagatacatgcaacccaatgttcatagcagcattatttacaattgccaaga tatggaagcaatctaagtgtccatcaacagatgaatggagagagaagatgtggtatatat acaataaaatactattctgtcataaaaatgaataaaattctgccatttgcaacaacatgg atagacctggagggtattatgctatgtgaaataagtcagacagagaaagacatatactat

>LTR_retrotransposon10011_ERVL-MaLR 
    taatttgaaaagatacatgcaacccaatgttcatagcagcattatttacaattgccaaga tatggaagcaatctaagtgtccatcaacagatgaatggagagagaagatgtggtatatat acaataaaatactattctgtcataaaaatgaataaaattctgccatttgcaacaacatgg atagacctggagggtattatgctatgtgaaataagtcagacagagaaagacatatactat

>LTR_retrotransposon100_Copia 
    cgtgcaccccaatgttcatagcagcactatttacaatagccaagacatggaaacaaccta aatgtcctttgacaggtgactggataaagaagctgtggtatatttatacaatggaatgct actcagccataaaaaagaataagataatgccatttacagcaacatggatagacctggaga atgttattctaagtgaagtaagccagaaagaaaaagaaaaataccatatgctatcactta

I would like to use awk or sed to remove everything between the underscore, including the underscore and replace it with / plus add sequential RNA followed by #.

and the target output could be:

>RNA1#LTR/Gypsy?
ctcagcagcactatttacaatagccaagacatggaaacaacctaaatgtcttatcaatag atgactggataaaggagctgtggtatatctatacaatggaataccatcagccataaaaaa gaataaaatattgccatttgcagcaacatggatggacctggagattatcattctaaggga agtaagccagaaagagaaagaaaaataccatatgatatcacttatatgtggaggtaaaaa aaaaaaaaaagacacaaatcaatttatttgcaaaacatacaTGGActttcagacatagaa

>RNA2#LTR/ERVL-MaLR
taatttgaaaagatacatgcaacccaatgttcatagcagcattatttacaattgccaaga tatggaagcaatctaagtgtccatcaacagatgaatggagagagaagatgtggtatatat acaataaaatactattctgtcataaaaatgaataaaattctgccatttgcaacaacatgg atagacctggagggtattatgctatgtgaaataagtcagacagagaaagacatatactat



>RNA3#LTR/ERVK
    taatttgaaaagatacatgcaacccaatgttcatagcagcattatttacaattgccaaga tatggaagcaatctaagtgtccatcaacagatgaatggagagagaagatgtggtatatat acaataaaatactattctgtcataaaaatgaataaaattctgccatttgcaacaacatgg atagacctggagggtattatgctatgtgaaataagtcagacagagaaagacatatactat

>RNA4#LTR/Copia 
    cgtgcaccccaatgttcatagcagcactatttacaatagccaagacatggaaacaaccta aatgtcctttgacaggtgactggataaagaagctgtggtatatttatacaatggaatgct actcagccataaaaaagaataagataatgccatttacagcaacatggatagacctggaga atgttattctaagtgaagtaagccagaaagaaaaagaaaaataccatatgctatcactta
$\endgroup$
  • $\begingroup$ Is this in fasta format, that is, does the header start with >, like so: >LTR_retrotransposon10011_ERVL-MaLR? It should not matter for this post, but it may be an important detail for later steps using other bioinformatics tools. $\endgroup$ – Timur Shtatland Feb 12 at 17:41
  • 1
    $\begingroup$ yes > included, thank you i will edit the question $\endgroup$ – BioInfo Feb 13 at 10:39
1
$\begingroup$

You have accepted your own answer but it doesn't seem to do what you ask as it doesn't add the sequential identifier to each sequence. Maybe try this?

awk -v rec=0 '{
    if($0 ~ "^>"){
        rec++
        sub(">", ">RNA" rec "#", $0)
        sub("_.*_", "/", $0);
    } 
    print $0
}' yourfile.fa
$\endgroup$
1
$\begingroup$
sed -e 's:^\(.*\)_.*_\(.*\)$:\1/\2:' < input > output
$\endgroup$
1
$\begingroup$

EDIT after OP's update:

Try this Perl one-liner:

perl -pe 'BEGIN { $i = 1 } $i += s{>([^_]+)_.*_}{>RNA${i}#${1}/}' input_file > output_file

Here, the command line flags are:
-e: tells the perl interpreter to look for the code inline, rather than in a file with the script,
-p: loop over the input one line at a time, execute the code in the one-liner for each iteration of the loop, and print the result into STDOUT.
The code has regex replacement (see perlre for more details):
s{>([^_]+)_.*_}{>RNA${i}#${1}/}:
replace an underscore, followed by zero or more occurrences (* quantifier) of any character (.), followed by an underscore, with slash (/).
Keep > and the original header part before the underscore, captured by parens into $1 variable.
Add string RNA${i}, where $i is the counter of the fasta headers. The counter is incremented by 1 when the substitution occurs, that is, when the next header occurs.
The variables are used like so in the substitution: ${i}, ${1} to prevent wrong interpolation.
I am also using s{}{} as delimiters for readability (as in most cases) instead of the default s///.

EXAMPLE:

echo ">LTR_retrotransposon100_Gypsy?\nACGT\n>LTR_retrotransposon10011_ERVK\nTCGA" | perl -pe 'BEGIN { $i = 1 } $i += s{>([^_]+)_.*_}{>RNA${i}#${1}/}'

Prints:

>RNA1#LTR/Gypsy?
ACGT
>RNA2#LTR/ERVK
TCGA
$\endgroup$
  • $\begingroup$ Dear thank you for your response, kindly check the updated question $\endgroup$ – BioInfo Feb 16 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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