Our Sponsors



Download BioinformaticsOnline(BOL) Apps in your chrome browser.




Perl script to convert Multi-line Fasta to Single-line Fasta !

  • Public
By BioStar 326 days ago
#!/usr/bin/perl use strict; use warnings; sub multi_to_single_line_fasta { my ($input_filename, $output_filename) = @_; open my $input_file, '<', $input_filename or die "Error: Could not open file '$input_filename': $!"; open my $output_file, '>', $output_filename or die "Error: Could not open file '$output_filename': $!"; my $current_sequence = ""; while (my $line = <$input_file>) { chomp $line; if ($line =~ /^>/) { # If a header line, write the previous sequence and then the new header print $output_file $current_sequence . "\n" if $current_sequence; print $output_file $line . "\n"; $current_sequence = ""; } else { # If a sequence line, concatenate to the current sequence $current_sequence .= $line; } } # Write the last sequence print $output_file $current_sequence . "\n" if $current_sequence; close $input_file; close $output_file; print "Successfully converted $input_filename to $output_filename in single-line FASTA format.\n"; } # Example usage: # multi_to_single_line_fasta('multi_line.fasta', 'single_line.fasta');