00001 #include "format.h" 00002 #include "log.h" 00003 #include "linestream.h" 00004 #include "common.h" 00005 #include "blastParser.h" 00006 00007 00008 00016 static LineStream ls = NULL; 00017 00018 00019 00024 void blastParser_initFromFile (char* fileName) 00025 { 00026 ls = ls_createFromFile (fileName); 00027 ls_bufferSet (ls,1); 00028 } 00029 00030 00031 00032 00033 00038 void blastParser_initFromPipe (char* command) 00039 { 00040 ls = ls_createFromPipe (command); 00041 ls_bufferSet (ls,1); 00042 } 00043 00044 00045 00046 00050 void blastParser_deInit (void) 00051 { 00052 ls_destroy (ls); 00053 } 00054 00055 00056 00057 static void blastParser_freeQuery (BlastQuery *currBlastQuery) 00058 { 00059 int i; 00060 BlastEntry *currBlastEntry; 00061 00062 if (currBlastQuery == NULL) { 00063 return; 00064 } 00065 hlr_free (currBlastQuery->qName); 00066 for (i = 0; i < arrayMax (currBlastQuery->entries); i++) { 00067 currBlastEntry = arrp (currBlastQuery->entries,i,BlastEntry); 00068 hlr_free (currBlastEntry->tName); 00069 } 00070 arrayDestroy (currBlastQuery->entries); 00071 freeMem (currBlastQuery); 00072 } 00073 00074 00075 00076 static void blastParser_processLine (char* line, BlastQuery* currBlastQuery) 00077 { 00078 WordIter w; 00079 BlastEntry *currEntry; 00080 00081 currEntry = arrayp (currBlastQuery->entries,arrayMax (currBlastQuery->entries),BlastEntry); 00082 w = wordIterCreate (line,"\t",0); 00083 currEntry->tName = hlr_strdup (wordNext (w)); 00084 currEntry->percentIdentity = atof (wordNext (w)); 00085 currEntry->alignmentLength = atoi (wordNext (w)); 00086 currEntry->misMatches = atoi (wordNext (w)); 00087 currEntry->gapOpenings = atoi (wordNext (w)); 00088 currEntry->qStart = atoi (wordNext (w)); 00089 currEntry->qEnd = atoi (wordNext (w)); 00090 currEntry->tStart = atoi (wordNext (w)); 00091 currEntry->tEnd = atoi (wordNext (w)); 00092 currEntry->evalue = atof (wordNext (w)); 00093 currEntry->bitScore = atof (wordNext (w)); 00094 wordIterDestroy (w); 00095 } 00096 00097 00098 00103 BlastQuery* blastParser_nextQuery (void) 00104 { 00105 char *line,*pos; 00106 static char *queryName = NULL; 00107 static char *prevBlastQueryName = NULL; 00108 static BlastQuery *currBlastQuery = NULL; 00109 int first; 00110 00111 if (!ls_isEof (ls)) { 00112 blastParser_freeQuery (currBlastQuery); 00113 currBlastQuery = NULL; 00114 AllocVar (currBlastQuery); 00115 currBlastQuery->entries = arrayCreate (5,BlastEntry); 00116 first = 1; 00117 while (line = ls_nextLine (ls)) { 00118 if (line[0] == '\0') { 00119 continue; 00120 } 00121 pos = strchr (line,'\t'); 00122 *pos = '\0'; 00123 strReplace (&queryName,line); 00124 if (first == 1 || strEqual (prevBlastQueryName,queryName)) { 00125 blastParser_processLine (pos + 1,currBlastQuery); 00126 } 00127 else { 00128 ls_back (ls,1); 00129 return currBlastQuery; 00130 } 00131 if (first == 1) { 00132 currBlastQuery->qName = hlr_strdup (queryName); 00133 first = 0; 00134 } 00135 strReplace(&prevBlastQueryName,queryName); 00136 } 00137 if (first == 1) { 00138 return NULL; 00139 } 00140 else { 00141 return currBlastQuery; 00142 } 00143 } 00144 blastParser_freeQuery (currBlastQuery); 00145 currBlastQuery = NULL; 00146 return NULL; 00147 } 00148 00149 00150