1 /** @source taxgetrank
2 **
3 ** Find taxons at specific higher ranks
4 **
5 ** @author Copyright (C) 2010 Jon Ison / EMBOSS
6 ** @version 1  First version</replaceable>
7 ** @modified July 2010  Jon Ison First version</replaceable>
8 ** @@
9 **
10 ** This program is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License
12 ** as published by the Free Software Foundation; either version 2
13 ** of the License, or (at your option) any later version.
14 **
15 ** This program is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ** GNU General Public License for more details.
19 **
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program; if not, write to the Free Software
22 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 ********************************************************************/
24 
25 
26 /* ==================================================================== */
27 /* ========================== include files =========================== */
28 /* ==================================================================== */
29 
30 #include "emboss.h"
31 
32 /* Inclusion of system and local header files goes here */
33 
34 
35 
36 /* ==================================================================== */
37 /* ============================ constants ============================= */
38 /* ==================================================================== */
39 
40 /* #define and enum statements go here */
41 
42 
43 
44 /* ==================================================================== */
45 /* ======================== global variables ========================== */
46 /* ==================================================================== */
47 
48 /* Global variables definitions go here */
49 
50 
51 
52 /* ==================================================================== */
53 /* ============================== data ================================ */
54 /* ==================================================================== */
55 
56 /* Definition of datatypes go here */
57 
58 
59 
60 /* ==================================================================== */
61 /* ==================== function prototypes =========================== */
62 /* ==================================================================== */
63 
64 /* Function prototypes for public (external) functions go here */
65 
66 
67 
68 
69 /* @prog taxgetrank ***********************************************************
70 **
71 ** Find NCBI taxons at selected rank(s) above search term
72 **
73 ******************************************************************************/
74 
main(int argc,char ** argv)75 int main(int argc, char **argv)
76 {
77     /* Variable declarations */
78     AjPOutfile  outfile = NULL;
79     AjPStr* ranks = NULL;
80     AjBool hidden = ajFalse;
81 
82     AjPTax tax = NULL;
83     AjPTax taxparent = NULL;
84     AjPTaxall taxall = NULL;
85     AjPTaxin taxinparent = NULL;
86 
87     AjPStr taxqryup = NULL;
88     AjPTable foundtable = NULL;
89     const AjPStr taxrank = NULL;
90 
91     ajuint up;
92     AjPList uplist = NULL;
93     ajuint i;
94 
95     /* ACD processing */
96     embInit("taxgetrank", argc, argv);
97 
98     taxall   = ajAcdGetTaxonall("taxons");
99     ranks   = ajAcdGetList("rank");
100     outfile  = ajAcdGetOuttaxon("outfile");
101     hidden = ajAcdGetBoolean("hidden");
102 
103     for(i=0; ranks[i]; i++)
104     {
105         ajStrExchangeKK(&ranks[i], '_', ' ');
106     }
107 
108     taxinparent = ajTaxinNew();
109     taxparent = ajTaxNew();
110     uplist = ajListNew();
111 
112     foundtable = ajTablestrNew(600);
113 
114     while(ajTaxallNext(taxall, &tax))
115     {
116         up = ajTaxGetParent(tax);
117         while (up > 1)
118         {
119             ajFmtPrintS(&taxqryup, "%S-id:%u", ajTaxGetDb(tax), up);
120             ajTaxinQryS(taxinparent, taxqryup);
121 
122             if(!ajTaxinRead(taxinparent, taxparent))
123                 break;
124 
125             if(hidden || !ajTaxIsHidden(taxparent))
126             {
127                 taxrank = ajTaxGetRank(taxparent);
128                 for(i=0; ranks[i]; i++)
129                 {
130                     if(ajStrMatchS(taxrank, ranks[i]))
131                     {
132                         if(!ajTableMatchS(foundtable, taxparent->Id))
133                         {
134                             ajTaxoutWrite(outfile, taxparent);
135                             ajTablePut(foundtable,
136                                        ajStrNewS(taxparent->Id),
137                                        (void *) 1);
138                         }
139                     }
140 
141                 }
142             }
143 
144             up = ajTaxGetParent(taxparent);
145         }
146     }
147 
148     /* Memory clean-up and exit */
149 
150     ajTaxallDel(&taxall);
151     ajTaxinDel(&taxinparent);
152     ajTaxDel(&tax);
153     ajTaxDel(&taxparent);
154 
155     ajListFree(&uplist);
156 
157     ajTablestrFreeKey(&foundtable);
158 
159     ajStrDel(&taxqryup);
160     ajStrDelarray(&ranks);
161     ajOutfileClose(&outfile);
162 
163     embExit();
164 
165     return 0;
166 }
167 
168 
169 /* ==================================================================== */
170 /* ============================ functions ============================= */
171 /* ==================================================================== */
172 
173