#!/usr/bin/perl

######################
#                    #
#  @rrays and input  #
#                    #
######################

# Create a program that reads in a sequence stored in a file handed to the program on the command line and 
# determines GC content of a sequence. 
#

unless(@ARGV==1) {die "please provide name of the file in the command line!!\n";}

#@ARGV refers to whatever is handed to the program in the command line, i.e. 
# the array containing the command-line arguments intended for the script.
#in the line above array @ARGV is used in a scalar context, returning the length of array
#unless the length of array is exactly 1, the program "dies", i.e. prints 
#the message and exits.


my$filename=$ARGV[0];  # in this case it starts at the first (and in this case only) element is in slot 0

#We will discuss input/output later, but it is very difficult to talk about arrays 
# w/o reasonably large dataset.
#the file I will read in contains a list of GI numbers

open(IN, "< $filename") or die "cannot open $filename:$!";

#again, the program will exit, if it cannot open the file 
#(good against typos in the filename)

my$seq=''; #initialized a string where I will store nucleotides
my$line='';

while(defined($line=<IN>)){
if ($line =~m/^>/) 
		{$header=$line;
		print "\nthe analyzed sequence  has the following comment line:\n$header \n\n"}
else 
	{chomp($line);
	$seq=$seq.$line}

}

@bases=split(//,$seq); # split searches for the pattern between the two // and 
# splits the string $seq at this pattern, the results of the split are then stored in @bases
# you can use any "regular" expession as pattern. Useful is /\s+/, which would split the string at any "white spaces" 
  
$length=@bases;
print "length of $seq is $length bases\n"; 

print "\n";

my$A=$T=$G=$C=0; #reset counters
foreach (@bases) {  # there is a much simpler way to do this via hashes ...
	if ($_ =~ A) {$A++};
	if ($_ =~ T) {$T++};
	if ($_ =~ G) {$G++};	
	if ($_ =~ C) {$C++};}
$GCpercent= 100*(($G+$C)/($G+$C+$A+$T));
print "GCcontent of sequence is $GCpercent percent\n";
	
	
# HOMEWORK ASSIGNMENTS
#For the following array declaration  
@myArray = ('AA', 'B', 'C', 'D', 'E'); 
#what is the value of the following expressions:
print $#myArray;
print "\n";
print length(@myArray); 
print "\n";
print $myArray[1];
print "\n";
$n=@myArray;
print $n; 
print "\n";
print reverse (@myArray);
print "\n";


# Create a program that reads in a sequence stored in a file handed to the program on the command line and 
# determines GC content of a sequence. 

# There are more than one way to implement this program
# One approach is to get the whole sequence into one array, 
# another would be to have a loop acting on every line

# if you are up for a challenge, modify the program to print the numbers of Gs and Cs 
# in a rolling window of variabe size (100 might be a good starting point?




