Review Session Scripts Perl II & III

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/perl                                                                                      
#Perl_3-5.pl                                                                                       
#finds occurrences of Nobody and somebody within text using index                                                                       
 
use strict;
use warnings;
 
my $nobody_infile = 'perl_III_nobody.txt';
my $nobody_outfile = 'Nobody.txt';
my $somebody_outfile = 'Somebody.txt';
 
open (IN, '<', $nobody_infile) or die "Cannot open infile: $!\n";
open (OUT1, '>', $nobody_outfile) or die "Cannot open outfile1: $!\n";
open (OUT2, '>', $somebody_outfile) or die "Cannot write to outfile2: $!\n";
 
 
while (my $line = <IN>){
  my $substr1 = 'Nobody';
  my $substr2 = 'somebody';
 
  my $position_nobody = index ($line, $substr1);
  my $position_somebody = index ($line, $substr2);
  if ($position_somebody > -1){
    warn ("somebody is here");
  }
 
 
print OUT1 $position_nobody,"\n";
print OUT2 $position_somebody,"\n";
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
 
#!/usr/bin/perl -w                                                                                           
#Perl_3-3.pl
#Find reverse complement sequence
 
use strict;
 
my $infile = 'perl_III.fasta';
my $outfile = 'fasta.out';
 
open IN, "<", $infile or die "Cannot read infile: $!\n";
open OUT, '>', $outfile or die "Cannot write to outfile $!\n";
 
 
while (my $line = <IN>){
  chomp $line;
  my $header = $line;
  my $sequence = <IN>;
  chomp $sequence;
  my $reverse_sequence = reverse ($sequence);
  my $complement = $reverse_sequence;
  $complement =~ tr/ACGTacgt/TGCAtgca/;
 
  print OUT "$header-reverse_complement\n$complement\n";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
#!/usr/bin/perl -w                                                                                           
#Perl_3-4.pl
#Find character and line totals within fasta file
 
use strict;
 
my $infile = 'perl_III.fastq';
my $outfile = 'fastq.out';
 
open IN, "<", $infile or die "Cannot read infile: $!\n";
open OUT, '>', $outfile or die "Cannot write to outfile $!\n";
 
my $number_of_lines = 0;
my $total_length = 0;
 
while (my $line = <IN>){
  chomp $line;
  $number_of_lines++;
  my $length = length ($line);
  $total_length += $length;
}
 
my $average_line_length = $total_length/$number_of_lines;
 
print OUT "Total number of lines = $number_of_lines\nAverage line length = $average_line_length\nTotal line length or number of characters = $total_length\n";
			

Comments are closed.