00001
00002
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
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
00024
00025
00026
00027
00028
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
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
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
00064
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
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
00101
00102
00103 {
00104 int offset, frame;
00105 if (t3List != NULL)
00106 {
00107
00108
00109
00110
00111
00112
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
00128 {
00129 if (t3List == NULL)
00130 return 0;
00131 else
00132 return 1 + trans3GenoPos(pt, NULL, t3List, FALSE)%3;
00133 }
00134