1 /****************************************************************\
2 *                                                                *
3 *  fastaexplode : break a fasta file into individual sequences   *
4 *                                                                *
5 *  Guy St.C. Slater..   mailto:guy@ebi.ac.uk                     *
6 *  Copyright (C) 2000-2009.  All Rights Reserved.                *
7 *                                                                *
8 *  This source code is distributed under the terms of the        *
9 *  GNU General Public License, version 3. See the file COPYING   *
10 *  or http://www.gnu.org/licenses/gpl.txt for details            *
11 *                                                                *
12 *  If you use this code, please keep this notice intact.         *
13 *                                                                *
14 \****************************************************************/
15 
16 #include <stdio.h>
17 
18 #include "argument.h"
19 #include "fastadb.h"
20 
fasta_explode_traverse_func(FastaDB_Seq * fdbs,gpointer user_data)21 static gboolean fasta_explode_traverse_func(FastaDB_Seq *fdbs,
22                                             gpointer user_data){
23     register gchar *dir_path = user_data;
24     register gchar *output_path = g_strconcat(dir_path,
25         G_DIR_SEPARATOR_S, fdbs->seq->id, ".fa", NULL);
26     register FILE *fp = fopen(output_path, "r");
27     if(fp){
28         fclose(fp);
29         g_error("File [%s] already exists", output_path);
30         }
31     fp = fopen(output_path, "w");
32     if(!fp)
33         g_error("Could not open [%s] to write output", output_path);
34     FastaDB_Seq_print(fdbs, fp, FastaDB_Mask_ID
35                                |FastaDB_Mask_DEF
36                                |FastaDB_Mask_SEQ);
37     g_free(output_path);
38     fclose(fp);
39     return FALSE;
40     }
41 
Argument_main(Argument * arg)42 int Argument_main(Argument *arg){
43     register FastaDB *fdb;
44     register ArgumentSet *as
45            = ArgumentSet_create("Sequence Input Options");
46     gchar *query_path, *dir_path;
47     ArgumentSet_add_option(as, 'f', "fasta", "path",
48         "Fasta input file", NULL,
49         Argument_parse_string, &query_path);
50     ArgumentSet_add_option(as, 'd', "directory", "path",
51         "Output file directory", ".",
52         Argument_parse_string, &dir_path);
53     Argument_absorb_ArgumentSet(arg, as);
54     Argument_process(arg, "fastaexplode",
55         "Split a fasta file up into individual sequences\n"
56         "Guy St.C. Slater. guy@ebi.ac.uk. 2000-2003.\n", NULL);
57     fdb = FastaDB_open(query_path, NULL);
58     FastaDB_traverse(fdb, FastaDB_Mask_ALL,
59                      fasta_explode_traverse_func, dir_path);
60     FastaDB_close(fdb);
61     return 0;
62     }
63 
64