PFB2013Review Session Code | PFB2013 | Programming for Biology @ CSHL Programming for Biology @ CSHL Fri, 01 Nov 2013 18:09:32 +0000 en-US hourly 1 http://wordpress.org/?v=3.6.1 Review Code: Tab-delimited parsing review-code-tab-delimited-parsing/ review-code-tab-delimited-parsing/#comments Thu, 17 Oct 2013 02:18:11 +0000 Steven Ahrendt ?p=654 read more)]]> Input file for this code:
test_data

#!/usr/bin/perl
# Takes a three-column, tab-delimited file and creates two hashes.
# The keys for both hashes will be the items in the first column,
# and the values will be the second and third column
###############################################################
use warnings;
use strict;
 
my $infile = shift @ARGV;
my %institute; # Hash for the 2nd column data
my %mutation;  # Hash for the 3rd column data
 
open(IN,'<', $infile) or die "Can't open file $infile: $!\n";
while(my $line = <IN>)
{
  chomp $line;
 
  # The following line checks for a header line,
  #   which in this case begins with "Sample"
  # Header lines can start with anything though,
  #   so it's really important to verify your input data first
  next if($line =~ /Sample/);
 
  # The split() function returns an array, so this next line
  #   puts each item in the returned array into three variables
  my ($sample, $inst, $mut) = split(/\t/,$line);
 
  # Then just assign the values to their respective hashes
  $institute{$sample} = $inst;
  $mutation{$sample} = $mut;
}
close(IN);
 
##Output
# Print columns in reversed order
print "Mut\tInst\tSample\n"; # print out a header
foreach my $key (sort keys %institute)
{
  my $ins = $institute{$key}; 
  my $mu = $mutation{$key};
  print "$mu\t$ins\t$key\n";
}
]]>
review-code-tab-delimited-parsing/feed/ 0
Review Session Scripts Perl II & III review-session-scripts-perl-ii-iii/ review-session-scripts-perl-ii-iii/#comments Wed, 16 Oct 2013 01:18:27 +0000 Sofia Robb ?p=558 read more)]]>
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";
]]>
			review-session-scripts-perl-ii-iii/feed/
		0