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$
2
  • $\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$ Feb 12, 2020 at 17:41
  • 1
    $\begingroup$ yes > included, thank you i will edit the question $\endgroup$
    – BioInfo
    Feb 13, 2020 at 10:39

3 Answers 3

4
$\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$
1
  • $\begingroup$ Dear thank you for your response, kindly check the updated question $\endgroup$
    – BioInfo
    Feb 16, 2020 at 6:39
3
$\begingroup$

Your own answer 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$
2
$\begingroup$
sed -e 's:^\(.*\)_.*_\(.*\)$:\1/\2:' < input > output
$\endgroup$

Your Answer

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

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