Last active
August 29, 2015 14:01
-
-
Save standage/4cd763b3192c67f0e43d to your computer and use it in GitHub Desktop.
Simulate exon skipping events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2014, Daniel S. Standage <daniel.standage@gmail.com> | |
Permission to use, copy, modify, and/or distribute this software for any | |
purpose with or without fee is hereby granted, provided that the above | |
copyright notice and this permission notice appear in all copies. | |
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
If GenomeTools and AEGeAn are installed, compile like so. | |
gcc -Wall -I /usr/local/include/genometools -I /usr/local/include/aegean \ | |
-DWITHOUT_CAIRO -o skexon skexon.c -lgenometools -laegean | |
*/ | |
#include <getopt.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include "aegean.h" | |
#include "genometools.h" | |
//------------------------------------------------------------------------------ | |
// Begin 'SkipExonStream' class | |
//------------------------------------------------------------------------------ | |
// Data structure | |
typedef struct | |
{ | |
const GtNodeStream parent_instance; | |
GtNodeStream *in_stream; | |
GtUword minexons; | |
} SkipExonStream; | |
// Method prototypes | |
static const GtNodeStreamClass* skip_exon_stream_class(void); | |
#define skip_exon_stream_cast(GS)\ | |
gt_node_stream_cast(skip_exon_stream_class(), GS) | |
static void skip_exon_stream_free(GtNodeStream *ns); | |
GtNodeStream* skip_exon_stream_new(GtNodeStream *in_stream, GtUword minexons); | |
static int skip_exon_stream_next(GtNodeStream *ns, GtGenomeNode **gn, | |
GtError *error); | |
// Method implementations | |
static const GtNodeStreamClass *skip_exon_stream_class(void) | |
{ | |
static const GtNodeStreamClass *nsc = NULL; | |
if(!nsc) | |
{ | |
nsc = gt_node_stream_class_new(sizeof (SkipExonStream), | |
skip_exon_stream_free, | |
skip_exon_stream_next); | |
} | |
return nsc; | |
} | |
static void skip_exon_stream_free(GtNodeStream *ns) | |
{ | |
SkipExonStream *stream = skip_exon_stream_cast(ns); | |
gt_node_stream_delete(stream->in_stream); | |
} | |
GtNodeStream* skip_exon_stream_new(GtNodeStream *in_stream, GtUword minexons) | |
{ | |
GtNodeStream *ns; | |
SkipExonStream *stream; | |
gt_assert(in_stream); | |
ns = gt_node_stream_create(skip_exon_stream_class(), false); | |
stream = skip_exon_stream_cast(ns); | |
stream->in_stream = gt_node_stream_ref(in_stream); | |
stream->minexons = minexons; | |
return ns; | |
} | |
static int skip_exon_stream_next(GtNodeStream *ns, GtGenomeNode **gn, | |
GtError *error) | |
{ | |
SkipExonStream *stream; | |
GtFeatureNode *fn; | |
int had_err; | |
gt_error_check(error); | |
stream = skip_exon_stream_cast(ns); | |
while(1) | |
{ | |
GtArray *cds_frags, *exons; | |
GtFeatureNode *cds; | |
GtRange cdsrange; | |
int random; | |
had_err = gt_node_stream_next(stream->in_stream, gn, error); | |
if(had_err) | |
return had_err; | |
if(!*gn) | |
return 0; | |
fn = gt_feature_node_try_cast(*gn); | |
if(!fn) | |
return 0; | |
gt_assert(gt_feature_node_has_type(fn, "mRNA")); | |
cds_frags = agn_typecheck_select(fn, agn_typecheck_cds); | |
if(gt_array_size(cds_frags) < stream->minexons) | |
{ | |
gt_genome_node_delete(*gn); | |
continue; | |
} | |
// We don't want the initial exon, thus the '1 +' at the beginning, | |
// and we don't want the terminal exon, thus the '- 1' at the end. | |
random = 1 + rand() % (gt_array_size(cds_frags) - 1 - 1); | |
cds = *(GtFeatureNode **)gt_array_get(cds_frags, random); | |
cdsrange = gt_genome_node_get_range((GtGenomeNode *)cds); | |
if(gt_feature_node_is_multi(cds)) | |
gt_assert(gt_feature_node_get_multi_representative(cds) != cds); | |
gt_feature_node_remove_leaf(fn, cds); | |
exons = agn_typecheck_select(fn, agn_typecheck_exon); | |
while(gt_array_size(exons) > 0) | |
{ | |
GtFeatureNode *exon = *(GtFeatureNode **)gt_array_pop(exons); | |
GtRange exonrange = gt_genome_node_get_range((GtGenomeNode *)exon); | |
if(gt_range_compare(&cdsrange, &exonrange) == 0) | |
{ | |
gt_feature_node_remove_leaf(fn, exon); | |
break; | |
} | |
} | |
gt_array_delete(exons); | |
gt_array_delete(cds_frags); | |
return 0; | |
} | |
} | |
//------------------------------------------------------------------------------ | |
// End 'SkipExonStream' class | |
//------------------------------------------------------------------------------ | |
void print_usage(FILE *outstream) | |
{ | |
fprintf(outstream, | |
"\nskexon: simulate exon skipping events; input is a GFF3 file with gene\n" | |
" annotations; output is a modified GFF3 file containing mRNAs with 1\n" | |
" internal coding exon removed at random\n\n" | |
"Usage: skexon [options] annot.gff3\n" | |
" Options:\n" | |
" -h|--help print this help message and exit\n" | |
" -m|--min: INT minimum number of exons required to be considered for\n" | |
" simulated exon skipping; default is 3\n\n"); | |
} | |
int main(int argc, const char **argv) | |
{ | |
GtUword minexons = 3; | |
int opt = 0; | |
int optindex = 0; | |
const char *optstr = "hm:"; | |
const struct option prg_options[] = | |
{ | |
{ "help", no_argument, NULL, 'h' }, | |
{ "min", required_argument, NULL, 'm' }, | |
}; | |
for(opt = getopt_long(argc, (char **)argv, optstr, prg_options, &optindex); | |
opt != -1; | |
opt = getopt_long(argc, (char **)argv, optstr, prg_options, &optindex)) | |
{ | |
switch(opt) | |
{ | |
case 'h': | |
print_usage(stdout); | |
return 0; | |
break; | |
case 'm': | |
minexons = atol(optarg); | |
break; | |
default: | |
fprintf(stderr, "error: unknown option '%c'\n", opt); | |
print_usage(stderr); | |
return 1; | |
} | |
} | |
if(argc - optind < 1) | |
{ | |
fprintf(stderr, "error: no input files provided; use - for stdin\n"); | |
print_usage(stderr); | |
return 1; | |
} | |
gt_lib_init(); | |
srand(time(NULL)); | |
GtNodeStream *current_stream, *last_stream; | |
GtQueue *streams = gt_queue_new(); | |
current_stream = gt_gff3_in_stream_new_unsorted(argc - optind, argv + optind); | |
gt_queue_add(streams, current_stream); | |
last_stream = current_stream; | |
GtHashmap *typestokeep = gt_hashmap_new(GT_HASH_STRING, NULL, NULL); | |
gt_hashmap_add(typestokeep, "mRNA", "mRNA"); | |
current_stream = agn_filter_stream_new(last_stream, typestokeep); | |
gt_queue_add(streams, current_stream); | |
last_stream = current_stream; | |
gt_hashmap_delete(typestokeep); | |
current_stream = skip_exon_stream_new(last_stream, minexons); | |
gt_queue_add(streams, current_stream); | |
last_stream = current_stream; | |
current_stream = gt_gff3_out_stream_new(last_stream, NULL); | |
gt_gff3_out_stream_retain_id_attributes((GtGFF3OutStream *)current_stream); | |
gt_queue_add(streams, current_stream); | |
last_stream = current_stream; | |
GtError *error = gt_error_new(); | |
int result = gt_node_stream_pull(last_stream, error); | |
if(result == -1) | |
fprintf(stderr, "[skexon] error: %s", gt_error_get(error)); | |
gt_error_delete(error); | |
while(gt_queue_size(streams) > 0) | |
{ | |
current_stream = gt_queue_get(streams); | |
gt_node_stream_delete(current_stream); | |
} | |
gt_queue_delete(streams); | |
gt_lib_clean(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment