1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
/* File: genecurate.c
* Author: Richard Durbin (rd@sanger.ac.uk)
* Copyright (C) Richard Durbin, Genome Research Limited, 2002
*-------------------------------------------------------------------
* This file is part of the ACEDB genome database package
*
* Acedb is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111
* or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
* ------------------------------------------------------------------
* Description: temporary standalone implementation of genecurate.h
* Exported functions:
* HISTORY:
* Last edited: Apr 19 18:43 2002 (edgrif)
* Created: Tue Jan 22 23:30:14 2002 (rd)
* CVS info: $Id: genecurate.c,v 1.2 2002/04/19 17:52:37 edgrif Exp $
*-------------------------------------------------------------------
*/
/* models required by this file
NB some changes from RD
?Gene Version UNIQUE Int
Name Public_name UNIQUE ?Gene_name XREF Public_name
// not directly editable
CGC_name UNIQUE ?Gene_name XREF CGC_name
Sequence_name UNIQUE ?Gene_name XREF Sequence_name
Other_name ?Gene_name XREF Other_name #Evidence
History Version_change Int UNIQUE DateType UNIQUE ?Person #Gene_history_action
Merged_into UNIQUE ?Gene XREF Acquires_merge
Acquires_merge ?Gene XREF Merged_into
Split_from UNIQUE ?Gene XREF Split_into
Split_into ?Gene XREF Split_from_parent
Live // remove live to make gene "dead"
Species ?Species
#Gene_history_action Event Created
Killed
Resurrected
Merged_into UNIQUE ?Gene
Acquires_merge UNIQUE ?Gene
Split_from UNIQUE ?Gene
Split_into UNIQUE ?Gene
Name_change
Name CGC_name UNIQUE Text
Sequence_name UNIQUE Text
Other_name UNIQUE Text
#Evidence Paper ?Paper
Person ?Person
// RD 020122 made ?Gene_name tag2 to track how name used
?Gene_name Gene Public_name ?Gene
CGC_name ?Gene
Sequence_name ?Gene
Other_name ?Gene
****************************/
#include "acedb.h"
#include "lex.h"
#include "bs.h"
#include "whooks/systags.h"
#include "genecurate.h"
#include "pick.h"
#include "main.h" /* for checkWriteAccess() */
#include "bindex.h"
#include "keyset.h"
struct GeneConnectionStruct {
KEY user ;
KEY species ;
} ;
static int classGene = 0 ;
static int classGeneName = 0 ;
static int classPerson = 0 ;
static char* idFormat = "WB:gn%06d" ;
static KEY typeTag[3] ;
static void geneCurateInitialise (void) ;
static KEY newGene (void) ;
static BOOL setPublicName (KEY gene) ;
static void updateHistory (KEY gene, KEY user,
char* event, KEY target,
GeneNameType type, char* name) ;
/*************************************************************/
/****** initialise a connection ******************************/
/*************************************************************/
GeneConnection geneConnection (char *host, char* user, KEY species,
STORE_HANDLE handle)
{
GeneConnection gx ;
geneCurateInitialise () ;
if (!user || !*user)
{ messerror ("geneConnection() requires a user string") ;
return 0 ;
}
gx = (GeneConnection) halloc(sizeof(struct GeneConnectionStruct),
handle) ;
lexaddkey (user, &gx->user, classPerson) ;
gx->species = species ;
return gx ;
}
/*************************************************************/
/****** primary actions to create, merge, split, kill etc. ***/
/*************************************************************/
KEY geneCreate (GeneConnection gx, GeneNameType type, char* name)
{
KEY gene, nameKey ;
OBJ obj ;
if (!gx || !checkWriteAccess ())
return 0 ;
if (type > OTHER_NAME)
{ messerror ("Bad type %d in geneCreate()", type) ; return 0 ; }
gene = newGene() ;
obj = bsUpdate (gene) ;
lexaddkey (name, &nameKey, classGeneName) ;
bsAddKey (obj, typeTag[type], nameKey) ;
if (gx->species) bsAddKey (obj, str2tag("Species"), gx->species) ;
bsAddTag (obj, str2tag("Live")) ;
bsSave (obj) ;
updateHistory (gene, gx->user, "Created", 0, type, name) ;
setPublicName (gene) ;
return gene ;
}
/**************************************************************/
BOOL geneKill (GeneConnection gx, KEY gene)
{
OBJ obj ;
if (!gx || !checkWriteAccess () ||
class(gene) != classGene || !(obj = bsUpdate(gene)))
return FALSE ;
if (bsFindTag (obj, str2tag("Live")))
{ bsRemove (obj) ;
bsSave (obj) ;
updateHistory (gene, gx->user, "Killed", 0, 0, 0) ;
return TRUE ;
}
else
{ bsDestroy (obj) ;
return FALSE ;
}
}
/**************************************************************/
BOOL geneResurrect (GeneConnection gx, KEY gene)
{
OBJ obj ;
if (!gx || !checkWriteAccess () ||
class(gene) != classGene || !(obj = bsUpdate(gene)))
return FALSE ;
if (bsAddTag (obj, str2tag("Live")))
{ bsSave (obj) ;
updateHistory (gene, gx->user, "Resurrected", 0, 0, 0) ;
return TRUE ;
}
else
{ bsDestroy (obj) ;
return FALSE ;
}
}
/**************************************************************/
BOOL geneMerge (GeneConnection gx, KEY gene, KEY target)
{
OBJ obj ;
if (!gx || !checkWriteAccess () || class(target) != classGene
|| !bIndexTag (target, str2tag("Live")) ||
class(gene) != classGene || !(obj = bsUpdate(gene)))
return FALSE ;
if (bsFindTag (obj, str2tag("Live")))
{ bsRemove (obj) ;
bsSave (obj) ;
updateHistory (gene, gx->user, "Merged_into", target, 0, 0) ;
updateHistory (target, gx->user, "Acquires_merge", gene, 0, 0) ;
return TRUE ;
}
else
{ bsDestroy (obj) ;
return FALSE ;
}
}
/**************************************************************/
KEY geneSplit (GeneConnection gx, KEY gene,
GeneNameType type, char *name)
{
KEY child, nameKey ;
OBJ obj ;
if (!gx || !checkWriteAccess () || class(gene) != classGene ||
!bIndexTag (gene, str2tag("Live")))
return FALSE ;
if (type > OTHER_NAME)
{ messerror ("Bad type %d in geneCreate()", type) ; return 0 ; }
child = newGene() ;
obj = bsUpdate (child) ;
lexaddkey (name, &nameKey, classGeneName) ;
bsAddKey (obj, typeTag[type], nameKey) ;
if (gx->species) bsAddKey (obj, str2tag("Species"), gx->species) ;
bsAddTag (obj, str2tag("Live")) ;
bsSave (obj) ;
updateHistory (child, gx->user, "Split_from", gene, type, name) ;
updateHistory (gene, gx->user, "Split_into", child, 0, 0) ;
setPublicName (child) ;
return child ;
}
/*************************************************************/
/****** name functions ***************************************/
/*************************************************************/
static KEYSET geneList = 0 ;
static GeneNameType lastType ;
static char* lastName ;
static void makeGeneList (GeneNameType type, char *name) ;
int geneNameCount (GeneConnection gx, GeneNameType type, char *name)
{
if (!gx) return 0 ;
if (!lastName || lastType != type || strcmp(lastName, name))
makeGeneList (type, name) ;
return keySetMax(geneList) ;
}
/****************************************/
KEY geneNameGet (GeneConnection gx,
GeneNameType type, char *name, int n)
{
if (!gx) return 0 ;
if (!lastName || lastType != type || strcmp(lastName, name))
makeGeneList (type, name) ;
if (n >= 0 && n < keySetMax(geneList))
return keySet(geneList, n) ;
else
return 0 ;
}
/****************************************/
static void makeGeneList (GeneNameType type, char *name)
{
KEY geneName ;
OBJ obj ;
int i ;
static Array units = 0 ;
geneList = keySetReCreate (geneList) ;
if (!lexword2key (name, &geneName, classGeneName) ||
!(obj = bsCreate (geneName)))
return ;
units = arrayReCreate (units, 16, BSunit) ;
bsGetArray (obj, str2tag("Gene"), units, 2) ; /* tag 2 */
for (i = 1 ; i < arrayMax(units) ; i += 2)
keySet(geneList, keySetMax(geneList)) = arr(units, i, BSunit).k ;
bsDestroy (obj) ;
lastType = type ;
if (lastName) messfree (lastName) ;
lastName = strnew (name, 0) ; /* local copy */
}
/*************************************************************/
/****** a little utility to follow a chain of merges *********/
/*************************************************************/
BOOL geneDescendant (GeneConnection gx, KEY *gene)
{
OBJ obj = 0 ;
while ((obj = bsCreate (*gene)))
{ if (bsFindTag (obj, str2tag ("Live")))
{ bsDestroy (obj) ;
return TRUE ;
}
if (!bsGetKey (obj, str2tag ("Merged_into"), gene))
{ bsDestroy (obj) ;
return FALSE ;
}
bsDestroy (obj) ;
}
return FALSE ;
}
/*************************************************************/
/****** local functions **************************************/
/*************************************************************/
static void geneCurateInitialise (void)
{
int isDone = FALSE ;
if (isDone) return ;
if (!(classGene = pickWord2Class ("Gene")))
messcrash ("No Gene class") ;
if (!(classGeneName = pickWord2Class ("Gene_name")))
messcrash ("No Gene_name class") ;
if (!(classPerson = pickWord2Class ("Person")))
messcrash ("No Person class") ;
typeTag[0] = str2tag("CGC_name") ;
typeTag[1] = str2tag("Sequence_name") ;
typeTag[2] = str2tag("Other_name") ;
isDone = TRUE ;
}
/*************************************************************/
static KEY newGene (void)
{
KEY gene ;
int n = 0 ;
while (!lexaddkey (messprintf (idFormat, ++n), &gene, classGene)) ;
return gene ;
}
/*************************************************************/
static BOOL setPublicName (KEY gene) /* return if name changes */
{
OBJ obj = bsUpdate (gene) ;
KEY name ;
BOOL result = FALSE ;
if (!obj)
return FALSE ;
if (bsGetKey (obj, str2tag("CGC_name"), &name))
result = bsAddKey (obj, str2tag("Public_name"), name) ;
else if (bsGetKey (obj, str2tag("Sequence_name"), &name))
result = bsAddKey (obj, str2tag("Public_name"), name) ;
else if (bsGetKey (obj, str2tag("Other_name"), &name))
result = bsAddKey (obj, str2tag("Public_name"), name) ;
bsSave (obj) ;
return result ;
}
/*************************************************************/
static void updateHistory (KEY gene, KEY user,
char* event, KEY target,
GeneNameType type, char* newName)
{
OBJ obj = bsUpdate (gene) ;
int version = 0 ;
mytime_t time = timeParse ("now") ;
if (!obj) return ;
bsGetData (obj, str2tag("Version"), _Int, &version) ;
++version ;
if (!bsAddData (obj, str2tag("Version"), _Int, &version))
messerror ("updateHistory could not update Version") ;
if (bsAddData (obj, str2tag("Version_change"), _Int, &version) &&
bsAddData (obj, _bsRight, _DateType, &time) &&
bsAddKey (obj, _bsRight, user) &&
bsPushObj (obj))
{ if (!bsAddTag (obj, str2tag(event)))
messerror ("Unknown event %s in updateHistory()", event) ;
if (target && !bsAddKey (obj, _bsRight, target))
messerror ("Couldn't add target in updateHistory()", event) ;
if (!bsAddData (obj, typeTag[type], _Text, newName))
messerror ("Couldn't add nameType %s in updateHistory()",
name(typeTag[type])) ;
}
else
messerror ("updateHistory could not add Version_change") ;
if (!strcmp (event, "Merged_into"))
bsAddKey (obj, str2tag(event), target) ;
else if (!strcmp (event, "Split_from"))
bsAddKey (obj, str2tag(event), target) ;
/* don't need reciprocals since done by XREF */
bsSave (obj) ;
}
/********************* end of file ******************/
|