Skip to content

Instantly share code, notes, and snippets.

@cjfields
Created August 4, 2010 04:42
Show Gist options
  • Select an option

  • Save cjfields/507650 to your computer and use it in GitHub Desktop.

Select an option

Save cjfields/507650 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Bio::DB::EUtilities;
use Bio::SeqIO;
use Bio::Seq::SeqBuilder;
#genbank file to clean up
my $file = 'temp_hold.gb';
#array to hold all records that are not suppressed
my @clean_array = ();
#array to hold all gi's from the file to clean up
my @original_array = ();
#file to print all suppressed gi's to
open( my $myfile, '>', 'removed_gis.txt' ) || die "Can't open file:$!";
#file to print all not repressed records to
open( my $out, '>', 'output.txt' ) || die "Can't open file:$!";
#parse through the genbank file and put all gi's into an array(@original_array)
my $in = Bio::SeqIO->new( -file => $file, -format => "genbank" );
my $builder = $in->sequence_builder();
$builder->want_none();
$builder->add_wanted_slot('primary_id');
while ( my $seq = $in->next_seq ) {
my $current_gi = $seq->primary_id;
push( @original_array, $current_gi );
}
print "finished first part, parse or gb file\n";
#initial number of records to post
my $number = scalar(@original_array);
print "Initial number of records of array before sub dividing " . $number
. "\n";
#starting and ending index of which records to post from the original array
my $min = 0;
my $max = scalar(@original_array); # change to whatever max number
my $step = $max - $min;
#subarray to hold the segment of records specified by min and max
my @subarray;
#cumulaitve count to keep track of if you are at the end of the original amount of records.
my $cumulativeCount;
#while loop to post and retreive chunks of the original array
while ( $min < $number ) {
if ( $max < $number ) {
print "start and end subarray index positions: $min $max\n";
@subarray = @original_array[ $min ... $max ];
}
else {
@subarray = @original_array[ $min ... ( $number - 1 ) ];
print "end of array last post, index positions: $min "
. ( $number - 1 ) . "\n";
}
print "subarray had this many elements " . scalar(@subarray) . "\n";
my $factory = Bio::DB::EUtilities->new(
-eutil => 'esummary',
-email => 'pat.boutet@gmail.com',
-db => 'protein',
-id => \@subarray,
);
#increment the sub index pointers
$min = ( $max + 1 );
$max += $step;
#get number of records posted and add it to the cumulative post count
my $record_count = scalar(@subarray);
$cumulativeCount += $record_count;
print
"beginning of second part, going through each summary record and testing\n";
while ( my $ds = $factory->next_DocSum ) {
my $gi_id = $ds->get_id;
# flattened mode
while ( my $item = $ds->next_Item('flattened') ) {
# not all Items have content, so need to check...
if ( $item->get_content ) {
my $temp_name = $item->get_name;
my $temp_content = $item->get_content;
if ( ( $temp_name eq "Status" )
&& ( $temp_content ne "suppressed" ) )
{
push( @clean_array, $gi_id );
}
elsif ( $temp_name eq "Status" ) {
print $myfile "$gi_id, $temp_name, $temp_content\n";
}
}
}
}
print "finished fourth part\n";
}
foreach (@clean_array) {
print $out "$_\n";
}
close $myfile;
close $out;
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment