« CLIPSeqTools/App/distribution_on_introns_exons.pm
↑ Summary
CLIPSeqTools/PreprocessApp/annotate_with_file.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::PreprocessApp::sam_to_sqlite - Load a SAM file in an SQLite database. | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | clipseqtools-preprocess sam_to_sqlite [options/parameters] | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Store alignments from a SAM file into and SQLite database. If SAM tag XC:i exists it will be used as the copy number of the record. | ||
| 12 | |||
| 13 | =head1 OPTIONS | ||
| 14 | |||
| 15 | Input options. | ||
| 16 | --sam_file <Str> sam file to be stored in database. If not | ||
| 17 | specified STDIN is used. | ||
| 18 | --records_class <Str> type of records stored in SAM file. [Default: | ||
| 19 | GenOOx::Data::File::SAMstar::Record] | ||
| 20 | |||
| 21 | Database options. | ||
| 22 | --database <Str> database name or path. Will be created. | ||
| 23 | --table <Str> database table. Will be created. | ||
| 24 | --drop <Str> drop table if it already exists. | ||
| 25 | |||
| 26 | Other options. | ||
| 27 | -v --verbose print progress lines and extra information. | ||
| 28 | -h -? --usage --help print help message | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | package CLIPSeqTools::PreprocessApp::sam_to_sqlite; | ||
| 33 | $CLIPSeqTools::PreprocessApp::sam_to_sqlite::VERSION = '1.0.0'; | ||
| 34 | |||
| 35 | # Make it an app command | ||
| 36 | use MooseX::App::Command; | ||
| 37 | extends 'CLIPSeqTools::PreprocessApp'; | ||
| 38 | |||
| 39 | |||
| 40 | ####################################################################### | ||
| 41 | ####################### Load External modules ##################### | ||
| 42 | ####################################################################### | ||
| 43 | use Modern::Perl; | ||
| 44 | use autodie; | ||
| 45 | use namespace::autoclean; | ||
| 46 | use DBI; | ||
| 47 | |||
| 48 | |||
| 49 | ############################################## | ||
| 50 | # Import GenOO | ||
| 51 | use GenOO::Data::File::SAM; | ||
| 52 | |||
| 53 | |||
| 54 | ####################################################################### | ||
| 55 | ####################### Command line options ###################### | ||
| 56 | ####################################################################### | ||
| 57 | option 'sam_file' => ( | ||
| 58 | is => 'rw', | ||
| 59 | isa => 'Str', | ||
| 60 | documentation => 'sam file to be stored in database. If not specified STDIN is used.', | ||
| 61 | ); | ||
| 62 | |||
| 63 | option 'records_class' => ( | ||
| 64 | is => 'rw', | ||
| 65 | isa => 'Str', | ||
| 66 | default => 'GenOOx::Data::File::SAMstar::Record', | ||
| 67 | documentation => 'type of records stored in SAM file.', | ||
| 68 | ); | ||
| 69 | |||
| 70 | option 'database' => ( | ||
| 71 | is => 'rw', | ||
| 72 | isa => 'Str', | ||
| 73 | required => 1, | ||
| 74 | documentation => 'database name or path. Will be created.', | ||
| 75 | ); | ||
| 76 | |||
| 77 | option 'table' => ( | ||
| 78 | is => 'rw', | ||
| 79 | isa => 'Str', | ||
| 80 | required => 1, | ||
| 81 | documentation => 'database table. Will be created.', | ||
| 82 | ); | ||
| 83 | |||
| 84 | option 'drop' => ( | ||
| 85 | is => 'rw', | ||
| 86 | isa => 'Bool', | ||
| 87 | documentation => 'drop table if it already exists.', | ||
| 88 | ); | ||
| 89 | |||
| 90 | |||
| 91 | ####################################################################### | ||
| 92 | ######################## Interface Methods ######################## | ||
| 93 | ####################################################################### | ||
| 94 | sub validate_args {} | ||
| 95 | |||
| 96 | sub run { | ||
| 97 | my ($self) = @_; | ||
| 98 | |||
| 99 | warn "Validating arguments\n" if $self->verbose; | ||
| 100 | $self->validate_args(); | ||
| 101 | |||
| 102 | # Load required classes | ||
| 103 | eval 'require ' . $self->records_class; | ||
| 104 | |||
| 105 | warn "Reading SAM\n" if $self->verbose; | ||
| 106 | my $sam = GenOO::Data::File::SAM->new( | ||
| 107 | file => $self->sam_file, | ||
| 108 | records_class => $self->records_class, | ||
| 109 | ); | ||
| 110 | |||
| 111 | warn "Connecting to the database\n" if $self->verbose; | ||
| 112 | my $dbh = DBI->connect('dbi:SQLite:database=' . $self->database) or die "Can't connect to database: $DBI::errstr\n"; | ||
| 113 | |||
| 114 | if ($self->drop) { | ||
| 115 | warn "Dropping table " . $self->table . "\n" if $self->verbose; | ||
| 116 | $dbh->do( q{DROP TABLE IF EXISTS } . $self->table ); | ||
| 117 | } | ||
| 118 | |||
| 119 | warn "Creating table " . $self->table . "\n" if $self->verbose; | ||
| 120 | { | ||
| 121 | local $dbh->{PrintError} = 0; #temporarily suppress the warning in case table already exists | ||
| 122 | |||
| 123 | $dbh->do( | ||
| 124 | 'CREATE TABLE '.$self->table.' ('. | ||
| 125 | 'id INTEGER PRIMARY KEY AUTOINCREMENT,'. | ||
| 126 | 'strand INT(1) NOT NULL,'. | ||
| 127 | 'rname VARCHAR(250) NOT NULL,'. | ||
| 128 | 'start UNSIGNED INT(10) NOT NULL,'. | ||
| 129 | 'stop UNSIGNED INT(10) NOT NULL,'. | ||
| 130 | 'copy_number UNSIGNED INT(6) NOT NULL DEFAULT 1,'. | ||
| 131 | 'sequence VARCHAR(250) NOT NULL,'. | ||
| 132 | 'cigar VARCHAR(250) NOT NULL,'. | ||
| 133 | 'mdz VARCHAR(250),'. | ||
| 134 | 'number_of_mappings UNSIGNED INT(5),'. | ||
| 135 | 'query_length UNSIGNED INT(4) NOT NULL,'. | ||
| 136 | 'alignment_length UNSIGNED INT(5) NOT NULL'. | ||
| 137 | ');' | ||
| 138 | ); | ||
| 139 | |||
| 140 | if ($dbh->err) { | ||
| 141 | die "Error: " . $dbh->errstr . "\n"; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | warn "Loading data to table " . $self->table . "\n" if $self->verbose; | ||
| 147 | $dbh->begin_work; | ||
| 148 | my $insert_statement = $dbh->prepare( | ||
| 149 | q{INSERT INTO } . $self->table . q{ (id, strand, rname, start, stop, copy_number, sequence, cigar, mdz, number_of_mappings, query_length, alignment_length) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)} | ||
| 150 | ); | ||
| 151 | while (my $record = $sam->next_record) { | ||
| 152 | my $copy_number = $record->copy_number; | ||
| 153 | if (defined $record->tag('XC:i')) { | ||
| 154 | $copy_number = $record->tag('XC:i'); | ||
| 155 | } | ||
| 156 | |||
| 157 | $insert_statement->execute(undef,$record->strand, $record->rname, $record->start, $record->stop, $copy_number, $record->query_seq, $record->cigar, $record->mdz, $record->number_of_mappings, $record->query_length, $record->alignment_length); | ||
| 158 | |||
| 159 | if ($sam->records_read_count % 100000 == 0) { | ||
| 160 | $dbh->commit; | ||
| 161 | $dbh->begin_work; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | $dbh->commit; | ||
| 165 | |||
| 166 | warn "Building index on " . $self->table . "\n" if $self->verbose; | ||
| 167 | $dbh->do(q{CREATE INDEX } . $self->table . q{_loc ON } . $self->table .q{ (rname, start);}); | ||
| 168 | |||
| 169 | warn "Disconnecting from the database\n" if $self->verbose; | ||
| 170 | $dbh->disconnect; | ||
| 171 | } | ||
| 172 | |||
| 173 | 1; |