Int4 LIBCALL PostLength (PostingPtr pst) { Int4 k; k = 0; if (pst != NULL) { k = (Int4) (BSLen (pst->uids) / (Int4) sizeof (DocUid)); } return k; }
LocalLinkSetPtr LIBCALL BSPtoLSP(ByteStorePtr bsp) { LocalLinkSetPtr lsp; if (bsp == NULL) { ErrPostEx(SEV_ERROR,0,0, "Invalid parameter."); return NULL; } if(BSLen(bsp) == 0) { ErrPostEx(SEV_ERROR,0,0, "Byte store contains no data."); return NULL; } lsp = LocalLinkSetNew(); if(lsp == NULL) { ErrPostEx(SEV_ERROR,0,0, "LocalLinkSetNew failed."); return NULL; } lsp->num = BSLen(bsp) / sizeof(DocUid); if ((lsp->uids = MemNew(BSLen(bsp))) == NULL) { /* platforms which can't allocate this are out of luck */ ErrPostEx(SEV_ERROR,0,0, "Cannot allocate memory for LinkSet"); lsp = LocalLinkSetFree(lsp); return NULL; } else { BSSeek (bsp, 0L, 0); BSRead (bsp, lsp->uids, lsp->num * sizeof (DocUid)); } return lsp; }
/* This constructs an ASN.1 SeqGraph that contains the quality scores from the consensus sequence */ static SeqGraphPtr SeqGraphFromContig (TContigPtr contig, BioseqPtr bsp) { SeqGraphPtr sgp; ByteStorePtr bs; Uint1 bytes[128]; Int2 max = INT2_MIN; Int2 min = INT2_MAX; Int4 q_pos, b_pos; SeqIntPtr sintp; if (contig == NULL || contig->num_qual_scores == 0 || contig->qual_scores == NULL || bsp == NULL) { return NULL; } sgp = SeqGraphNew (); bs = BSNew (1000); q_pos = 0; while (q_pos < contig->num_qual_scores) { b_pos = 0; while (b_pos < sizeof (bytes) && q_pos < contig->num_qual_scores) { max = MAX (max, (Int2) contig->qual_scores[q_pos]); min = MIN (min, (Int2) contig->qual_scores[q_pos]); bytes[b_pos++] = (Uint1) contig->qual_scores[q_pos++]; } BSWrite (bs, (Pointer) bytes, (Int4) b_pos); } sgp->numval = BSLen (bs); BSPutByte (bs, EOF); sgp->title = StringSave ("Phrap Quality"); sgp->flags [0] = 0; sgp->compr = 1; sgp->flags [1] = 0; sgp->flags [2] = 3; sgp->axis.intvalue = 0; sgp->min.intvalue = min; sgp->max.intvalue = max; sgp->a = 1.0; sgp->b = 0; sgp->values = (Pointer) bs; sintp = SeqIntNew (); sintp->from = 0; sintp->to = bsp->length - 1; sintp->id = SeqIdDup (bsp->id); ValNodeAddPointer (&(sgp->loc), SEQLOC_INT, (Pointer) sintp); return sgp; }
/***************************************************************************** * * StdFormatPrint(data, func, templatename, options) * *****************************************************************************/ NLM_EXTERN Boolean StdFormatPrint (Pointer data, AsnWriteFunc func, CharPtr templatename, StdPrintOptionsPtr options) { PrintStackPtr psp; CharPtr ptr = NULL; PrintFormatListPtr pflp; if ((data == NULL) || (func == NULL) || (templatename == NULL) || (options == NULL)) return FALSE; pflp = PrintFormatListGet(templatename); if (pflp == NULL) return FALSE; if (options->fp == NULL) { options->bstp = BSNew(80); options->ptr = NULL; } psp = PrintStackBuild(data, func, pflp); /*** printf("Before sort\n"); PrintStackDump(psp, stdout, 0); ***/ PrintStackSort(psp); /*** printf("After sort\n"); PrintStackDump(psp, stdout, 0); printf("\n\n"); ***/ PrintStackPrint(psp, options); PrintStackFree(psp); if (options->fp == NULL) { if (BSLen(options->bstp)) options->ptr = BSMerge(options->bstp, NULL); BSFree(options->bstp); } return TRUE; }
PostingPtr LIBCALL NewPost (ByteStorePtr lst, Int4 defsize) { PostingPtr pst; pst = NULL; if (lst != NULL) { pst = MemNew (sizeof (Posting)); if (pst != NULL) { pst->uids = lst; pst->buffer = NULL; if (defsize == 0) { pst->bufsize = (Int4) MIN (16384L, BSLen (lst)); } else { pst->bufsize = (Int4) MIN (16384L, defsize); } pst->count = 0; pst->index = 0; } } return pst; }
static void DoGraphs (BioseqPtr bsp, Pointer userdata) { Byte bases [400]; ByteStorePtr bs; Int2 ctr, i, j, val; Int4 curroffset = 0, gphlen = 0, seqlen = 0, slplen, bslen, min = INT4_MAX, max = INT4_MIN, NsWithScore, ACGTsWithoutScore; DeltaSeqPtr dsp; Uint2 entityID, olditemid = 0, olditemtype = 0, numdsp = 0, numsgp = 0; GphItemPtr gip; ValNodePtr head, vnp; Boolean noErrors = TRUE; Uint1 residue; ScanDataPtr sdp; SeqGraphPtr sgp; SeqIntPtr sintp; SeqLocPtr slocp; SeqLitPtr slp; SeqPortPtr spp; Char str [128]; if (bsp == NULL) return; if (! ISA_na (bsp->mol)) return; if (SeqMgrGetParentOfPart (bsp, NULL) != NULL) return; sdp = (ScanDataPtr) userdata; entityID = ObjMgrGetEntityIDForPointer (bsp); head = GetSeqGraphsOnBioseq (entityID, bsp); if (head == NULL) return; for (vnp = head; vnp != NULL; vnp = vnp->next) { gip = (GphItemPtr) vnp->data.ptrvalue; if (gip == NULL) continue; sgp = gip->sgp; min = MIN ((Int4) min, (Int4) sgp->min.intvalue); max = MAX ((Int4) max, (Int4) sgp->max.intvalue); if (sgp->min.intvalue < 0 || sgp->min.intvalue > 100) { sprintf (str, "Graph min (%ld) out of range", (long) sgp->min.intvalue); PrintGraphMessage (bsp, sdp, str, NULL); noErrors = FALSE; } if (sgp->max.intvalue < 0 || sgp->max.intvalue > 100) { sprintf (str, "Graph max (%ld) out of range", (long) sgp->max.intvalue); PrintGraphMessage (bsp, sdp, str, NULL); noErrors = FALSE; } gphlen += sgp->numval; bs = (ByteStorePtr) sgp->values; if (bs != NULL) { bslen = BSLen (bs); if (sgp->numval != bslen) { sprintf (str, "SeqGraph (%ld) and ByteStore (%ld) length mismatch", (long) sgp->numval, (long) bslen); PrintGraphMessage (bsp, sdp, str, NULL); noErrors = FALSE; } } } if (bsp->repr == Seq_repr_raw) { seqlen = bsp->length; } else if (bsp->repr == Seq_repr_delta) { for (dsp = (DeltaSeqPtr) (bsp->seq_ext); dsp != NULL; dsp = dsp->next) { switch (dsp->choice) { case 1 : slocp = (SeqLocPtr) dsp->data.ptrvalue; if (slocp == NULL) break; if (slocp->choice != SEQLOC_NULL) { seqlen += SeqLocLen (slocp); } break; case 2 : slp = (SeqLitPtr) dsp->data.ptrvalue; if (slp == NULL || slp->seq_data == NULL) break; seqlen += slp->length; break; default : break; } } } if (seqlen != gphlen) { sprintf (str, "SeqGraph (%ld) and Bioseq (%ld) length mismatch", (long) gphlen, (long) seqlen); PrintGraphMessage (bsp, sdp, str, NULL); noErrors = FALSE; } if (bsp->repr == Seq_repr_delta) { for (dsp = (DeltaSeqPtr) (bsp->seq_ext), vnp = head; dsp != NULL && vnp != NULL; dsp = dsp->next) { gip = (GphItemPtr) vnp->data.ptrvalue; if (gip == NULL) continue; sgp = gip->sgp; if (sgp == NULL) continue; switch (dsp->choice) { case 1 : slocp = (SeqLocPtr) dsp->data.ptrvalue; if (slocp != NULL && slocp->choice != SEQLOC_NULL) { slplen = SeqLocLen (slocp); curroffset += slplen; if (sgp->numval != slplen) { sprintf (str, "SeqGraph (%ld) and SeqLoc (%ld) length mismatch", (long) sgp->numval, (long) slplen); PrintGraphMessage (bsp, sdp, str, NULL); noErrors = FALSE; } numdsp++; if (vnp != NULL) { vnp = vnp->next; numsgp++; } } break; case 2 : slp = (SeqLitPtr) dsp->data.ptrvalue; if (slp != NULL && slp->seq_data != NULL) { if (sgp->numval != slp->length) { sprintf (str, "SeqGraph (%ld) and SeqLit (%ld) length mismatch", (long) sgp->numval, (long) slp->length); PrintGraphMessage (bsp, sdp, str, NULL); noErrors = FALSE; } slocp = sgp->loc; if (slocp != NULL && slocp->choice == SEQLOC_INT) { sintp = (SeqIntPtr) slocp->data.ptrvalue; if (sintp != NULL) { if (sintp->from != curroffset) { sprintf (str, "SeqGraph (%ld) and SeqLit (%ld) start do not coincide", (long) sintp->from, (long) curroffset); PrintGraphMessage (bsp, sdp, str, NULL); noErrors = FALSE; } if (sintp->to != slp->length + curroffset - 1) { sprintf (str, "SeqGraph (%ld) and SeqLit (%ld) stop do not coincide", (long) sintp->to, (long) (slp->length + curroffset - 1)); PrintGraphMessage (bsp, sdp, str, NULL); noErrors = FALSE; } } } numdsp++; if (vnp != NULL) { vnp = vnp->next; numsgp++; } } if (slp != NULL) { curroffset += slp->length; } break; default : break; } } if (numdsp != numsgp) { sprintf (str, "Different number of SeqGraph (%d) and SeqLit (%d) components", (int) numsgp, (int) numdsp); PrintGraphMessage (bsp, sdp, str, NULL); noErrors = FALSE; } } for (vnp = head; vnp != NULL; vnp = vnp->next) { gip = (GphItemPtr) vnp->data.ptrvalue; if (gip == NULL) continue; sgp = gip->sgp; if (sgp == NULL) continue; spp = SeqPortNewByLoc (sgp->loc, Seq_code_iupacna); if (spp == NULL) continue; slplen = SeqLocLen (sgp->loc); if (bsp->repr == Seq_repr_delta || bsp->repr == Seq_repr_virtual) { SeqPortSet_do_virtual (spp, TRUE); } bs = (ByteStorePtr) sgp->values; BSSeek (bs, 0, SEEK_SET); j = 0; val = 0; ctr = SeqPortRead (spp, bases, sizeof (bases)); i = 0; residue = (Uint1) bases [i]; NsWithScore = 0; ACGTsWithoutScore = 0; while (residue != SEQPORT_EOF && j < sgp->numval) { if (IS_residue (residue)) { val = (Int2) BSGetByte (bs); j++; switch (residue) { case 'A' : case 'C' : case 'G' : case 'T' : if (val == 0) { ACGTsWithoutScore++; } break; case 'N' : if (val > 0) { NsWithScore++; } break; default : break; } } i++; if (i >= ctr) { i = 0; ctr = SeqPortRead (spp, bases, sizeof (bases)); if (ctr < 0) { bases [0] = -ctr; } else if (ctr < 1) { bases [0] = SEQPORT_EOF; } } residue = (Uint1) bases [i]; } if (ACGTsWithoutScore > 0) { sprintf (str, "%ld ACGT bases have zero score value", (long) ACGTsWithoutScore); PrintGraphMessage (bsp, sdp, str, NULL); noErrors = FALSE; } if (NsWithScore > 0) { sprintf (str, "%ld N bases have positive score value", (long) NsWithScore); PrintGraphMessage (bsp, sdp, str, NULL); noErrors = FALSE; } SeqPortFree (spp); } if (noErrors) { PrintGraphMessage (bsp, sdp, "Quality scores okay", NULL); } ValNodeFreeData (head); }
NLM_EXTERN Int4 print_protein_for_cds(SeqFeatPtr sfp, CharPtr buf, SeqLocPtr loc, Boolean reverse_minus) { CdRegionPtr crp; Int4 frame_offset, start_offset; Uint1 f_strand; Boolean reverse; Int4 cd_len; GatherRange gr; Int2 p_pos, buf_len; Int4 a_left, a_right; Int4 aa, val; SeqLocPtr slp; SeqPortPtr spp; ByteStorePtr p_data; Int4 end_pos, start_pos = -1; Uint1 residue; Boolean seal_ends = FALSE; Boolean reverse_order; if(sfp == NULL || sfp->data.choice != 3) return -1; if(buf == NULL || loc == NULL) return -1; crp = sfp->data.value.ptrvalue; if(crp == NULL) return -1; if(buf[0] == '\0') seal_ends = TRUE; spp = NULL; p_data = NULL; if(sfp->product !=NULL && !IS_BOGO_Product(sfp->ext)) { spp = SeqPortNewByLoc(sfp->product, Seq_code_ncbieaa); if(spp !=NULL) { SeqPortSeek(spp, 0, SEEK_SET); end_pos = spp->totlen-1; } } if(spp == NULL) { p_data = ProteinFromCdRegion(sfp, TRUE); /* p_data = ProteinFromCdRegion(sfp, FALSE); */ if(p_data !=NULL) { BSSeek(p_data, 0, SEEK_SET); end_pos = BSLen(p_data)-1; } } if(spp == NULL && p_data == NULL) return -1; if(crp->frame == 0) frame_offset = 0; else frame_offset = (Int4)crp->frame-1; start_offset = frame_offset; f_strand = SeqLocStrand(sfp->location); reverse = ck_reverse(f_strand, SeqLocStrand(loc)); /*if reverse == TRUE, the translated protein is written backwards*/ if(reverse && reverse_minus) reverse_order = TRUE; else reverse_order = FALSE; slp = NULL; cd_len = 0; aa = 0; buf_len = SeqLocLen(loc); if(reverse_order) { p_pos = buf_len -1; if(seal_ends) { buf[p_pos+1] = '\0'; seal_ends = FALSE; } } else p_pos = 0; while((slp = SeqLocFindNext(sfp->location, slp))!=NULL) { if(SeqLocOffset(loc, slp, &gr, 0)) { if(reverse_order) { if(gr.right < p_pos) p_pos = (Int2)(gr.right); } else { if(p_pos < gr.left) p_pos = (Int2)(gr.left); } SeqLocOffset(slp, loc, &gr, 0); a_left = gr.left + cd_len; a_right = gr.right + cd_len; /* if(reverse_order) { temp = a_right; a_right = -a_left; a_left = -temp; } */ for(; a_left<=a_right; ++a_left) { val = ABS(a_left) - start_offset; aa = val/3; if(aa < 0 || aa > end_pos)/*stop & partial codon*/ { buf[p_pos] = '^'; } else { if(val%3==1)/*label aa in the middle of 3-bp codon*/ { if(start_pos == -1) start_pos = aa; if(spp !=NULL) { SeqPortSeek(spp, aa, SEEK_SET); residue = SeqPortGetResidue(spp); } else { BSSeek(p_data, aa, SEEK_SET); residue = (Uint1)BSGetByte(p_data); } if(IS_ALPHA(residue) || residue == '*' || residue == '-') buf[p_pos] = residue; else buf[p_pos] = '?'; } else buf[p_pos] = ' '; } if(reverse_order) -- p_pos; else { ++p_pos; if (p_pos > buf_len) break; } } } cd_len += SeqLocLen(slp); /*frame_offset = (cd_len - start_offset)%3; if(frame_offset > 0) --frame_offset;*/ } if(spp != NULL) SeqPortFree(spp); if(p_data != NULL) BSFree(p_data); if(p_pos == 0) /*all the residues are introns*/ { if(seal_ends) { end_pos = buf_len; MemSet((Pointer)buf, '~', (size_t)(end_pos) * sizeof(Char)); buf[end_pos] = '\0'; } } else { if(seal_ends) { buf[p_pos] = '\0'; } if(start_pos == -1) start_pos = aa; } return start_pos; }