<?xml version='1.0'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:atom="http://www.w3.org/2005/Atom" >
<channel>
	<title><![CDATA[BOL: Count GC Content in nucleotide sequence with Perl]]></title>
	<link>https://bioinformaticsonline.com/snippets/view/27467/count-gc-content-in-nucleotide-sequence-with-perl?</link>
	<atom:link href="https://bioinformaticsonline.com/snippets/view/27467/count-gc-content-in-nucleotide-sequence-with-perl?" rel="self" type="application/rss+xml" />
	<description><![CDATA[]]></description>
	
	<item>
	<guid isPermaLink="true">https://bioinformaticsonline.com/snippets/view/27467/count-gc-content-in-nucleotide-sequence-with-perl</guid>
	<pubDate>Sat, 21 May 2016 22:56:18 -0500</pubDate>
	<link>https://bioinformaticsonline.com/snippets/view/27467/count-gc-content-in-nucleotide-sequence-with-perl</link>
	<title><![CDATA[Count GC Content in nucleotide sequence with Perl]]></title>
	<description><![CDATA[<code>#!/usr/bin/perl -w

### Usage: get_gc_content.pl &lt;fasta file&gt;                                                        ###

#---------------------------------------------------------------------------------------------------------------------------
#Deal with passed parameters
#---------------------------------------------------------------------------------------------------------------------------
if ($#ARGV == -1) {
    usage();
    exit;
}
$fasta_file = $ARGV[0];
$out_file = &quot;gc_out.txt&quot;;
unless ( open(IN, &quot;$fasta_file&quot;) ) {    
    print &quot;Got a bad fasta file: $fasta_file\n\n&quot;;
    exit;
}
unless ( open(OUT, &quot;&gt;$out_file&quot;) ) {
    print &quot;Couldn&#039;t create $out_file\n&quot;;
    exit;
}
print &quot;Parameters:\nfasta file = $fasta_file\noutput file = $out_file\n\n&quot;;
#---------------------------------------------------------------------------------------------------------------------------
#The main event
#---------------------------------------------------------------------------------------------------------------------------
print OUT &quot;ID\t% GCContent\tTotal Count\tG Count\tC Count\tA Count\tT Count\n&quot;;
$seq = &quot;&quot;;
while (&lt;IN&gt;) {
    chomp;
    if (/^&gt;/) {
	#finish up previous line.
	if (length($seq) &gt; 0) {
	    &amp;process_it;
	}
	#start new line.
	$id = $_;
	$id =~ s/^&gt;(.+?)\s.+$/$1/g;
	print OUT &quot;$id\t&quot;;
    }
    else {
	$seq = $seq . $_;
    }
}

#finish up last line.
&amp;process_it;

close(IN);
close(OUT);

sub usage {
   $0 get_gc_content.pl &lt;fasta file&gt;    
}

sub process_it {
    @letters = split(//, $seq);
    $gccount = 0;
    $totalcount = 0;
    $acount = 0;
    $tcount = 0;
    $gcount = 0;
    $ccount = 0;
    foreach $i (@letters) {
	if (lc($i) =~ /[a-z]/) {
	    $totalcount++;
	}
	if (lc($i) eq &quot;g&quot; || lc($i) eq &quot;c&quot;) {
	    $gccount++;
	}
	if (lc($i) eq &quot;a&quot;) {
	    $acount++;
	}
	if (lc($i) eq &quot;t&quot;) {
	    $tcount++;
	}
	if (lc($i) eq &quot;g&quot;) {
	    $gcount++;
	}
	if (lc($i) eq &quot;c&quot;) {
	    $ccount++;
	}
    }
    if ($totalcount &gt; 0) {
	$gccontent = (100 * $gccount) / $totalcount;
    }
    else {
	$gccontent = 0;
    }
    print OUT &quot;$gccontent\t$totalcount\t$gcount\t$ccount\t$acount\t$tcount\n&quot;;
    $seq = &quot;&quot;;
}</code>]]></description>
	<dc:creator>Radha Agarkar</dc:creator>
</item>

</channel>
</rss>