1 /* @source cachedas application
2  **
3  ** Prepares EMBOSS cache file for DAS servers or for the DAS registry.
4  **
5  **
6  ** This program is free software; you can redistribute it and/or
7  ** modify it under the terms of the GNU General Public License
8  ** as published by the Free Software Foundation; either version 2
9  ** of the License, or (at your option) any later version.
10  **
11  ** This program is distributed in the hope that it will be useful,
12  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  ** GNU General Public License for more details.
15  **
16  ** You should have received a copy of the GNU General Public License
17  ** along with this program; if not, write to the Free Software
18  ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  ******************************************************************************/
20 
21 #include "emboss.h"
22 #include "ajdas.h"
23 #include "ajfeatread.h"
24 
25 
26 
27 
28 static AjPTable     cachedasGetTitleCount(const AjPDasServer server);
29 
30 
31 
32 
33 /* @prog cachedas *************************************************************
34  **
35  ** Prepares EMBOSS cache file for DAS servers or for the DAS registry.
36  **
37  *****************************************************************************/
38 
main(int argc,char ** argv)39 int main(int argc, char **argv)
40 {
41     AjPDasServer server   = NULL;
42     AjPDasSource source   = NULL;
43 
44     AjPStr host  = NULL;
45     AjPStr path  = NULL;
46     AjPFile outf = NULL;
47     AjPFile cachef = NULL;
48 
49     ajint port = 80;
50 
51     AjBool sequencesourcesonly;
52     AjBool quickexit = ajFalse;
53 
54     AjIList iter     = NULL;
55     AjPFilebuff buff = NULL;
56     AjPStr url       = NULL;
57 
58     AjPStr dbhttpver = ajStrNew();
59     AjPStr dbname    = ajStrNew();
60     AjPStr dbproxy   = ajStrNew();
61 
62     AjPStr servername   = NULL;
63     AjPTable titlecount = NULL;
64 
65     embInit("cachedas", argc, argv);
66 
67     host = ajAcdGetString("host");
68     path = ajAcdGetString("path");
69     port = ajAcdGetInt("port");
70     sequencesourcesonly = ajAcdGetBoolean("sequencesourcesonly");
71 
72     servername = ajAcdGetString("servername");
73 
74     outf   = ajAcdGetOutfile("outfile");
75     cachef = ajAcdGetOutfile("cachefile");
76 
77     server = ajDasServerNew();
78 
79     if(cachef)
80     {
81 	url = ajStrNew();
82 
83 	if(!ajNamServer(servername))
84 	{
85 	    ajWarn("following das server is required to be defined "
86 		    "for test queries...");
87 	    ajWarn("\nSERVER %S [\n"
88 		    "   type: \"sequence\"\n"
89 		    "   method: \"das\"\n"
90 		    "   url: \"http://%S%S\"\n"
91 		    "]\n",servername, host,path);
92 	}
93 	else
94 	{
95 	    ajNamSvrGetUrl(servername, &url);
96 	    ajHttpUrlDeconstruct(url, &port, &host, &path);
97 
98 	    ajStrDel(&url);
99 	}
100     }
101 
102     ajDasServerSethostS(server,host);
103     ajDasServerSetport(server,port);
104 
105 
106     if(ajStrGetCharLast(path)!='/')
107 	ajStrAppendK(&path,'/');
108 
109     ajStrAppendC(&path,"sources");
110 
111     ajDasServerSetpathS(server,path);
112 
113     ajFmtPrintF(outf,"host = %S\npath = %S\nport = %d\n",
114 	    server->host, server->path, server->port);
115 
116     buff = ajHttpRead(dbhttpver, dbname, dbproxy, host, port, path);
117 
118     if(!buff)
119 	ajExitBad();
120 
121     ajFilebuffHtmlNoheader(buff);
122 
123     ajDasParseRegistry(buff, server->sources);
124     ajFmtPrintF(outf,"DAS sources and descriptions\n\n");
125 
126     titlecount = cachedasGetTitleCount(server);
127 
128     if(cachef)
129 	ajDasPrintCachefile(server, cachef);
130 
131     iter = ajListIterNew(server->sources);
132 
133     while(!ajListIterDone(iter) && !quickexit)
134     {
135 	source = ajListIterGet(iter);
136 
137 
138 	if ((sequencesourcesonly && !source->sequence))
139 	    continue;
140 
141 	ajFmtPrintF(outf,"%-30S %-50S\n%S\n",source->uri,source->title,
142 		source->description);
143     }
144 
145     ajListIterDel(&iter);
146 
147     ajDasServerDel(&server);
148     ajFilebuffDel(&buff);
149 
150     ajStrDel(&host);
151     ajStrDel(&path);
152     ajStrDel(&servername);
153 
154     ajStrDel(&dbhttpver);
155     ajStrDel(&dbname);
156     ajStrDel(&dbproxy);
157 
158     ajFileClose(&outf);
159     ajFileClose(&cachef);
160 
161     ajTableDelValdel(&titlecount, &ajMemFree);
162 
163     embExit();
164 
165     return 0;
166 }
167 
168 
169 
170 
171 /* @funcstatic cachedasGetTitleCount ******************************************
172 **
173 ** Returns record of in how many different sources titles are used.
174 **
175 ** If a title is used in more than once 'source', 'uri' attributes
176 ** are also used to get DB names to make sure they are unique.
177 **
178 ** @param [r] server [const AjPDasServer] DAS server obj
179 ** @return [AjPTable] table of usage count for each title
180 ** @@
181 ******************************************************************************/
182 
cachedasGetTitleCount(const AjPDasServer server)183 static AjPTable cachedasGetTitleCount(const AjPDasServer server)
184 {
185     AjIList iter  = NULL;
186 
187     AjPTable titlecount = NULL;
188     AjPDasSource source = NULL;
189 
190     AjPStr title = NULL;
191 
192     ajuint* count = NULL;
193 
194     titlecount = ajTablestrNewCaseConst(ajListGetLength(server->sources)+20);
195 
196 
197     iter = ajListIterNew(server->sources);
198 
199     while(!ajListIterDone(iter))
200     {
201 	source = ajListIterGet(iter);
202 
203 	title = source->title;
204 
205 	count = ajTableFetchmodS(titlecount,title);
206 
207 	if (count != NULL)
208         {
209 	    (*count)++;
210         }
211 	else
212 	{
213 	    AJNEW(count);
214 	    *count = 1;
215             ajTablePut(titlecount, title, (void*)count);
216 	}
217 
218     }
219 
220     ajListIterDel(&iter);
221 
222     return titlecount;
223 }
224