Model::EntityDefinition* DefParser::nextDefinition() { Token token = m_tokenizer.nextToken(); while (token.type() != Eof && token.type() != ODefinition) token = m_tokenizer.nextToken(); if (token.type() == Eof) return NULL; expect(ODefinition, token); StringList baseClasses; ClassInfo classInfo; token = m_tokenizer.nextToken(); expect(Word, token); classInfo.name = token.data(); token = m_tokenizer.peekToken(); expect(OParenthesis | Newline, token); if (token.type() == OParenthesis) { classInfo.setColor(parseColor()); token = m_tokenizer.peekToken(); expect(OParenthesis | Question, token); if (token.type() == OParenthesis) { classInfo.setSize(parseBounds()); } else { m_tokenizer.nextToken(); } token = m_tokenizer.peekToken(); if (token.type() == Word) { Model::FlagsPropertyDefinition::Ptr spawnflags = parseFlags(); classInfo.properties[spawnflags->name()] = spawnflags; } } expect(Newline, token = m_tokenizer.nextToken()); parseProperties(classInfo.properties, classInfo.models, baseClasses); classInfo.setDescription(parseDescription()); expect(CDefinition, token = m_tokenizer.nextToken()); Model::EntityDefinition* definition = NULL; if (classInfo.hasColor) { ClassInfo::resolveBaseClasses(m_baseClasses, baseClasses, classInfo); if (classInfo.hasSize) { // point definition definition = new Model::PointEntityDefinition(classInfo.name, classInfo.color, classInfo.size, classInfo.description, classInfo.propertyList(), classInfo.models); } else { definition = new Model::BrushEntityDefinition(classInfo.name, classInfo.color, classInfo.description, classInfo.propertyList()); } } else { // base definition m_baseClasses[classInfo.name] = classInfo; definition = nextDefinition(); } return definition; }
Map* parseElements(xmlDocPtr doc, xmlNodePtr cur){ Map* map = initMap(); Node *node; Way* way; Relation *r; Avl* aNode = NULL; Avl* avlWay = NULL; ListNode* ln = NULL; ListWay* wO = NULL; ListWay* wW = NULL; ListWay* wG = NULL; ListWay* wH = NULL; ListWay* wB = NULL; ListWay* wC = NULL; ListRelation* lr= NULL; int flagN = 1; int flagW = 1; cur = cur->xmlChildrenNode; while (cur != NULL) { if (cur->type == XML_ELEMENT_NODE) { if (!xmlStrcmp(cur->name, (const xmlChar *)"bounds")){ map->bounds = parseBounds(cur); } if ((!xmlStrcmp(cur->name, (const xmlChar *)"node"))){ node = parseNode (doc, cur, map->bounds); if(node->name!=NULL){ ln = addRefListNode(node->id,ln); } if(flagN == 1){ init(&aNode,node,NULL); flagN = 0; } else{ insert(&aNode,node,NULL); } } if ((!xmlStrcmp(cur->name, (const xmlChar *)"way"))){ way = parseWay (doc, cur, map->referenceTag); if(way != NULL){ if((way->tag->type) == 0){ wO = addRefListWay(way->id," ", wO); } else if(way->tag->type == 1){ wW = addRefListWay(way->id," ", wW); } else if(way->tag->type == 2){ wG = addRefListWay(way->id," ", wG); } else if(way->tag->type == 3){ wH = addRefListWay(way->id," ", wH); } else if(way->tag->type == 4){ wB = addRefListWay(way->id," ", wB); } else if(way->tag->type == 5){ wC = addRefListWay(way->id," ", wC); } if(flagW == 1){ init(&avlWay,NULL, way); flagW = 0; } else{ insert(&avlWay,NULL,way); } } } if ((!xmlStrcmp(cur->name, (const xmlChar *)"relation"))){ r = parseRelation(doc, cur); if(r != NULL){ lr = addRefListRelation(r,lr); } } } cur = cur->next; } map->avl = aNode; map->avlWay = avlWay; map->nodeOther = ln; map->wayWater = wW; map->wayOther = wO; map->wayGreen = wG; map->wayBuilding = wB; map->wayHighway = wH; map->wayCadastre = wC; map->listRelation = lr; Node* n0 = initNode(0, map->bounds->min->y, map->bounds->min->x, "true", map->bounds, NULL); Node* n1 = initNode(1, map->bounds->max->y, map->bounds->min->x, "true", map->bounds, NULL); Node* n2 = initNode(2, map->bounds->max->y, map->bounds->max->x, "true", map->bounds, NULL); Node* n3 = initNode(3, map->bounds->min->y, map->bounds->max->x, "true", map->bounds, NULL); insert(&aNode,n0,NULL); insert(&aNode,n1,NULL); insert(&aNode,n2,NULL); insert(&aNode,n3,NULL); map->bounds = convertBounds(map->bounds); return map; }
int extract_main(int argc, char *argv[]) { char *opref = NULL, *oname, *p; int c, i; Config config; //Defaults config.keepCpG = 1; config.keepCHG = 0; config.keepCHH = 0; config.minMapq = 10; config.minPhred = 5; config.keepDupes = 0; config.keepSingleton = 0, config.keepDiscordant = 0; config.merge = 0; config.maxDepth = 2000; config.fai = NULL; config.fp = NULL; config.bai = NULL; config.reg = NULL; config.bedName = NULL; config.bed = NULL; config.fraction = 0; config.counts = 0; config.logit = 0; for(i=0; i<16; i++) config.bounds[i] = 0; static struct option lopts[] = { {"opref", 1, NULL, 'o'}, {"fraction", 0, NULL, 'f'}, {"counts", 0, NULL, 'c'}, {"logit", 0, NULL, 'm'}, {"noCpG", 0, NULL, 1}, {"CHG", 0, NULL, 2}, {"CHH", 0, NULL, 3}, {"keepDupes", 0, NULL, 4}, {"keepSingleton",0, NULL, 5}, {"keepDiscordant",0,NULL, 6}, {"OT", 1, NULL, 7}, {"OB", 1, NULL, 8}, {"CTOT", 1, NULL, 9}, {"CTOB", 1, NULL, 10}, {"mergeContext", 0, NULL, 11}, {"help", 0, NULL, 'h'}, {0, 0, NULL, 0} }; while((c = getopt_long(argc, argv, "q:p:r:l:o:D:f:c:m:", lopts,NULL)) >=0) { switch(c) { case 'h' : extract_usage(); return 0; case 'o' : opref = strdup(optarg); break; case 'D' : config.maxDepth = atoi(optarg); break; case 'r': config.reg = strdup(optarg); break; case 'l' : config.bedName = optarg; break; case 1 : config.keepCpG = 0; break; case 2 : config.keepCHG = 1; break; case 3 : config.keepCHH = 1; break; case 4 : config.keepDupes = 1; break; case 5 : config.keepSingleton = 1; break; case 6 : config.keepDiscordant = 1; break; case 7 : parseBounds(optarg, config.bounds, 0); break; case 8 : parseBounds(optarg, config.bounds, 1); break; case 9 : parseBounds(optarg, config.bounds, 2); break; case 10 : parseBounds(optarg, config.bounds, 3); break; case 11 : config.merge = 1; break; case 'q' : config.minMapq = atoi(optarg); break; case 'p' : config.minPhred = atoi(optarg); break; case 'm' : config.logit = 1; break; case 'f' : config.fraction = 1; break; case 'c' : config.counts = 1; break; case '?' : default : fprintf(stderr, "Invalid option '%c'\n", c); extract_usage(); return 1; } } if(argc == 1) { extract_usage(); return 0; } if(argc-optind != 2) { fprintf(stderr, "You must supply a reference genome in fasta format and an input BAM file!!!\n"); extract_usage(); return -1; } //Are the options reasonable? if(config.minPhred < 1) { fprintf(stderr, "-p %i is invalid. resetting to 1, which is the lowest possible value.\n", config.minPhred); config.minPhred = 1; } if(config.minMapq < 0) { fprintf(stderr, "-q %i is invalid. Resetting to 0, which is the lowest possible value.\n", config.minMapq); config.minMapq = 0; } if(config.fraction+config.counts+config.logit > 1) { fprintf(stderr, "More than one of --fraction, --counts, and --logit were specified. These are mutually exclusive.\n"); extract_usage(); return 1; } //Has more than one output format been requested? if(config.fraction + config.counts + config.logit > 1) { fprintf(stderr, "You may specify AT MOST one of -c/--counts, -f/--fraction, or -m/--logit.\n"); return -6; } //Is there still a metric to output? if(!(config.keepCpG + config.keepCHG + config.keepCHH)) { fprintf(stderr, "You haven't specified any metrics to output!\nEither don't use the --noCpG option or specify --CHG and/or --CHH.\n"); return -1; } //Open the files if((config.fai = fai_load(argv[optind])) == NULL) { fprintf(stderr, "Couldn't open the index for %s!\n", argv[optind]); extract_usage(); return -2; } if((config.fp = hts_open(argv[optind+1], "rb")) == NULL) { fprintf(stderr, "Couldn't open %s for reading!\n", argv[optind+1]); return -4; } if((config.bai = sam_index_load(config.fp, argv[optind+1])) == NULL) { fprintf(stderr, "Couldn't load the index for %s, will attempt to build it.\n", argv[optind+1]); if(bam_index_build(argv[optind+1], 0) < 0) { fprintf(stderr, "Couldn't build the index for %s! File corrupted?\n", argv[optind+1]); return -5; } if((config.bai = sam_index_load(config.fp, argv[optind+1])) == NULL) { fprintf(stderr, "Still couldn't load the index, quiting.\n"); return -5; } } //Output files config.output_fp = malloc(sizeof(FILE *) * 3); assert(config.output_fp); if(opref == NULL) { opref = strdup(argv[optind+1]); assert(opref); p = strrchr(opref, '.'); if(p != NULL) *p = '\0'; fprintf(stderr, "writing to prefix:'%s'\n", opref); } if(config.fraction) { oname = malloc(sizeof(char) * (strlen(opref)+19)); } else if(config.counts) { oname = malloc(sizeof(char) * (strlen(opref)+21)); } else if(config.logit) { oname = malloc(sizeof(char) * (strlen(opref)+20)); } else { oname = malloc(sizeof(char) * (strlen(opref)+14)); } assert(oname); if(config.keepCpG) { if(config.fraction) { sprintf(oname, "%s_CpG.meth.bedGraph", opref); } else if(config.counts) { sprintf(oname, "%s_CpG.counts.bedGraph", opref); } else if(config.logit) { sprintf(oname, "%s_CpG.logit.bedGraph", opref); } else { sprintf(oname, "%s_CpG.bedGraph", opref); } config.output_fp[0] = fopen(oname, "w"); if(config.output_fp[0] == NULL) { fprintf(stderr, "Couldn't open the output CpG metrics file for writing! Insufficient permissions?\n"); return -3; } printHeader(config.output_fp[0], "CpG", opref, config); } if(config.keepCHG) { if(config.fraction) { sprintf(oname, "%s_CHG.meth.bedGraph", opref); } else if(config.counts) { sprintf(oname, "%s_CHG.counts.bedGraph", opref); } else if(config.logit) { sprintf(oname, "%s_CHG.logit.bedGraph", opref); } else { sprintf(oname, "%s_CHG.bedGraph", opref); } config.output_fp[1] = fopen(oname, "w"); if(config.output_fp[1] == NULL) { fprintf(stderr, "Couldn't open the output CHG metrics file for writing! Insufficient permissions?\n"); return -3; } printHeader(config.output_fp[1], "CHG", opref, config); } if(config.keepCHH) { if(config.fraction) { sprintf(oname, "%s_CHH.meth.bedGraph", opref); } else if(config.counts) { sprintf(oname, "%s_CHH.counts.bedGraph", opref); } else if(config.logit) { sprintf(oname, "%s_CHH.logit.bedGraph", opref); } else { sprintf(oname, "%s_CHH.bedGraph", opref); } config.output_fp[2] = fopen(oname, "w"); if(config.output_fp[2] == NULL) { fprintf(stderr, "Couldn't open the output CHH metrics file for writing! Insufficient permissions?\n"); return -3; } printHeader(config.output_fp[2], "CHH", opref, config); } //Run the pileup extractCalls(&config); //Close things up hts_close(config.fp); fai_destroy(config.fai); if(config.keepCpG) fclose(config.output_fp[0]); if(config.keepCHG) fclose(config.output_fp[1]); if(config.keepCHH) fclose(config.output_fp[2]); hts_idx_destroy(config.bai); free(opref); if(config.reg) free(config.reg); if(config.bed) destroyBED(config.bed); free(oname); free(config.output_fp); return 0; }