jkOwnLib/trans3.c

Go to the documentation of this file.
00001 /* trans3 - a sequence and three translated reading frames. */
00002 /* Copyright 2000-2003 Jim Kent.  All rights reserved. */
00003 
00004 #include "common.h"
00005 #include "dnaseq.h"
00006 #include "trans3.h"
00007 
00008 static char const rcsid[] = "$Id: trans3.c,v 1.5 2007/04/20 22:43:37 kent Exp $";
00009 
00010 struct trans3 *trans3New(struct dnaSeq *seq)
00011 /* Create a new set of translated sequences. */
00012 {
00013 struct trans3 *t3;
00014 int frame;
00015 int lastPos = seq->size - 1;
00016 
00017 AllocVar(t3);
00018 t3->name = seq->name;
00019 t3->seq = seq;
00020 t3->end = seq->size;
00021 for (frame=0; frame<3; ++frame)
00022     {
00023     /* Position and frame are the same except in the
00024      * very rare case where we are trying to translate 
00025      * something less than 3 bases.  In this case this
00026      * somewhat cryptic construction will force it to
00027      * return empty sequences for the missing frames
00028      * avoiding an assert in translateSeq. */
00029     int pos = frame;
00030     if (pos > lastPos) pos = lastPos;
00031     t3->trans[frame] = translateSeq(seq, pos, FALSE);
00032     }
00033 return t3;
00034 }
00035 
00036 void trans3Free(struct trans3 **pT3)
00037 /* Free a trans3 structure. */
00038 {
00039 struct trans3 *t3 = *pT3;
00040 if (t3 != NULL)
00041     {
00042     freeDnaSeq(&t3->trans[0]);
00043     freeDnaSeq(&t3->trans[1]);
00044     freeDnaSeq(&t3->trans[2]);
00045     freez(pT3);
00046     }
00047 }
00048 
00049 void trans3FreeList(struct trans3 **pList)
00050 /* Free a list of dynamically allocated trans3's */
00051 {
00052 struct trans3 *el, *next;
00053 
00054 for (el = *pList; el != NULL; el = next)
00055     {
00056     next = el->next;
00057     trans3Free(&el);
00058     }
00059 *pList = NULL;
00060 }
00061 
00062 struct trans3 *trans3Find(struct hash *t3Hash, char *name, int start, int end)
00063 /* Find trans3 in hash which corresponds to sequence of given name and includes
00064  * bases between start and end. */
00065 {
00066 struct trans3 *t3;
00067 for (t3 = hashFindVal(t3Hash, name); t3 != NULL; t3 = t3->next)
00068     {
00069     if (t3->start <= start && t3->end >= end)
00070         return t3;
00071     }
00072 internalErr();
00073 return NULL;
00074 }
00075 
00076 void trans3Offset(struct trans3 *t3List, AA *aa, int *retOffset, int *retFrame)
00077 /* Figure out offset of peptide in context of larger sequences. */
00078 {
00079 struct trans3 *t3;
00080 int frame;
00081 aaSeq *seq;
00082 
00083 for (t3 = t3List; t3 != NULL; t3 = t3->next)
00084     {
00085     for (frame = 0; frame < 3; ++frame)
00086         {
00087         seq = t3->trans[frame];
00088         if (seq->dna <= aa && aa < seq->dna + seq->size)
00089             {
00090             *retOffset = aa - seq->dna + t3->start/3;
00091             *retFrame = frame;
00092             return;
00093             }
00094         }
00095     }
00096 internalErr();
00097 }
00098 
00099 int trans3GenoPos(char *pt, bioSeq *seq, struct trans3 *t3List, boolean isEnd)
00100 /* Convert from position in one of three translated frames in
00101  * t3List to genomic offset. If t3List is NULL then just use seq
00102  * instead. */
00103 {
00104 int offset, frame;
00105 if (t3List != NULL)
00106     {
00107     /* Special processing at end. The end coordinate is
00108      * not included.  In most cases this makes things
00109      * easier.  Here we have to move it back one
00110      * amino acid, so that in the edge case it will
00111      * be included in the block that's loaded.  Then
00112      * we move it back. */
00113     if (isEnd)
00114         pt -= 1;
00115     trans3Offset(t3List, pt, &offset, &frame);
00116     if (isEnd)
00117         offset += 1;
00118     return 3*offset + frame;
00119     }
00120 else
00121    {
00122    return pt - seq->dna;
00123    }
00124 }
00125 
00126 int trans3Frame(char *pt, struct trans3 *t3List)
00127 /* Figure out which frame pt is in or 0 if no frame. */
00128 {
00129 if (t3List == NULL)
00130     return 0;
00131 else
00132     return 1 + trans3GenoPos(pt, NULL, t3List, FALSE)%3;
00133 }
00134 

Generated on Tue Dec 25 18:39:30 2007 for blat by  doxygen 1.5.2