ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/fqtrim/fqtrim.cpp
(Generate patch)
# Line 3 | Line 3
3   #include "GHash.hh"
4   #include "GList.hh"
5   #include <ctype.h>
6 + #include "GAlnExtend.h"
7  
8   #define USAGE "Usage:\n\
9   fqtrim [{-5 <5adapter> -3 <3adapter>|-f <adapters_file>}] [-a <min_matchlen>]\\\n\
# Line 30 | Line 31
31      (e.g. -5 CGACAGGTTCAGAGTTCTACAGTCCGACGATC)\n\
32   -3  trim the given adapter sequence at the 3' end of each read\n\
33      (e.g. -3 TCGTATGCCGTCTTCTGCTTG)\n\
34 < -a  minimum length of exact match to adaptor sequence at the proper end (6)\n\
34 > -A  disable polyA/T trimming (enabled by default)\n\
35 > -y  minimum length of exact match to adaptor sequence at the proper end (6)\n\
36   -q  trim bases with quality value lower than <minq> (starting at the 3' end)\n\
37   -t  for -q option, maximum trimming at the 3' end is limited to <trim_max_len>\n\
38   -m  maximum percentage of Ns allowed in a read after trimming (default 7)\n\
# Line 66 | Line 68
68   bool verbose=false;
69   bool doCollapse=false;
70   bool doDust=false;
71 + bool doPolyTrim=true;
72   bool fastaOutput=false;
73   bool trashReport=false;
74   //bool rawFormat=false;
# Line 75 | Line 78
78   bool isfasta=false;
79   bool convert_phred=false;
80   GStr outsuffix; // -o
78 //GStr adapter3;
79 //GStr adapter5;
81   GStr prefix;
82   GStr zcmd;
83   int num_trimmed5=0;
# Line 91 | Line 92
92   int qv_phredtype=0; // could be 64 or 33 (0 means undetermined yet)
93   int qv_cvtadd=0; //could be -31 or +31
94  
95 < int a3len=0;
96 < int a5len=0;
97 < // adaptor matching metrics -- for extendMatch() function
98 < const int a_m_score=2; //match score
99 < const int a_mis_score=-3; //mismatch
100 < const int a_dropoff_score=7;
101 < int a_min_score=12; //an exact match of 6 bases at the proper ends WILL be trimmed
102 < const int a_min_chain_score=15; //for gapped alignments
103 <
104 < class CSegChain;
105 <
106 < class CSegPair {
107 <  public:
108 <   GSeg a;
108 <   GSeg b; //the adapter segment
109 <   int score;
110 <   int flags;
111 <   CSegChain* chain;
112 <   CSegPair(int astart=0, int aend=0, int bstart=0, int bend=0, int mscore=0):a(astart,aend),b(bstart, bend) {
113 <      score=mscore;
114 <      if (score==0) score=a.len()*a_m_score;
115 <      flags=0;
116 <      chain=NULL;
117 <      }
118 <   int len() { return  a.len(); }
119 <   bool operator==(CSegPair& d){
120 <      //return (a.start==d.a.start && a.end==d.a.end && b.start==d.b.start && b.end==d.b.end);
121 <      //make equal even segments that are included into one another:
122 <      return (d.a.start>=a.start && d.a.end<=a.end && d.b.start>=b.start && d.b.end<=b.end);
123 <      }
124 <   bool operator>(CSegPair& d){ //ordering based on b (adaptor) start coord and score
125 <     if (b.start==d.b.start) {
126 <        if (score==d.score) {
127 <           //just try to be consistent:
128 <           if (b.end==d.b.end) {
129 <             return (a.start==d.a.start)?(a.end<d.a.end):(a.start<d.a.start);
130 <             }
131 <           return (b.end>d.b.end);
132 <           }
133 <         else return (score<d.score);
134 <        }
135 <     return (b.start>d.b.start);
136 <     }
137 <   bool operator<(CSegPair& d){ //ordering based on b (adaptor) coord
138 <     /*if (b.start==d.b.start && b.end==d.b.end) {
139 <          return (a.start==d.a.start)?(a.end<d.a.end):(a.start<d.a.start);
140 <          }
141 <     return (b.start==d.b.start)?(b.end<d.b.end):(b.start<d.b.start);*/
142 <     if (b.start==d.b.start) {
143 <        if (score==d.score) {
144 <           //just try to be consistent:
145 <           if (b.end==d.b.end) {
146 <             return (a.start==d.a.start)?(a.end>d.a.end):(a.start>d.a.start);
147 <             }
148 <           return (b.end<d.b.end);
149 <           }
150 <         else return (score>d.score);
151 <        }
152 <     return (b.start<d.b.start);
153 <     }
154 < };
95 > // adaptor matching metrics -- for X-drop ungapped extension
96 > const int match_reward=2;
97 > const int mismatch_penalty=3;
98 > const int Xdrop=8;
99 >
100 > const int poly_m_score=2; //match score
101 > const int poly_mis_score=-3; //mismatch
102 > const int poly_dropoff_score=7;
103 > int poly_minScore=12; //i.e. an exact match of 6 bases at the proper ends WILL be trimmed
104 >
105 > const char *polyA_seed="AAAA";
106 > const char *polyT_seed="TTTT";
107 > GVec<GStr> adapters5;
108 > GVec<GStr> adapters3;
109  
110 < int cmpSegEnds(pointer sa, pointer sb) { //sort by adaptor seg ends AND score
111 < CSegPair& x = *(CSegPair *)sa;
158 < CSegPair& y = *(CSegPair *)sb;
159 < /*
160 < if (x.b.end==y.b.end) {
161 <     if (x.b.start==y.b.start) {
162 <         if (x.a.end==y.a.end) {
163 <            if (x.a.start==y.a.start) return 0;
164 <            return ((x.a.start>y.a.start) ? -1 : 1);
165 <            }
166 <          else {
167 <            return ((x.a.end>y.a.end) ? -1 : 1);
168 <            }
169 <          }
170 <      else {
171 <       return ((x.b.start>y.b.start) ? -1 : 1);
172 <       }
173 <     }
174 <    else {
175 <     return ((x.b.end>y.b.end) ? -1 : 1);
176 <     }
177 < */
178 <  if (x.b.end==y.b.end) {
179 <     if (x.score==y.score) {
180 <     if (x.b.start==y.b.start) {
181 <         if (x.a.end==y.a.end) {
182 <            if (x.a.start==y.a.start) return 0;
183 <            return ((x.a.start<y.a.start) ? -1 : 1);
184 <            }
185 <          else {
186 <            return ((x.a.end<y.a.end) ? -1 : 1);
187 <            }
188 <          }
189 <      else {
190 <       return ((x.b.start<y.b.start) ? -1 : 1);
191 <       }
192 <      } else return ((x.score>y.score) ? -1 : 1);
193 <     }
194 <    else {
195 <     return ((x.b.end>y.b.end) ? -1 : 1);
196 <     }
197 <
198 < }
199 <
200 < class CSegChain:public GList<CSegPair> {
201 < public:
202 <   uint astart;
203 <   uint aend;
204 <   uint bstart;
205 <   uint bend;
206 <   int score;
207 <   bool endSort;
208 <  CSegChain(bool aln5=false):GList<CSegPair>(true,true,true) {//sorted, free elements, unique
209 <   //as SegPairs are inserted, they will be sorted by a.start coordinate
210 <   score=0;
211 <   astart=MAX_UINT;
212 <   aend=0;
213 <   bstart=MAX_UINT;
214 <   bend=0;
215 <   endSort=aln5;
216 <   if (aln5) { setSorted(cmpSegEnds); }
217 <   }
218 < bool operator==(CSegChain& d) {
219 <   //return (score==d.score);
220 <    return (astart==d.astart && aend==d.aend && bstart==d.bstart && bend==d.bend);
221 <   }
222 < bool operator>(CSegChain& d) { // order based on b (adaptor) coordinate
223 <   //return (score<d.score);
224 <   if (bstart==d.bstart && bend==d.bend) {
225 <          return (astart==d.astart)?(aend>d.aend):(astart>d.astart);
226 <          }
227 <     return (bstart==d.bstart)?(bend>d.bend):(bstart>d.bstart);
228 <   }
229 < bool operator<(CSegChain& d) {
230 <   //return (score>d.score);
231 <   if (bstart==d.bstart && bend==d.bend) {
232 <          return (astart==d.astart)?(aend<d.aend):(astart<d.astart);
233 <          }
234 <     return (bstart==d.bstart)?(bend<d.bend):(bstart<d.bstart);
235 <   }
236 < void addSegPair(CSegPair* segp) {
237 <   if (AddIfNew(segp)!=segp) return;
238 <   score+=segp->score;
239 <   if (astart>segp->a.start) astart=segp->a.start;
240 <   if (aend<segp->a.end) aend=segp->a.end;
241 <   if (bstart>segp->b.start) bstart=segp->b.start;
242 <   if (bend<segp->b.end) bend=segp->b.end;
243 <   }
244 < //for building actual chains:
245 < bool extendChain(CSegPair* segp) { //segp expected to be "Greater Than" current chain
246 <   int bgap=0;
247 <   int agap=0;
248 <   //if (endSort) {
249 <   if (bstart>segp->b.start) {
250 <      bgap = (int)(bstart-segp->b.end);
251 <      if (abs(bgap)>2) return false;
252 <      agap = (int)(astart-segp->a.end);
253 <      if (abs(agap)>2) return false;
254 <      }
255 <     else {
256 <      bgap = (int) (segp->b.start-bend);
257 <      if (abs(bgap)>2) return false;
258 <      agap = (int)(segp->a.start-aend);
259 <      if (abs(agap)>2) return false;
260 <      }
261 <   if (agap*bgap<0) return false;
262 <   addSegPair(segp);
263 <   score-=abs(agap)+abs(bgap);
264 <   return true;
265 <   }
266 < };
110 > CGreedyAlignData* gxmem_l=NULL;
111 > CGreedyAlignData* gxmem_r=NULL;
112  
113   // element in dhash:
114   class FqDupRec {
# Line 313 | Line 158
158  
159   GHash<FqDupRec> dhash; //hash to keep track of duplicates
160  
161 + int loadAdapters(const char* fname);
162 +
163   void setupFiles(FILE*& f_in, FILE*& f_in2, FILE*& f_out, FILE*& f_out2,
164                         GStr& s, GStr& infname, GStr& infname2);
165   // uses outsuffix to generate output file names and open file handles as needed
# Line 329 | Line 176
176   bool ntrim(GStr& rseq, int &l5, int &l3); //returns true if any trimming occured
177   bool qtrim(GStr& qvs, int &l5, int &l3); //return true if any trimming occured
178   int dust(GStr& seq);
179 < bool trim_adapter3(GStr& seq, int &l5, int &l3); //returns true if any trimming occured
179 > bool trim_poly5(GStr &seq, int &l5, int &l3, const char* poly_seed); //returns true if any trimming occured
180 > bool trim_poly3(GStr &seq, int &l5, int &l3, const char* poly_seed);
181   bool trim_adapter5(GStr& seq, int &l5, int &l3); //returns true if any trimming occured
182 + bool trim_adapter3(GStr& seq, int &l5, int &l3);
183  
184   void convertPhred(char* q, int len);
185   void convertPhred(GStr& q);
186  
187   int main(int argc, char * const argv[]) {
188 <  GArgs args(argc, argv, "YQDCVl:d:3:5:m:n:r:p:q:f:t:o:z:a:");
188 >  GArgs args(argc, argv, "YQDCVAl:d:3:5:m:n:r:p:q:f:t:o:z:a:");
189    int e;
190    if ((e=args.isError())>0) {
191        GMessage("%s\nInvalid argument: %s\n", USAGE, argv[e]);
# Line 347 | Line 196
196    convert_phred=(args.getOpt('Q')!=NULL);
197    doCollapse=(args.getOpt('C')!=NULL);
198    doDust=(args.getOpt('D')!=NULL);
199 +  if (args.getOpt('A')) doPolyTrim=false;
200    /*
201    rawFormat=(args.getOpt('R')!=NULL);
202    if (rawFormat) {
# Line 387 | Line 237
237         else
238           GMessage("%s\nInvalid value for -p option (can only be 64 or 33)!\n",USAGE);
239       }
240 <  if (args.getOpt('3')!=NULL) {
241 <    adapter3=args.getOpt('3');
242 <    adapter3.upper();
243 <    a3len=adapter3.length();
244 <    }
245 <  if (args.getOpt('5')!=NULL) {
246 <    adapter5=args.getOpt('5');
247 <    adapter5.upper();
248 <    a5len=adapter5.length();
240 >  s=args.getOpt('f');
241 >  if (!s.is_empty()) {
242 >   loadAdapters(s.chars());
243 >   }
244 >  bool fileAdapters=adapters5.Count()+adapters3.Count();
245 >  s=args.getOpt('5');
246 >  if (!s.is_empty()) {
247 >    if (fileAdapters)
248 >      GError("Error: options -5 and -f cannot be used together!\n");
249 >    s.upper();
250 >    adapters5.Add(s);
251 >    }
252 >  s=args.getOpt('3');
253 >  if (!s.is_empty()) {
254 >    if (fileAdapters)
255 >      GError("Error: options -3 and -f cannot be used together!\n");
256 >      s.upper();
257 >      adapters3.Add(s);
258      }
259 <  s=args.getOpt('a');
259 >  s=args.getOpt('y');
260    if (!s.is_empty()) {
261 <     int a_minmatch=s.asInt();
262 <     a_min_score=a_minmatch<<1;
261 >     int minmatch=s.asInt();
262 >     poly_minScore=minmatch*poly_m_score;
263       }
264    
265    if (args.getOpt('o')!=NULL) outsuffix=args.getOpt('o');
# Line 419 | Line 278
278    if (trashReport)
279      openfw(freport, args, 'r');
280    char* infile=NULL;
281 +
282 +  if (adapters5.Count()>0)
283 +    gxmem_l=new CGreedyAlignData(match_reward, mismatch_penalty, Xdrop-2);
284 +  if (adapters3.Count()>0)
285 +    gxmem_r=new CGreedyAlignData(match_reward, mismatch_penalty, Xdrop);
286 +
287    while ((infile=args.nextNonOpt())!=NULL) {
288 +    //for each input file
289      int incounter=0; //counter for input reads
290      int outcounter=0; //counter for output reads
291      int trash_s=0; //too short from the get go
292      int trash_Q=0;
293      int trash_N=0;
294      int trash_D=0;
295 +    int trash_poly=0;
296      int trash_A3=0;
297      int trash_A5=0;
298      s=infile;
# Line 450 | Line 317
317         int a5=0, a3=0, b5=0, b3=0;
318         char tcode=0, tcode2=0;
319         tcode=process_read(seqid, rseq, rqv, a5, a3);
320 <       //if (!doCollapse) {
454 <         if (fq2!=NULL) {
320 >       if (fq2!=NULL) {
321              getFastxRec(*fq2, rseq2, rqv2, seqid2, seqinfo2, infname2);
322              if (seqid.substr(0,seqid.length()-1)!=seqid2.substr(0,seqid2.length()-1)) {
323                 GError("Error: no paired match for read %s vs %s (%s,%s)\n",
# Line 483 | Line 349
349                 int nocounter=0;
350                 writeRead(f_out2, seqid2, seqinfo2, rseq2, rqv2, nocounter);
351                 }
352 <            } //paired read
487 <       // }
352 >            } //pair read
353         if (tcode>1) { //trashed
354 +         #ifdef GDEBUG
355 +         GMessage(" !!!!TRASH => 'N'\n");
356 +         #endif
357            if (tcode=='s') trash_s++;
358 +          else if (tcode=='A' || tcode=='T') trash_poly++;
359              else if (tcode=='Q') trash_Q++;
360                else if (tcode=='N') trash_N++;
361                 else if (tcode=='D') trash_D++;
# Line 499 | Line 368
368              rseq=rseq.substr(a5,a3-a5+1);
369              if (!rqv.is_empty()) rqv=rqv.substr(a5,a3-a5+1);
370              }
371 +         #ifdef GDEBUG
372 +            GMessage("  After trimming:\n");
373 +            GMessage("<==%s\n",rseq.chars());
374 +         #endif
375            writeRead(f_out, seqid, seqinfo, rseq, rqv, outcounter);
376            }
377         } //for each fastq record
# Line 579 | Line 452
452           GMessage("         Trashed by N%%:%9d\n", trash_N);
453         if (trash_Q>0)
454           GMessage("Trashed by low quality:%9d\n", trash_Q);
455 +       if (trash_poly>0)
456 +         GMessage("   Trashed by poly-A/T:%9d\n", trash_poly);
457         if (trash_A5>0)
458           GMessage(" Trashed by 5' adapter:%9d\n", trash_A5);
459         if (trash_A3>0)
# Line 590 | Line 465
465      FWCLOSE(f_out);
466      FWCLOSE(f_out2);
467     } //while each input file
468 <
468 > delete gxmem_l;
469 > delete gxmem_r;
470   //getc(stdin);
471   }
472  
# Line 856 | Line 732
732   return ncount;
733   }
734  
859
860 // ------------------ adapter matching - simple k-mer seed & extend, no indels for now
861 //when a k-mer match is found, simply try to extend the alignment using a drop-off scheme
862 //check minimum score and
863 //for 3' adapter trimming:
864 //     require that the right end of the alignment for either the adaptor OR the read must be
865 //     < 3 distance from its right end
866 // for 5' adapter trimming:
867 //     require that the left end of the alignment for either the adaptor OR the read must
868 //     be at coordinate < 3 from start
869
870 bool extendMatch(const char* a, int alen, int ai,
871                 const char* b, int blen, int bi, int mlen, int& l5, int& l3, CSegChain& segs, bool end5=false) {
872 //so the alignment starts at ai in a, bi in b, with a perfect match of length mlen
873 #ifdef DEBUG
874 GStr dbg(b);
875 #endif
876 //if (debug) {
877 //  GMessage(">> in %s\n\textending hit: %s at position %d\n", a, (dbg.substr(bi, mlen)).chars(), ai);
878 //  }
879 int a_l=ai; //alignment coordinates on a
880 int a_r=ai+mlen-1;
881 int b_l=bi; //alignment coordinates on b
882 int b_r=bi+mlen-1;
883 int ai_maxscore=ai;
884 int bi_maxscore=bi;
885 int score=mlen*a_m_score;
886 int maxscore=score;
887 int mism5score=a_mis_score;
888 if (end5 && ai<(alen>>1)) mism5score-=2; // increase penalty for mismatches at 5' end
889 //try to extend to the left first, if possible
890 while (ai>0 && bi>0) {
891   ai--;
892   bi--;
893   score+= (a[ai]==b[bi])? a_m_score : mism5score;
894   if (score>maxscore) {
895       ai_maxscore=ai;
896       bi_maxscore=bi;
897       maxscore=score;
898       }
899     else if (maxscore-score>a_dropoff_score) break;
900   }
901 a_l=ai_maxscore;
902 b_l=bi_maxscore;
903 //if (debug) GMessage("  after l-extend: %*s%s\t\t(score=%d)\n",a_l," ",dbg.substr(b_l,b_r-b_l+1).chars(),maxscore);
904 //now extend to the right
905 ai_maxscore=a_r;
906 bi_maxscore=b_r;
907 ai=a_r;
908 bi=b_r;
909 score=maxscore;
910 //sometimes there are extra AAAAs at the end of the read, ignore those
911 if (strcmp(&a[alen-4],"AAAA")==0) {
912    alen-=3;
913    while (a[alen-1]=='A' && alen>ai) alen--;
914    }
915 while (ai<alen-1 && bi<blen-1) {
916   ai++;
917   bi++;
918   //score+= (a[ai]==b[bi])? a_m_score : a_mis_score;
919   if (a[ai]==b[bi]) { //match
920      score+=a_m_score;
921      if (ai>=alen-2) {
922           score+=a_m_score-(alen-ai-1);
923           }
924      }
925    else { //mismatch
926      score+=a_mis_score;
927      }  
928   if (score>maxscore) {
929       ai_maxscore=ai;
930       bi_maxscore=bi;
931       maxscore=score;
932       }
933     else if (maxscore-score>a_dropoff_score) break;
934   }
935  a_r=ai_maxscore;
936  b_r=bi_maxscore;
937  int a_ovh3=alen-a_r-1;
938  int b_ovh3=blen-b_r-1;
939  int mmovh3=(a_ovh3<b_ovh3)? a_ovh3 : b_ovh3;
940  int mmovh5=(a_l<b_l)? a_l : b_l;
941  //if (debug) GMessage("  after r-extend: %*s%s\t\t(score=%d)\n",a_l," ",dbg.substr(b_l,b_r-b_l+1).chars(),maxscore);
942 #ifdef DEBUG
943  if (debug) GMessage("     extended to: %*s\n",a_r+1,dbg.substr(b_l,b_r-b_l+1).chars());
944 #endif
945  if (maxscore>=a_min_score && mmovh3<2 && mmovh5<2) {
946     if (a_l<a_ovh3) {
947        //adapter closer to the left end (typical for 5' adapter)
948        l5=a_r+1;
949        l3=alen-1;
950        }
951      else {
952        //adapter matching at the right end (typical for 3' adapter)
953        l5=0;
954        l3=a_l-1;
955        }
956     return true;
957     }
958 else { //keep this segment pair for later (gapped alignment)
959   segs.addSegPair(new CSegPair(a_l+1, a_r+1, b_l+1, b_r+1, maxscore));
960   //this will also update min & max coordinates in segs (segs.astart, .aend, .bstart, .bend)
961   }
962  //do not trim:
963  l5=0;
964  l3=alen-1;
965  return false;
966 }
967
968 /*
969 int getWordValue(const char* s, int wlen) {
970 int r=0;
971 while (wlen--) { r+=(((int)s[wlen])<<wlen) }
972 return r;
973 }
974 */
735   int get3mer_value(const char* s) {
736   return (s[0]<<16)+(s[1]<<8)+s[2];
737   }
# Line 995 | Line 755
755   return -1;
756   }
757  
758 < int fast4match(int32 qv, const char* str, int slen, int start_index=0) {
759 < if (start_index>=slen || start_index<0) return -1;
760 < for (int i=start_index;i<slen-4;i++) {
761 <   int32* rv=(int32*)(str+i);
762 <   if (*rv==qv) return i;
763 <   }
764 < return -1;
765 < }
758 > struct SLocScore {
759 >  int pos;
760 >  int score;
761 >  SLocScore(int p=0,int s=0) {
762 >    pos=p;
763 >    score=s;
764 >    }
765 >  void set(int p, int s) {
766 >    pos=p;
767 >    score=s;
768 >    }
769 >  void add(int p, int add) {
770 >    pos=p;
771 >    score+=add;
772 >    }
773 > };
774  
775 < int fast4rmatch(int32 qv, const char* str, int slen, int end_index=-1) {
776 < if (end_index>=slen) return -1;
777 < if (end_index<0) end_index=slen-1;
778 < for (int i=end_index-3;i>=0;i--) {
779 <   int32* rv=(int32*)(str+i);
780 <   if (*rv==qv) return i;
775 > bool trim_poly3(GStr &seq, int &l5, int &l3, const char* poly_seed) {
776 > if (!doPolyTrim) return false;
777 > int rlen=seq.length();
778 > l5=0;
779 > l3=rlen-1;
780 > int32 seedVal=*(int32*)poly_seed;
781 > char polyChar=poly_seed[0];
782 > //assumes N trimming was already done
783 > //so a poly match should be very close to the end of the read
784 > // -- find the initial match (seed)
785 > int lmin=GMAX((rlen-12), 0);
786 > int li;
787 > for (li=rlen-4;li>lmin;li--) {
788 >   if (seedVal==*(int*)&(seq[li])) {
789 >      break;
790 >      }
791     }
792 < return -1;
793 < }
794 <
795 < #ifdef DEBUG
796 < void dbgPrintChain(CSegChain& chain, const char* aseq) {
797 <  GStr s(aseq);
798 <  for (int i=0;i<chain.Count();i++) {
799 <   CSegPair& seg=*chain[i];
800 <   GMessage("  dbg chain seg%d: %*s [%d-%d:%d-%d]\n",i,seg.a.start-1+seg.len(),
801 <            s.substr(seg.b.start-1, seg.len()).chars(), seg.b.start,seg.b.end,seg.a.start,seg.a.end);
792 > if (li<=lmin) return false;
793 > //seed found, try to extend it both ways
794 > //extend right
795 > int ri=li+3;
796 > SLocScore loc(ri, poly_m_score<<2);
797 > SLocScore maxloc(loc);
798 > //extend right
799 > while (ri<rlen-2) {
800 >   ri++;
801 >   if (seq[ri]==polyChar) {
802 >                loc.add(ri,poly_m_score);
803 >                }
804 >   else if (seq[ri]=='N') {
805 >                loc.add(ri,0);
806 >                }
807 >   else { //mismatch
808 >        loc.add(ri,poly_mis_score);
809 >        if (maxloc.score-loc.score>poly_dropoff_score) break;
810 >        }
811 >   if (maxloc.score<=loc.score) {
812 >      maxloc=loc;
813 >      }
814     }
815 + ri=maxloc.pos;
816 + if (ri<rlen-6) return false; //no trimming wanted, too far from 3' end
817 + //ri = right boundary for the poly match
818 + //extend left
819 + loc.set(li, maxloc.score);
820 + maxloc.pos=li;
821 + while (li>0) {
822 +    li--;
823 +    if (seq[li]==polyChar) {
824 +                 loc.add(li,poly_m_score);
825 +                 }
826 +    else if (seq[li]=='N') {
827 +                 loc.add(li,0);
828 +                 }
829 +    else { //mismatch
830 +         loc.add(li,poly_mis_score);
831 +         if (maxloc.score-loc.score>poly_dropoff_score) break;
832 +         }
833 +    if (maxloc.score<=loc.score) {
834 +       maxloc=loc;
835 +       }
836 +    }
837 + if ((maxloc.score>poly_minScore && ri>=rlen-3) ||
838 +    (maxloc.score==poly_minScore && ri==rlen-1) ||
839 +    (maxloc.score>(poly_minScore<<1) && ri>=rlen-6)) {
840 +    l5=li;
841 +    l3=ri;
842 +    return true;
843 +    }
844 + return false;
845   }
1026 #endif
846  
847 < bool trim_adapter3(GStr& seq, int&l5, int &l3) {
847 > bool trim_poly5(GStr &seq, int &l5, int &l3, const char* poly_seed) {
848 > if (!doPolyTrim) return false;
849   int rlen=seq.length();
850   l5=0;
851   l3=rlen-1;
852 < //first try a full match, we might get lucky
853 < int fi=-1;
854 < if ((fi=seq.index(adapter3))>=0) {
855 <   if (fi<rlen-fi-a3len) {//match is closer to the right end
856 <      l5=fi+a3len;
857 <      l3=rlen-1;
858 <      }
859 <    else {
860 <      l5=0;
861 <      l3=fi-1;
852 > int32 seedVal=*(int32*)poly_seed;
853 > char polyChar=poly_seed[0];
854 > //assumes N trimming was already done
855 > //so a poly match should be very close to the end of the read
856 > // -- find the initial match (seed)
857 > int lmax=GMIN(8, rlen-4);//how far from 5' end to look for 4-mer seeds
858 > int li;
859 > for (li=0;li<=lmax;li++) {
860 >   if (seedVal==*(int*)&(seq[li])) {
861 >      break;
862        }
1043   return true;
863     }
864 < #ifdef DEBUG
865 < if (debug) GMessage(">TRIM3 >>   Read: %s\n",seq.chars());
866 < #endif
867 <
868 < //also, for fast detection of other adapter-only reads that start past
869 < // the beginning of the adapter sequence, try to see if the first a3len-4
870 < // bases of the read are a substring of the adapter
871 < if (rlen>a3len-3) {
872 <   GStr rstart=seq.substr(1,a3len-4);
873 <   if ((fi=adapter3.index(rstart))>=0) {
874 <     l3=rlen-1;
875 <     l5=a3len-4;
876 <     while (fi+l5<a3len && l5<l3 && adapter3[fi+l5]==seq[l5]) l5++;
877 <     return true;
878 <     }
879 <  }
880 < CSegChain a3segs; //no chains here, just an ordered collection of segment pairs
881 <  //check the easy cases - 11 bases exact match at the end
882 < int fdlen=11;
883 <  if (a3len<16) {
1065 <   fdlen=a3len>>1;
1066 <   }
1067 < if (fdlen>4) {
1068 <     //check if we're lucky enough to have the last 11 bases of the read a part of the adapter
1069 <     GStr rstart=seq.substr(-fdlen-3,fdlen);
1070 <     if ((fi=adapter3.index(rstart))>=0) {
1071 < #ifdef DEBUG
1072 <       if (debug) GMessage("  W11match found: %*s\n", rlen-3, (adapter3.substr(fi,fdlen)).chars());
1073 < #endif
1074 <       if (extendMatch(seq.chars(), rlen, rlen-fdlen-3,
1075 <                     adapter3.chars(), a3len, fi,  fdlen, l5,l3, a3segs))
1076 <            return true;
1077 <       }
1078 <     //another easy case: first 11 characters of the adaptor found as a substring of the read
1079 <     GStr bstr=adapter3.substr(0, fdlen);
1080 <     if ((fi=seq.rindex(bstr))>=0) {
1081 < #ifdef DEBUG
1082 <       if (debug) GMessage("  A11match found: %*s\n", fi+fdlen, bstr.chars());
1083 < #endif
1084 <       if (extendMatch(seq.chars(), rlen, fi,
1085 <                     adapter3.chars(), a3len, 0,  fdlen, l5,l3, a3segs))
1086 <            return true;
864 > if (li>lmax) return false;
865 > //seed found, try to extend it both ways
866 > //extend left
867 > int ri=li+3; //save rightmost base of the seed
868 > SLocScore loc(li, poly_m_score<<2);
869 > SLocScore maxloc(loc);
870 > while (li>0) {
871 >    li--;
872 >    if (seq[li]==polyChar) {
873 >                 loc.add(li,poly_m_score);
874 >                 }
875 >    else if (seq[li]=='N') {
876 >                 loc.add(li,0);
877 >                 }
878 >    else { //mismatch
879 >         loc.add(li,poly_mis_score);
880 >         if (maxloc.score-loc.score>poly_dropoff_score) break;
881 >         }
882 >    if (maxloc.score<=loc.score) {
883 >       maxloc=loc;
884         }
885 <     } //tried to match 11 bases first
886 <    
887 < //no easy cases, so let's do the wmer hashing for the first 12 bases of the adaptor
888 < //-- only extend if the match is in the 3' (ending) region of the read
889 < int wordSize=3;
890 < int hlen=12;
891 < if (hlen>a3len-wordSize) hlen=a3len-wordSize;
892 < int imin=rlen>>1; //last half of the read, left boundary for the wmer match
893 < if (imin<a3len) { imin=GMIN(a3len, rlen-wordSize); }
894 < imin=rlen-imin;
895 < for (int iw=0;iw<hlen;iw++) {
896 <   //int32* qv=(int32*)(adapter3.chars()+iw);
897 <   int qv=get3mer_value(adapter3.chars()+iw);
898 <   fi=-1;
899 <   //while ((fi=fast4rmatch(*qv, seq.chars(), rlen, fi))>=0 && fi>=imin) {
900 <   while ((fi=w3_rmatch(qv, seq.chars(), rlen, fi))>=0 && fi>=imin) {
901 <     //GMessage(" ... fi=%d after w3_rmatch() (imin=%d)\n", fi, imin);
902 <
903 < #ifdef DEBUG
1107 <     if (debug) GMessage("    Wmatch found: %*s\n", fi+wordSize, (adapter3.substr(iw,wordSize)).chars());
1108 < #endif
1109 <     if (extendMatch(seq.chars(), rlen, fi, adapter3.chars(),
1110 <                   a3len, iw, wordSize, l5,l3, a3segs)) return true;
1111 <     fi--;
1112 <     if (fi<imin) break;
1113 <     }
1114 <   } //for each wmer in the first hlen bases of the adaptor
1115 < /*
1116 < //couldn't find a good trimming extension, hash 12 more bases of the adapter to collect more segment pairs there
1117 < //but only do this if we already have segment pairs collected in the last 12 bases of the adapter
1118 < if (a3segs.bstart>3 || a3segs.bend<(uint)(hlen-wordSize)) return false;
1119 < int hlen2=a3len-wordSize;
1120 < //if (hlen2>a3len-4) hlen2=a3len-4;
1121 < if (hlen2>hlen) {
1122 < #ifdef DEBUG
1123 <     if (debug && a3segs.Count()>0) {
1124 <        GMessage("  >>>>>2nd. hash: %s\n",seq.chars());
885 >    }
886 > li=maxloc.pos;
887 > if (li>5) return false; //no trimming wanted, too far from 5' end
888 > //li = right boundary for the poly match
889 >
890 > //extend right
891 > loc.set(ri, maxloc.score);
892 > maxloc.pos=ri;
893 > while (ri<rlen-2) {
894 >   ri++;
895 >   if (seq[ri]==polyChar) {
896 >                loc.add(ri,poly_m_score);
897 >                }
898 >   else if (seq[ri]=='N') {
899 >                loc.add(ri,0);
900 >                }
901 >   else { //mismatch
902 >        loc.add(ri,poly_mis_score);
903 >        if (maxloc.score-loc.score>poly_dropoff_score) break;
904          }
905 < #endif
906 <     for (int iw=hlen;iw<hlen2;iw++) {
1128 <         //int* qv=(int32 *)(adapter3.chars()+iw);
1129 <         int qv=get3mer_value(adapter3.chars()+iw);
1130 <         fi=-1;
1131 <         //while ((fi=fast4rmatch(*qv, seq.chars(), rlen, fi))>=0 && fi>=imin) {
1132 <         while ((fi=w3_rmatch(qv, seq.chars(), rlen, fi))>=0 && fi>=imin) {
1133 <           extendMatch(seq.chars(), rlen, fi, adapter3.chars(),
1134 <                         a3len, iw, wordSize, l5,l3, a3segs);
1135 <           fi--;
1136 <           if (fi<imin) break;
1137 <           }
1138 <         } //for each wmer between hlen2 and hlen bases of the adaptor
1139 <     }
1140 < //lastly, analyze collected a3segs for a possible gapped alignment:
1141 < GList<CSegChain> segchains(false,true,false);
1142 < #ifdef DEBUG
1143 < if (debug && a3segs.Count()>0) {
1144 <   GMessage(">>>>>>>>>   Read: %s\n",seq.chars());
1145 <   }
1146 < #endif
1147 < for (int i=0;i<a3segs.Count();i++) {
1148 <   if (a3segs[i]->chain==NULL) {
1149 <       if (a3segs[i]->b.start>3) continue; //don't start a hopeless chain
1150 <       CSegChain* newchain=new CSegChain();
1151 <       newchain->setFreeItem(false);
1152 <       newchain->addSegPair(a3segs[i]);
1153 <       a3segs[i]->chain=newchain;
1154 <       segchains.Add(newchain); //just to free them when done
1155 <       }
1156 <   for (int j=i+1;j<a3segs.Count();j++) {
1157 <      CSegChain* chain=a3segs[i]->chain;
1158 <      if (chain->extendChain(a3segs[j])) {
1159 <          a3segs[j]->chain=chain;
1160 < #ifdef DEBUG
1161 <          if (debug) dbgPrintChain(*chain, adapter3.chars());
1162 < #endif      
1163 <          //save time by checking here if the extended chain is already acceptable for trimming
1164 <          if (chain->aend>(uint)(rlen-4) && chain->bstart<4 && chain->score>a_min_chain_score) {
1165 <            l5=0;
1166 <            l3=chain->astart-2;
1167 < #ifdef DEBUG
1168 <          if (debug && a3segs.Count()>0) {
1169 <            GMessage(">>> >> trimmed-3: %*s\n",l3-l5+1,seq.substr(l5,l3-l5+1).chars());
1170 <            }
1171 < #endif
1172 <            return true;
1173 <            }
1174 <          } //chain can be extended
905 >   if (maxloc.score<=loc.score) {
906 >      maxloc=loc;
907        }
908 <   } //collect segment alignments into chains
1177 < */  
1178 < return false; //no adapter parts found
1179 < }
908 >   }
909  
910 < bool trim_adapter5(GStr& seq, int&l5, int &l3) {
911 < //if (debug) GMessage("trim_adapter5 on: %s\n", seq.chars());
910 > if ((maxloc.score==poly_minScore && li==0) ||
911 >     (maxloc.score>poly_minScore && li<2)
912 >     || (maxloc.score>(poly_minScore<<1) && li<6)) {
913 >    l5=li;
914 >    l3=ri;
915 >    return true;
916 >    }
917 > return false;
918 > }
919 >
920 > bool trim_adapter3(GStr& seq, int&l5, int &l3) {
921 > if (adapters3.Count()==0) return false;
922   int rlen=seq.length();
923   l5=0;
924   l3=rlen-1;
925 < //try to see if adapter is fully included in the read
926 < int fi=-1;
927 < if ((fi=seq.index(adapter5))>=0) {
928 <   if (fi<rlen-fi-a5len) {//match is closer to the right end
929 <      l5=fi+a5len;
930 <      l3=rlen-1;
931 <      }
932 <    else {
933 <      l5=0;
934 <      l3=fi-1;
935 <      }
936 <   return true;
937 <   }
938 < #ifdef DEBUG
939 < if (debug) GMessage(">TRIM5 >>   Read: %s\n",seq.chars());
940 < #endif
941 <
1203 < CSegChain a5segs(true); //list of segment pairs to analyze later if no extendMatch succeeded
1204 <
1205 < //try the easy way out first - look for an exact match of 11 bases
1206 < int fdlen=11;
1207 <  if (a5len<16) {
1208 <   fdlen=a5len>>1;
1209 <   }
1210 < if (fdlen>4) {
1211 <     GStr rstart=seq.substr(1,fdlen); //skip the first base as it's sometimes bogus
1212 <     if ((fi=adapter5.index(rstart))>=0) {
1213 < #ifdef DEBUG
1214 <       if (debug) GMessage("  W11match found: %*s\n", 1+fdlen, (adapter3.substr(fi,fdlen)).chars());
1215 < #endif
1216 <       if (extendMatch(seq.chars(), rlen, 1,
1217 <                     adapter5.chars(), a5len, fi,  fdlen, l5,l3, a5segs, true))
1218 <           return true;
1219 <       }
1220 <     //another easy case: last 11 characters of the adaptor found as a substring of the read
1221 <     GStr bstr=adapter5.substr(-fdlen);
1222 <     if ((fi=seq.index(bstr))>=0) {
1223 < #ifdef DEBUG
1224 <       if (debug) GMessage("  A11match found: %*s\n", fi+fdlen, bstr.chars());
1225 < #endif
1226 <       if (extendMatch(seq.chars(), rlen, fi,
1227 <                     adapter5.chars(), a5len, a5len-fdlen,  fdlen, l5,l3,a5segs,true))
1228 <          return true;
1229 <       }
1230 <     } //tried to matching at most 11 bases first
1231 <    
1232 < //-- no easy cases, do the wmer hashing for the last 12 bases of the adaptor
1233 < //-- only extend a wmer if it matches in the 5' (beginning) region of the read
1234 < int wordSize=3;
1235 < int hlen=12;
1236 < if (hlen>a5len-wordSize) hlen=a5len-wordSize;
1237 < int imax=rlen>>1; //first half of the read, right boundary for the wmer match
1238 < if (imax<a5len) { imax=GMIN(a5len, rlen-wordSize); }
1239 < for (int iw=0;iw<=hlen;iw++) {
1240 <   int apstart=a5len-iw-wordSize;
1241 <   fi=0;
1242 <   //int* qv=(int32 *)(adapter5.chars()+apstart);
1243 <   int qv=get3mer_value(adapter5.chars()+apstart);
1244 <   //while ((fi=fast4match(*qv, seq.chars(), rlen, fi))>=0 && fi<=imax) {
1245 <   while ((fi=w3_match(qv, seq.chars(), rlen, fi))>=0 && fi<=imax) {
1246 < #ifdef DEBUG
1247 <     if (debug) GMessage("    Wmatch found: %*s\n", fi+wordSize, (adapter5.substr(apstart,wordSize)).chars());
1248 < #endif
1249 <     if (extendMatch(seq.chars(), rlen, fi, adapter5.chars(),
1250 <                a5len, apstart, wordSize, l5,l3, a5segs, true)) return true;
1251 <     fi++;
1252 <     if (fi>imax) break;
925 > bool trimmed=false;
926 > GStr wseq(seq.chars());
927 > int wlen=rlen;
928 > for (int ai=0;ai<adapters3.Count();ai++) {
929 >  if (adapters3[ai].is_empty()) continue;
930 >  int alen=adapters3[ai].length();
931 >  GStr& aseq=adapters3[ai];
932 >  GXAlnInfo* r_bestaln=match_RightEnd(aseq.chars(), alen, wseq.chars(), wlen, gxmem_r, 74);
933 >  if (r_bestaln) {
934 >     trimmed=true;
935 >     //keep unmatched region on the left, if any
936 >     l3-=(wlen-r_bestaln->sl+1);
937 >     delete r_bestaln;
938 >     if (l3<0) l3=0;
939 >     if (l3-l5+1<min_read_len) return true;
940 >     wseq=seq.substr(l5,l3-l5+1);
941 >     wlen=wseq.length();
942       }
943 <   } //for each wmer in the last hlen bases of the adaptor
944 < /*
943 >  }//for each 5' adapter
944 >  return trimmed;
945 > }
946  
947 < //couldn't find a good trimming extension, hash 12 more bases of the adapter to collect more segment pairs there
948 < //but only do this if we already have segment pairs collected in the last 12 bases of the adapter
949 < if (a5segs.bend<(uint)(a5len-3) || a5segs.bstart>(uint)(a5len-hlen+4)) return false;
950 < int hlen2=a5len-wordSize;
951 < //if (hlen2>a5len-wordSize) hlen2=a5len-wordSize;
952 < #ifdef DEBUG
953 <      if (debug && a5segs.Count()>0) {
954 <        GMessage("  >>>>>2nd. hash: %s\n",seq.chars());
955 <        }
956 < #endif
957 < if (hlen2>hlen) {
958 <     for (int iw=hlen+1;iw<=hlen2;iw++) {
959 <         int apstart=a5len-iw-wordSize;
960 <         fi=0;
961 <         //int* qv=(int32 *)(adapter5.chars()+apstart);
962 <         int qv=get3mer_value(adapter5.chars()+apstart);
963 <         //while ((fi=fast4match(*qv, seq.chars(), rlen, fi))>=0 && fi<=imax) {
964 <         while ((fi=w3_match(qv, seq.chars(), rlen, fi))>=0 && fi<=imax) {
965 <           extendMatch(seq.chars(), rlen, fi, adapter5.chars(),
966 <                      a5len, apstart, wordSize, l5,l3, a5segs, true);
967 <           fi++;
1278 <           if (fi>imax) break;
1279 <           }
1280 <         } //for each wmer between hlen2 and hlen bases of the adaptor
947 > bool trim_adapter5(GStr& seq, int&l5, int &l3) {
948 > if (adapters5.Count()==0) return false;
949 > int rlen=seq.length();
950 > l5=0;
951 > l3=rlen-1;
952 > bool trimmed=false;
953 > GStr wseq(seq.chars());
954 > int wlen=rlen;
955 > for (int ai=0;ai<adapters5.Count();ai++) {
956 >  if (adapters5[ai].is_empty()) continue;
957 >  int alen=adapters5[ai].length();
958 >  GStr& aseq=adapters5[ai];
959 >  GXAlnInfo* l_bestaln=match_LeftEnd(aseq.chars(), alen, wseq.chars(), wlen, gxmem_l, 84);
960 >  if (l_bestaln) {
961 >     trimmed=true;
962 >     l5+=l_bestaln->sr;
963 >     delete l_bestaln;
964 >     if (l5>=rlen) l5=rlen-1;
965 >     if (l3-l5+1<min_read_len) return true;
966 >     wseq=seq.substr(l5,l3-l5+1);
967 >     wlen=wseq.length();
968       }
969 < if (a5segs.bend<(uint)(a5len-3) || a5segs.bstart>(uint)(a5len-hlen+4)) return false;
970 < // lastly, analyze collected a5segs for a possible gapped alignment:
971 < GList<CSegChain> segchains(false,true,false);
1285 < #ifdef DEBUG
1286 < if (debug && a5segs.Count()>0) {
1287 <   GMessage(">>>>>>>>>   Read: %s\n",seq.chars());
1288 <   }
1289 < #endif
1290 < for (int i=0;i<a5segs.Count();i++) {
1291 <   if (a5segs[i]->chain==NULL) {
1292 <       if (a5segs[i]->b.end<(int)(a5len-4)) continue; //don't start a hopeless chain
1293 <       CSegChain* newchain=new CSegChain(true);
1294 <       newchain->setFreeItem(false);
1295 <       newchain->addSegPair(a5segs[i]);
1296 <       a5segs[i]->chain=newchain;
1297 <       segchains.Add(newchain); //just to free them when done
1298 <       }
1299 <   for (int j=i+1;j<a5segs.Count();j++) {
1300 <      CSegChain* chain=a5segs[i]->chain;
1301 <      if (chain->extendChain(a5segs[j])) {
1302 <         a5segs[j]->chain=chain;
1303 < #ifdef DEBUG
1304 <         if (debug) dbgPrintChain(*chain, adapter5.chars());
1305 < #endif      
1306 <      //save time by checking here if the extended chain is already acceptable for trimming
1307 <         if (chain->bend>(uint)(a5len-3) && chain->astart<4 && chain->score>a_min_chain_score) {
1308 <            l5=chain->aend;
1309 <            l3=rlen-1;
1310 <            return true;
1311 <            }
1312 <         } //chain can be extended
1313 <      }
1314 <   } //collect segment alignments into chains
1315 < */
1316 < return false; //no adapter parts found
1317 < }
969 >  }//for each 5' adapter
970 >  return trimmed;
971 > }
972  
973   //convert qvs to/from phred64 from/to phread33
974   void convertPhred(GStr& q) {
# Line 1378 | Line 1032
1032        }
1033      }// fastq
1034   // } //<-- FASTA or FASTQ
1035 < rseq.upper(); //TODO: what if we care about masking?
1035 > rseq.upper();
1036   return true;
1037   }
1038  
# Line 1387 | Line 1041
1041   // and a trash code if it was trashed
1042   l5=0;
1043   l3=rseq.length()-1;
1044 + #ifdef GDEBUG
1045 +   GMessage(">%s\n", rname.chars());
1046 +   GMessage("==>%s\n",rseq.chars());
1047 + #endif
1048   if (l3-l5+1<min_read_len) {
1049     return 's';
1050     }
# Line 1422 | Line 1080
1080     w5=0;
1081     w3=wseq.length()-1;
1082     }
1083 < if (a3len>0) {
1084 <  if (trim_adapter3(wseq, w5, w3)) {
1083 > char trim_code;
1084 > do {
1085 >  trim_code=0;
1086 >  if (trim_poly5(wseq, w5, w3, polyA_seed)) {
1087 >      trim_code='A';
1088 >      #ifdef GDEBUG
1089 >       GMessage("\t-trimmed poly-A at 5' end\n");
1090 >      #endif
1091 >      }
1092 >  else if (trim_poly5(wseq, w5, w3, polyT_seed)) {
1093 >      trim_code='T';
1094 >      #ifdef GDEBUG
1095 >       GMessage("\t-trimmed poly-T at 5' end\n");
1096 >      #endif
1097 >      }
1098 >  else if (trim_adapter5(wseq, w5, w3)) {
1099 >      trim_code='5';
1100 >      #ifdef GDEBUG
1101 >       GMessage("\t-trimmed adapter at 5' end\n");
1102 >      #endif
1103 >      }
1104 >  if (trim_code) {
1105       int trimlen=wseq.length()-(w3-w5+1);
1106 <     num_trimmed3++;
1107 <     if (trimlen<min_trimmed3)
1108 <         min_trimmed3=trimlen;
1106 >     num_trimmed5++;
1107 >     if (trimlen<min_trimmed5)
1108 >         min_trimmed5=trimlen;
1109       l5+=w5;
1110       l3-=(wseq.length()-1-w3);
1111       if (w3-w5+1<min_read_len) {
1112 <         return '3';
1112 >         return trim_code;
1113           }
1114        //-- keep only the w5..w3 range
1115        wseq=wseq.substr(w5, w3-w5+1);
1116        if (!wqv.is_empty())
1117           wqv=wqv.substr(w5, w3-w5+1);
1118 <      }//some adapter was trimmed
1119 <   } //adapter trimming
1120 < if (a5len>0) {
1121 <  if (trim_adapter5(wseq, w5, w3)) {
1118 >      }// trimmed at 5' end
1119 > } while (trim_code);
1120 >
1121 > do {
1122 >  trim_code=0;
1123 >  if (trim_poly3(wseq, w5, w3, polyA_seed)) {
1124 >      trim_code='A';
1125 >      }
1126 >  else if (trim_poly3(wseq, w5, w3, polyT_seed)) {
1127 >      trim_code='T';
1128 >      }
1129 >  else if (trim_adapter3(wseq, w5, w3)) {
1130 >      trim_code='3';
1131 >      }
1132 >  if (trim_code) {
1133       int trimlen=wseq.length()-(w3-w5+1);
1134 <     num_trimmed5++;
1135 <     if (trimlen<min_trimmed5)
1136 <         min_trimmed5=trimlen;
1134 >     num_trimmed3++;
1135 >     if (trimlen<min_trimmed3)
1136 >         min_trimmed3=trimlen;
1137       l5+=w5;
1138       l3-=(wseq.length()-1-w3);
1139       if (w3-w5+1<min_read_len) {
1140 <         return '5';
1140 >         return trim_code;
1141           }
1142        //-- keep only the w5..w3 range
1143        wseq=wseq.substr(w5, w3-w5+1);
1144        if (!wqv.is_empty())
1145           wqv=wqv.substr(w5, w3-w5+1);
1146 <      }//some adapter was trimmed
1147 <   } //adapter trimming
1146 >      }//trimming at 3' end
1147 > } while (trim_code);
1148 >
1149 >
1150   if (doCollapse) {
1151     //keep read for later
1152     FqDupRec* dr=dhash.Find(wseq.chars());
# Line 1514 | Line 1205
1205   void trash_report(char trashcode, GStr& rname, FILE* freport) {
1206   if (freport==NULL || trashcode<=' ') return;
1207   if (trashcode=='3' || trashcode=='5') {
1208 <   fprintf(freport, "%s\tA%c\n",rname.chars(),trashcode);
1208 >   fprintf(freport, "%s\ta%c\n",rname.chars(),trashcode);
1209     }
1210   else {
1211     fprintf(freport, "%s\t%c\n",rname.chars(),trashcode);
# Line 1606 | Line 1297
1297      }
1298   }
1299  
1300 +
1301 + int loadAdapters(const char* fname) {
1302 +  GLineReader lr(fname);
1303 +  char* l;
1304 +  while ((l=lr.nextLine())!=NULL) {
1305 +   if (lr.length()<=3 || l[0]=='#') continue;
1306 +   if ( l[0]==' ' || l[0]=='\t' || l[0]==',' ||
1307 +        l[0]==';'|| l[0]==':' ) {
1308 +      int i=1;
1309 +      while (l[i]!=0 && isspace(l[i])) {
1310 +        i++;
1311 +        }
1312 +      if (l[i]!=0) {
1313 +        GStr s(&(l[i]));
1314 +        adapters3.Add(s);
1315 +        continue;
1316 +        }
1317 +      }
1318 +    else {
1319 +      GStr s(l);
1320 +      s.startTokenize("\t ;,:");
1321 +      GStr a5,a3;
1322 +      if (s.nextToken(a5))
1323 +         s.nextToken(a3);
1324 +      a5.upper();
1325 +      a3.upper();
1326 +      adapters5.Add(a5);
1327 +      adapters3.Add(a3);
1328 +      }
1329 +   }
1330 +   return adapters5.Count()+adapters3.Count();
1331 + }
1332 +
1333   void setupFiles(FILE*& f_in, FILE*& f_in2, FILE*& f_out, FILE*& f_out2,
1334                         GStr& s, GStr& infname, GStr& infname2) {
1335   // uses outsuffix to generate output file names and open file handles as needed

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines