-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomp_seq.c
More file actions
1214 lines (1195 loc) · 34.2 KB
/
comp_seq.c
File metadata and controls
1214 lines (1195 loc) · 34.2 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* comp_seq.c
*
* Copyright 2019 Ryan Koehler, VerdAscend Sciences, ryan@verdascend.com
*
* The programs and source code of the vertools collection are free software.
* They are distributed in the hope that they will be useful,
* WITHOUT ANY WARRANTY OF FITNESS FOR ANY PARTICULAR PURPOSE.
*
* Permission is granted for research, educational, and possibly commercial use
* and modification as long as 1) Code and any derived works are not
* redistributed for any fee, and 2) Proper credit is given to the authors.
* If you wish to include this software in a product, or use it commercially,
* please contact the authors.
*
* See https://www.verdascend.com/ for more
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#define __MAIN__
#include "prim.h"
#include "dna.h"
#include "dna_pair.h"
#include "comp_seq.h"
#define DB_COMSEQ if(DB[114])
#define MAX_LINE 70
/**************************************************************************/
int main(int argc, char **argv)
{ Init(argc,argv); exit(AllDoneI(Comp_seqI(argc,argv),NULL)); }
/**************************************************************************/
void Comp_seqUse(void)
{
VersionSplash(NULL,VERSION_S,"# ",TRUE);
printf("Use: <infile> ['-' for stdin] [-sim|-com|-ham|-self|-psel|-loop] [...options]\n");
printf(" <infile> Is a sequence file (\"raw\" format)\n");
printf(" -iraw Treat input as \"raw\" format; <name> <seq> / line\n");
printf(" -iseq Treat input as simmple sequence; <seq> / line\n");
printf(" -ifas Treat input as fasta format\n");
printf(" -out XXX Set output to XXX\n");
printf(" -self -psel Evaluate for self complimentarity / parallel\n");
printf(" -loop # Evaluate hairpin with min \"loop\" size #\n");
printf(" -sim Evaluate similarity between seqs (best no-gap align)\n");
printf(" -com Evaluate complementarity between seqs (best no-gap align)\n");
printf(" -ham Evaluate \"Hamming distance\" (Similarity & No align)\n");
printf(" -smat -scon Score via Simple match / Contiguous match\n");
printf(" -sco3 -sco5 Score via contiguous match at 3' / 5' end\n");
printf(" -scb Score via Block-weighted match\n");
printf(" -swm Score via thermo weighted matching (Only complement)\n");
printf(" -sw32 Score via GC=3 AT=2 matching (Only complement)\n");
printf(" -mwf XXX Match weights from XXX (AA x, AC x, one pair/line)\n");
printf(" -rset XXX Compare infile seqs to reference set seqs XXX\n");
printf(" -crs Compare rset in serial order (not full set)\n");
printf(" -cl3 -cl5 # Force 3' / 5' end \"clamps\" in matches\n");
printf(" -mwd # -th # Set minimum word size / threshold to #\n");
printf(" -tot -num Report over-thresh Total / Number as score\n");
printf(" -fmat Report full pair-wise score matrix\n");
printf(" -rm Report matches (high score with position offset)\n");
printf(" -sa Show alignments (of high score match)\n");
printf(" -anp # Alignments name padding width (def = %d)\n",DEF_ANP);
printf(" -ds Dump (report) sequences appended as last column\n");
printf(" -alf # Set threhold for full alignment display\n");
printf(" -alp # Set threhold for partial alignment display\n");
printf(" -norm Normalize scores by maximum possible\n");
printf(" -flg # # Flag sequences with scores in the range # to #\n");
printf(" -eraw Extract flagged sequences in raw format\n");
printf(" -not Invert qualification for flagging / extraction\n");
printf("\n");
}
/**************************************************************************
* Main program
*/
int Comp_seqI(int argc, char **argv)
{
int scon,swm,scb,sw32,smat,cl3,cl5,sco3,sco5,iraw,ifas,iseq;
int r_max,r_num,r_tot,mword,quiet,nsco;
int i,flen,slen;
char smfS[DEF_BS], *fPC, *sPC, fnameS[NSIZEB];
COMPSEQ *csPO;
SEQ *seqPO, *sseqPO;
REAL scR,thR,partmatR;
csPO = CreateCompseqPO();
scon = swm = sw32 = scb = smat = FALSE;
sco3 = sco5 = r_tot = r_max = r_num = quiet = iraw = iseq = ifas = FALSE;
cl3 = cl5 = 0;
thR = 0.0;
partmatR = BAD_R;
mword = 1;
INIT_S(smfS);
if(!ParseArgsI(argc,argv,
"S -out S -sa B -sim B -com B -self B\
-scon B -rm B -swm B -rset S -mwf S -flg R2 -scb B\
-smat B -loop I -norm B -cl3 I -cl5 I -sco3 B -sco5 B -num B\
-tot B -th R -mwd I -fmat B -psel B -crs B -not B -eraw B \
-iraw B -ifas B -alf R -alp R -sw32 B -ham B -iseq B -ds B -anp I",
csPO->inname, csPO->outname, &csPO->do_sa, &csPO->do_sim, &csPO->do_com,
&csPO->do_self, &scon, &csPO->do_rm, &swm, csPO->rinname,
smfS, &csPO->flo,&csPO->fhi,
&scb, &smat, &csPO->do_loop,&csPO->do_norm,
&cl3, &cl5, &sco3, &sco5, &r_num, &r_tot,
&thR, &mword, &csPO->do_fmat, &csPO->do_pself, &csPO->do_crs,
&csPO->do_not, &csPO->do_eraw, &iraw, &ifas,
&csPO->fullmat, &partmatR, &sw32, &csPO->do_ham,
&iseq, &csPO->do_ds, &csPO->sa_anp,
(int *)NULL))
{
Comp_seqUse();
CHECK_COMPSEQ(csPO);
return(TRUE);
}
/***
* How are we comparing / what output?
*/
if(csPO->do_loop>0){
csPO->owhat = CSO_LOOP;
}
else if(csPO->do_pself){
csPO->owhat = CSO_PSELF;
}
else if(csPO->do_self){
csPO->owhat = CSO_SELF;
}
else if(csPO->do_crs){
csPO->owhat = CSO_RSER;
}
else {
csPO->owhat = CSO_FULL;
}
if(csPO->do_ham) {
SetPparsHammingI(csPO->pp, csPO->do_ham);
csPO->do_sim = TRUE;
}
/***
* Load / set comparison algorithms;
*/
if(csPO->do_sim) {
SetPair_parIdentMatch(csPO->pp);
}
else {
SetPair_parCompMatch(csPO->pp);
}
if( (swm) || (sw32) || (!NO_S(smfS)) ) {
if(!NO_S(smfS)) {
if(!LoadPairingParsI(smfS,csPO->pp)) {
CHECK_COMPSEQ(csPO);
return(FALSE);
}
}
else {
if(sw32) {
SetPair_parS3W2Wmatch(csPO->pp);
}
else {
SetPair_parThermoWmatch(csPO->pp);
if(BAD_REAL(partmatR)) {
csPO->partmat = -0.05;
}
}
}
}
if(!BAD_REAL(partmatR)) {
csPO->partmat = partmatR;
}
/***
* Default algorithm case = simple match; arg "smat" is this case
*/
if(scb) {
SetPparsAlgI(csPO->pp,ALGO_BMATCH);
}
else if(scon) {
SetPparsAlgI(csPO->pp,ALGO_CONT);
}
else if(sco3) {
SetPparsAlgI(csPO->pp,ALGO_CON3);
}
else if(sco5) {
SetPparsAlgI(csPO->pp,ALGO_CON5);
}
else if(smat || TRUE) {
SetPparsAlgI(csPO->pp,ALGO_MATCH);
}
SetPparsClampsI(csPO->pp,cl3,cl5);
SetPparsWordMinI(csPO->pp,mword);
/***
* Output-specific settings
*/
switch(csPO->owhat)
{
case CSO_LOOP:
SetPparsLoopMinI(csPO->pp,csPO->do_loop);
SetPparsCtypeI(csPO->pp,PP_COM_LOOP);
csPO->do_self = TRUE;
break;
case CSO_PSELF:
SetPparsCtypeI(csPO->pp,PP_COM_PCOM);
csPO->do_self = TRUE;
break;
case CSO_SELF:
SetPparsCtypeI(csPO->pp,PP_COM_COM);
csPO->do_self = TRUE;
break;
case CSO_RSER:
case CSO_FULL:
if(csPO->do_com)
{ SetPparsCtypeI(csPO->pp,PP_COM_COM); }
else if(csPO->do_sim)
{ SetPparsCtypeI(csPO->pp,PP_COM_SIM); }
csPO->do_self = FALSE;
break;
}
/***
* What score to report?
* r_max == default
*/
if(r_tot) {
SetPparsRvalI(csPO->pp,PP_R_TOT);
}
else if(r_num) {
SetPparsRvalI(csPO->pp,PP_R_NUM);
}
else if(r_max || TRUE) /* xxx SHAM? */ {
SetPparsRvalI(csPO->pp,PP_R_MAX);
}
SetPparsThreshI(csPO->pp,thR);
/***
* Get input format; guess then override with command line
*/
csPO->iform = FigureSeqFileTypeI(iraw, iseq, ifas, csPO->inname, TRUE);
if(!csPO->iform) {
CHECK_COMPSEQ(csPO);
ABORTLINE;
return(FALSE);
}
/***
* Get the sequences / input file; and allocate space for results
*/
if(!GetCompSeqSeqsI(csPO,quiet)) {
CHECK_COMPSEQ(csPO);
ABORTLINE;
return(FALSE);
}
if(!AddCompseqScoreSpaceI(csPO)) {
CHECK_COMPSEQ(csPO);
ABORTLINE;
return(FALSE);
}
/***
* Check options now that we have sequences
*/
if( (!csPO->do_sa) && (!csPO->do_rm) ) {
csPO->ostat = TRUE;
}
if(!CheckCompseqOptionsI(csPO)) {
CHECK_COMPSEQ(csPO);
ABORTLINE;
return(FALSE);
}
/***
* Output files?
*/
if(!OpenCompseqOutputI(csPO)) {
CHECK_COMPSEQ(csPO);
ABORTLINE;
return(FALSE);
}
/***
* Write headers
*/
WriteCompseqHeader(csPO,csPO->out);
/***
* Run the comparisions
*/
DB_COMSEQ DB_PrI("+ looking at nseqs %d\n",csPO->nseqs);
i = 0;
while(TRUE)
{
if(!GetNextSeqI(csPO,i,&seqPO)) {
break;
}
flen = GetSeqLenI(seqPO);
/* xx
if(csPO->do_ham) {
SetPparsWordMinI(csPO->pp,flen);
}
*/
GetSeqSeqI(seqPO,&fPC);
FillSeqNameStringI(seqPO,fnameS,-1);
/***
* Get pair-wise scores
*/
DB_COMSEQ DB_PrI("+ seq[%d]",i);
switch(csPO->owhat)
{
case CSO_LOOP:
DB_COMSEQ DB_PrI(" loop\n");
csPO->offs[0] = ScoreLoopOverlapI(fPC,flen,csPO->pp,&scR);
csPO->scores[0] = scR;
break;
case CSO_SELF:
case CSO_PSELF:
DB_COMSEQ DB_PrI(" [p]self\n");
csPO->offs[0] = ScoreSeqCompareI(fPC,flen,fPC,flen, csPO->pp,&scR);
csPO->scores[0] = scR;
break;
case CSO_RSER:
DB_COMSEQ DB_PrI(" rser\n");
GetSeqsetSeqStringI(csPO->target,i,&sPC);
slen = GetSeqsetSeqLenI(csPO->target,i);
csPO->offs[0] = ScoreSeqCompareI(fPC,flen,sPC,slen, csPO->pp,&scR);
csPO->scores[0] = scR;
break;
case CSO_FULL:
DB_COMSEQ DB_PrI(" full\n");
CompareSeqToSeqsetI(seqPO, csPO->target, csPO->pp, csPO->scores, csPO->offs);
break;
}
/***
* Process score collection and different outputs
*/
nsco = ProcessScoresI(csPO,i,fPC,flen);
if(HandleSeqFlaggingI(csPO,seqPO,csPO->out)) {
i++;
continue;
}
if(HandleMatrixOutI(csPO,fnameS,csPO->out)) {
i++;
continue;
}
switch(csPO->owhat)
{
case CSO_LOOP:
HandleSeqLoopOut(csPO,seqPO,0);
break;
case CSO_PSELF:
HandleSeqPairOut(csPO,seqPO,NULL,0,FALSE,TRUE);
break;
case CSO_SELF:
HandleSeqPairOut(csPO,seqPO,NULL,0,TRUE,FALSE);
break;
case CSO_RSER:
GetSeqsetSeqI(csPO->target,i,&sseqPO);
if(csPO->do_com) {
HandleSeqPairOut(csPO,seqPO,sseqPO,0,TRUE,FALSE);
}
else {
HandleSeqPairOut(csPO,seqPO,sseqPO,0,FALSE,FALSE);
}
break;
case CSO_FULL:
HandleFullScoreOut(csPO,nsco,i,seqPO);
}
/***
* Next seq
*/
i++;
}
/***
* All done
*/
CHECK_COMPSEQ(csPO);
return(TRUE);
}
/*****************************************************************************
* Return the next seq to be processed, or FALSE if at end of list
*/
int GetNextSeqI(COMPSEQ *csPO,int s, SEQ **seqPPO)
{
SEQ *seqPO;
if(csPO->do_self) {
if(!ParseSeqI(csPO->in, csPO->iform, s+1, TRUE, TRUE, csPO->seq)) {
return(FALSE);
}
seqPO = csPO->seq;
}
else {
if(!GetSeqsetSeqI(csPO->seqs,s,&seqPO)) {
return(FALSE);
}
}
*seqPPO = seqPO;
return(TRUE);
}
/*****************************************************************************/
void HandleNoScoreOut(COMPSEQ *csPO,SEQ *seqPO)
{
char fnameS[NSIZEB], *fPC;
FillSeqNameStringI(seqPO,fnameS,-1);
fprintf(csPO->out,"%-10s = 0 (NO SCORE)",fnameS);
if(csPO->do_ds) {
GetSeqSeqI(seqPO, &fPC);
fprintf(csPO->out,"\t%s",fPC);
}
fprintf(csPO->out,"\n");
}
/*****************************************************************************
* Output sequence pair info
*/
void HandleSeqPairOut(COMPSEQ *csPO, SEQ *fseqPO, SEQ *sseqPO, int sc, int
comp, int antip)
{
char fseqS[MAX_CSL],sseqS[MAX_CSL],pS[MAX_CSL],compS[MAX_CSL];
char *fPC,*sPC,fnameS[NSIZEB],snameS[NSIZEB],formS[DEF_BS];
int i,n,fi,si,p,flen,slen,off;
REAL scR,alscR;
DB_COMSEQ DB_PrI(">> HandleSeqPairOut sc=%d com=%d ap=%d\n",sc,comp,antip);
HAND_NFILE(csPO->out);
off = csPO->offs[sc];
scR = csPO->scores[sc];
DB_COMSEQ DB_PrI("+ scR=%f off=%d\n",scR,off);
/***
* Get names, lens, seq pointers
*/
FillSeqNameStringI(fseqPO,fnameS,-1);
flen = GetSeqLenI(fseqPO);
GetSeqSeqI(fseqPO, &fPC);
if(csPO->ostat) {
fprintf(csPO->out,"%-20s %6.2f",fnameS,scR);
if(csPO->do_ds) {
fprintf(csPO->out,"\t%s",fPC);
}
fprintf(csPO->out,"\n");
return;
}
/***
* Secondary sequence set? Otherwise, set secondary things to first sequence
*/
if( sseqPO != NULL) {
FillSeqNameStringI(sseqPO,snameS,-1);
slen = GetSeqLenI(sseqPO);
GetSeqSeqI(sseqPO,&sPC);
}
else {
strcpy(snameS,fnameS);
slen = flen;
sPC = fPC;
}
/***
* If failing / bogus score then don't draw anything
*/
if(scR < 0.0) {
if(csPO->do_rm) {
fprintf(csPO->out,"%-10s -vs- %-10s = 0 (NO SCORE)", fnameS,snameS);
}
if(csPO->do_ds) {
fprintf(csPO->out,"\t%s\t%s",fPC,sPC);
}
fprintf(csPO->out,"\n");
return;
}
/***
* What will be printed as comparison? actual or inverse (3' to 5')
*/
if(comp) {
ReverseDNASeqI(sPC,slen,compS);
}
else {
strncpy(compS,sPC,slen);
compS[slen] = '\0';
}
/***
* Build up front part of match strings
*/
si = fi = p = n = 0;
for(i=0;i<ABS_VAL(off);i++)
{
if(off < 0) {
fseqS[p] = ' ';
sseqS[p] = compS[si++];
}
else {
fseqS[p] = fPC[fi++];
sseqS[p] = ' ';
}
pS[p] = ' ';
p++;
}
/***
* Build matching part, counting observed matchs
*/
while(TRUE)
{
if( (fi >= flen) || (si >= slen) ) {
break;
}
fseqS[p] = fPC[fi];
sseqS[p] = compS[si];
if(GetPair_parMatchScoreI(csPO->pp,fseqS[p],sseqS[p],&alscR))
{
if(alscR >= csPO->fullmat) {
pS[p] = '|';
n++;
}
else if(alscR >= csPO->partmat) {
pS[p] = ':';
n++;
}
else {
pS[p] = ' ';
}
}
else {
pS[p] = 'X';
}
fi++;
si++;
p++;
}
/***
* Anything left?
*/
while( (fi < flen) || (si < slen) )
{
fseqS[p] = sseqS[p] = ' ';
if(fi < flen) {
fseqS[p] = fPC[fi];
}
if(si < slen) {
sseqS[p] = compS[si];
}
pS[p] = ' ';
fi++;
si++;
p++;
}
fseqS[p] = sseqS[p] = pS[p] = '\0';
/***
* Summary statement?
* xxx 5/2/18 RTK put in tabs; Add fraciton match
*/
if(csPO->do_rm) {
fprintf(csPO->out,"%-10s -vs- %-10s = %2.2f",fnameS,snameS,scR);
fprintf(csPO->out,"\t@[%d]",off);
fprintf(csPO->out,"\t%d/%d",n,flen);
fprintf(csPO->out,"\t%3.2f",RNUM(n)/RNUM(flen));
if(csPO->do_ds) {
fprintf(csPO->out,"\t%s\t%s",fPC,sPC);
}
fprintf(csPO->out,"\n");
}
/***
* If not actually showing alignment, bail
*/
if(!csPO->do_sa) {
return;
}
/***
* Ajdustable format string for different name lengths
* Max of names, name-vs-user, then limit
*/
n = MAX_NUM(strlen(fnameS),strlen(snameS));
n = MAX_NUM(n,csPO->sa_anp);
LIMIT_NUM(n, 10, 60);
sprintf(formS,"%%-%ds",n);
/***
* Report the match
*/
if(comp) {
fprintf(csPO->out,formS,fnameS);
fprintf(csPO->out," >5' %s\n",fseqS);
fprintf(csPO->out,formS,"matching:");
fprintf(csPO->out," %s\n",pS);
fprintf(csPO->out,formS,snameS);
fprintf(csPO->out," <3' %s\n",sseqS);
}
else {
fprintf(csPO->out,formS,fnameS);
fprintf(csPO->out," >5' %s\n",fseqS);
fprintf(csPO->out,formS,"matching:");
fprintf(csPO->out," %s\n",pS);
fprintf(csPO->out,formS,snameS);
fprintf(csPO->out," >5' %s\n",sseqS);
}
fprintf(csPO->out,"\n");
}
/****************************************************************************
* Make pretty picture of a loop'd sequence
* off = offset of 3'end relative to 5' end.
*/
void HandleSeqLoopOut(COMPSEQ *csPO, SEQ *seqPO, int sc)
{
int i,p,n,ls,fi,si,len,off;
char startS[MAX_CSL],endS[MAX_CSL],pS[MAX_CSL];
char nameS[NSIZEB], *sPC;
REAL scR;
off = csPO->offs[sc];
scR = csPO->scores[sc];
HAND_NFILE(csPO->out);
/***
* Sequence pointer, len, name
*/
len = GetSeqLenI(seqPO);
GetSeqSeqI(seqPO,&sPC);
FillSeqNameStringI(seqPO,nameS,-1);
/***
* If only stats, report name and score
*/
if(csPO->ostat) {
fprintf(csPO->out,"%-20s %6.2f",nameS,scR);
if(csPO->do_ds) {
fprintf(csPO->out,"\t%s",sPC);
}
fprintf(csPO->out,"\n");
return;
}
/***
* Fill in any overhaning parts
*/
si = len -1;
p = fi = 0;
for(i=0;i<ABS_VAL(off);i++)
{
if(off < 0) {
startS[p] = ' ';
endS[p] = sPC[si--];
}
else {
startS[p] = sPC[fi++];
endS[p] = ' ';
}
pS[p] = ' ';
p++;
}
/***
* Fill in matching "stem" parts
*/
n = 0;
ls = (len - csPO->pp->min_loop - ABS_VAL(off))/2;
for(i=0;i<ls;i++)
{
startS[p] = sPC[fi++];
endS[p] = sPC[si--];
if(GoodDNACompBasesI(startS[p],endS[p])) {
pS[p] = '|';
n++;
}
else {
pS[p] = ' ';
}
p++;
}
/***
* End loop stuff
*/
startS[p] = endS[p] = '-';
pS[p++] = ' ';
ls = (csPO->pp->min_loop +1)/2;
for(i=0;i<ls; i++)
{
startS[p] = sPC[fi++];
if((ABS_VAL(off + len) % 2)>0) {
if(i==0) {
endS[p] = '-';
}
endS[p+1] = sPC[si--];
}
else {
endS[p] = sPC[si--];
}
pS[p] = ' ';
p++;
}
pS[p-1] = ':';
/***
* Final output
*/
startS[p] = endS[p] = pS[p] = '\0';
p = 0;
if(csPO->do_rm) {
fprintf(csPO->out,"%s LoopScore = %6.2f",nameS,scR);
fprintf(csPO->out," @[%d]",off);
if(csPO->do_ds) {
fprintf(csPO->out,"\t%s",sPC);
}
fprintf(csPO->out,"\n");
p++;
}
if(csPO->do_sa) {
fprintf(csPO->out,"%-20s 5'> %s\n",nameS,startS);
fprintf(csPO->out,"%-20s %s\n",nameS,pS);
fprintf(csPO->out,"%-20s 3'< %s\n",nameS,endS);
p++;
}
if(p) {
fprintf(csPO->out,"\n");
}
}
/***************************************************************************
* Load sequences
* If self-compare, only open the input file to eat one at time
*/
int GetCompSeqSeqsI(COMPSEQ *csPO,int quiet)
{
SEQSET *ssPO, *rssPO;
int max;
DB_COMSEQ DB_PrI(">> GetCompSeqSeqsI\n");
/***
* Special case for self-compare; only open input file, don't load
*/
if(csPO->do_self)
{
if(!(csPO->in=OpenUFilePF(csPO->inname,"r",NULL)))
{
return(FALSE);
}
csPO->nseqs = 1;
return(TRUE);
}
/***
* Load sequence sets
*/
ssPO = rssPO = NULL;
if(!ReadInSeqsetI(csPO->inname,csPO->iform,TRUE,&ssPO,TRUE))
{
PROBLINE;
printf("Didn't get sequences from %s\n",csPO->inname);
return(FALSE);
}
csPO->seqs = ssPO;
if(!quiet)
{
printf("# Have %d sequences loaded\n",ssPO->n);
}
max = SeqsetMaxLenI(ssPO);
if(max >= MAX_CSL)
{
PROBLINE;
printf("Max sequence length exceeded (%d)\n",max);
printf("May only compare to length %d\n",MAX_CSL);
return(FALSE);
}
/***
* Reference set?
*/
if(!NO_S(csPO->rinname))
{
if(!ReadInSeqsetI(csPO->rinname,csPO->iform,TRUE,&rssPO,TRUE))
{
printf("Didn't get reference seqs from %s\n",csPO->rinname);
return(FALSE);
}
csPO->rseqs = rssPO;
if(!quiet)
{
printf("# Have %d reference sequences loaded\n",rssPO->n);
}
max = SeqsetMinLenI(rssPO);
if(max >= MAX_CSL)
{
PROBLINE;
printf("Maximum seq length exceeded (ref set %d)\n",max);
printf("May only compare to length %d\n",MAX_CSL);
return(FALSE);
}
}
/***
* Set pointer to actual sequences; Which one is target?
* If a ref seqset is real, use that, else use the first set
*/
csPO->nseqs = ssPO->n;
if(rssPO)
{
csPO->target = rssPO;
csPO->ntargseqs = rssPO->n;
}
else
{
csPO->target = ssPO;
csPO->ntargseqs = ssPO->n;
}
DB_COMSEQ DB_PrI("<< GetCompSeqSeqsI ntargseqs=%d TRUE\n",csPO->ntargseqs);
return(TRUE);
}
/***************************************************************************
* Header info to dump with answers
*/
void WriteCompseqHeader(COMPSEQ *csPO,FILE *outPF)
{
char bufS[DEF_BS];
HAND_NFILE(outPF);
fprintf(outPF,"# %s, %s %s %s\n",VERSION_S,BD_S,__DATE__,__TIME__);
fprintf(outPF,"# Input........ %s\n",csPO->inname);
if(!NO_S(csPO->rinname)) {
fprintf(outPF,"# Compared to.. %s\n",csPO->rinname);
}
FillCompseqOutWhatString(csPO->owhat,bufS);
fprintf(outPF,"# Reporting.... %s\n",bufS);
/* xx ? better place .... or just replace whole sham! */
if(csPO->do_ham) {
fprintf(outPF,"# No alignment; Hamming distance\n");
}
else {
fprintf(outPF,"# Gap-free alignment max score\n");
}
fprintf(outPF,"# \n");
DumpPpars(csPO->pp,outPF);
if(csPO->do_fmat) {
WriteHeaderForFmat(csPO,outPF);
return;
}
if(csPO->ostat) {
if(csPO->do_flag) {
if(csPO->do_eraw) {
fprintf(outPF,"# Name Sequence\n");
}
else {
fprintf(outPF,"# Flag Name Score\n");
}
}
else if( (csPO->do_self) || (csPO->owhat==CSO_RSER) ) {
fprintf(outPF,"# Name Score\n");
}
else {
fprintf(outPF,"# %-20s\t%s\t%s\t%s\t%s\n",
"Name", "Min", "Ave", "Max", "N-Max");
}
}
return;
}
/***************************************************************************/
void FillCompseqOutWhatString(int owhat,char *bufS)
{
switch(owhat)
{
case CSO_LOOP: sprintf(bufS,"Hairpin loop stems"); break;
case CSO_PSELF: sprintf(bufS,"Parallel self complement"); break;
case CSO_SELF: sprintf(bufS,"Self complement"); break;
case CSO_RSER: sprintf(bufS,"Serially listed reference"); break;
case CSO_FULL: sprintf(bufS,"Full pair-wise comparison"); break;
default:
INIT_S(bufS);
}
return;
}
/***************************************************************************
* Column labes for matrix dump
*/
void WriteHeaderForFmat(COMPSEQ *csPO,FILE *outPF)
{
int i,j;
char snameS[NSIZEB];
HAND_NFILE(outPF);
fprintf(outPF,"Name");
for(i=0;i<csPO->ntarg;i++)
{
if(i>=csPO->ntargseqs) {
j = i - csPO->ntargseqs;
}
else {
j = i;
}
FillSeqsetSeqNameI(csPO->target,j,snameS,-1);
fprintf(outPF,"\t%s",snameS);
}
fprintf(outPF,"\n");
return;
}
/***************************************************************************
* Add space to hold scores
*/
int AddCompseqScoreSpaceI(COMPSEQ *csPO)
{
DB_COMSEQ DB_PrI(">> AddCompseqScoreSpaceI sim=%d com=%d\n",
csPO->do_sim,csPO->do_com);
if(csPO->do_sim || csPO->do_com) {
csPO->ntarg = csPO->ntargseqs;
}
else {
csPO->ntarg = 1;
}
DB_COMSEQ DB_PrI("+ ntarg = %d\n",csPO->ntarg);
csPO->scores = (REAL *)ALLOC(csPO->ntarg,sizeof(REAL));
csPO->offs = (int *)ALLOC(csPO->ntarg,sizeof(int));
if( (!csPO->scores) || (!csPO->offs) ) {
PROBLINE;
printf("Failed to allocate for %d scores/offsets\n",csPO->ntarg);
return(FALSE);
}
DB_COMSEQ DB_PrI("<< AddCompseqScoreSpaceI TRUE\n");
return(TRUE);
}
/***************************************************************************/
int OpenCompseqOutputI(COMPSEQ *csPO)
{
if(!NO_S(csPO->outname)) {
if(!(csPO->out=OpenUFilePF(csPO->outname,"w",NULL))) {
return(FALSE);
}
}
HAND_NFILE(csPO->out);
return(TRUE);
}
/***************************************************************************
* Alloc and init compseq structure
*/
COMPSEQ *CreateCompseqPO()
{
COMPSEQ *csPO;
if(!(csPO = (COMPSEQ *)ALLOC(1,sizeof(COMPSEQ)))) {
return(NULL);
}
csPO->ID = COMPSEQ_ID;
csPO->pp = CreatePparsPO();
csPO->seq = CreateSeqPO(0,NULL,NULL);
if( (!csPO->pp) || (!csPO->seq) ) {
CHECK_COMPSEQ(csPO);
return(NULL);
}
InitCompseq(csPO);
return(csPO);
}
/***************************************************************************
* Free up structure and sub-structs
*/
int DestroyCompseqI(COMPSEQ *csPO)
{
VALIDATE(csPO,COMPSEQ_ID);
CHECK_FREE(csPO->offs);
CHECK_FREE(csPO->scores);
CHECK_PPARS(csPO->pp);
CHECK_SEQ(csPO->seq);
CHECK_SEQSET(csPO->seqs);
CHECK_SEQSET(csPO->rseqs);
CHECK_FILE(csPO->in);
CHECK_NFILE(csPO->out,csPO->outname);
CHECK_FREE(csPO);
return(TRUE);
}
/****************************************************************************
* Initialize global structure
*/
void InitCompseq(COMPSEQ *csPO)
{
INIT_S(csPO->inname);
INIT_S(csPO->rinname);
INIT_S(csPO->outname);
INIT_S(csPO->com);
csPO->do_ds = csPO->do_sa = csPO->do_rm = FALSE;
csPO->sa_anp = DEF_ANP;
csPO->do_norm = FALSE;
csPO->do_flag = csPO->do_eraw = FALSE;
csPO->flo = csPO->fhi = BAD_R;
csPO->do_sim = csPO->do_com = csPO->do_self = FALSE;
csPO->do_loop = -1;
csPO->fullmat = 1.0;
csPO->partmat = 1.0;
}
/***************************************************************************
* Check options
*/
int CheckCompseqOptionsI(COMPSEQ *csPO)
{
/***
* Anything to really do?
*/
if( (!csPO->do_sim) && (!csPO->do_com) && (!csPO->do_self) ) {
Comp_seqUse();
printf("\n");
printf(" You must specify -sim, -comp, -self, -psel, or -loop!\n");
printf("\n");
return(FALSE);
}
if( (csPO->do_sim) && (csPO->do_com) ) {
PROBLINE;
printf("\n");
printf(" Both -sim and -com are not allowed together\n");
printf("\n");
return(FALSE);
}
if( (csPO->owhat==CSO_RSER) && NO_S(csPO->rinname) ) {
PROBLINE;
printf("Serial comparision (-crs) only works with ref set (-rset)\n");
return(FALSE);
}
if(!BAD_REAL(csPO->flo)) {
if( csPO->flo > csPO->fhi ) {
PROBLINE;
printf("Bad flagging values: %4.2f to %4.2f\n",csPO->flo,csPO->fhi);
return(FALSE);
}
csPO->do_flag = TRUE;
}
if( (csPO->do_fmat) && (csPO->do_self) )