#!/usr/local/bin/perl

#######################
#                     #
#  @rrays and %ashes  #
#                     #
#######################

#first, I would like to pass a name of the file to my perl program
#I want to make sure that the program does not proceed unless it has 
#the name of the file, since what I do further in the program does not make sense
#without this file

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

#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.

$filename=$ARGV[0];

#We will discuss input/output later, but it is very difficult to talk about arrays 
#and hashes 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)

@gi=(); #initialized an array where I will store GI numbers

while(defined($line=<IN>)){

	chomp($line);
	push(@gi,$line);

}
close(IN);


#how many gi numbers did I read in?
$number_gis=@gi;
print "I read in $number_gis GI numbers\n";

$number_gis=$#gi+1;
print "I read in $number_gis GI numbers\n";


#with list of GI numbers I could now send a request to NCBI to return me sequences
#corresponding to these GI numbers

#Now, suppose that not all GI numbers in my file are unique, i.e. the same GIs 
#occur more than once in the list. This often happens when GIs from several
#BLAST searches are combined.
#So, we want to get rid of duplicates.

%gi_hash=();
foreach $i (@gi){

	if(!(exists $gi_hash{$i})){
	 	$gi_hash{$i}=1;
	}
} 

#challenge #1
#HOW MANY DUPLICATES PER GI NUMBER?

#################################
#       YOUR CODE GOES HERE
#################################


#challenge #2 for you
#count how many unique GI numbers are in the list, i.e. count number of elements 
#in our %gi_hash hash.

#################################
#       YOUR CODE GOES HERE
#################################

# you can test your code by running the script 
# (both script and datafile are available on the class web page)

#one very useful function -- split function
#Example: split DNA sequence into an array of nucleotide bases
$seq="ATCGATGCGCGCGAAAA";
@bases=split(//,$seq);
$length=@bases;
print "length of $seq is $length bases\n"; 

print "First codon positions only:\n";	
for($i=0;$i<@bases;$i++){
	#print only first codon position (remember that counting of array starts with 0!)
 	if($i%3==0){
		print $bases[$i];
	} 
}
print "\n";



#HOMEWORK ASSIGNMENTS
#1.Read Chapter 4
#2. Create a program that determines GC content of a sequence. 
#For now you can assume that the sequence is a constant given in your program
#e.g. $seq="AAAAAAACCCTTTTTTGCGCGCGCTTATATATAGGG";
#We will improve the program as we learn more, so it can read any sequence from a file.
#You can use control structures from Chapter 4 

#There are more than one way to implement this program
#Hint: proper use of hashes might make your program very short

#3. two challenges mentioned above. 




