#! /usr/bin/perl -w

##### Program description #######
#
# Title: intro.pl
#
# Author(s): Mattias Ohlsson
#
# Description: 
#  This is a small Perl program, with the purpose
#  of showing things that you can do with the Perl programming
#  language.
#
# List of subroutines:
#  PE();
#
# Overall procedure:
#  N/A
#  
# Usage:
#  ./intro.pl
#
##################################

use Math::Trig;
use IO::Handle;
autoflush STDOUT 1;
use Bio::DB::GenBank;
use strict;

# Declare varaibles
our($ans, $reply, $var);

###########
# Part 1  #
###########
print "\n";
print "Hello, I'm the Perl interpreter\nWhat's your name? ";
$ans = <STDIN>;
chomp $ans;

my $rev = reverse uc $ans;
$reply = "$ans (or backwards: $rev) - You are welcome to this Perl course.\n";

print "\n>>  $reply\n\n";

###########
# Part 2  #
###########
PE();

print "Perl is often used to process and analyze text files. Here is an example!\n\n";

PE();

$var = 'GATCCTCCATATACAACGGTATCTCCACCTCAGGTTTAGATCTCAACAACGGAACCATTGCCGACATGAGACAGTTAGGTATCGTCGAGAGTTACAAGCTAAAACGAGCAGTAGTCAGCTCTGCATCTGAAGCCGCTGAAGTTCTACTAAGGGTGGATAACATCATCCGTGCAAGACCAAGAACCGCCAATAGACAACATATGTAACATATTTAGGATATACCTCGAAAATAATAAACCGCCACACTGTCATTATTATAATTAGAAACAGAACGCAAAAATTATCCACTATATAATTCAAAGACGCGAAAAAAAAAGAACAACGCGTCATAGAACTTTTGGCAATTCGCGTCACAAATAAATTTTGGCAACTTATGTTTCCTCTTCGAGCAGTACTCGAGCCCTGTCTCAAGAATGTAATAATACCCATCGTAGGTATGGTTAAAGATAGCATCTCCACAACCTCAAAGCTCCTTGCCGAGAGTCGCCCTCCTTTGTCGAGTAATTTTCACTTTTCATATGAGAACTTATTTTCTTATTCTTTACTCTCACATCCTGTAGTGATTGACACTGCAACAGCCACCATCACTAGAAGAACAGAACAATTACTTAATAGAAAAATTATATCTTCCTCGAAACGATTTCCTGCTTCCAACATCTACGTATATCAAGAAGCATTCACTTACCATGACACA';

print "This is a small DNA sub-sequence:\n\n",
    $var, "\n\n";

print "How many substrings of A's with length >= 4 is there? Using regexp expressions \
it becomes easy to answer that. The following code can locate substrings of A's \
larger than or equal to 4.\n";

print <<CodeEnd;

\$count = 0;
while ( \$var =~ /A{4,}/g ) {
    \$count += 1;
}
print "Found \$count occurences\\n";
CodeEnd
    ;

print "\nPress Enter to run this code.\n";
$ans = <STDIN>;
my $count = 0;
while ( $var =~ /A{4,}/g ) {
    $count += 1;
}
print "Found $count occurences!\n\n";

###########
# Part 3  #
###########
PE();
print "Here is another example\n\n";

print <<ExEnd;
ID       M1      M2   Color
A12      34.2    4    Red
B232     6.5     6    Red
C342     -34.6   0    Blue
D1       2.4     10   Green
V34      5.6     4    Green
R343     7.6     66   Magenta
F454     -0.34   4    Red
F452     -4.5    10   Blue
G454     6.4     7    Black
GHT1     30.5    9    White
R343     4.5     10   Green
H565     6.5     20   Green
ExEnd
    ;

$var = "
ID       M1      M2   Color
A12      34.2    4    Red
B232     6.5     6    Red
C342     -34.6   0    Blue
D1       2.4     10   Green
V34      5.6     4    Green
R343     7.6     66   Magenta
F454     -0.34   4    Red
F452     -4.5    10   Blue
G454     6.4     7    Black
GHT1     30.5    9    White
R343     4.5     10   Green
H565     6.5     20   Green\n";

print "\fTask: Find ID's that are Green and have M2 >= 10.\n",
    "The following code will do that!\n\n";

print <<CodeEnd2;
\@var2 = split "\\n", \$var;
for ( \$i = 1; \$i < scalar \@var2; \$i++ ) {
    \@cols = split ' ', \$var2[\$i];

    if ( \$cols[3] eq 'Green' && \$cols[2] >= 10 ) {
	print "\$cols[0]\\n";
    }
}
CodeEnd2
    ;

print "\nPress Enter to run this code.\n";
$ans = <STDIN>;
my @var2 = split "\n", $var;
for ( my $i = 1; $i < scalar @var2; $i++ ) {
    my @cols = split ' ', $var2[$i];

    if ( $cols[3] eq 'Green' && $cols[2] >= 10 ) {
	print "$cols[0]\n";
    }
}
print "\n";


###########
# Part 4  #
###########
PE();
print "One can of course also use Perl for calculations. Common mathematical \
operations are available.\n\n";

PE();

print "Let's wait a few seconds, then I will show you some examples!\n";
my $tmp = '#####';
for ( my $i = 1; $i <= 3; $i++ ) { 
    sleep 1;
    print "$tmp\r";
    $tmp = $tmp . '#####';
}
print "\n\n";

print "x\tsin(x)\tcos(x)\ttanh(x)\texp(x)\n";
for ( my $i = 1; $i <= 10; $i++ ) { 
    my $x = pi * $i / 10;
    printf "%5.2f\t%5.2f\t%5.2f\t%5.2f\t%5.2f\n", 
    $x, sin($x), cos($x), tanh($x), exp($x);
}
print "\n";

###########
# Part 5  #
###########
PE();

my $prog = 'xaos';
print "\nPerl can also run external programs.\n\n";
print "Press Enter to run the $prog program.\n"; 
$ans = <STDIN>;
system "$prog >& /dev/null &";

###########
# Part 6  #
###########
PE();

print "\nThere are a lot of so called modules for Perl, including modules for \
Bioinformatics. \n\
The BioPerl project (www.bioperl.org) has a lot of usable Perl code.\n\n";

PE();

print "Lets download a DNA sequence from NCBI's GenBank!\n\n";

print "Give an accession number for a particular gene \n
E.g.: U49845, AF303112, AF369032 \n
Your choice? ";
$ans = <STDIN>;
chomp $ans;

print "Downloading $ans ...\n";
my $gb = new Bio::DB::GenBank();
my $seq1 = $gb->get_Seq_by_acc($ans);


my $id = $seq1->display_id();
my $seq = $seq1->seq();
my $desc = $seq1->desc();

print "You have just downloaded the following gene\n\n";
print "ID         : $id\n";
print "Beskrivning: $desc\n";

print "\nPress Enter to show the sequence!\n";
$ans = <STDIN>;

print "$seq\n\n";

print "That's all\n";



## Here follows subroutines

sub PE {

   print "Press Enter to continue\n";
   $ans = <STDIN>;

}
