~ubuntu-branches/ubuntu/trusty/bioperl/trusty

« back to all changes in this revision

Viewing changes to scripts/seqstats/gccalc.PLS

  • Committer: Package Import Robot
  • Author(s): Charles Plessy
  • Date: 2013-09-22 13:39:48 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20130922133948-c6z62zegjyp7ztou
Tags: 1.6.922-1
* New upstream release.
* Replaces and Breaks grinder (<< 0.5.3-3~) because of overlaping contents.
  Closes: #722910
* Stop Replacing and Breaking bioperl ( << 1.6.9 ): not needed anymore. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!perl -w
2
 
 
3
 
use strict;
4
 
 
5
 
use Bio::SeqIO;
6
 
use Bio::Tools::SeqStats;
7
 
use Getopt::Long;
8
 
my $format = 'fasta';
9
 
my $file;
10
 
my $help =0;
11
 
my $aggregate;
12
 
GetOptions(
13
 
            'f|format:s' => \$format,
14
 
            'i|in:s'     => \$file,
15
 
            'h|help|?'    => \$help,
16
 
            'a|aggregate!'=> \$aggregate,
17
 
            );
18
 
 
19
 
 
20
 
my $USAGE = "usage: gccalc.pl -f format -i filename\n";
21
 
if( $help ) {
22
 
    die $USAGE;
23
 
}
24
 
 
25
 
$file = shift unless $file;
26
 
my $seqin;
27
 
if( defined $file ) {
28
 
    print "Could not open file [$file]\n$USAGE" and exit unless -e $file;
29
 
    $seqin = new Bio::SeqIO(-format => $format,
30
 
                            -file   => $file);
31
 
} else {
32
 
    $seqin = new Bio::SeqIO(-format => $format,
33
 
                            -fh     => \*STDIN);
34
 
}
35
 
my ($total_base, $total_gc);
36
 
while( my $seq = $seqin->next_seq ) {
37
 
    next if( $seq->length == 0 );
38
 
    if( $seq->alphabet eq 'protein' ) {
39
 
        warn("gccalc does not work on amino acid sequences ...skipping this seq");
40
 
        next;
41
 
    }
42
 
 
43
 
    my $seq_stats  =  Bio::Tools::SeqStats->new('-seq'=>$seq);
44
 
    my $hash_ref = $seq_stats->count_monomers();  # for DNA sequence
45
 
    print "Seq: ", $seq->display_id, " ";
46
 
    print $seq->desc if $seq->desc;
47
 
    print " Len:", $seq->length, "\n";
48
 
    $total_base += $seq->length;
49
 
    $total_gc   += $hash_ref->{'G'} + $hash_ref->{'C'};
50
 
    printf "GC content is %.4f\n", ($hash_ref->{'G'} + $hash_ref->{'C'}) /
51
 
        $seq->length();
52
 
 
53
 
    foreach my $base (sort keys %{$hash_ref}) {
54
 
        print "Number of bases of type ", $base, "= ", $hash_ref->{$base},"\n";
55
 
    }
56
 
    print "--\n";
57
 
}
58
 
if( $aggregate ) {
59
 
  printf "Total GC content is %.4f out of %d bases\n", $total_gc / $total_base, $total_base;
60
 
}
61
 
# alternatively one could use code submitted by
62
 
# cckim@stanford.edu
63
 
 
64
 
sub calcgc {
65
 
    my $seq = $_[0];
66
 
    my @seqarray = split('',$seq);
67
 
    my $count = 0;
68
 
    foreach my $base (@seqarray) {
69
 
        $count++ if $base =~ /[G|C]/i;
70
 
    }
71
 
 
72
 
    my $len = $#seqarray+1;
73
 
    return $count / $len;
74
 
}
75
 
 
76
 
 
77
 
__END__
78
 
 
79
 
=head1 NAME
80
 
 
81
 
gccalc - GC content of nucleotide sequences
82
 
 
83
 
=head1 SYNOPSIS
84
 
 
85
 
  gccalc [-f/--format FORMAT] [-h/--help] filename
86
 
  or
87
 
  gccalc [-f/--format FORMAT] < filename
88
 
  or
89
 
  gccalc [-f/--format FORMAT] -i filename
90
 
 
91
 
=head1 DESCRIPTION
92
 
 
93
 
This scripts prints out the GC content for every nucleotide sequence
94
 
from the input file.
95
 
 
96
 
=head1 OPTIONS
97
 
 
98
 
The default sequence format is fasta.
99
 
 
100
 
The sequence input can be provided using any of the three methods:
101
 
 
102
 
=over 3
103
 
 
104
 
=item unnamed argument
105
 
 
106
 
  gccalc filename
107
 
 
108
 
=item named argument
109
 
 
110
 
  gccalc -i filename
111
 
 
112
 
=item standard input
113
 
 
114
 
  gccalc < filename
115
 
 
116
 
=back
117
 
 
118
 
=head1 FEEDBACK
119
 
 
120
 
=head2 Mailing Lists
121
 
 
122
 
User feedback is an integral part of the evolution of this and other
123
 
Bioperl modules. Send your comments and suggestions preferably to
124
 
the Bioperl mailing list.  Your participation is much appreciated.
125
 
 
126
 
  bioperl-l@bioperl.org                  - General discussion
127
 
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists
128
 
 
129
 
=head2 Reporting Bugs
130
 
 
131
 
Report bugs to the Bioperl bug tracking system to help us keep track
132
 
of the bugs and their resolution. Bug reports can be submitted via the
133
 
web:
134
 
 
135
 
  https://redmine.open-bio.org/projects/bioperl/
136
 
 
137
 
=head1 AUTHOR - Jason Stajich
138
 
 
139
 
Email jason@bioperl.org
140
 
 
141
 
=head1 HISTORY
142
 
 
143
 
Based on script code (see bottom) submitted by cckim@stanford.edu
144
 
 
145
 
Submitted as part of bioperl script project 2001/08/06
146
 
 
147
 
=cut