So this is a educational portal right? Please do not take this the wrong way, because I am personally advocate of "if it works don't fix it" principle, but from the above code it looks like you are just starting with Perl, so allow me to make a few suggestions:
1. use strict; # excellent practice. Even better would be to also use warnings
2.
my $input_file = $ARGV[0];
my $output_file = $ARGV[1];
No need for that you are just wasting memory. Though this may not be relevant for this particular case since only few bytes are lost better practice is to either directly use ARGV's or use Getopt module. So:
open INFILE, $ARGV[0] or die "Can't open $infile: $!\n";
instead of
my $input_file = $ARGV[0];
my $infile = $input_file;
open INFILE, $infile or die "Can't open $infile: $!\n";
Or if you use Getopt module then :
use Getopt::Long;
my ($infile,$outfile);
GetOptions ('i=s' => \$infile, 'o=s' => \$outfile);
This way you don't need to have your ins and outs defined in a specific order, plus you have "option flags" and it is less likely to mix-up ins and outs
3.
if ($#ARGV !=1) {
print "\n ** Wrong Arguments **\n\n";
print " - USE: fasta_remove.pl InFile.fasta OutFile.fasta\n";
}
Very bad practice, plus I think you should not allow a user to continue executing code if the condition is not satisfied. so there should either be a die or exit function call after the last print. There are may ways how this could be done safely but I'll suggest one of the simplest ones in accordance to your code
if(!$infile or !$outfile){
print "\n ** Wrong Arguments **\n\n";
print "Usage: perl program [options]\n";
print "\t-i\tinput file [fasta]\n";
print "\t-o\toutput file [fasta]\n";
exit(1);
}
or die
in this type of situations is usually a safety measure reserved for verifying if the file is where it is supposed to be and if the write/read/execute permissions are allowing the action to take place.
4. You have a lot of if-else statements. Totally not perl-ish. If this was a c code I would understand (personally I am a c/c++ programer) but this ... no. Moreover, do you really need all those conditions? Do they really need to be mutually exclusive? To me the logic of perl is like a logic of thinking and speaking, therefore if you phrase you conditions as you speak (English as a frame of reference ) you are probably off to a good start. Below you have my version of the parser:
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
my ($infile,$outfile);
GetOptions ('i=s' => \$infile, 'o=s' => \$outfile);
if(!$infile or !$outfile){
print "\n ** Wrong Arguments **\n\n";
print "Usage: perl program [options]\n";
print "\t-i\tinput file [fasta]\n";
print "\t-o\toutput file [fasta]\n";
exit(1);
}
open(IN, "<", $infile) or die "$!";
open(OUT, ">", $outfile) or die "$!";
my $lock = 0;
while(<IN>){
chomp;
if(/>/){
($lock == 0) ? (print OUT "$_\n") : (print OUT "\n$_\n");
$lock = 1;
next;
}
s/[\s|-]//g;
print OUT "$_";
}
close IN;
close OUT;
It is important to close the filehandle after you are done unless you intend to loose some of the data. Perl flushes upon the exit but if you do not exit the program directly after you are done writing and intend to use the written data in another procedure, you will get into problems.
Or you could use a simple one-liner instead if you are really eager to use perl:
test.txt
is your input file andout
is your output.