In this program, the user is asked to enter the file name containing the sequence.
For example, use the CEACAM3.fasta file as input.
#!/usr/local/bin/perl
use strict;
use warnings;
#declare all variables
my ($seq_file, $seq_name, $sequence, $seq_length);
my ($line);
#receive file name from user
print "please enter file name or path: ";
$seq_file = <STDIN>;
chop ($seq_file);
#open file for reading
open (SEQ, $seq_file) || die "cannot open \"$seq_file\": $!";
#read sequence name and description from first line
$seq_name = <SEQ>;
chop ($seq_name);
#initialize a variable to contain the entire sequence
$sequence = "";
#read sequence lines from file
while ($line = <SEQ>) {
chop ($line);
$sequence .= $line; #add line to $sequence
}
#close file
close (SEQ);
#print sequence length on the screen, for validation
$seq_length = length ($sequence);
print "Sequence length: $seq_length\n";
#!/usr/local/bin/perl
use strict;
use warnings;
#declare all variables
my ($file1, $file2, $count, $line);
#store file names in variables (good habit)
$file1 = "the_ostrich.txt";
$file2 = "the_ostrich_numbered.txt";
#open the files
open (SOURCE, $file1) || die "cannot open \"$file1\": $!";
open (RESULT, ">$file2") || die "cannot open \"$file2\": $!";
#initialize a line counter
$count = 0;
#read lines from file1. No need to chop them, since you
#will need the line breaks when printing to file2
while ($line = <SOURCE>) {
$count++;
print RESULT "[$count] $line";
}
#close files, though this is actually not necessary
#in this case
close (SOURCE);
close (RESULT);