| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <unistd.h> |
| 4 |
#include <string.h> |
| 5 |
#include <errno.h> |
| 6 |
|
| 7 |
#include "bio.h" |
| 8 |
#include "sim4.H" |
| 9 |
|
| 10 |
int |
| 11 |
main(int argc, char ** argv) { |
| 12 |
uint32 minC = 0; |
| 13 |
uint32 minI = 0; |
| 14 |
uint32 minL = 0; |
| 15 |
uint32 cdna = ~uint32ZERO; |
| 16 |
uint32 geno = ~uint32ZERO; |
| 17 |
uint32 minExons = 0; |
| 18 |
uint32 maxExons = ~uint32ZERO; |
| 19 |
uint32 beVerbose = 0; |
| 20 |
int GOODsilent = 0; |
| 21 |
sim4polishWriter *GOOD = 0L; |
| 22 |
int CRAPsilent = 0; |
| 23 |
sim4polishWriter *CRAP = 0L; |
| 24 |
sim4polishWriter *JUNK = 0L; |
| 25 |
uint64 pmod = 1; |
| 26 |
uint64 good = 0; |
| 27 |
uint64 crap = 0; |
| 28 |
uint64 junk = 0; |
| 29 |
int doSelfFilter = 0; |
| 30 |
int doSegregation = 0; |
| 31 |
uint32 doSegregationLo = 0; |
| 32 |
uint32 doSegregationHi = 0; |
| 33 |
char *filePrefixGOOD = 0L; |
| 34 |
char *filePrefixCRAP = 0L; |
| 35 |
char *filePrefixJUNK = 0L; |
| 36 |
sim4polishWriter **SEGREGATE = 0L; |
| 37 |
bool noDefLines = false; |
| 38 |
bool noAlignments = false; |
| 39 |
bool doGFF3 = false; |
| 40 |
sim4polishStyle style = sim4polishStyleDefault; |
| 41 |
|
| 42 |
// We limit scaffolds to be below the number of open files per |
| 43 |
// process. |
| 44 |
// |
| 45 |
uint32 maxScaffold = sysconf(_SC_OPEN_MAX); |
| 46 |
|
| 47 |
int arg = 1; |
| 48 |
while (arg < argc) { |
| 49 |
if (strncmp(argv[arg], "-verbose", 2) == 0) { |
| 50 |
beVerbose = 1; |
| 51 |
|
| 52 |
} else if (strncmp(argv[arg], "-c", 2) == 0) { |
| 53 |
minC = atoi(argv[++arg]); |
| 54 |
|
| 55 |
} else if (strncmp(argv[arg], "-i", 2) == 0) { |
| 56 |
minI = atoi(argv[++arg]); |
| 57 |
|
| 58 |
} else if (strncmp(argv[arg], "-l", 2) == 0) { |
| 59 |
minL = atoi(argv[++arg]); |
| 60 |
|
| 61 |
} else if (strncmp(argv[arg], "-minexons", 3) == 0) { |
| 62 |
minExons = atoi(argv[++arg]); |
| 63 |
|
| 64 |
} else if (strncmp(argv[arg], "-maxexons", 3) == 0) { |
| 65 |
maxExons = atoi(argv[++arg]); |
| 66 |
|
| 67 |
} else if (strncmp(argv[arg], "-o", 2) == 0) { |
| 68 |
filePrefixGOOD = argv[++arg]; |
| 69 |
GOODsilent = 0; |
| 70 |
|
| 71 |
} else if (strncmp(argv[arg], "-O", 2) == 0) { |
| 72 |
GOODsilent = 1; |
| 73 |
|
| 74 |
} else if (strncmp(argv[arg], "-d", 2) == 0) { |
| 75 |
filePrefixCRAP = argv[++arg]; |
| 76 |
CRAPsilent = 0; |
| 77 |
|
| 78 |
} else if (strncmp(argv[arg], "-q", 2) == 0) { |
| 79 |
CRAPsilent = 1; |
| 80 |
|
| 81 |
} else if (strncmp(argv[arg], "-D", 2) == 0) { |
| 82 |
CRAPsilent = 1; |
| 83 |
|
| 84 |
} else if (strncmp(argv[arg], "-j", 2) == 0) { |
| 85 |
filePrefixJUNK = argv[++arg]; |
| 86 |
|
| 87 |
} else if (strncmp(argv[arg], "-C", 2) == 0) { |
| 88 |
cdna = atoi(argv[++arg]); |
| 89 |
|
| 90 |
} else if (strncmp(argv[arg], "-G", 2) == 0) { |
| 91 |
geno = atoi(argv[++arg]); |
| 92 |
|
| 93 |
} else if (strncmp(argv[arg], "-selfhits", 4) == 0) { |
| 94 |
doSelfFilter = 1; |
| 95 |
|
| 96 |
} else if (strncmp(argv[arg], "-segregate", 4) == 0) { |
| 97 |
doSegregation = 1; |
| 98 |
doSegregationLo = atoi(argv[++arg]); |
| 99 |
doSegregationHi = atoi(argv[++arg]); |
| 100 |
if (doSegregationHi - doSegregationLo + 1 > maxScaffold) |
| 101 |
fprintf(stderr, "error: -segregate range too big; must be less than %u.\n", maxScaffold), exit(1); |
| 102 |
SEGREGATE = new sim4polishWriter * [maxScaffold]; |
| 103 |
memset(SEGREGATE, 0, sizeof(sim4polishWriter *) * maxScaffold); |
| 104 |
|
| 105 |
} else if (strncmp(argv[arg], "-nodeflines", 4) == 0) { |
| 106 |
noDefLines = true; |
| 107 |
|
| 108 |
} else if (strncmp(argv[arg], "-noalignments", 4) == 0) { |
| 109 |
noAlignments = true; |
| 110 |
|
| 111 |
} else if (strncmp(argv[arg], "-gff3", 4) == 0) { |
| 112 |
doGFF3 = true; |
| 113 |
style = sim4polishGFF3; |
| 114 |
|
| 115 |
} else { |
| 116 |
fprintf(stderr, "UNKNOWN option '%s'\n", argv[arg]); |
| 117 |
exit(1); |
| 118 |
} |
| 119 |
|
| 120 |
arg++; |
| 121 |
} |
| 122 |
|
| 123 |
if (isatty(fileno(stdin))) { |
| 124 |
fprintf(stderr, "usage: %s [-c c] [-i i] [-o o]\n", argv[0]); |
| 125 |
fprintf(stderr, " -verbose Report progress\n"); |
| 126 |
fprintf(stderr, "\n"); |
| 127 |
fprintf(stderr, " -c c Discard polishes below c%% composite (default: 0).\n"); |
| 128 |
fprintf(stderr, " -i i Discard polishes below i%% identity (default: 0).\n"); |
| 129 |
fprintf(stderr, " -l l Discard polishes below l identities (default: 0).\n"); |
| 130 |
fprintf(stderr, "\n"); |
| 131 |
fprintf(stderr, " -minexons e Discard polishes below e exons (default: 0).\n"); |
| 132 |
fprintf(stderr, " -maxexons e Discard polishes above e exons (default: infinity).\n"); |
| 133 |
fprintf(stderr, "\n"); |
| 134 |
fprintf(stderr, " -C c Discard polishes that are not from cDNA idx 'c'\n"); |
| 135 |
fprintf(stderr, " -G g Discard polishes that are not from genomic idx 'g'\n"); |
| 136 |
fprintf(stderr, "\n"); |
| 137 |
fprintf(stderr, " -o o Write saved polishes to the 'o' file (default == stdout).\n"); |
| 138 |
fprintf(stderr, " -O Don't write saved polishes.\n"); |
| 139 |
fprintf(stderr, "\n"); |
| 140 |
fprintf(stderr, " -d o Write discarded polishes to the 'o' file (default == stdout).\n"); |
| 141 |
fprintf(stderr, " -D Don't write discarded polishes.\n"); |
| 142 |
fprintf(stderr, "\n"); |
| 143 |
fprintf(stderr, " -j o Write intractable and aborted polishes to the 'o' file. By\n"); |
| 144 |
fprintf(stderr, " default these are silently discarded.\n"); |
| 145 |
fprintf(stderr, "\n"); |
| 146 |
fprintf(stderr, " -selfhits Filter out alignments to ourself -- if you did an all-to-all\n"); |
| 147 |
fprintf(stderr, " mapping of a set onto itself. Deflines needed!\n"); |
| 148 |
fprintf(stderr, "\n"); |
| 149 |
fprintf(stderr, " -segregate a b Segregate polishes by genomic idx, for idx's between a and b inclusive.\n"); |
| 150 |
fprintf(stderr, " b-a must be less than %u.\n", maxScaffold); |
| 151 |
fprintf(stderr, " Must be used with -o.\n"); |
| 152 |
fprintf(stderr, " Will create numerous files 'o.%%05d'.\n"); |
| 153 |
fprintf(stderr, "\n"); |
| 154 |
fprintf(stderr, " -nodeflines Strip out deflines.\n"); |
| 155 |
fprintf(stderr, " -noalignments Strip out alignments.\n"); |
| 156 |
fprintf(stderr, " -gff3 Write output in GFF3 format.\n"); |
| 157 |
fprintf(stderr, "\n"); |
| 158 |
fprintf(stderr, " All conditions must be met.\n"); |
| 159 |
exit(1); |
| 160 |
} |
| 161 |
|
| 162 |
if ((CRAPsilent == 0) && (GOODsilent == 0) && (filePrefixGOOD == 0L) && (filePrefixCRAP == 0L)) { |
| 163 |
fprintf(stderr, "error: filter has no effect; saved and discarded polishes\n"); |
| 164 |
fprintf(stderr, " both printed to the same place!\n"); |
| 165 |
fprintf(stderr, " (try using one of -o, -O, -d, -D)\n"); |
| 166 |
exit(1); |
| 167 |
} |
| 168 |
|
| 169 |
if (doSegregation && (filePrefixGOOD == 0L)) { |
| 170 |
fprintf(stderr, "error: you must specify a file prefix when segregating (-s requires -o)\n"); |
| 171 |
exit(1); |
| 172 |
} |
| 173 |
|
| 174 |
if (noDefLines && doGFF3) |
| 175 |
fprintf(stderr, "warning: No deflines option inactive with GFF3.\n"); |
| 176 |
|
| 177 |
if (beVerbose) { |
| 178 |
fprintf(stderr, "Filtering at "uint32FMT"%% coverage and "uint32FMT"%% identity and "uint32FMT"bp.\n", minC, minI, minL); |
| 179 |
|
| 180 |
if ((cdna != ~uint32ZERO) && (cdna != ~uint32ZERO)) |
| 181 |
fprintf(stderr, "Filtering for cDNA idx "uint32FMT" and genomic idx "uint32FMT"\n", cdna, geno); |
| 182 |
else if (cdna != ~uint32ZERO) |
| 183 |
fprintf(stderr, "Filtering for cDNA idx "uint32FMT".\n", cdna); |
| 184 |
else if (geno != ~uint32ZERO) |
| 185 |
fprintf(stderr, "Filtering for genomic idx "uint32FMT".\n", geno); |
| 186 |
} |
| 187 |
|
| 188 |
// Prepare input files |
| 189 |
|
| 190 |
sim4polishReader *R = new sim4polishReader("-"); |
| 191 |
sim4polish *p = 0L; |
| 192 |
|
| 193 |
if (R->getsim4polishStyle() != style) |
| 194 |
fprintf(stderr, "warning: input format and output format differ.\n"); |
| 195 |
|
| 196 |
// Prepare output files |
| 197 |
|
| 198 |
if (filePrefixGOOD != 0L) |
| 199 |
GOOD = new sim4polishWriter(filePrefixGOOD, style); |
| 200 |
if (filePrefixCRAP != 0L) |
| 201 |
CRAP = new sim4polishWriter(filePrefixCRAP, style); |
| 202 |
if (filePrefixJUNK != 0L) |
| 203 |
JUNK = new sim4polishWriter(filePrefixJUNK, style); |
| 204 |
|
| 205 |
if ((CRAPsilent == 0) && (CRAP == 0L)) |
| 206 |
CRAP = new sim4polishWriter("-", sim4polishS4DB); |
| 207 |
|
| 208 |
if ((GOODsilent == 0) && (GOOD == 0L)) |
| 209 |
GOOD = new sim4polishWriter("-", sim4polishS4DB); |
| 210 |
|
| 211 |
|
| 212 |
// Start processing |
| 213 |
|
| 214 |
while (R->nextAlignment(p)) { |
| 215 |
|
| 216 |
if (noDefLines && (doGFF3 == false)) |
| 217 |
p->s4p_removeDefLines(); |
| 218 |
if (noAlignments) |
| 219 |
p->s4p_removeAlignments(); |
| 220 |
|
| 221 |
if (JUNK && ((p->_strandOrientation == SIM4_STRAND_INTRACTABLE) || |
| 222 |
(p->_strandOrientation == SIM4_STRAND_FAILED))) { |
| 223 |
junk++; |
| 224 |
JUNK->writeAlignment(p); |
| 225 |
} else { |
| 226 |
if ((p->_percentIdentity >= minI) && |
| 227 |
(p->_querySeqIdentity >= minC) && |
| 228 |
(p->_numCovered >= minL) && |
| 229 |
((cdna == ~uint32ZERO) || (cdna == p->_estID)) && |
| 230 |
((geno == ~uint32ZERO) || (geno == p->_genID)) && |
| 231 |
(minExons <= p->_numExons) && |
| 232 |
(p->_numExons <= maxExons) && |
| 233 |
((doSelfFilter == 0) || (strcmp(p->_estDefLine, p->_genDefLine) != 0))) { |
| 234 |
good++; |
| 235 |
if (doSegregation) { |
| 236 |
if ((doSegregationLo <= p->_genID) && |
| 237 |
(p->_genID <= doSegregationHi)) { |
| 238 |
if (SEGREGATE[p->_genID - doSegregationLo] == 0L) { |
| 239 |
char filename[1024]; |
| 240 |
sprintf(filename, "%s.%04d", filePrefixGOOD, (int)p->_genID); |
| 241 |
SEGREGATE[p->_genID - doSegregationLo] = new sim4polishWriter(filename, sim4polishS4DB); |
| 242 |
} |
| 243 |
SEGREGATE[p->_genID - doSegregationLo]->writeAlignment(p); |
| 244 |
} |
| 245 |
} else { |
| 246 |
if (!GOODsilent) |
| 247 |
GOOD->writeAlignment(p); |
| 248 |
} |
| 249 |
} else { |
| 250 |
crap++; |
| 251 |
if (!CRAPsilent) |
| 252 |
CRAP->writeAlignment(p); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
if ((beVerbose) && ((good+crap) == pmod)) { |
| 257 |
pmod += 8888 + (random() % 1000); |
| 258 |
if (junk > 0) |
| 259 |
fprintf(stderr, " Filter: %6.2f%% ("uint64FMT" matches processed) ("uint64FMT" failed/intractable)\r", |
| 260 |
100.0 * good / (good+crap), |
| 261 |
good+crap, |
| 262 |
junk); |
| 263 |
else |
| 264 |
fprintf(stderr, " Filter: %6.2f%% ("uint64FMT" matches processed)\r", |
| 265 |
100.0 * good / (good+crap), |
| 266 |
good+crap); |
| 267 |
fflush(stderr); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
if (beVerbose) { |
| 273 |
if (junk > 0) |
| 274 |
fprintf(stderr, " Filter: %6.2f%% ("uint64FMT" matches processed) ("uint64FMT" failed/intractable)\n", |
| 275 |
100.0 * good / (good+crap), |
| 276 |
good+crap, |
| 277 |
junk); |
| 278 |
else |
| 279 |
fprintf(stderr, " Filter: %6.2f%% ("uint64FMT" matches processed)\n", |
| 280 |
100.0 * good / (good+crap), |
| 281 |
good+crap); |
| 282 |
} |
| 283 |
|
| 284 |
delete R; |
| 285 |
|
| 286 |
if (doSegregation) { |
| 287 |
for (uint32 i=0; i<maxScaffold; i++) |
| 288 |
if (SEGREGATE[i]) |
| 289 |
delete SEGREGATE[i]; |
| 290 |
delete [] SEGREGATE; |
| 291 |
} |
| 292 |
|
| 293 |
delete GOOD; |
| 294 |
delete JUNK; |
| 295 |
|
| 296 |
return(0); |
| 297 |
} |