vector<Alignment> Sampler::alignment_pair(size_t read_length, size_t fragment_length, double fragment_std_dev, double base_error, double indel_error) { // simulate forward/reverse pair by first simulating a long read normal_distribution<> norm_dist(fragment_length, fragment_std_dev); int frag_len = round(norm_dist(rng)); auto fragment = alignment_with_error(frag_len, base_error, indel_error); // then taking the ends auto fragments = alignment_ends(fragment, read_length, read_length); auto& aln1 = fragments.front(); auto& aln2 = fragments.back(); { // name the alignments string data; aln1.SerializeToString(&data); aln2.SerializeToString(&data); int n; #pragma omp critical(nonce) n = nonce++; data += std::to_string(n); const string hash = sha1head(data, 16); aln1.set_name(hash + "_1"); aln2.set_name(hash + "_2"); } // set the appropriate flags for pairing aln1.mutable_fragment_next()->set_name(aln2.name()); aln2.mutable_fragment_prev()->set_name(aln1.name()); // reverse complement the back fragment fragments.back() = reverse_complement_alignment(fragments.back(), (function<int64_t(int64_t)>) ([&](int64_t id) { return (int64_t)node_length(id); })); return fragments; }
queue_head* extract_helper(queue_head* qh, int num) { queue_head *temp_r, *temp; if ((MAX(node_length(qh->left_subtree), node_length(qh->right_subtree))) == node_length(qh->left_subtree)) { if (node_length(qh->left_subtree) <= num) { temp_r = qh->left_subtree; qh->left_subtree = NULL; } else { if (qh->left_subtree != NULL) temp_r = extract_helper(qh->left_subtree, num); } } else { if (qh->right_subtree!=NULL && node_length(qh->right_subtree) <= num) { temp_r = qh->right_subtree; qh->right_subtree = NULL; } else { if (qh->right_subtree != NULL) temp_r = extract_helper(qh->right_subtree, num); } } if (node_distance(qh->right_subtree) > node_distance(qh->left_subtree)) { SWAP(qh->left_subtree, qh->right_subtree); } qh->distance = (MIN(node_distance(qh->left_subtree), node_distance(qh->right_subtree))) +1; qh->length = node_length(qh->left_subtree) + node_length(qh->right_subtree) + 1; return temp_r; }
queue_head* prune_helper(queue_head* q, float min_bound) { queue_head* temp; if (q == NULL) return NULL; if (q->priority > min_bound) return NULL; q->left_subtree = prune_helper(q->left_subtree, min_bound); q->right_subtree = prune_helper(q->right_subtree, min_bound); if (node_distance(q->right_subtree) > node_distance(q->left_subtree)) { SWAP(q->left_subtree, q->right_subtree); } q->distance = (MIN(node_distance(q->right_subtree), node_distance(q->left_subtree))) + 1; q->length = node_length(q->left_subtree) + node_length(q->right_subtree) + 1; return q; }
// put the new node into the front of list void node_push_front(node** head, node* n){ int len = node_length(*head); if(len == 0){ *head = n; return; } (*head)->prev = n; n->next = *head; n->prev = NULL; *head = n; return; }
queue_head* merge_helper(queue_head* q1, queue_head* q2) { queue_head* temp; if (q2 == NULL) return q1; if (q1 == NULL) return q2; if (q2->priority < q1->priority) { SWAP(q1, q2); } q1->right_subtree = merge_helper(q1->right_subtree, q2); if (node_distance(q1->right_subtree) > node_distance(q1->left_subtree)) { SWAP(q1->left_subtree, q1->right_subtree); } q1->distance = (MIN(node_distance(q1->right_subtree), node_distance(q1->left_subtree))) + 1; q1->length = node_length(q1->left_subtree) + node_length(q1->right_subtree) + 1; return q1; }
/* NOT THREAD SAFE */ void sprint_if_list(router_state *rs, char **buf, int *len) { assert(rs); assert(buf); assert(len); node *iface_walker = 0; iface_entry *iface = 0; struct in_addr ip; struct in_addr mask; char *buffer = 0; int total_len = 0; char ip_str[16]; char mask_str[16]; buffer = calloc(strlen(IFACE_COL) + IFACE_ENTRY_TO_STRING_LEN * node_length(rs->if_list), sizeof(char)); COPY_STRING(buffer, total_len, IFACE_COL); iface_walker = rs->if_list; while(iface_walker) { iface = (iface_entry *)iface_walker->data; char if_name[IFACE_MAX_IFACE_LEN+1]; snprintf(if_name, IFACE_MAX_IFACE_LEN, "%s", iface->name); if_name[IFACE_MAX_IFACE_LEN] = '\0'; ip.s_addr = iface->ip; mask.s_addr = iface->mask; char line[IFACE_ENTRY_TO_STRING_LEN]; snprintf(line, IFACE_ENTRY_TO_STRING_LEN, "%-21s %02X:%02X:%02X:%02X:%02X:%02X %-15s %-15s %-5i %-2s %-2s\n", if_name, (unsigned char)iface->addr[0], (unsigned char)iface->addr[1], (unsigned char)iface->addr[2], (unsigned char)iface->addr[3], (unsigned char)iface->addr[4], (unsigned char)iface->addr[5], inet_ntop(AF_INET, &ip, ip_str, 16), inet_ntop(AF_INET, &mask, mask_str, 16), iface->speed, (iface->is_active == 1) ? "Y" : "N", (iface->is_wan == 1) ? "Y" : "N"); COPY_STRING(buffer, total_len, line); iface_walker = iface_walker->next; } *buf = buffer; *len = total_len; }
/* NOT THREAD SAFE */ void sprint_arp_cache(router_state *rs, char **buf, int *len) { assert(rs); assert(buf); assert(len); node *arp_walker = 0; arp_cache_entry *arp_entry = 0; time_t now; double diff; char *buffer = 0; int total_len = 0; buffer = calloc(strlen(ARP_CACHE_COL) + ARP_CACHE_ENTRY_TO_STRING_LEN * node_length(rs->arp_cache), sizeof(char)); COPY_STRING(buffer, total_len, ARP_CACHE_COL); arp_walker = rs->arp_cache; while(arp_walker) { arp_entry = (arp_cache_entry *)arp_walker->data; char addr[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &(arp_entry->ip), addr, INET_ADDRSTRLEN); char ttl[47]; if (arp_entry->is_static == 0) { time(&now); diff = difftime(now, arp_entry->TTL); snprintf(ttl, 47, "%f", rs->arp_ttl - diff); } else { snprintf(ttl, 47, "%s", "static"); } char line[ARP_CACHE_ENTRY_TO_STRING_LEN]; snprintf(line, ARP_CACHE_ENTRY_TO_STRING_LEN, "%-15s %02X:%02X:%02X:%02X:%02X:%02X %-46s\n", addr, (unsigned char)arp_entry->arp_ha[0], (unsigned char)arp_entry->arp_ha[1], (unsigned char)arp_entry->arp_ha[2], (unsigned char)arp_entry->arp_ha[3], (unsigned char)arp_entry->arp_ha[4], (unsigned char)arp_entry->arp_ha[5], ttl); COPY_STRING(buffer, total_len, line); arp_walker = arp_walker->next; } *buf = buffer; *len = total_len; }
void sprint_nat_table(router_state *rs, char **buf, unsigned int *len) { char ext_ip_str[16]; char int_ip_str[16]; int nat_table_size = node_length(rs->nat_table); time_t now; uint32_t diff = 0; char *buffer = (char *)calloc(strlen(NAT_COL) + nat_table_size*NAT_ENTRY_TO_STR_LEN, sizeof(char)); unsigned int total_len = 0; COPY_STRING(buffer, total_len, NAT_COL); node *n = rs->nat_table; while(n) { nat_entry *ne = (nat_entry *)n->data; char last_update[47]; bzero(last_update, 47); time(&now); diff = (int)difftime(now, ne->last_hits_time); inet_ntop(AF_INET, &(ne->nat_ext.ip), ext_ip_str, 16); inet_ntop(AF_INET, &(ne->nat_int.ip), int_ip_str, 16); char line[NAT_ENTRY_TO_STR_LEN]; bzero(line, NAT_ENTRY_TO_STR_LEN); snprintf(line, NAT_ENTRY_TO_STR_LEN, "%-15s %-6u %-15s %-6u %-6u %-6u %-7.2f %-12u %-3u %-1s\n", ext_ip_str, ntohs(ne->nat_ext.port), int_ip_str, ntohs(ne->nat_int.port), ne->hits, ne->last_hits, ne->avg_hits_per_second, diff, ne->hw_row, (ne->is_static == 1) ? "Y" : "N"); COPY_STRING(buffer, total_len, line); n = n->next; } *buf = buffer; *len = total_len; }
/* NOT THREAD SAFE */ void sprint_rtable(router_state *rs, char **buf, int *len) { assert(rs); assert(buf); assert(len); node *rtable_walker = 0; rtable_entry *re = 0; char *buffer = 0; int total_len = 0; char ip_str[16]; char gw_str[16]; char mask_str[16]; buffer = calloc(strlen(RTABLE_COL) + RTABLE_ENTRY_TO_STRING_LEN * node_length(rs->rtable), sizeof(char) ); COPY_STRING(buffer, total_len, RTABLE_COL); rtable_walker = rs->rtable; while(rtable_walker) { re = (rtable_entry *)rtable_walker->data; char if_name[RTABLE_MAX_IFACE_LEN+1]; snprintf(if_name, RTABLE_MAX_IFACE_LEN, "%s", re->iface); if_name[RTABLE_MAX_IFACE_LEN] = '\0'; char line[RTABLE_ENTRY_TO_STRING_LEN]; snprintf(line, RTABLE_ENTRY_TO_STRING_LEN, "%-15s %-15s %-15s %-18s %-6s %-6s\n", inet_ntop(AF_INET, &(re->ip), ip_str, 16), inet_ntop(AF_INET, &(re->gw), gw_str, 16), inet_ntop(AF_INET, &(re->mask), mask_str, 16), if_name, (re->is_static == 1) ? "Y" : "N", (re->is_active == 1) ? "Y" : "N"); rtable_walker = rtable_walker->next; COPY_STRING(buffer, total_len, line); } *buf = buffer; *len = total_len; }
pair<size_t, size_t> PathIndex::round_outward(size_t start, size_t past_end) const { // Find the node occurrence the start position is on auto start_occurrence = find_position(start); // Seek to the start of that occurrence size_t start_rounded = start_occurrence->first; // Now try and round the end size_t past_end_rounded; if (past_end == 0) { // Range must have been empty anyway, so keep it ending before the first // node. past_end_rounded = 0; } else { // Look for the node holding the last included base auto end_occurrence = find_position(past_end - 1); // Then go out past its end. past_end_rounded = end_occurrence->first + node_length(end_occurrence); } return make_pair(start_rounded, past_end_rounded); }
PathIndex::iterator PathIndex::find_position(size_t position) const { assert(!by_start.empty()); // Look up the iterator to whatever starts after here auto starts_next = by_start.upper_bound(position); // This can't work if we try to look before the first node. assert(starts_next != by_start.begin()); // Walk one back, to the node that has to own the position we asked about. starts_next--; #ifdef debug cerr << "At " << position << " we have " << starts_next->second << endl; #endif // Make sure we didn't fall off the ends assert(position - starts_next->first < node_length(starts_next)); // Return that return starts_next; }
void sprint_pwospf_router_list(router_state *rs, char **buf, int *len) { assert(rs); assert(buf); assert(len); char *buffer = 0; int total_len = 0; time_t now; double diff; char rid_str[16]; char subnet_str[16]; char mask_str[16]; int num_entries = 0; node *rl = rs->pwospf_router_list; while(rl) { pwospf_router *pr = (pwospf_router *)rl->data; node *pi = pr->interface_list; num_entries = num_entries + (1 + node_length(pi)); rl = rl->next; } buffer = calloc(strlen(PWOSPF_ROUTER_LIST_COL) + (num_entries+1)*PWOSPF_ROUTER_LIST_TO_STRING_LEN, sizeof(char)); COPY_STRING(buffer, total_len, PWOSPF_ROUTER_LIST_COL); node *router_list_walker = rs->pwospf_router_list; while(router_list_walker) { pwospf_router *rle = (pwospf_router *)router_list_walker->data; char last_update[47]; bzero(last_update, 47); time(&now); diff = difftime(now, rle->last_update); if( (int)diff > (rs->pwospf_lsu_interval * 3) ) { snprintf(last_update, 47, "%s", "EXP"); } else { snprintf(last_update, 47, "%i", (int)diff); } char line[PWOSPF_ROUTER_LIST_TO_STRING_LEN]; bzero(line, PWOSPF_ROUTER_LIST_TO_STRING_LEN); snprintf(line, PWOSPF_ROUTER_LIST_TO_STRING_LEN, "%-20s%-5d%-5d%-5s%-5d%-5d\n", inet_ntop(AF_INET, &(rle->router_id), rid_str, 16), ntohl(rle->area_id), rle->seq, last_update, rle->distance, rle->shortest_path_found); COPY_STRING(buffer, total_len, line); node *interface_walker = rle->interface_list; while(interface_walker) { pwospf_interface *iface = (pwospf_interface *)interface_walker->data; bzero(line, PWOSPF_ROUTER_LIST_TO_STRING_LEN); snprintf(line, PWOSPF_ROUTER_LIST_TO_STRING_LEN, " %-20s%-20s%-20s%-5i\n", inet_ntop(AF_INET, &(iface->subnet), subnet_str, 16), inet_ntop(AF_INET, &(iface->mask), mask_str, 16), inet_ntop(AF_INET, &(iface->router_id), rid_str, 16), iface->is_active); COPY_STRING(buffer, total_len, line); interface_walker = interface_walker->next; } router_list_walker = router_list_walker->next; } *buf = buffer; *len = total_len; }
/* NOT THREAD SAFE */ void sprint_pwospf_if_list(router_state *rs, char **buf, int *len) { assert(rs); assert(buf); assert(len); node *iface_walker = 0; iface_entry *iface = 0; struct in_addr ip; bzero(&ip, sizeof(struct in_addr)); struct in_addr mask; bzero(&mask, sizeof(struct in_addr)); struct in_addr nid; bzero(&nid, sizeof(struct in_addr)); struct in_addr nip; bzero(&nip, sizeof(struct in_addr)); time_t now; double diff; char *buffer = 0; int total_len = 0; char ip_str[16]; char mask_str[16]; char nid_str[16]; char nip_str[16]; buffer = calloc(strlen(PWOSPF_IFACE_COL) + PWOSPF_IFACE_ENTRY_TO_STRING_LEN * node_length(rs->if_list), sizeof(char)); COPY_STRING(buffer, total_len, PWOSPF_IFACE_COL); iface_walker = rs->if_list; while(iface_walker) { iface = (iface_entry *)iface_walker->data; char if_name[PWOSPF_IFACE_MAX_IFACE_LEN+1]; snprintf(if_name, PWOSPF_IFACE_MAX_IFACE_LEN, "%s", iface->name); if_name[PWOSPF_IFACE_MAX_IFACE_LEN] = '\0'; ip.s_addr = iface->ip; mask.s_addr = iface->mask; char lsent_hello[47]; bzero(lsent_hello, 47); time(&now); diff = difftime(now, iface->last_sent_hello); if( (int)diff > rs->pwospf_hello_interval) { snprintf(lsent_hello, 47, "%s", "EXP"); } else { snprintf(lsent_hello, 47, "%i", (int)diff); } char lrecv_hello[47]; bzero(lrecv_hello, 47); time(&now); if (iface->nbr_routers) { nbr_router* nbr = (nbr_router*)iface->nbr_routers->data; nid.s_addr = nbr->router_id; nip.s_addr = nbr->ip.s_addr; diff = difftime(now, nbr->last_rcvd_hello); if( (int)diff > (3*rs->pwospf_hello_interval)) { snprintf(lrecv_hello, 47, "%s", "EXP"); } else { snprintf(lrecv_hello, 47, "%i", (int)diff); } } char line[PWOSPF_IFACE_ENTRY_TO_STRING_LEN]; snprintf(line, PWOSPF_IFACE_ENTRY_TO_STRING_LEN, "%-5s%-20s%-20s%-5s%-20s%-20s%-5s%-1s \n", if_name, inet_ntop(AF_INET, &ip, ip_str, 16), inet_ntop(AF_INET, &mask, mask_str, 16), lsent_hello, (nip.s_addr != 0) ? inet_ntop(AF_INET, &nid, nid_str, 16) : "", (nip.s_addr != 0) ? inet_ntop(AF_INET, &nip, nip_str, 16) : "", (nip.s_addr != 0) ? lrecv_hello : "", (iface->is_active == 1) ? "Y" : "N"); COPY_STRING(buffer, total_len, line); /* iterate through any remaining neighbor routers */ node* cur = iface->nbr_routers; /* advance one since we printed it above */ if (cur) { cur = cur->next; } while (cur) { nbr_router* nbr = (nbr_router*)cur->data; nid.s_addr = nbr->router_id; nip.s_addr = nbr->ip.s_addr; diff = difftime(now, nbr->last_rcvd_hello); if( (int)diff > (3*rs->pwospf_hello_interval)) { snprintf(lrecv_hello, 47, "%s", "EXP"); } else { snprintf(lrecv_hello, 47, "%i", (int)diff); } snprintf(line, PWOSPF_IFACE_ENTRY_TO_STRING_LEN, "%-5s%-20s%-20s%-5s%-20s%-20s%-5s%-1s \n", "", "", "", "", inet_ntop(AF_INET, &nid, nid_str, 16), inet_ntop(AF_INET, &nip, nip_str, 16), lrecv_hello, ""); COPY_STRING(buffer, total_len, line); cur = cur->next; } iface_walker = iface_walker->next; } *buf = buffer; *len = total_len; }