forked from galosh/profillic-hmmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofillic-esl_msa.hpp
1604 lines (1427 loc) · 59.2 KB
/
profillic-esl_msa.hpp
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
#ifndef __GALOSH_PROFILLICESLMSA_HPP__
#define __GALOSH_PROFILLICESLMSA_HPP__
/*::cexcerpt::header_example::begin::*/
/* Multiple sequence alignment file i/o.
*
* Contents:
* 1. The <ESL_MSA> object
* 2. The <ESL_MSAFILE> object
* 3. Digital mode MSA's (augmentation: alphabet)
* 4. Random MSA database access (augmentation: ssi)
* 5. General i/o API, for all alignment formats
* 6. Miscellaneous functions for manipulating MSAs
* 7. Stockholm (Pfam/Rfam) format
* 8. A2M format
* 9. PSIBLAST format
* 10. SELEX format
* 11. AFA (aligned FASTA) format
* 12. Memory efficient routines for PFAM format
* 13. Debugging/development routines
* 14. Benchmark driver
* 15. Unit tests
* 16. Test driver
* 17. Examples
* 18. Copyright and license information
*
* Augmentations:
* alphabet: adds support for digital MSAs
* keyhash: speeds up Stockholm file input
* ssi: enables indexed random access in a file of many MSAs
*
* to do: SRE, Sat Jan 3 09:43:42 2009 (after selex parser added)
* - SELEX parser is better in some respects than older Stockholm
* parser; stricter, better error detection, better modularity.
* Generalize the SELEX parser routines and use them for Stockholm.
* - Test files for SELEX parser are in esl_msa_testfiles/selex, with
* tabular summary list in 00MANIFEST. This is an experiment with
* writing tests that require lots of external files, such as
* format parsers. Write test driver routine that reads 00MANIFEST
* and runs esl_msa_Read() against these files, checking for proper
* return status, including errors.
* - The selex parser's read_block() reads lines into memory and
* parses them later. afp->linenumber is thus no longer an
* accurate record of where a parse error occurs. read_xxx()
* format parsers now need to include line number in their
* afp->errbuf[] message upon eslEFORMAT error. Stockholm parser
* doesn't do this. Make it so, and document in examples.
* - Format autodetection doesn't work yet. Coordinate w/ how sqio
* does it, and implement. May require buffering input to make
* it work with .gz, pipes without rewinding a stream. Might be
* a good idea to generalize input buffering - perhaps making
* it part of ESL_FILEPARSER.
* - PSIBLAST, A2M format only supported on output, not input.
* Implement input parsers.
* - SELEX format only supported on input, not output.
* Implement output writer.
* - More formats need to be parsed. Check on formats for current
* best MSA programs, such as MUSCLE, MAFFT; implement i/o.
*
* SRE, Thu Jan 20 08:50:43 2005 [St. Louis]
* SVN $Id: esl_msa.c 573 2010-03-27 15:13:52Z eddys $
*/
/*::cexcerpt::header_example::end::*/
/*::cexcerpt::include_example::begin::*/
extern "C" {
#include "esl_config.h"
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
extern "C" {
#include "easel.h"
#ifdef eslAUGMENT_KEYHASH
#include "esl_keyhash.h"
#endif
#ifdef eslAUGMENT_ALPHABET
#include "esl_alphabet.h"
#endif
#ifdef eslAUGMENT_SSI
#include "esl_ssi.h"
#endif
#include "esl_msa.h"
#include "esl_vectorops.h"
#include "esl_wuss.h"
}
/*::cexcerpt::include_example::end::*/
/////////////// For profillic-hmmer //////////////////////////////////
#include "profillic-hmmer.hpp"
extern "C" {
#include "esl_msa.h"
}
#define eslMSAFILE_PROFILLIC 98103 /* A galosh profile (from profillic) */
/////////////// End profillic-hmmer //////////////////////////////////
/******************************************************************************
*# 1. The <ESL_MSA> object
*****************************************************************************/
/* get_seqidx()
*
* Find the index of a given sequence <name> in an <msa>.
* If caller has a good guess (for instance, the sequences are
* coming in a previously seen order in a block of seqs or annotation),
* the caller can pass this information in <guess>, or -1 if
* it has no guess.
*
* This function behaves differently depending on whether
* keyhash augmentation is available or not. Without keyhashing,
* the name is identified by bruteforce search of the names
* in the <msa>. With keyhashing, we hash search, which should
* improve performance for large alignments.
*
* If the name does not already exist in the MSA, then it
* is assumed to be a new sequence name that we need to store.
* seqidx is set to msa->nseq, the MSA is Expand()'ed if necessary
* to make room, the name is stored in msa->sqname[msa->nseq],
* (and in the hash table, if we're keyhash augmented)
* and msa->nseq is incremented.
*
* Returns: <eslOK> on success, and the seqidx is
* passed back via <ret_idx>. If <name> is new
* in the <msa>, the <name> is stored and the <msa>
* may be internally reallocated if needed.
*
* Throws: <eslEMEM> if we try to add a name and allocation fails.
* <eslEINVAL> if we try to add a name to a non-growable MSA.
*/
static int
get_seqidx(ESL_MSA *msa, char *name, int guess, int *ret_idx)
{
int seqidx;
int status;
*ret_idx = -1;
/* can we guess? */
if (guess >= 0 &&
guess < msa->nseq &&
strcmp(name, msa->sqname[guess]) == 0)
{ *ret_idx = guess; return eslOK; }
/* Else look it up - either brute force
* or, if we're keyhash-augmented, by hashing.
*/
#ifdef eslAUGMENT_KEYHASH
status = esl_key_Store(msa->index, name, &seqidx);
if (status == eslEDUP) { *ret_idx = seqidx; return eslOK; }
if (status != eslOK) return status; /* an error. */
#else
for (seqidx = 0; seqidx < msa->nseq; seqidx++)
if (strcmp(msa->sqname[seqidx], name) == 0) break;
if (seqidx < msa->nseq)
{ *ret_idx = seqidx; return eslOK; }
#endif
/* If we reach here, then this is a new name that we're
* adding.
*/
if (seqidx >= msa->sqalloc &&
(status = esl_msa_Expand(msa)) != eslOK)
return status;
status = esl_strdup(name, -1, &(msa->sqname[seqidx]));
msa->nseq++;
if (ret_idx != NULL) *ret_idx = seqidx;
return status;
}
/* verify_parse()
*
* Last function called after a multiple alignment parser thinks it's
* done. Checks that parse was successful; makes sure required
* information is present; makes sure required information is
* consistent. Some fields that are only use during parsing may be
* freed (sqlen, for example), and some fields are finalized now
* (<msa->alen> is set, for example).
*
* <errbuf> is a place to sprintf an informative message about the
* reason for a parse error. The caller provides an <errbuf>
* of at least 512 bytes.
*
* Returns: <eslOK>, and errbuf is set to an empty string.
*
* Throws: <eslEFORMAT> if a problem is detected, and an
* informative message about the failure is in errbuf.
*/
static int
verify_parse(ESL_MSA *msa, char *errbuf)
{
int idx;
if (msa->nseq == 0) ESL_FAIL(eslEFORMAT, errbuf, "parse error: no alignment data found");
/* set alen, until proven otherwise; we'll check that the other seqs
* have the same length later.
*/
msa->alen = msa->sqlen[0];
/* We can rely on msa->sqname[] being valid for any index,
* because of the way the line parsers always store any name
* they add to the index.
*/
for (idx = 0; idx < msa->nseq; idx++)
{
#ifdef eslAUGMENT_ALPHABET
if ((msa->flags & eslMSA_DIGITAL) && (msa->ax == NULL || msa->ax[idx] == NULL))
ESL_FAIL(eslEFORMAT, errbuf, "MSA %s parse error: no sequence for %s",
msa->name != NULL ? msa->name : "", msa->sqname[idx]);
#endif
if (! (msa->flags & eslMSA_DIGITAL) && (msa->aseq == NULL || msa->aseq[idx] == NULL))
ESL_FAIL(eslEFORMAT, errbuf, "MSA %s parse error: no sequence for %s",
msa->name != NULL ? msa->name : "", msa->sqname[idx]);
/* either all weights must be set, or none of them */
if ((msa->flags & eslMSA_HASWGTS) && msa->wgt[idx] == -1.0)
ESL_FAIL(eslEFORMAT, errbuf, "MSA %s parse error: expected a weight for seq %s",
msa->name != NULL ? msa->name : "", msa->sqname[idx]);
/* all aseq must be same length. */
if (msa->sqlen[idx] != msa->alen)
ESL_FAIL(eslEFORMAT, errbuf, "MSA %s parse error: sequence %s: length %ld, expected %ld",
msa->name != NULL ? msa->name : "", msa->sqname[idx], static_cast<long int>( msa->sqlen[idx] ), static_cast<long int>( msa->alen ) );
/* if individual SS is present, it must have length right too */
if (msa->ss != NULL && msa->ss[idx] != NULL && msa->sslen[idx] != msa->alen)
ESL_FAIL(eslEFORMAT, errbuf, "MSA %s parse error: GR SS for %s: length %ld, expected %ld",
msa->name != NULL ? msa->name : "", msa->sqname[idx], msa->sslen[idx], msa->alen);
/* if SA is present, must have length right */
if (msa->sa != NULL && msa->sa[idx] != NULL && msa->salen[idx] != msa->alen)
ESL_FAIL(eslEFORMAT, errbuf, "MSA %s parse error: GR SA for %s: length %ld, expected %ld",
msa->name != NULL ? msa->name : "", msa->sqname[idx], msa->salen[idx], msa->alen);
/* if PP is present, must have length right */
if (msa->pp != NULL && msa->pp[idx] != NULL && msa->pplen[idx] != msa->alen)
ESL_FAIL(eslEFORMAT, errbuf, "MSA %s parse error: GR PP for %s: length %ld, expected %ld",
msa->name != NULL ? msa->name : "", msa->sqname[idx], msa->pplen[idx], msa->alen);
}
/* if cons SS is present, must have length right */
if (msa->ss_cons != NULL && strlen(msa->ss_cons) != msa->alen)
ESL_FAIL(eslEFORMAT, errbuf, "MSA %s parse error: GC SS_cons markup: len %zd, expected %ld",
msa->name != NULL ? msa->name : "", strlen(msa->ss_cons), msa->alen);
/* if cons SA is present, must have length right */
if (msa->sa_cons != NULL && strlen(msa->sa_cons) != msa->alen)
ESL_FAIL(eslEFORMAT, errbuf, "MSA %s parse error: GC SA_cons markup: len %zd, expected %ld",
msa->name != NULL ? msa->name : "", strlen(msa->sa_cons), msa->alen);
/* if cons PP is present, must have length right */
if (msa->pp_cons != NULL && strlen(msa->pp_cons) != msa->alen)
ESL_FAIL(eslEFORMAT, errbuf, "MSA %s parse error: GC PP_cons markup: len %zd, expected %ld",
msa->name != NULL ? msa->name : "", strlen(msa->pp_cons), msa->alen);
/* if RF is present, must have length right */
if (msa->rf != NULL && strlen(msa->rf) != msa->alen)
ESL_FAIL(eslEFORMAT, errbuf, "MSA %s parse error: GC RF markup: len %zd, expected %ld",
msa->name != NULL ? msa->name : "", strlen(msa->rf), msa->alen);
/* If no weights were set, set 'em all to 1.0 */
if (!(msa->flags & eslMSA_HASWGTS))
for (idx = 0; idx < msa->nseq; idx++)
msa->wgt[idx] = 1.0;
/* Clean up a little from the parser */
if (msa->sqlen != NULL) { free(msa->sqlen); msa->sqlen = NULL; }
if (msa->sslen != NULL) { free(msa->sslen); msa->sslen = NULL; }
if (msa->salen != NULL) { free(msa->salen); msa->salen = NULL; }
if (msa->pplen != NULL) { free(msa->pplen); msa->pplen = NULL; }
return eslOK;
}
static int read_stockholm(ESL_MSAFILE *afp, ESL_MSA **ret_msa);
static int read_selex (ESL_MSAFILE *afp, ESL_MSA **ret_msa);
static int read_afa (ESL_MSAFILE *afp, ESL_MSA **ret_msa);
template <typename ProfileType>
static int profillic_read_profile (ESL_MSAFILE *afp, ESL_MSA **ret_msa, ProfileType * profile_ptr);
/* Function: esl_msa_Read()
* Synopsis: Read next MSA from a file.
* Incept: SRE, Fri Jan 28 08:10:49 2005 [St. Louis]
*
* Purpose: Reads the next MSA from an open MSA file <afp>,
* and returns it via <ret_msa>.
*
* Returns: <eslOK> on success, and <ret_msa> points at the
* new MSA object.
*
* Returns <eslEOF> if there are no more alignments in the file.
*
* Returns <eslEFORMAT> if there is a parse error, and <afp->errbuf>
* is set to an informative message.
*
* <eslEINVAL> if we're trying to read a digital alignment,
* but one or more residues are seen in the file that
* aren't valid in our alphabet.
*
* Throws: <eslEMEM> on allocation failure.
* <eslEINCONCEIVABLE> on internal error.
*/
template <class ProfileType>
int
profillic_esl_msa_Read(ESL_MSAFILE *afp, ESL_MSA **ret_msa, ProfileType * profile_ptr)
{
ESL_MSA *msa;
int status;
*ret_msa = NULL;
/* If we've just used GuessAlphabet(), we have an MSA already read
* and stored in the MSAFILE's cache. Just return it, after worrying
* about whether it's supposed to be in digital or text mode. (It
* should always be in text mode, and maybe in need of Digitize(),
* given how GuessAlphabet works now; but this is coded for more
* generality in case we use the MSA cache some other way in the
* future.)
*/
if (afp->msa_cache != NULL)
{
#ifdef eslAUGMENT_ALPHABET
if (afp->do_digital && !(afp->msa_cache->flags & eslMSA_DIGITAL)) {
if ((status = esl_msa_Digitize(afp->abc, afp->msa_cache, afp->errbuf)) != eslOK) return status;
}
else if (! afp->do_digital && (afp->msa_cache->flags & eslMSA_DIGITAL)) {
if ((status = esl_msa_Textize(afp->msa_cache)) != eslOK) return status;
}
#endif
*ret_msa = afp->msa_cache;
afp->msa_cache = NULL;
return eslOK;
}
/* Otherwise, read the next MSA from the file.
*/
switch (afp->format) {
case eslMSAFILE_STOCKHOLM: status = read_stockholm(afp, &msa); break;
case eslMSAFILE_PFAM: status = read_stockholm(afp, &msa); break;
case eslMSAFILE_A2M: ESL_FAIL(eslEFORMAT, afp->errbuf, "A2M format input parser not implemented yet.");
case eslMSAFILE_PSIBLAST: ESL_FAIL(eslEFORMAT, afp->errbuf, "PSIBLAST format input parser not implemented yet.");
case eslMSAFILE_SELEX: status = read_selex (afp, &msa); break;
case eslMSAFILE_AFA: status = read_afa (afp, &msa); break;
case eslMSAFILE_PROFILLIC: status = profillic_read_profile (afp, &msa, profile_ptr); break;
default: ESL_EXCEPTION(eslEINCONCEIVABLE, "no such format");
}
*ret_msa = msa;
return status;
}
/* Function: esl_msa_EncodeFormat()
* Synopsis: Convert text string to an MSA file format code.
* Incept: SRE, Fri Oct 24 13:21:08 2008 [Janelia]
*
* Purpose: Given a text string, match it case-insensitively
* against a list of possible formats, and return the
* appropriate MSA file format code. For example,
* <esl_msa_EncodeFormat("Stockholm")> returns
* <eslMSAFILE_STOCKHOLM>.
*
* If the format is unrecognized, return
* <eslMSAFILE_UNKNOWN>.
*
* Note: Keep in sync with <esl_sqio_EncodeFormat()>,
* which decodes all possible sequence file formats,
* both unaligned and aligned.
*/
int
profillic_esl_msa_EncodeFormat(char *fmtstring)
{
if (strcasecmp(fmtstring, "stockholm") == 0) return eslMSAFILE_STOCKHOLM;
if (strcasecmp(fmtstring, "pfam") == 0) return eslMSAFILE_PFAM;
if (strcasecmp(fmtstring, "a2m") == 0) return eslMSAFILE_A2M;
if (strcasecmp(fmtstring, "psiblast") == 0) return eslMSAFILE_PSIBLAST;
if (strcasecmp(fmtstring, "selex") == 0) return eslMSAFILE_SELEX;
if (strcasecmp(fmtstring, "afa") == 0) return eslMSAFILE_AFA;
if (strcasecmp(fmtstring, "profillic") == 0) return eslMSAFILE_PROFILLIC;
return eslMSAFILE_UNKNOWN;
}
/*****************************************************************
* 7. Stockholm (Pfam/Rfam) format
*****************************************************************/
/* msafile_getline():
* load the next line of <afp> into <afp->buf>.
* Returns eslOK on success, eslEOF on normal eof.
* Throws eslEMEM on alloc failure.
*/
static int
msafile_getline(ESL_MSAFILE *afp)
{
int status;
status = esl_fgets(&(afp->buf), &(afp->buflen), afp->f);
afp->linenumber++;
return status;
}
/* maxwidth()
* Return the length of the longest string in
* an array of strings.
*/
static int64_t
maxwidth(char **s, int n)
{
int64_t max,len;
int i;
max = 0;
for (i = 0; i < n; i++)
if (s[i] != NULL)
{
len = strlen(s[i]);
if (len > max) max = len;
}
return max;
}
static int
is_blankline(char *s)
{
for (; *s != '\0'; s++)
if (! isspace((int) *s)) return FALSE;
return TRUE;
}
/* Format of a GF line:
* #=GF <tag> <text>
* Returns eslOK on success; eslEFORMAT on parse failure.
* Throws eslEMEM on allocation failure.
*/
static int
parse_gf(ESL_MSA *msa, char *buf)
{
char *gf;
char *tag;
char *text;
char *tok;
char *s;
int n;
int status;
s = buf;
if (esl_strtok(&s, " \t\n\r", &gf) != eslOK) return eslEFORMAT;
if (esl_strtok(&s, " \t\n\r", &tag) != eslOK) return eslEFORMAT;
/* text might be empty; watch out for this. (for example, a blank #=GF CC line) */
status = esl_strtok_adv(&s, "\n\r", &text, &n, NULL);
if (status == eslOK) { while (*text && (*text == ' ' || *text == '\t')) text++; }
else if (status == eslEOL){ text = NULL; n = 0; }
else return eslEFORMAT;
if (strcmp(tag, "ID") == 0) status = esl_strdup(text, n, &(msa->name));
else if (strcmp(tag, "AC") == 0) status = esl_strdup(text, n, &(msa->acc));
else if (strcmp(tag, "DE") == 0) status = esl_strdup(text, n, &(msa->desc));
else if (strcmp(tag, "AU") == 0) status = esl_strdup(text, n, &(msa->au));
else if (strcmp(tag, "GA") == 0)
{ /* Pfam has GA1, GA2. Rfam just has GA1. */
s = text;
if ((esl_strtok(&s, " \t\n\r", &tok)) != eslOK)
return eslEFORMAT;
msa->cutoff[eslMSA_GA1] = atof(tok);
msa->cutset[eslMSA_GA1] = TRUE;
if ((esl_strtok(&s, " \t\n\r", &tok)) == eslOK)
{
msa->cutoff[eslMSA_GA2] = atof(tok);
msa->cutset[eslMSA_GA2] = TRUE;
}
status = eslOK;
}
else if (strcmp(tag, "NC") == 0)
{
s = text;
if ((esl_strtok(&s, " \t\n\r", &tok)) != eslOK)
return eslEFORMAT;
msa->cutoff[eslMSA_NC1] = atof(tok);
msa->cutset[eslMSA_NC1] = TRUE;
if ((esl_strtok(&s, " \t\n\r", &tok)) == eslOK)
{
msa->cutoff[eslMSA_NC2] = atof(tok);
msa->cutset[eslMSA_NC2] = TRUE;
}
status = eslOK;
}
else if (strcmp(tag, "TC") == 0)
{
s = text;
if ((esl_strtok(&s, " \t\n\r", &tok)) != eslOK)
return eslEFORMAT;
msa->cutoff[eslMSA_TC1] = atof(tok);
msa->cutset[eslMSA_TC1] = TRUE;
if ((esl_strtok(&s, "\t\n\r", &tok)) == eslOK)
{
msa->cutoff[eslMSA_TC2] = atof(tok);
msa->cutset[eslMSA_TC2] = TRUE;
}
status = eslOK;
}
else /* an unparsed #=GF: */
status = esl_msa_AddGF(msa, tag, text);
return status;
}
/* Format of a GS line:
* #=GS <seqname> <tag> <text>
* Return <eslOK> on success; <eslEFORMAT> on parse error.
* Throws <eslEMEM> on allocation error (trying to grow for a new
* name; <eslEINVAL> if we try to grow an ungrowable MSA.
*/
static int
parse_gs(ESL_MSA *msa, char *buf)
{
char *gs;
char *seqname;
char *tag;
char *text;
int seqidx;
char *s;
int status;
s = buf;
if (esl_strtok(&s, " \t\n\r", &gs) != eslOK) return eslEFORMAT;
if (esl_strtok(&s, " \t\n\r", &seqname) != eslOK) return eslEFORMAT;
if (esl_strtok(&s, " \t\n\r", &tag) != eslOK) return eslEFORMAT;
if (esl_strtok(&s, "\n\r", &text) != eslOK) return eslEFORMAT;
while (*text && (*text == ' ' || *text == '\t')) text++;
/* GS usually follows another GS; guess lastidx+1 */
status = get_seqidx(msa, seqname, msa->lastidx+1, &seqidx);
if (status != eslOK) return status;
msa->lastidx = seqidx;
if (strcmp(tag, "WT") == 0)
{
msa->wgt[seqidx] = atof(text);
msa->flags |= eslMSA_HASWGTS;
status = eslOK;
}
else if (strcmp(tag, "AC") == 0)
status = esl_msa_SetSeqAccession(msa, seqidx, text);
else if (strcmp(tag, "DE") == 0)
status = esl_msa_SetSeqDescription(msa, seqidx, text);
else
status = esl_msa_AddGS(msa, tag, seqidx, text);
return status;
}
/* parse_gc():
* Format of a GC line:
* #=GC <tag> <aligned text>
*/
static int
parse_gc(ESL_MSA *msa, char *buf)
{
char *gc;
char *tag;
char *text;
char *s;
int len;
int status;
s = buf;
if (esl_strtok (&s, " \t\n\r", &gc) != eslOK) return eslEFORMAT;
if (esl_strtok (&s, " \t\n\r", &tag) != eslOK) return eslEFORMAT;
if (esl_strtok_adv(&s, " \t\n\r", &text, &len, NULL) != eslOK) return eslEFORMAT;
if (strcmp(tag, "SS_cons") == 0) status = esl_strcat(&(msa->ss_cons), -1, text, len);
else if (strcmp(tag, "SA_cons") == 0) status = esl_strcat(&(msa->sa_cons), -1, text, len);
else if (strcmp(tag, "PP_cons") == 0) status = esl_strcat(&(msa->pp_cons), -1, text, len);
else if (strcmp(tag, "RF") == 0) status = esl_strcat(&(msa->rf), -1, text, len);
else status = esl_msa_AppendGC(msa, tag, text);
return status;
}
/* parse_gr():
* Format of a GR line:
* #=GR <seqname> <featurename> <text>
*/
static int
parse_gr(ESL_MSA *msa, char *buf)
{
char *gr;
char *seqname;
char *tag;
char *text;
int seqidx;
int len;
int j;
char *s;
int status;
s = buf;
if (esl_strtok (&s, " \t\n\r", &gr) != eslOK) return eslEFORMAT;
if (esl_strtok (&s, " \t\n\r", &seqname) != eslOK) return eslEFORMAT;
if (esl_strtok (&s, " \t\n\r", &tag) != eslOK) return eslEFORMAT;
if (esl_strtok_adv(&s, " \t\n\r", &text, &len, NULL) != eslOK) return eslEFORMAT;
/* GR usually follows sequence it refers to; guess msa->lastidx */
status = get_seqidx(msa, seqname, msa->lastidx, &seqidx);
if (status != eslOK) return status;
msa->lastidx = seqidx;
if (strcmp(tag, "SS") == 0)
{
if (msa->ss == NULL)
{
ESL_ALLOC_CPP(char *, msa->ss, sizeof(char *) * msa->sqalloc);
ESL_ALLOC_CPP(int64_t, msa->sslen, sizeof(int64_t)* msa->sqalloc);
for (j = 0; j < msa->sqalloc; j++)
{
msa->ss[j] = NULL;
msa->sslen[j] = 0;
}
}
status = esl_strcat(&(msa->ss[seqidx]), msa->sslen[seqidx], text, len);
msa->sslen[seqidx] += len;
}
else if (strcmp(tag, "SA") == 0)
{
if (msa->sa == NULL)
{
ESL_ALLOC_CPP(char *, msa->sa, sizeof(char *) * msa->sqalloc);
ESL_ALLOC_CPP(int64_t, msa->salen, sizeof(int64_t)* msa->sqalloc);
for (j = 0; j < msa->sqalloc; j++)
{
msa->sa[j] = NULL;
msa->salen[j] = 0;
}
}
status = esl_strcat(&(msa->sa[seqidx]), msa->salen[seqidx], text, len);
msa->salen[seqidx] += len;
}
else if (strcmp(tag, "PP") == 0)
{
if (msa->pp == NULL)
{
ESL_ALLOC_CPP(char *, msa->pp, sizeof(char *) * msa->sqalloc);
ESL_ALLOC_CPP(int64_t, msa->pplen, sizeof(int64_t)* msa->sqalloc);
for (j = 0; j < msa->sqalloc; j++)
{
msa->pp[j] = NULL;
msa->pplen[j] = 0;
}
}
status = esl_strcat(&(msa->pp[seqidx]), msa->pplen[seqidx], text, len);
msa->pplen[seqidx] += len;
}
else
status = esl_msa_AppendGR(msa, tag, seqidx, text);
return status;
ERROR:
return status;
}
/* parse_comment():
* comments are simply stored verbatim, not parsed
*/
static int
parse_comment(ESL_MSA *msa, char *buf)
{
char *s;
char *comment;
s = buf + 1; /* skip leading '#' */
if (*s == '\n' || *s == '\r') { *s = '\0'; comment = s; } /* deal with blank comment */
else if (esl_strtok(&s, "\n\r", &comment)!= eslOK) return eslEFORMAT;
return (esl_msa_AddComment(msa, comment));
}
/* parse_sequence():
* Format of line is:
* <name> <aligned text>
*
* On digital sequence, returns <eslEINVAL> if any of the residues can't be digitized.
*/
static int
parse_sequence(ESL_MSA *msa, char *buf)
{
char *s;
char *seqname;
char *text;
int seqidx;
int len;
int status;
s = buf;
if (esl_strtok (&s, " \t\n\r", &seqname) != eslOK) return eslEFORMAT;
if (esl_strtok_adv(&s, " \t\n\r", &text, &len, NULL) != eslOK) return eslEFORMAT;
/* seq usually follows another seq; guess msa->lastidx +1 */
status = get_seqidx(msa, seqname, msa->lastidx+1, &seqidx);
if (status != eslOK) return status;
msa->lastidx = seqidx;
#ifdef eslAUGMENT_ALPHABET
if (msa->flags & eslMSA_DIGITAL)
{
status = esl_abc_dsqcat(msa->abc, &(msa->ax[seqidx]), &(msa->sqlen[seqidx]), text, len);
}
#endif
if (! (msa->flags & eslMSA_DIGITAL))
{
status = esl_strcat(&(msa->aseq[seqidx]), msa->sqlen[seqidx], text, len);
msa->sqlen[seqidx] += len;
}
return status;
}
/* read_stockholm():
* SRE, Sun Jan 23 08:33:32 2005 [St. Louis]
*
* Purpose: Parse the next alignment from an open Stockholm format alignment
* file <afp>, leaving the alignment in <ret_msa>.
*
* Returns: <eslOK> on success, and the alignment is in <ret_msa>.
* Returns <eslEOF> if there are no more alignments in <afp>,
* and <ret_msa> is set to NULL.
* <eslEFORMAT> if parse fails because of a file format problem,
* in which case afp->errbuf is set to contain a formatted message
* that indicates the cause of the problem, and <ret_msa> is
* set to NULL.
*
* Returns <eslEINVAL> if we're trying to read a digital alignment,
* and an invalid residue is found that can't be digitized.
*
* Throws: <eslEMEM> on allocation error.
*
* Xref: squid's ReadStockholm(), 1999.
*/
static int
read_stockholm(ESL_MSAFILE *afp, ESL_MSA **ret_msa)
{
ESL_MSA *msa = NULL;
char *s;
int status;
int status2;
#ifdef eslAUGMENT_SSI
off_t offset;
#endif
if (feof(afp->f)) { status = eslEOF; goto ERROR; }
afp->errbuf[0] = '\0';
/* Initialize allocation of the MSA:
* make it growable, by giving it an initial blocksize of
* 16 seqs of 0 length.
*/
#ifdef eslAUGMENT_ALPHABET
if (afp->do_digital == TRUE && (msa = esl_msa_CreateDigital(afp->abc, 16, -1)) == NULL)
{ status = eslEMEM; goto ERROR; }
#endif
if (afp->do_digital == FALSE && (msa = esl_msa_Create(16, -1)) == NULL)
{ status = eslEMEM; goto ERROR; }
if (msa == NULL)
{ status = eslEMEM; goto ERROR; }
/* Check the magic Stockholm header line.
* We have to skip blank lines here, else we perceive
* trailing blank lines in a file as a format error when
* reading in multi-record mode.
*/
do {
#ifdef eslAUGMENT_SSI
offset = ftello(afp->f);
#endif
if ((status = msafile_getline(afp)) != eslOK) goto ERROR; /* includes EOF */
} while (is_blankline(afp->buf));
if (strncmp(afp->buf, "# STOCKHOLM 1.", 14) != 0)
ESL_XFAIL(eslEFORMAT, afp->errbuf, "parse failed (line %d): missing \"# STOCKHOLM\" header", afp->linenumber);
#ifdef eslAUGMENT_SSI
msa->offset = offset;
#endif
/* Read the alignment file one line at a time.
*/
while ((status2 = msafile_getline(afp)) == eslOK)
{
s = afp->buf;
while (*s == ' ' || *s == '\t') s++; /* skip leading whitespace */
if (*s == '#') {
if (strncmp(s, "#=GF", 4) == 0)
{
if ((status = parse_gf(msa, s)) != eslOK)
ESL_XFAIL(status, afp->errbuf, "parse failed (line %d): bad #=GF line", afp->linenumber);
}
else if (strncmp(s, "#=GS", 4) == 0)
{
if ((status = parse_gs(msa, s)) != eslOK)
ESL_XFAIL(status, afp->errbuf, "parse failed (line %d): bad #=GS line", afp->linenumber);
}
else if (strncmp(s, "#=GC", 4) == 0)
{
if ((status = parse_gc(msa, s)) != eslOK)
ESL_XFAIL(status, afp->errbuf, "parse failed (line %d): bad #=GC line", afp->linenumber);
}
else if (strncmp(s, "#=GR", 4) == 0)
{
if ((status = parse_gr(msa, s)) != eslOK)
ESL_XFAIL(status, afp->errbuf, "parse failed (line %d): bad #=GR line", afp->linenumber);
}
else if ((status = parse_comment(msa, s)) != eslOK)
ESL_XFAIL(status, afp->errbuf, "parse failed (line %d): bad comment line", afp->linenumber);
}
else if (strncmp(s, "//", 2) == 0) break; /* normal way out */
else if (*s == '\n' || *s == '\r') continue;
else if ((status = parse_sequence(msa, s)) != eslOK)
ESL_XFAIL(status, afp->errbuf, "parse failed (line %d): bad sequence line", afp->linenumber);
}
/* If we saw a normal // end, we would've successfully read a line,
* so when we get here, status (from the line read) should be eslOK.
*/
if (status2 != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "parse failed (line %d): didn't find // at end of alignment", afp->linenumber);
/* Stockholm fmt is complex, so give the newly parsed MSA a good
* going-over, and finalize the fields of the MSA data structure.
* verify_parse will fill in errbuf if it sees a problem.
*/
if (verify_parse(msa, afp->errbuf) != eslOK) { status = eslEFORMAT; goto ERROR; }
if (ret_msa != NULL) *ret_msa = msa; else esl_msa_Destroy(msa);
return eslOK;
ERROR:
if (msa != NULL) esl_msa_Destroy(msa);
if (ret_msa != NULL) *ret_msa = NULL;
return status;
}
/*****************************************************************
* 10. SELEX format
*****************************************************************/
#define eslMSA_LINE_SQ 1
#define eslMSA_LINE_RF 2
#define eslMSA_LINE_CS 3
#define eslMSA_LINE_SS 4
#define eslMSA_LINE_SA 5
static int read_block(ESL_MSAFILE *afp, char ***line_p, int **llen_p, int **lpos_p, int **rpos_p, int *lalloc_p, int *nlines_p, int *ret_starti);
static int first_selex_block(char *errbuf, int starti, char **line, int *lpos, int *rpos, int nlines, ESL_MSA **ret_msa, int **ret_ltype);
static int other_selex_block(char *errbuf, int starti, char **line, int *lpos, int *rpos, int nlines, ESL_MSA *msa, int *ltype);
static int append_selex_block(ESL_MSA *msa, char **line, int *ltype, int *lpos, int *rpos, int nlines);
/* read_selex()
* Read an alignment in SELEX format.
* SRE, Mon Dec 29 10:19:32 2008 [Pamplona]
*
* Purpose: Parse an alignment from an open SELEX format alignment
* file <afp>, returning the alignment in <ret_msa>.
*
* Returns: <eslOK> on success, and the alignment is in <ret_msa>.
*
* Returns <eslEFORMAT> if parse fails because of a file
* format problem.
* Returns <eslEOF> if no alignment is found in the file.
* Returns <eslEINVAL> if we're trying to read a digital
* alignment, and an invalid residue is found that
* can't be digitized.
*
* On all normal error conditions, <afp->errbuf> contains
* an informative error message for the user, and the
* <*ret_msa> is <NULL>. The error message looks like
* "parse failed (line 156): too many #=SS lines for seq"
* The caller can prefix with filename if it likes.
*
* Throws: <eslEMEM> on allocation error.
*/
static int
read_selex(ESL_MSAFILE *afp, ESL_MSA **ret_msa)
{
ESL_MSA *msa = NULL;
char **line = NULL;
int *ltype = NULL;
int *llen = NULL;
int *lpos = NULL;
int *rpos = NULL;
int lalloc = 0;
int nlines = 0;
int nblocks = 0;
int starti;
int i, apos;
int status;
if (feof(afp->f)) { status = eslEOF; goto ERROR; }
afp->errbuf[0] = '\0';
/* For each alignment block: */
while ( (status = read_block(afp, &line, &llen, &lpos, &rpos, &lalloc, &nlines, &starti)) == eslOK)
{ /* now line[0..nlines-1] are data lines; llen[0..nlines-1] are max line lengths exc. \0 */
/* lpos[0..nlines-1] are 0; and rpos[0..nlines-1] are idx of last nonwhitespace char on lines */
nblocks++;
if (nblocks == 1) status = first_selex_block(afp->errbuf, starti, line, lpos, rpos, nlines, &msa, <ype);
else status = other_selex_block(afp->errbuf, starti, line, lpos, rpos, nlines, msa, ltype);
if (status != eslOK) goto ERROR;
if ((status = append_selex_block(msa, line, ltype, lpos, rpos, nlines)) != eslOK) goto ERROR;
}
if (status != eslEOF || nblocks == 0) goto ERROR;
#ifdef eslAUGMENT_SSI
msa->offset = 0; /* SELEX files are single MSA only; offset is always 0. */
#endif
/* SELEX format allows ' ' as gaps, but easel doesn't */
if (msa->rf != NULL)
for (apos = 0; apos < msa->alen; apos++)
if (msa->rf[apos] == ' ') msa->rf[apos] = '.';
if (msa->ss_cons != NULL)
for (apos = 0; apos < msa->alen; apos++)
if (msa->ss_cons[apos] == ' ') msa->ss_cons[apos] = '.';
if (msa->ss != NULL)
for (i = 0; i < msa->nseq; i++)
if (msa->ss[i] != NULL)
for (apos = 0; apos < msa->alen; apos++)
if (msa->ss[i][apos] == ' ') msa->ss[i][apos] = '.';
if (msa->sa != NULL)
for (i = 0; i < msa->nseq; i++)
if (msa->sa[i] != NULL)
for (apos = 0; apos < msa->alen; apos++)
if (msa->sa[i][apos] == ' ') msa->sa[i][apos] = '.';
for (i = 0; i < msa->nseq; i++)
for (apos = 0; apos < msa->alen; apos++)
if (msa->aseq[i][apos] == ' ') msa->aseq[i][apos] = '.';
#ifdef eslAUGMENT_ALPHABET
if (afp->do_digital) status = esl_msa_Digitize(afp->abc, msa, afp->errbuf);
#endif
*ret_msa = msa;
free(ltype);
return eslOK;
ERROR:
if (msa != NULL) esl_msa_Destroy(msa);
if (ltype != NULL) free(ltype);
return status;
}
/* read_block()
* Read one block of alignment data into memory.
*
* If we're trying to read the *first* block in an alignment, then at start:
* No lines of the alignment file have been read into <afp->buf> yet.
* *line_p, *llen_p, *lpos_p, *rpos_p are all NULL
* *lalloc_p is 0
* *nlines_p is 0
* On success, returns <eslOK> (even if last line is end of file), and:
* afp->buf either contains a blank line (immediately after block end), or <afp> is at EOF.
* *nlines_p points to the number of lines stored
* *line_p points to line[0..nlines-1][] array of \0 terminated strings
* *llen_p points to llen[0..nlines-1] array of string allocations in chars, not including \0
* *lpos_p points to lpos[0..nlines-1] array, all initialized to 0
* *rpos_p points to rpos[0..nlines-1] array, all initialized to idx of last nonwhitespace char on line
* *lalloc_p is >= *nlines
* If file is empty (no data), returns <eslEOF>.
* If an allocation fails at any point, throw <eslEMEM>.
*
* If we are trying to read a *subsequent* block in the file, at start:
* <afp->buf> is where a previous read left it: on a blank line, or <afp> is at EOF.
* *line_p points to previous line[][] array; we'll reuse it, reallocating if needed.
* *llen_p points to previous llen[] array; ditto
* *lpos_p points to lpos[] array: all 0, same as first block
* *rpos_p points to rpos[] array; idx of last nonwhitespace char on line
* *lalloc_p is >0, and is what the previous read_block call reported
* *nlines_p points to the number of lines we saw in the *first* block.
* On success, returns <eslOK> as above.
* If the number of lines seen in the block doesn't match the expected number, return <eslEFORMAT>.
* If no more data remain in the file, return <eslEOF>.
* If an allocation fails at any point, throw <eslEMEM>.
*
* Memory for line[][] and llen[] are entirely managed here - not by caller. They are
* initially allocated on the first block (*lalloc_p == 0); reallocated