comparison GEMBASSY-1.0.3/src/gbaserelativeentropy.c @ 0:8300eb051bea draft

Initial upload
author ktnyt
date Fri, 26 Jun 2015 05:19:29 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8300eb051bea
1 /******************************************************************************
2 ** @source gbaserelativeentropy
3 **
4 ** Calculates and graphs the sequence conservation using Kullback-Leibler
5 ** divergence (relative entropy)
6 **
7 ** @author Copyright (C) 2012 Hidetoshi Itaya
8 ** @version 1.0.3
9 ** @modified 2012/1/20 Hidetoshi Itaya Created!
10 ** @modified 2013/6/16 Revision 1
11 ** @modified 2015/2/7 Refactor
12 ** @@
13 **
14 ** This program is free software; you can redistribute it and/or
15 ** modify it under the terms of the GNU General Public License
16 ** as published by the Free Software Foundation; either version 2
17 ** of the License, or (at your option) any later version.
18 **
19 ** This program is distributed in the hope that it will be useful,
20 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ** GNU General Public License for more details.
23 **
24 ** You should have received a copy of the GNU General Public License
25 ** along with this program; if not, write to the Free Software
26 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ******************************************************************************/
28
29 #include "emboss.h"
30 #include "glibs.h"
31
32
33
34
35 /* @prog gbaserelativeentropy *************************************************
36 **
37 ** Calculates and graphs the sequence conservation using Kullback-Leibler
38 ** divergence (relative entropy)
39 **
40 ******************************************************************************/
41
42 int main(int argc, char *argv[])
43 {
44 embInitPV("gbaserelativeentropy", argc, argv, "GEMBASSY", "1.0.3");
45
46 AjPSeqall seqall;
47 AjPSeq seq;
48 AjPStr inseq = NULL;
49
50 AjPStr position = 0;
51 ajint PatLen = 0;
52 ajint upstream = 0;
53 ajint downstream = 0;
54
55 AjBool accid = ajFalse;
56 AjPStr restid = NULL;
57 AjPStr seqid = NULL;
58
59 AjPStr base = NULL;
60 AjPStr url = NULL;
61
62 AjPFile tmpfile = NULL;
63 AjPStr tmpname = NULL;
64
65 AjBool plot = 0;
66 AjPFile outf = NULL;
67 AjPFilebuff buff = NULL;
68 AjPGraph mult = NULL;
69
70 gPlotParams gpp;
71 AjPStr title = NULL;
72
73 seqall = ajAcdGetSeqall("sequence");
74 position = ajAcdGetSelectSingle("position");
75 PatLen = ajAcdGetInt("patlen");
76 upstream = ajAcdGetInt("upstream");
77 downstream = ajAcdGetInt("downstream");
78 accid = ajAcdGetBoolean("accid");
79
80 plot = ajAcdGetToggle("plot");
81 outf = ajAcdGetOutfile("outfile");
82 mult = ajAcdGetGraphxy("graph");
83
84 base = ajStrNewC("rest.g-language.org");
85
86 gAssignUniqueName(&tmpname);
87
88 while(ajSeqallNext(seqall, &seq))
89 {
90 inseq = NULL;
91
92 if(!accid)
93 {
94 if(gFormatGenbank(seq, &inseq))
95 {
96 gAssignUniqueName(&tmpname);
97
98 tmpfile = ajFileNewOutNameS(tmpname);
99
100 if(!tmpfile)
101 {
102 ajFmtError("Output file (%S) open error\n", tmpname);
103 embExitBad();
104 }
105
106 ajFmtPrintF(tmpfile, "%S", inseq);
107 ajFileClose(&tmpfile);
108 ajFmtPrintS(&url, "http://%S/upload/upl.pl", base);
109 gFilePostSS(url, tmpname, &restid);
110 ajStrDel(&url);
111 ajSysFileUnlinkS(tmpname);
112 }
113 else
114 {
115 ajWarn("Sequence does not have features\n"
116 "Proceeding with sequence accession ID\n");
117 accid = ajTrue;
118 }
119 }
120
121 ajStrAssignS(&seqid, ajSeqGetAccS(seq));
122
123 if(ajStrGetLen(seqid) == 0)
124 {
125 ajStrAssignS(&seqid, ajSeqGetNameS(seq));
126 }
127
128 if(ajStrGetLen(seqid) == 0)
129 {
130 ajWarn("No valid header information\n");
131 }
132
133 if(accid)
134 {
135 ajStrAssignS(&restid, seqid);
136 if(ajStrGetLen(seqid) == 0)
137 {
138 ajDie("Cannot proceed without header with -accid\n");
139 }
140
141 if(!gValID(seqid))
142 {
143 ajDie("Invalid accession ID:%S, exiting\n", seqid);
144 }
145 }
146
147 url = ajStrNew();
148
149 ajFmtPrintS(&url, "http://%S/%S/base_relative_entropy/position=%S/"
150 "PatLen=%d/upstream=%d/downstream=%d/output=f/tag=gene",
151 base, restid, position, PatLen, upstream, downstream);
152
153 if(plot)
154 {
155 title = ajStrNew();
156
157 ajStrAppendC(&title, argv[0]);
158 ajStrAppendC(&title, " of ");
159 ajStrAppendS(&title, seqid);
160
161 gpp.title = ajStrNewS(title);
162 gpp.xlab = ajStrNewC("position");
163 gpp.ylab = ajStrNewC("relative entropy");
164
165 if(!gFilebuffURLS(url, &buff))
166 {
167 ajDie("File downloading error from:\n%S\n", url);
168 }
169
170 if(!gPlotFilebuff(buff, mult, &gpp))
171 {
172 ajDie("Error in plotting\n");
173 }
174
175 AJFREE(gpp.title);
176 AJFREE(gpp.xlab);
177 AJFREE(gpp.ylab);
178 ajStrDel(&title);
179 ajFilebuffDel(&buff);
180 }
181 else
182 {
183 ajFmtPrintF(outf, "Sequence: %S\n", seqid);
184 if(!gFileOutURLS(url, &outf))
185 {
186 ajDie("File downloading error from:\n%S\n", url);
187 }
188 }
189
190 ajStrDel(&url);
191 ajStrDel(&restid);
192 ajStrDel(&seqid);
193 ajStrDel(&inseq);
194 }
195
196 ajFileClose(&outf);
197
198 ajSeqallDel(&seqall);
199 ajSeqDel(&seq);
200 ajStrDel(&base);
201
202 ajStrDel(&position);
203
204 embExit();
205
206 return 0;
207 }