1 /*****************************************************************
2 * HMMER - Biological sequence analysis with profile HMMs
3 * Copyright (C) 1992-1999 Washington University School of Medicine
6 * This source code is distributed under the terms of the
7 * GNU General Public License. See the files COPYING and LICENSE
9 *****************************************************************/
12 * SRE, Tue Nov 9 18:47:02 1999 [Saint Louis]
14 * afetch -- a program to extract alignments from the Pfam database
16 * CVS $Id: afetch_main.c,v 1.1.1.1 2005/03/22 08:34:30 cmzmasek Exp $
25 static char banner[] = "afetch - retrieve an alignment from Pfam";
27 static char usage[] = "\
28 Usage: afetch [-options] <alignment database> <name or accession>\n\
29 or: afetch --index <alignment database>\n\
31 Get an alignment from a database.\n\
33 -h : help; print version and usage info\n\
36 static char experts[] = "\
37 --index : construct indices for the database\n\
40 struct opt_s OPTIONS[] = {
41 { "-h", TRUE, sqdARG_NONE },
42 { "--index", FALSE, sqdARG_NONE }
44 #define NOPTIONS (sizeof(OPTIONS) / sizeof(struct opt_s))
47 main(int argc, char **argv)
49 char *afile; /* name of alignment file to read */
50 MSAFILE *afp; /* pointer to open index file */
51 char *key; /* name/accession of alignment to fetch */
52 MSA *msa; /* the fetched alignment */
53 int format; /* format of afile */
54 int do_index; /* TRUE to index instead of retrieve */
60 /***********************************************
61 * Parse the command line
62 ***********************************************/
64 /* initializations and defaults */
65 format = MSAFILE_STOCKHOLM; /* period. It's the only multi-MSA file format. */
69 while (Getopt(argc, argv, OPTIONS, NOPTIONS, usage,
70 &optind, &optname, &optarg))
72 if (strcmp(optname, "--index") == 0) { do_index = TRUE; }
73 else if (strcmp(optname, "-h") == 0) {
74 Banner(stdout, banner);
81 if ((do_index && argc - optind != 1) || (! do_index && argc - optind != 2))
82 Die("Incorrect number of command line arguments.\n%s\n", usage);
84 afile = argv[optind++];
85 if (! do_index) key = argv[optind++];
87 if ((afp = MSAFileOpen(afile, format, NULL)) == NULL)
88 Die("Alignment file %s could not be opened for reading", afile);
90 /***********************************************
91 * Section 1. Alignment database indexing
92 ***********************************************/
103 /* Not that we're expecting an alignment file so
104 * large that it would require a 64-bit index, but...
106 if ((mode = SSIRecommendMode(afile)) == -1)
107 Die("File %s doesn't exist, or is too large for your OS", afile);
109 ssifile = sre_strdup(afile, -1);
110 sre_strcat(&ssifile, -1, ".ssi", -1);
112 if ((si = SSICreateIndex(mode)) == NULL)
113 Die("Couldn't allocate/initialize the new SSI index");
114 if (SSIAddFileToIndex(si, afile, afp->format, &fh) != 0)
115 Die("SSIAddFileToIndex() failed");
117 status = SSIGetFilePosition(afp->f, mode, &offset);
118 if (status != 0) Die("SSIGetFilePosition() failed");
120 while ((msa = MSAFileRead(afp)) != NULL)
122 if (msa->name == NULL)
123 Die("SSI index requires that every MSA has a name");
125 status = SSIAddPrimaryKeyToIndex(si, msa->name, fh, &offset, NULL, 0);
126 if (status != 0) Die("SSIAddPrimaryKeyToIndex() failed");
128 if (msa->acc != NULL) {
129 status = SSIAddSecondaryKeyToIndex(si, msa->acc, msa->name);
130 if (status != 0) Die("SSIAddSecondaryKeyToIndex() failed");
133 status = SSIGetFilePosition(afp->f, mode, &offset);
134 if (status != 0) Die("SSIGetFilePosition() failed");
140 status = SSIWriteIndex(ssifile, si);
141 if (status != 0) Die("SSIWriteIndex() failed");
143 printf ("%d alignments indexed in SSI index %s\n", n, ssifile);
148 exit (0); /* exit indexing program here */
151 /***********************************************
152 * Section 2. Alignment retrieval
153 ***********************************************/
155 /* Indexed retrieval:
157 if (afp->ssi != NULL) {
158 if (! MSAFilePositionByKey(afp, key))
159 Die("No such alignment %s found in file %s", key, afile);
160 msa = MSAFileRead(afp);
162 /* Brute force retrieval:
165 while ((msa = MSAFileRead(afp)) != NULL)
167 if (strcmp(msa->name, key) == 0) break;
168 if (strcmp(msa->acc, key) == 0) break;
173 if (msa == NULL) Die("Failed to retrieve %s from file %s", key, afile);
175 /* Output the alignment we retrieved
177 WriteStockholm(stdout, msa);