Introduktion till Perl

Skapad 3 år sedan, skriven i Perl, med 298 visningar.
URL http://kod.perl.se/view/aab83d79 Bädda in
Ladda hem koden eller visa koden i råformatVisa bredare version
  1. #! /usr/bin/perl -w
  2.  
  3. ##### Program description #######
  4. #
  5. # Title: intro.pl
  6. #
  7. # Author(s): Mattias Ohlsson
  8. #
  9. # Description:
  10. #  This is a small Perl program, with the purpose
  11. #  of showing things that you can do with the Perl programming
  12. #  language.
  13. #
  14. # List of subroutines:
  15. #  PE();
  16. #
  17. # Overall procedure:
  18. #  N/A
  19. #  
  20. # Usage:
  21. #  ./intro.pl
  22. #
  23. ##################################
  24.  
  25. use Math::Trig;
  26. use IO::Handle;
  27. autoflush STDOUT 1;
  28. use Bio::DB::GenBank;
  29. use strict;
  30.  
  31. # Declare varaibles
  32. our($ans, $reply, $var);
  33.  
  34. ###########
  35. # Part 1  #
  36. ###########
  37. print "\n";
  38. print "Hello, I'm the Perl interpreter\nWhat's your name? ";
  39. $ans = <STDIN>;
  40. chomp $ans;
  41.  
  42. my $rev = reverse uc $ans;
  43. $reply = "$ans (or backwards: $rev) - You are welcome to this Perl course.\n";
  44.  
  45. print "\n>>  $reply\n\n";
  46.  
  47. ###########
  48. # Part 2  #
  49. ###########
  50. PE();
  51.  
  52. print "Perl is often used to process and analyze text files. Here is an example!\n\n";
  53.  
  54. PE();
  55.  
  56. $var = 'GATCCTCCATATACAACGGTATCTCCACCTCAGGTTTAGATCTCAACAACGGAACCATTGCCGACATGAGACAGTTAGGTATCGTCGAGAGTTACAAGCTAAAACGAGCAGTAGTCAGCTCTGCATCTGAAGCCGCTGAAGTTCTACTAAGGGTGGATAACATCATCCGTGCAAGACCAAGAACCGCCAATAGACAACATATGTAACATATTTAGGATATACCTCGAAAATAATAAACCGCCACACTGTCATTATTATAATTAGAAACAGAACGCAAAAATTATCCACTATATAATTCAAAGACGCGAAAAAAAAAGAACAACGCGTCATAGAACTTTTGGCAATTCGCGTCACAAATAAATTTTGGCAACTTATGTTTCCTCTTCGAGCAGTACTCGAGCCCTGTCTCAAGAATGTAATAATACCCATCGTAGGTATGGTTAAAGATAGCATCTCCACAACCTCAAAGCTCCTTGCCGAGAGTCGCCCTCCTTTGTCGAGTAATTTTCACTTTTCATATGAGAACTTATTTTCTTATTCTTTACTCTCACATCCTGTAGTGATTGACACTGCAACAGCCACCATCACTAGAAGAACAGAACAATTACTTAATAGAAAAATTATATCTTCCTCGAAACGATTTCCTGCTTCCAACATCTACGTATATCAAGAAGCATTCACTTACCATGACACA';
  57.  
  58. print "This is a small DNA sub-sequence:\n\n",
  59.     $var, "\n\n";
  60.  
  61. print "How many substrings of A's with length >= 4 is there? Using regexp expressions \
  62. it becomes easy to answer that. The following code can locate substrings of A's \
  63. larger than or equal to 4.\n";
  64.  
  65. print <<CodeEnd;
  66.  
  67. \$count = 0;
  68. while ( \$var =~ /A{4,}/g ) {
  69.     \$count += 1;
  70. }
  71. print "Found \$count occurences\\n";
  72. CodeEnd
  73.     ;
  74.  
  75. print "\nPress Enter to run this code.\n";
  76. $ans = <STDIN>;
  77. my $count = 0;
  78. while ( $var =~ /A{4,}/g ) {
  79.     $count += 1;
  80. }
  81. print "Found $count occurences!\n\n";
  82.  
  83. ###########
  84. # Part 3  #
  85. ###########
  86. PE();
  87. print "Here is another example\n\n";
  88.  
  89. print <<ExEnd;
  90. ID       M1      M2   Color
  91. A12      34.2    4    Red
  92. B232     6.5     6    Red
  93. C342     -34.6   0    Blue
  94. D1       2.4     10   Green
  95. V34      5.6     4    Green
  96. R343     7.6     66   Magenta
  97. F454     -0.34   4    Red
  98. F452     -4.5    10   Blue
  99. G454     6.4     7    Black
  100. GHT1     30.5    9    White
  101. R343     4.5     10   Green
  102. H565     6.5     20   Green
  103. ExEnd
  104.     ;
  105.  
  106. $var = "
  107. ID       M1      M2   Color
  108. A12      34.2    4    Red
  109. B232     6.5     6    Red
  110. C342     -34.6   0    Blue
  111. D1       2.4     10   Green
  112. V34      5.6     4    Green
  113. R343     7.6     66   Magenta
  114. F454     -0.34   4    Red
  115. F452     -4.5    10   Blue
  116. G454     6.4     7    Black
  117. GHT1     30.5    9    White
  118. R343     4.5     10   Green
  119. H565     6.5     20   Green\n";
  120.  
  121. print "\fTask: Find ID's that are Green and have M2 >= 10.\n",
  122.     "The following code will do that!\n\n";
  123.  
  124. print <<CodeEnd2;
  125. \@var2 = split "\\n", \$var;
  126. for ( \$i = 1; \$i < scalar \@var2; \$i++ ) {
  127.     \@cols = split ' ', \$var2[\$i];
  128.  
  129.     if ( \$cols[3] eq 'Green' && \$cols[2] >= 10 ) {
  130.         print "\$cols[0]\\n";
  131.     }
  132. }
  133. CodeEnd2
  134.     ;
  135.  
  136. print "\nPress Enter to run this code.\n";
  137. $ans = <STDIN>;
  138. my @var2 = split "\n", $var;
  139. for ( my $i = 1; $i < scalar @var2; $i++ ) {
  140.     my @cols = split ' ', $var2[$i];
  141.  
  142.     if ( $cols[3] eq 'Green' && $cols[2] >= 10 ) {
  143.         print "$cols[0]\n";
  144.     }
  145. }
  146. print "\n";
  147.  
  148.  
  149. ###########
  150. # Part 4  #
  151. ###########
  152. PE();
  153. print "One can of course also use Perl for calculations. Common mathematical \
  154. operations are available.\n\n";
  155.  
  156. PE();
  157.  
  158. print "Let's wait a few seconds, then I will show you some examples!\n";
  159. my $tmp = '#####';
  160. for ( my $i = 1; $i <= 3; $i++ ) {
  161.     sleep 1;
  162.     print "$tmp\r";
  163.     $tmp = $tmp . '#####';
  164. }
  165. print "\n\n";
  166.  
  167. print "x\tsin(x)\tcos(x)\ttanh(x)\texp(x)\n";
  168. for ( my $i = 1; $i <= 10; $i++ ) {
  169.     my $x = pi * $i / 10;
  170.     printf "%5.2f\t%5.2f\t%5.2f\t%5.2f\t%5.2f\n",
  171.     $x, sin($x), cos($x), tanh($x), exp($x);
  172. }
  173. print "\n";
  174.  
  175. ###########
  176. # Part 5  #
  177. ###########
  178. PE();
  179.  
  180. my $prog = 'xaos';
  181. print "\nPerl can also run external programs.\n\n";
  182. print "Press Enter to run the $prog program.\n";
  183. $ans = <STDIN>;
  184. system "$prog >& /dev/null &";
  185.  
  186. ###########
  187. # Part 6  #
  188. ###########
  189. PE();
  190.  
  191. print "\nThere are a lot of so called modules for Perl, including modules for \
  192. Bioinformatics. \n\
  193. The BioPerl project (www.bioperl.org) has a lot of usable Perl code.\n\n";
  194.  
  195. PE();
  196.  
  197. print "Lets download a DNA sequence from NCBI's GenBank!\n\n";
  198.  
  199. print "Give an accession number for a particular gene \n
  200. E.g.: U49845, AF303112, AF369032 \n
  201. Your choice? ";
  202. $ans = <STDIN>;
  203. chomp $ans;
  204.  
  205. print "Downloading $ans ...\n";
  206. my $gb = new Bio::DB::GenBank();
  207. my $seq1 = $gb->get_Seq_by_acc($ans);
  208.  
  209.  
  210. my $id = $seq1->display_id();
  211. my $seq = $seq1->seq();
  212. my $desc = $seq1->desc();
  213.  
  214. print "You have just downloaded the following gene\n\n";
  215. print "ID         : $id\n";
  216. print "Beskrivning: $desc\n";
  217.  
  218. print "\nPress Enter to show the sequence!\n";
  219. $ans = <STDIN>;
  220.  
  221. print "$seq\n\n";
  222.  
  223. print "That's all\n";
  224.  
  225.  
  226.  
  227. ## Here follows subroutines
  228.  
  229. sub PE {
  230.  
  231.    print "Press Enter to continue\n";
  232.    $ans = <STDIN>;
  233.  
  234. }
  235.  

Svara på "Introduktion till Perl"

Här kan du skriva ett svar till kodsnutten ovan