This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Reformat A Fasta File To A Custom Tabular Format

I have a fasta format file. It looks like. test.fa

>WP1_11_x10
CCATGCGCGGGTTCAATTCCTGTCGTTCGACCC
>WP1_16_x1
ACAAGCGAAGGCTCCTCAACGACGCCTCATCGGATG
>WP1_17_x1
AGAACAAGATTGTTGAAAACTTGAGGA
>WP1_21_x11
CCTGGGATGCGCAAGGAAGCTGAC

I want the below format.The number(10,1,1,11) is in the fasta header .

CCATGCGCGGGTTCAATTCCTGTCGTTCGACCC   10
ACAAGCGAAGGCTCCTCAACGACGCCTCATCGGATG    1
AGAACAAGATTGTTGAAAACTTGAGGA     1
CCTGGGATGCGCAAGGAAGCTGAC  11

Who can help me?Thank you very much!!!

perl awk format

4 answers

hai 21.., Please refer to code below written in perl

  #! /usr/bin/perl -w


open(F1, '<', "$ARGV[0]") or die;

while (<F1>){

    if ($.%2 != 0) {

         @val = split(/\_/);
         $cnt = $val[1];
    }
    else{ chomp($_); print "$_ $cnt \n"; }
}

This code was not written in a defensive way, this should work normally with out any problems.

Thank you ,but I 'm sorry i give you the wrong number ,I have edit it.I have got the answer,thank you

  #! /usr/bin/perl -w


open(F1, '<', "$ARGV[0]") or die;

while (<F1>){

if ($.%2 != 0) {

     @val = split(/\_x/);
     $cnt = $val[1];
}
else{ chomp($_); print "$_ $cnt \n"; }

}

Consider upvoting the answer, if it was useful for you.

Or at the command line:

more test.fa | perl -ane 'if(/>/){($dum,$x)=split(/x/,$F[0]);}else{print "$F[0]\t$x\n";}'

Here's another option:

use strict;
use warnings;

my $num;
while (<>) {
    if (/>/) {
        ($num) = /(\d+)$/;
    }
    elsif ( $num and /\S/ ) {
        chomp;
        print "$_\t$num\n";
    }
}

Usage: perl script.pl fastaFile [>resultsFile]

The last, optional parameter will redirect output to a file.

Results on your dataset:

CCATGCGCGGGTTCAATTCCTGTCGTTCGACCC    10
ACAAGCGAAGGCTCCTCAACGACGCCTCATCGGATG    1
AGAACAAGATTGTTGAAAACTTGAGGA    1
CCTGGGATGCGCAAGGAAGCTGAC    11

Here is how to do it with Biopieces www.biopieces.org):

read_fasta -i test.fa | split_vals -k SEQ_NAME -d 'x' | write_tab -k SEQ,SEQ_NAME_1 -x

Log in to answer this question.