rarenas has asked for the wisdom of the Perl Monks concerning the following question:
Hello. I started learning perl two weeks ago and I have a practice assignment to convert fasta files into tabular format using perl. I managed to write a program that converted a fasta into tbl but only when there is one sequence in the fasta file. If I have multiple sequences in a fasta file, I cannot manage to convert them to tbl properly. This is the code I wrote that works with one sequence in the fasta:
#!/usr/bin/perl # fasta_to_tbl.pl use strict; use warnings; die "Please specify suitable file\n" if (@ARGV != 1); my ($fasta) = @ARGV; my $outfile = "$fasta.tbl"; open(my $in, "<", "$fasta") or die "error reading $fasta. $!"; open(my $out, ">", "$outfile") or die "error creating $outfile. $!"; my $identifier = ""; my $union = ""; while (<$in>) { chomp; next unless m/\w/; if ($_ =~ m/>/) { # Identifier! $_ =~ s/>//; ($identifier) = split /\:|\s|\||,|;/, $_; print "$identifier\n"; } else { # We have a line with sequence $union = $union . $_; } } print $out "$identifier\t$union\n"; close($in); close($out);
I realized that it would be a lot better to use hashes instead of arrays to separate the different sequences. I want to have the sequence title/name be the key and the sequence be the value. I also thought it would be good to use the local command so that I can separate based on ">" symbol instead of by line because all fasta file titles start with that symbol. I am stuck on actually implementing those realizations and then using a loop to edit the formatting for each sequence. Any suggestions? Thank you in advance!
I am using the simple fasta file below for practice but do note that many fasta files contain extra information in the title and may have a space between the title and the sequence. We only want the name in tbl and not the extra information. The code above takes care of those extras only for one sequence.
Fasta format
>sequence1 ACTCCCCGTGCGCGCCCGGCCCGTAGCGTCCTCGTCGCCGCCCCTCGTCTCGCAGCCGCAGCCCGCGTGG ACGCTCTCGCCTGAGCGCCGCGGACTAGCCCGGGTGGCC >sequence2 CAGTCCGGCAGCGCCGGGGTTAAGCGGCCCAAGTAAACGTAGCGCAGCGATCGGCGCCGGAGATTCGCGA ACCCGACACTCCGCGCCGCCCGCCGGCCAGGACCCGCGGCGCGATCGCGGCGCCGCGCTACAGCCAGCCT CACTGGCGCGCGGGCGAGCGCACGGGCGCTC >sequence3 CACGACAGGCCCGCTGAGGCTTGTGCCAGACCTTGGAAACCTCAGGTATATACCTTTCCAGACGCGGGAT CTCCCCTCCCC >sequence4 CAGCAGACATCTGAATGAAGAAGAGGGTGCCAGCGGGTATGAGGAGTGCATTATCGTTAATGGGAACTTC AGTGACCAGTCCTCAGACACGAAGGATGCTCCCTCACCCCCAGTCTTGGAGGCAATCTGCACAGAGCCAG TCTGCACACC
Tabular format
>sequence1 ACTCCCCGTGCGCGCCCGGCCCGTAGCGTCCTCGTCGCCGCCCCTCGTCTCGCAGCC +GCAGCCCGCGTGGACGCTCTCGCCTGAGCGCCGCGGACTAGCCCGGGTGGCC >sequence2 CAGTCCGGCAGCGCCGGGGTTAAGCGGCCCAAGTAAACGTAGCGCAGCGATCGGCGC +CGGAGATTCGCGAACCCGACACTCCGCGCCGCCCGCCGGCCAGGACCCGCGGCGCGATCGCGGCGCCGC +GCTACAGCCAGCCTCACTGGCGCGCGGGCGAGCGCACGGGCGCTC >sequence3 CACGACAGGCCCGCTGAGGCTTGTGCCAGACCTTGGAAACCTCAGGTATATACCTTT +CCAGACGCGGGATCTCCCCTCCCC >sequence4 CAGCAGACATCTGAATGAAGAAGAGGGTGCCAGCGGGTATGAGGAGTGCATTATCGT +TAATGGGAACTTCAGTGACCAGTCCTCAGACACGAAGGATGCTCCCTCACCCCCAGTCTTGGAGGCAAT +CTGCACAGAGCCAGTCTGCACACC
|
|---|