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
|
/*******************************************************************************************
*
* Display a specified set of reads of a database in fasta format.
*
* Author: Gene Myers
* Date : September 2013
* Mod : With DB overhaul, made this a routine strictly for printing a selected subset
* and created DB2fasta for recreating all the fasta files of a DB
* Date : April 2014
* Mod : Added options to display QV streams
* Date : July 2014
*
********************************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "DB.h"
#ifdef HIDE_FILES
#define PATHSEP "/."
#else
#define PATHSEP "/"
#endif
static char Usage[] = "[-p] <path:db|dam> [ <reads:FILE> | <reads:range> ... ]";
#define LAST_READ_SYMBOL '$'
#define MAX_BUFFER 10001
typedef struct
{ FILE *input;
int lineno;
int read;
int beg;
int end;
} File_Iterator;
File_Iterator *init_file_iterator(FILE *input)
{ File_Iterator *it;
it = Malloc(sizeof(File_Iterator),"Allocating file iterator");
it->input = input;
it->lineno = 1;
rewind(input);
return (it);
}
int next_read(File_Iterator *it)
{ static char nbuffer[MAX_BUFFER];
char *eol;
int x;
if (fgets(nbuffer,MAX_BUFFER,it->input) == NULL)
{ if (feof(it->input))
return (1);
SYSTEM_READ_ERROR;
}
if ((eol = index(nbuffer,'\n')) == NULL)
{ fprintf(stderr,"%s: Line %d in read list is longer than %d chars!\n",
Prog_Name,it->lineno,MAX_BUFFER-1);
return (1);
}
*eol = '\0';
x = sscanf(nbuffer," %d %d %d",&(it->read),&(it->beg),&(it->end));
if (x == 1)
it->beg = -1;
else if (x != 3)
{ fprintf(stderr,"%s: Line %d of read list is improperly formatted\n",Prog_Name,it->lineno);
return (1);
}
it->lineno += 1;
return (0);
}
int main(int argc, char *argv[])
{ DAZZ_DB _db, *db = &_db;
DAZZ_TRACK *map;
int reps, *pts;
int input_pts;
File_Iterator *iter = NULL;
FILE *input;
int PRETTY;
// Process arguments
{ int i, j, k;
int flags[128];
ARG_INIT("DASmap")
j = 1;
for (i = 1; i < argc; i++)
if (argv[i][0] == '-')
ARG_FLAGS("p")
else
argv[j++] = argv[i];
argc = j;
PRETTY = flags['p'];
if (argc <= 1)
{ fprintf(stderr,"Usage: %s %s\n",Prog_Name,Usage);
fprintf(stderr,"\n");
fprintf(stderr," -p: Pretty print (vs easy to parse).\n");
exit (1);
}
}
// Open DB or DAM, and if a DAM open also .hdr file
{ int status, kind;
status = Open_DB(argv[1],db);
if (status < 0)
exit (1);
if (status == 1)
{ fprintf(stderr,"%s: Cannot be called on a .dam index: %s\n",Prog_Name,argv[1]);
exit (1);
}
if (db->part)
{ fprintf(stderr,"%s: Cannot be called on a block: %s\n",Prog_Name,argv[1]);
exit (1);
}
status = Check_Track(db,"map",&kind);
if (status == -2)
{ fprintf(stderr,"%s: 'map' track not found.\n",Prog_Name);
exit (1);
}
else if (status == -1)
{ fprintf(stderr,"%s: Warning: 'map' track not sync'd with db.\n",Prog_Name);
exit (1);
}
map = Load_Track(db,"map");
}
// Process read index arguments into a list of read ranges
input_pts = 0;
if (argc == 3)
{ if (argv[2][0] != LAST_READ_SYMBOL || argv[2][1] != '\0')
{ char *eptr, *fptr;
int b, e;
b = strtol(argv[2],&eptr,10);
if (eptr > argv[2] && b > 0)
{ if (*eptr == '-')
{ if (eptr[1] != LAST_READ_SYMBOL || eptr[2] != '\0')
{ e = strtol(eptr+1,&fptr,10);
input_pts = (fptr <= eptr+1 || *fptr != '\0' || e <= 0);
}
}
else
input_pts = (*eptr != '\0');
}
else
input_pts = 1;
}
}
if (input_pts)
{ input = Fopen(argv[2],"r");
if (input == NULL)
exit (1);
iter = init_file_iterator(input);
}
else
{ pts = (int *) Malloc(sizeof(int)*2*(argc-1),"Allocating read parameters");
if (pts == NULL)
exit (1);
reps = 0;
if (argc > 2)
{ int c, b, e;
char *eptr, *fptr;
for (c = 2; c < argc; c++)
{ if (argv[c][0] == LAST_READ_SYMBOL)
{ b = db->nreads;
eptr = argv[c]+1;
}
else
b = strtol(argv[c],&eptr,10);
if (eptr > argv[c])
{ if (b <= 0)
{ fprintf(stderr,"%s: %d is not a valid index\n",Prog_Name,b);
exit (1);
}
if (*eptr == 0)
{ pts[reps++] = b;
pts[reps++] = b;
continue;
}
else if (*eptr == '-')
{ if (eptr[1] == LAST_READ_SYMBOL)
{ e = db->nreads;
fptr = eptr+2;
}
else
e = strtol(eptr+1,&fptr,10);
if (fptr > eptr+1 && *fptr == 0 && e > 0)
{ pts[reps++] = b;
pts[reps++] = e;
if (b > e)
{ fprintf(stderr,"%s: Empty range '%s'\n",Prog_Name,argv[c]);
exit (1);
}
continue;
}
}
}
fprintf(stderr,"%s: argument '%s' is not an integer range\n",Prog_Name,argv[c]);
exit (1);
}
}
else
{ pts[reps++] = 1;
pts[reps++] = db->nreads;
}
}
// Display each read (and/or QV streams) in the active DB according to the
// range pairs in pts[0..reps) and according to the display options.
{ int c, b, e, i;
int64 *anno;
int *data;
int64 s, f, j;
anno = (int64 *) map->anno;
data = (int *) map->data;
c = 0;
while (1)
{ if (input_pts)
{ if (next_read(iter))
break;
e = iter->read;
b = e-1;
}
else
{ if (c >= reps)
break;
b = pts[c]-1;
e = pts[c+1];
if (e > db->nreads)
e = db->nreads;
c += 2;
}
if (PRETTY)
for (i = b; i < e; i++)
{ s = (anno[i] >> 2);
f = (anno[i+1] >> 2);
printf(" %d -> %d(%d)",i+1,data[s]+1,data[s+1]);
for (j = s+2; j < f; j += 3)
{ printf(" [%d,%d]",data[j],data[j+1]);
if (j+2 < f)
printf(" %d",data[j+2]);
}
printf("\n");
}
else
for (i = b; i < e; i++)
{ s = (anno[i] >> 2);
f = (anno[i+1] >> 2);
printf(" %d %d %d %lld",i+1,data[s]+1,data[s+1],(f-s)-2);
for (j = s+2; j < f; j += 3)
{ printf(" %d %d",data[j],data[j+1]);
if (j+2 < f)
printf(" %d",data[j+2]);
}
printf("\n");
}
}
}
if (input_pts)
{ fclose(input);
free(iter);
}
else
free(pts);
Close_DB(db);
exit (0);
}
|