Score:1

Duplicate every two lines a variable number of times

do flag
LML

I have multiple .fasta files (that are named barcode*_consensus.fasta) that look like this:

>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT

I would like to duplicate/repeat every two lines n number of times, as specified after 'total supporting reads'. So for example, I would like to duplicate the first two lines 12 times, the second two lines 6 times, etc.

With awk, I did manage to select every line that starts with '>' and the next line:

awk '/>/{nr[NR]; nr[NR+1]} NR in nr' barcode01_consensus.fasta

But I can't find out how to print this n number of times with a variable.

Any help is much appreciated.

Updated: So I would like the final file to look something like:

|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000 TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTT

|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000 TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTT

|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000 TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTT

....x 12 times....

Score:1
cn flag

I'd use space or underscore as the field separator. Then, the count is the 8th field:

awk -F'[ _]' '
    $1 ~ /[>|]+consensus$/ {n = $8; print; next}
    {while (--n >= 0) print}
' file

output

>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT

To print each pair of lines n times requires just minor changes:

awk -F'[ _]' '
    $1 ~ /[>|]+consensus$/ {firstline = $0; n = $8; next}
    {while (--n >= 0) print firstline ORS $0}
' file

output

>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
LML avatar
do flag
LML
Thank you! This works for most of it, however, I would like to print both line 1+2 n number of times and this only prints the second line n number of times. I have updated my question to clarify this in a better way.
Score:0
hr flag

I suspect there's a proper bioinformatics toolkit that would do what you want more robustly, but with GNU awk you could match and capture the repeat number like

gawk '
  /^>?[|]>/ {
    if((getline seq) > 0) $0 = $0 ORS seq
  } 
  match($0,/total_supporting_reads_([0-9]+)/,a) {
    while(a[1]--) print
  }
' file.fasta

You can do the same with non-GNU awk, except that you would need to extract the numeric portion of the match as a substring using the RSTART and RMATCH values instead of capturing it in an array.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.