| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <errno.h> |
| 4 | |
| 5 | #include "bio++.H" |
| 6 | //#include "fasta.H" |
| 7 | #include "sim4.H" |
| 8 | |
| 9 | // usage: mergeInput -m match1 cdna1 -m match2 cdna2 -m ... -o match cdna [-gff3] |
| 10 | // |
| 11 | // Merges the results from two ESTmapper runs. The runs MUST be on |
| 12 | // the same genomic sequence using DIFFERENT cDNA inputs. |
| 13 | |
| 14 | static |
| 15 | void |
| 16 | loadNext(uint32 idx, sim4polish **polishes, sim4polishReader **inMatch, uint32 *numSeqs) { |
| 17 | if (inMatch[idx]->nextAlignment(polishes[idx])) |
| 18 | polishes[idx]->_estID += numSeqs[idx]; |
| 19 | } |
| 20 | |
| 21 | int |
| 22 | main(int argc, char **argv) { |
| 23 | char **inMatchName = new char * [argc]; |
| 24 | char **inSeqName = new char * [argc]; |
| 25 | char *otMatchName = 0L; |
| 26 | char *otSeqName = 0L; |
| 27 | |
| 28 | sim4polishReader **inMatch = new sim4polishReader * [argc]; |
| 29 | sim4polish **polishes = new sim4polish * [argc]; |
| 30 | sim4polishWriter *otMatch = 0L; |
| 31 | uint32 *numSeqs = new uint32 [argc]; |
| 32 | |
| 33 | uint32 numIn = 0; |
| 34 | |
| 35 | sim4polishStyle style = sim4polishStyleDefault; |
| 36 | |
| 37 | int arg = 1; |
| 38 | while (arg < argc) { |
| 39 | if (strcmp(argv[arg], "-m") == 0) { |
| 40 | arg++; |
| 41 | inMatchName[numIn] = (char *)argv[arg++]; |
| 42 | inSeqName[numIn] = (char *)argv[arg++]; |
| 43 | |
| 44 | inMatch[numIn] = new sim4polishReader(inMatchName[numIn]); |
| 45 | numIn++; |
| 46 | |
| 47 | } else if (strcmp(argv[arg], "-o") == 0) { |
| 48 | arg++; |
| 49 | otMatchName = (char *)argv[arg++]; |
| 50 | otSeqName = (char *)argv[arg++]; |
| 51 | |
| 52 | } else if (strcmp(argv[arg], "-gff3") == 0) { |
| 53 | style = sim4polishGFF3; |
| 54 | |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if ((numIn < 1) || (otMatchName == 0L)) { |
| 59 | fprintf(stderr, "usage: %s -o match cdna -m match1 cdna1 -m match2 cdna2 -m ... [-gff3]\n", argv[0]); |
| 60 | exit(1); |
| 61 | } |
| 62 | |
| 63 | otMatch = new sim4polishWriter(otMatchName, style); |
| 64 | |
| 65 | for (uint32 i=0; i<numIn; i++) |
| 66 | if (inMatch[i]->getsim4polishStyle() != style) { |
| 67 | fprintf(stderr, "warning: input format and output format may differ.\n"); |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | // Merge the input sequences into the output sequence. We also count the number of sequences |
| 72 | // here, so we don't need random-access of the input. |
| 73 | // |
| 74 | fprintf(stderr, "Merging sequences.\n"); |
| 75 | |
| 76 | FILE *O = fopen(otSeqName, "w"); |
| 77 | for (uint32 i=0; i<numIn; i++) { |
| 78 | seqCache *I = new seqCache(inSeqName[i]); |
| 79 | seqInCore *B = I->getSequenceInCore(); |
| 80 | |
| 81 | numSeqs[i] = 0; |
| 82 | |
| 83 | while (B) { |
| 84 | fprintf(O, ">%s\n%s\n", B->header(), B->sequence()); |
| 85 | numSeqs[i]++; |
| 86 | |
| 87 | delete B; |
| 88 | B = I->getSequenceInCore(); |
| 89 | } |
| 90 | |
| 91 | delete I; |
| 92 | } |
| 93 | fclose(O); |
| 94 | |
| 95 | |
| 96 | // Make numSeqs[] be the offset needed to convert a polish in each inMatch[] file into a polish |
| 97 | // in the merged file. |
| 98 | // |
| 99 | uint32 o = 0; |
| 100 | uint32 s = 0; |
| 101 | for (uint32 i=0; i<numIn; i++) { |
| 102 | o = numSeqs[i]; |
| 103 | numSeqs[i] = s; |
| 104 | s += o; |
| 105 | } |
| 106 | |
| 107 | // Load the initial polishes |
| 108 | // |
| 109 | for (uint32 i=0; i<numIn; i++) |
| 110 | loadNext(i, polishes, inMatch, numSeqs); |
| 111 | |
| 112 | // Merge, until no more input is left. Each round we scan the list of loaded polishes[] and |
| 113 | // remember the lowest, which is then output and a new polish is loaded in its place. |
| 114 | // |
| 115 | bool keepGoing = true; |
| 116 | while (keepGoing) { |
| 117 | |
| 118 | uint32 first = 0; |
| 119 | while ((polishes[first] == 0L) && (first < numIn)) |
| 120 | first++; |
| 121 | |
| 122 | if (polishes[first] == 0L) { |
| 123 | keepGoing = 0L; |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | for (uint32 i=first+1; i<numIn; i++) |
| 128 | if ((polishes[i]) && |
| 129 | (s4p_genIDcompare(polishes + first, polishes + i) > 0)) |
| 130 | first = i; |
| 131 | |
| 132 | otMatch->writeAlignment(polishes[first]); |
| 133 | |
| 134 | loadNext(first, polishes, inMatch, numSeqs); |
| 135 | } |
| 136 | |
| 137 | delete [] polishes; |
| 138 | |
| 139 | delete inMatch; |
| 140 | delete otMatch; |
| 141 | } |
| 142 | |
| 143 | |
| 144 |