File: logolMain.c

package info (click to toggle)
logol 1.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,660 kB
  • ctags: 1,144
  • sloc: java: 10,613; perl: 2,651; ansic: 362; xml: 322; ruby: 280; sh: 115; makefile: 35
file content (177 lines) | stat: -rw-r--r-- 5,125 bytes parent folder | download | duplicates (5)
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
#include <stdio.h>
#include <sicstus/sicstus.h>

// This program call the logolMain function defined in logol.pro
// It sends as argument a prolog file to load dynamically then executes entry point of this file (via logolMain).

     int user_main(int argc, char **argv)
     {
       int rval;

       int i=1;
       int maxSolutions=0;
       int offset=0;
       int header=0;
       int solutions=0;
       
       SP_pred_ref pred;
       SP_qid goal;
  
       SP_term_ref ResultFile, File, OutFile, Res, Offset, HeaderSize, LogolFile;

       
       if(argc!=8) {
    	   fprintf(stderr,"Error in arguments. Usage is:\n#myprogram inputfile outputfile maxresults offset inputHeaderSize logolFile logolSavFile\n");
    	   //exit(1);
           return 1;
       }
     
       maxSolutions=atoi(argv[3]);
       
       offset=atoi(argv[4]);
       
       /* Initialize Prolog engine. The third arg to SP_initialize is
          reserved and should always be NULL */
       if (SP_FAILURE == SP_initialize(argc, argv, NULL))
         {
           fprintf(stderr, "SP_initialize failed: %s\n",
                   SP_error_message(SP_errno));
           //exit(1);
           return 1;
         }
       /* take logol.sav from arguments */
       rval = SP_restore(argv[7]);
     
       if (rval == SP_ERROR || rval == SP_FAILURE)
         {
           fprintf(stderr, "Could not restore \"logol.sav\".\n");
           //exit(1);
           return 1;
         }
       
       // Write header of result file.
       
       if (!(pred = SP_predicate("initResultFile",1,"user")))
         {
           fprintf(stderr, "Could not find initResultFile/1.\n");
           //exit(1);
           return 1;
         }
       SP_put_string(ResultFile = SP_new_term_ref(), argv[2]);
       
       SP_query(pred, ResultFile);
       
       SP_put_string(File = SP_new_term_ref(), argv[1]);
       /*
        * Open stream on sequence file 
        */  
       
       //Size of header in bytes
       header=atoi(argv[5]);
       
       SP_put_integer(HeaderSize = SP_new_term_ref(), header);
       
       if (!(pred = SP_predicate("openSequenceStream",2,"user")))
         {
           fprintf(stderr, "Could not find openSequenceStream/2.\n");
           //exit(1);
           return 1;
         }              
       SP_query(pred, File,HeaderSize);       
       
       
  	// Call predicate logolMain
       if (!(pred = SP_predicate("logolMain",5,"user")))
         {
           fprintf(stderr, "Could not find logolMain/5.\n");
           //exit(1);
           return 1;
         }
     
       /* Create the three arguments to connected/4. */
       //SP_put_string(File = SP_new_term_ref(), argv[1]);
       SP_put_string(OutFile = SP_new_term_ref(), argv[2]);
       SP_put_integer(Offset = SP_new_term_ref(), offset);
       SP_put_variable(Res = SP_new_term_ref());
       SP_put_string(LogolFile = SP_new_term_ref(), argv[6]);
     
       /* Open the query. the query looks like:
        *
        * | ?- logolMain('myfastasequencefile','myresultfile',Offset,Res,'mylogolfile').
        */
       if (!(goal = SP_open_query(pred,LogolFile,File,OutFile,Offset,Res)))
         {
           fprintf(stderr, "Failed to open query.\n");
           //exit(1);
           return 1;
         }
     
       /*
        * Loop through all the solutions.
        */
       
        printf("Looking for matches\n ");
        
       
       while (SP_next_solution(goal)==SP_SUCCESS && (i <= (maxSolutions-1) || maxSolutions==-1))
         {
	       i=i+1;
	       solutions++;
               printf(".");
         }
        printf("\nSearch is over, found %d solutions\n",solutions);
     
       SP_close_query(goal);

       /*
        * Close stream on sequence file 
        */
       if (!(pred = SP_predicate("closeSequenceStream",0,"user")))
         {
           fprintf(stderr, "Could not find closeSequenceStream/0.\n");
           //exit(1);
           return 1;
         }              
       SP_query(pred);        
       

       /*
        * If optimization was set, write the matches now
        */
       if (!(pred = SP_predicate("writeFinalMatches",0,"user")))
         {
           fprintf(stderr, "Could not find writeFinalMatches/0.\n");
           //exit(1);
           return 1;
        }

       if (!(goal = SP_open_query(pred)))
         {
           fprintf(stderr, "Failed to open query.\n");
           //exit(1);
           return 1;
         }
         solutions=0;
         while (SP_next_solution(goal)==SP_SUCCESS) {
              solutions++;
          }
          SP_close_query(goal);
         printf("\nOptimization filter: found %d solutions\n",solutions);
    
       
       
       // Write closure of result file.
       
       if (!(pred = SP_predicate("closeResultFile",1,"user")))
         {
           fprintf(stderr, "Could not find closeResultFile/1.\n");
           //exit(1);
           return 1;
         }
       SP_put_string(ResultFile = SP_new_term_ref(), argv[2]);
       
       SP_query(pred, ResultFile);
       
       //exit(0);
       return 0;
     }