/* plasEndPairs.c was originally generated by the autoSql program, which also 
 * generated plasEndPairs.h and plasEndPairs.sql.  This module links the database and
 * the RAM representation of objects. */

/* Copyright (C) 2014 The Regents of the University of California 
 * See kent/LICENSE or http://genome.ucsc.edu/license/ for licensing information. */

#include "common.h"
#include "linefile.h"
#include "dystring.h"
#include "jksql.h"
#include "plasEndPairs.h"


struct plasEndPairs *plasEndPairsLoad(char **row)
/* Load a plasEndPairs from row fetched with select * from plasEndPairs
 * from database.  Dispose of this with plasEndPairsFree(). */
{
struct plasEndPairs *ret;
int sizeOne;

AllocVar(ret);
ret->lfCount = sqlUnsigned(row[8]);
ret->bin = sqlSigned(row[0]);
ret->chrom = cloneString(row[1]);
ret->chromStart = sqlUnsigned(row[2]);
ret->chromEnd = sqlUnsigned(row[3]);
ret->name = cloneString(row[4]);
ret->score = sqlUnsigned(row[5]);
strcpy(ret->strand, row[6]);
ret->pslTable = cloneString(row[7]);
sqlUnsignedDynamicArray(row[9], &ret->lfStarts, &sizeOne);
assert(sizeOne == ret->lfCount);
sqlUnsignedDynamicArray(row[10], &ret->lfSizes, &sizeOne);
assert(sizeOne == ret->lfCount);
sqlStringDynamicArray(row[11], &ret->lfNames, &sizeOne);
assert(sizeOne == ret->lfCount);
return ret;
}

struct plasEndPairs *plasEndPairsLoadAll(char *fileName) 
/* Load all plasEndPairs from a tab-separated file.
 * Dispose of this with plasEndPairsFreeList(). */
{
struct plasEndPairs *list = NULL, *el;
struct lineFile *lf = lineFileOpen(fileName, TRUE);
char *row[12];

while (lineFileRow(lf, row))
    {
    el = plasEndPairsLoad(row);
    slAddHead(&list, el);
    }
lineFileClose(&lf);
slReverse(&list);
return list;
}

struct plasEndPairs *plasEndPairsLoadWhere(struct sqlConnection *conn, char *table, char *where)
/* Load all plasEndPairs from table that satisfy where clause. The
 * where clause may be NULL in which case whole table is loaded
 * Dispose of this with plasEndPairsFreeList(). */
{
struct plasEndPairs *list = NULL, *el;
struct dyString *query = dyStringNew(256);
struct sqlResult *sr;
char **row;

sqlDyStringPrintf(query, "select * from %s", table);
if (where != NULL)
    sqlDyStringPrintf(query, " where %-s", where);
sr = sqlGetResult(conn, query->string);
while ((row = sqlNextRow(sr)) != NULL)
    {
    el = plasEndPairsLoad(row);
    slAddHead(&list, el);
    }
slReverse(&list);
sqlFreeResult(&sr);
dyStringFree(&query);
return list;
}

struct plasEndPairs *plasEndPairsCommaIn(char **pS, struct plasEndPairs *ret)
/* Create a plasEndPairs out of a comma separated string. 
 * This will fill in ret if non-null, otherwise will
 * return a new plasEndPairs */
{
char *s = *pS;
int i;

if (ret == NULL)
    AllocVar(ret);
ret->bin = sqlSignedComma(&s);
ret->chrom = sqlStringComma(&s);
ret->chromStart = sqlUnsignedComma(&s);
ret->chromEnd = sqlUnsignedComma(&s);
ret->name = sqlStringComma(&s);
ret->score = sqlUnsignedComma(&s);
sqlFixedStringComma(&s, ret->strand, sizeof(ret->strand));
ret->pslTable = sqlStringComma(&s);
ret->lfCount = sqlUnsignedComma(&s);
s = sqlEatChar(s, '{');
AllocArray(ret->lfStarts, ret->lfCount);
for (i=0; i<ret->lfCount; ++i)
    {
    ret->lfStarts[i] = sqlUnsignedComma(&s);
    }
s = sqlEatChar(s, '}');
s = sqlEatChar(s, ',');
s = sqlEatChar(s, '{');
AllocArray(ret->lfSizes, ret->lfCount);
for (i=0; i<ret->lfCount; ++i)
    {
    ret->lfSizes[i] = sqlUnsignedComma(&s);
    }
s = sqlEatChar(s, '}');
s = sqlEatChar(s, ',');
s = sqlEatChar(s, '{');
AllocArray(ret->lfNames, ret->lfCount);
for (i=0; i<ret->lfCount; ++i)
    {
    ret->lfNames[i] = sqlStringComma(&s);
    }
s = sqlEatChar(s, '}');
s = sqlEatChar(s, ',');
*pS = s;
return ret;
}

void plasEndPairsFree(struct plasEndPairs **pEl)
/* Free a single dynamically allocated plasEndPairs such as created
 * with plasEndPairsLoad(). */
{
struct plasEndPairs *el;

if ((el = *pEl) == NULL) return;
freeMem(el->chrom);
freeMem(el->name);
freeMem(el->pslTable);
freeMem(el->lfStarts);
freeMem(el->lfSizes);
/* All strings in lfNames are allocated at once, so only need to free first. */
if (el->lfNames != NULL)
    freeMem(el->lfNames[0]);
freeMem(el->lfNames);
freez(pEl);
}

void plasEndPairsFreeList(struct plasEndPairs **pList)
/* Free a list of dynamically allocated plasEndPairs's */
{
struct plasEndPairs *el, *next;

for (el = *pList; el != NULL; el = next)
    {
    next = el->next;
    plasEndPairsFree(&el);
    }
*pList = NULL;
}

void plasEndPairsOutput(struct plasEndPairs *el, FILE *f, char sep, char lastSep) 
/* Print out plasEndPairs.  Separate fields with sep. Follow last field with lastSep. */
{
int i;
fprintf(f, "%d", el->bin);
fputc(sep,f);
if (sep == ',') fputc('"',f);
fprintf(f, "%s", el->chrom);
if (sep == ',') fputc('"',f);
fputc(sep,f);
fprintf(f, "%u", el->chromStart);
fputc(sep,f);
fprintf(f, "%u", el->chromEnd);
fputc(sep,f);
if (sep == ',') fputc('"',f);
fprintf(f, "%s", el->name);
if (sep == ',') fputc('"',f);
fputc(sep,f);
fprintf(f, "%u", el->score);
fputc(sep,f);
if (sep == ',') fputc('"',f);
fprintf(f, "%s", el->strand);
if (sep == ',') fputc('"',f);
fputc(sep,f);
if (sep == ',') fputc('"',f);
fprintf(f, "%s", el->pslTable);
if (sep == ',') fputc('"',f);
fputc(sep,f);
fprintf(f, "%u", el->lfCount);
fputc(sep,f);
if (sep == ',') fputc('{',f);
for (i=0; i<el->lfCount; ++i)
    {
    fprintf(f, "%u", el->lfStarts[i]);
    fputc(',', f);
    }
if (sep == ',') fputc('}',f);
fputc(sep,f);
if (sep == ',') fputc('{',f);
for (i=0; i<el->lfCount; ++i)
    {
    fprintf(f, "%u", el->lfSizes[i]);
    fputc(',', f);
    }
if (sep == ',') fputc('}',f);
fputc(sep,f);
if (sep == ',') fputc('{',f);
for (i=0; i<el->lfCount; ++i)
    {
    if (sep == ',') fputc('"',f);
    fprintf(f, "%s", el->lfNames[i]);
    if (sep == ',') fputc('"',f);
    fputc(',', f);
    }
if (sep == ',') fputc('}',f);
fputc(lastSep,f);
}

