align.cpp 2.28 KB
Newer Older
Mikaël Salson's avatar
Mikaël Salson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <stdlib.h>
#include "../core/dynprog.h"
#include "../core/fasta.h"
#include "../core/lazy_msa.h"

using namespace std;


int main(int argc, char* argv[])
{
  
  const char* fdata;
  ostringstream ost; 
  ostream * p; 
  p=&ost;
  char filename[L_tmpnam];
  tmpnam(filename);
23

Mikaël Salson's avatar
Mikaël Salson committed
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
  bool cgi_mode;
  
    if (argc <= 1){
      cgi_mode=true;
      
      cout <<"Content-type: text/html"<<endl<<endl;
      cout<< "{"<<endl;
      
      char * requestMethod = getenv("REQUEST_METHOD");
      cout<<"\"requestMethod\" : \""<<requestMethod<<"\""<<endl;
      if(!requestMethod) {
	cout<<",\"Error\": \"requestMethod\"}"<<endl;
	return 0;
      }
      if(strcmp(requestMethod, "POST") != 0) {
	cout<<",\"Error\": \"requestMethod =/= post\"}"<<endl;
	return 0;
      }
      char * contentType = getenv("CONTENT_TYPE");
      if(!contentType) {
	cout<<",\"Error\": \"content_type\"}"<<endl;
	return 0;
      }
	cout<<",\"contentType\": \""<<contentType<<"\""<<endl;

      char temp[1024];
      FILE *f;
      f = fopen(filename,"w");
      if(f == NULL){
	cout<<",\"Error\": \"save\""<<filename<<"}"<<endl;

	return 0;
      }else{
	while(cin) {
	  cin.getline(temp, 1024);
	  fputs(temp, f);
	  fputs("\n", f);
	}
      }
      
      fclose(f);
      fdata = filename;
      
    }else{
      cgi_mode=false;
      fdata = argv[1];
      p=&cout;
    }
    
    if (!cgi_mode) cout <<ost<<endl;
    Fasta fa(fdata, 1, " ", *p);
    
    
    string seq0 = fa.sequence(0);
    
79
    LazyMsa lm = LazyMsa(fa.size(), seq0);
Mikaël Salson's avatar
Mikaël Salson committed
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
    
    for (int i=1; i < fa.size(); i++){
      string seq1 = fa.sequence(i);
      lm.add(seq1);
    }
    
    string *result;
    result =new string[lm.sizeUsed+2];
    lm.align(result);
    
    if (cgi_mode){
      cout<<",\"seq\" : [\""<<result[0]<<"\""<<endl;
      for (int i=1; i<lm.sizeUsed+2; i++){
	cout<<",\""<<result[i]<<"\""<<endl;
      }
      cout<<"]}";
      
      remove(filename);
      
    }else{
      int length=60;
      int n=result[0].size();
      int j=0;
      
      while (n > 0){
	for (int i=0; i<lm.sizeUsed+2; i++){
	  
	  cout<<" >  "<<result[i].substr(j*length,length)<<"  <  "<<fa.label(i)<<endl;
	}
	cout<<endl;
	n=n-length;
	j++;
      }
      
    }
      
}