in reply to Converting fasta (with multiple sequences) into tabular using perl

Here is another technique for your toolbox. I don't see the need to separate the identifier or create $union.
#!/usr/bin/perl use strict; use warnings; while (my $line = <DATA>) { process_record($line) if $line=~ /^>/; } sub process_record { my $line = shift; $line =~ s/\s*$//; #or chomp $line print "$line\t"; while (defined ($line=<DATA>) and $line !~ /^\s*$/) { $line =~ s/\s*$//; #or chomp $line print $line; } print "\n"; } =Prints: >sequence1 ACTCCCCGTGCGCGCCCGGCCCGTAGCGTCCTCGTCGCCGCCCCTCGTCTCGCAGC +CGCAGCCCGCGTGGACGCTCTCGCCTGAGCGCCGCGGACTAGCCCGGGTGGCC >sequence2 CAGTCCGGCAGCGCCGGGGTTAAGCGGCCCAAGTAAACGTAGCGCAGCGATCGGCG +CCGGAGATTCGCGAACCCGACACTCCGCGCCGCCCGCCGGCCAGGACCCGCGGCGCGATCGCGGCGCCG +CGCTACAGCCAGCCTCACTGGCGCGCGGGCGAGCGCACGGGCGCTC >sequence3 CACGACAGGCCCGCTGAGGCTTGTGCCAGACCTTGGAAACCTCAGGTATATACCTT +TCCAGACGCGGGATCTCCCCTCCCC >sequence4 CAGCAGACATCTGAATGAAGAAGAGGGTGCCAGCGGGTATGAGGAGTGCATTATCG +TTAATGGGAACTTCAGTGACCAGTCCTCAGACACGAAGGATGCTCCCTCACCCCCAGTCTTGGAGGCAA +TCTGCACAGAGCCAGTCTGCACACC =cut __DATA__ >sequence1 ACTCCCCGTGCGCGCCCGGCCCGTAGCGTCCTCGTCGCCGCCCCTCGTCTCGCAGCCGCAGCCCGCGTGG ACGCTCTCGCCTGAGCGCCGCGGACTAGCCCGGGTGGCC >sequence2 CAGTCCGGCAGCGCCGGGGTTAAGCGGCCCAAGTAAACGTAGCGCAGCGATCGGCGCCGGAGATTCGCGA ACCCGACACTCCGCGCCGCCCGCCGGCCAGGACCCGCGGCGCGATCGCGGCGCCGCGCTACAGCCAGCCT CACTGGCGCGCGGGCGAGCGCACGGGCGCTC >sequence3 CACGACAGGCCCGCTGAGGCTTGTGCCAGACCTTGGAAACCTCAGGTATATACCTTTCCAGACGCGGGAT CTCCCCTCCCC >sequence4 CAGCAGACATCTGAATGAAGAAGAGGGTGCCAGCGGGTATGAGGAGTGCATTATCGTTAATGGGAACTTC AGTGACCAGTCCTCAGACACGAAGGATGCTCCCTCACCCCCAGTCTTGGAGGCAATCTGCACAGAGCCAG TCTGCACACC
Update:
I missed the idea that there could be some "kruft" after the id because an example of that was not present in the example data provided. I would recommend something like this change to process_record():
(my $id) = $line =~ /^(>\w+)/; #allow for kruft after id print "$id\t";
In general, use a regex when you know what you want to extract. Use a split when you know what you want to throw away. With the above regex, it is not necessary to enumerate all of these potential non-word things like "|;(space)", etc. However, note that underscore "_" is a "word character" (any character valid in a Perl variable name is allowed for \w). If underscore could directly follow the sequence id, then of course the regex would need to change. However, I see no evidence that would be necessary.

Replies are listed 'Best First'.
Re^2: Converting fasta (with multiple sequences) into tabular using perl
by rarenas (Acolyte) on Dec 14, 2017 at 10:43 UTC

    Thanks for the response! The multi fasta file I put up is a very simple file but I know, from working with fasta files, that sometimes the titles can be complex and may be divided in strange ways. Sometimes there isn't a space between the name and the rest of the information but instead other symbols that can be used as separators. That's the reason why I had all those non-word symbols. But, as you say, it is not necessary if I know what I want to throw away. This really helps! Thanks again!