forked from WieheLab/ARMADiLLO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathARMADiLLO_main.cpp
2349 lines (2138 loc) · 83.6 KB
/
ARMADiLLO_main.cpp
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
#include <math.h>
#include <cstdlib>
#include <random>
#include <iostream>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <boost/algorithm/string.hpp>
//#include <algorithm>
#include <boost/filesystem/operations.hpp>
//#include "boost/filesystem.hpp"
#include <sys/types.h>
//#include <dirent.h>
#include <thread>
#include <mutex>
//#include <boost/filesystem/fstream.hpp>
//#define BOOST_FILESYSTEM_NO_DEPRECATED
//#include <boost/optional.hpp>
// include headers that implement a archive in simple text format
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/map.hpp>
#include "ARMADiLLO_main.hpp"
#include "HTML.hpp"
#include "utilities.hpp"
#include "nab.hpp"
#include "readInputFiles.hpp"
using namespace std;
using namespace boost;
using namespace boost::filesystem;
///GLOBALS
int stop_codon_count;
///OLD TODO:
//0. The gap bases should have n/a for mutability score in HTML
//1. Use the markup string to highlight CDRs in the HTML
//2. Make sure last cell in ladder is 1 always
void helpMenu()
{
cout << "ARMADiLLO\n\n";
cout << "USAGE:\n\t ARMADiLLO [seq file options] -m [S5F mutability file] -s [S5F substitution file] <opt arguments>\n\n";
cout << "required arguments:\n";
cout << "\t -m [S5F mutability file]\n";
cout << "\t -s [S5F substitution file]\n";
cout << "Sequence Files options - either SMUA, partis or seq argument required:\n";
cout << "\t -SMUA [SMUA file] : argument for SMUA file from Cloanalyst\n";
cout << "\t -partis [partis file] : file from partis either yaml or cvs\n";
cout << "\t -seq [seq fasta file] : fasta file containing sequences to process requires a uca file\n";
cout << "\t -uca [uca fasta file] : UCA fasta can contain either 1 seq or matching sequences to the seq file\n";
cout << "\t -markup [markup fasta file] : optional fasta for seq and uca sequence files\n";
cout << "output arguments\n";
//cout << "\t -no_text\n";
cout << "\t -simple_text : flag to print out simple text files\n";
cout << "\t -text : flag to print out all text files\n";
cout << "\t -HTML : (default) flag to print out HTML files\n";
cout << "\t -fulloutput : flag to print out all text and HTML files\n";
cout << "\t -annotate : flag to print out annotation of the sequences\n";
cout << "optional arguments:\n";
//cout << "\t -(d)ir : sets the output directory of the files\n";
cout << "\t -freq_dir [V, J Frequency file directory] : directory to pull the frequency tables for quick analysis\n";
cout << "\t -amofile [amo file] : sets the amo file to use for the quick analysis\n";
cout << "\t -resetamo : flag to reset the amo file associated\n";
cout <<"\t -w [line wrap length (60)]\n\t -max_iter [cycles of B cell maturation(1000)]\n\t -c [cutoff for highlighting low prob (1=1%)]\n\t -replace_J_upto [number of replacements in J allowed]\n\t -chain [chain type (heavy=default|kappa|lambda)]\n\t -species [(human=default|rhesus)]\n\t -(l)ineage [number of trees] : argument to generate the mutations through a lineage generation instead of linear generation\n";
cout <<"\t -p [percent mutation] : argument to set percent of mutations to generate instead of taking from mutant sequence\n";
cout <<"\t -(n)umber [number of mutations] : argument to set number of mutations to generate instead of taking from mutant sequence\n";
cout <<"\t -clean_first : flag to turn on cleaning the SMUA prior to running\n\t -output_seqs : flag to turn on printing out simulated seq]\n";
cout <<"\t -ignore_CDR3 : flag to ignore CDR3, default is false\n";
cout <<"\t -ignore_V : flag to ignore V, default is false\n";
cout <<"\t -ignore_J : flag to ignore J, default is false\n";
cout <<"\t -igoreStopCodon : set flag to ignore stop codons during generation\n";
cout << "\t -threads [number] : sets the number of threads to use during processing - default is number of processors\n";
cout <<"\t -random_seed [provide a random seed]\n";
exit(1);
return;
}
int main(int argc, char *argv[])
{
if (argc <2){
helpMenu();
}
///get cmdline args
int i=0, mutation_count_from_cmdline=-1, random_seed=0;
string fasta_filename="", mutability_filename="", substitution_filename="", SMUA_filename="";
string treefile="";
int SMUA_start=0, SMUA_end=-1;
string freq_dir="";
// bool ignore_CDR3=false,ignoreV=false,ignoreJ=false;
bool resetARMOfile=false;
bool user_provided_random_seed=false;
string amoFile="";
string UCAtype="cloanalyst";
string ucaname="";
string markupFile="";
string annotationFile;
int num_threads=0, max_num_threads=thread::hardware_concurrency();
bool estimate=false;
bool reverse=false;
string htmlStackName="";
map<string,vector<string>> annotation;
Arguments arguments;
while(i<argc)
{
string arg=argv[i];
string next_arg;
if (arg=="-h" or arg=="-help")
{
helpMenu();
}
if (i<argc-1){next_arg=argv[i+1];}else{next_arg="";}
// if ((arg.substr(0,1)=="-")&&(next_arg.substr(0,1)=="-")){cerr << "incorrectly formatted cmdline\n"; exit(1);}
if (arg == "-SMUA")
{
UCAtype="cloanalyst";
SMUA_filename=next_arg;
}
if (arg=="-partis")
{
UCAtype="partis";
SMUA_filename=next_arg;
}
if(arg=="-seq" or arg=="-seqs")
{
UCAtype="individual";
SMUA_filename=next_arg;
}
if(arg=="-uca")
{
ucaname=next_arg;
}
if(arg=="-markup")
{
markupFile=next_arg;
}
if (arg == "-m")
{
mutability_filename=next_arg;
}
if (arg == "-s")
{
substitution_filename=next_arg;
}
if (arg == "-freq_dir")
{
freq_dir=next_arg;
arguments.quick=true;
char sep = '/';
#ifdef _WIN32
sep = '\\';
#endif
amoFile=freq_dir+sep+"Freq_Table.amo";
}
if(arg=="-amofile" or arg=="-amo_file")
{
amoFile=next_arg;
arguments.quick=true;
if(!fexists(amoFile))
{
map<string,map<int, map<char,double> >> v_input;
ofstream outfs(amoFile);
boost::archive::binary_oarchive outa(outfs);
outa << v_input;
//cout << amoFile<< " can not be found\n";
//exit(1);
}
}
if (arg == "-w")
{
arguments.line_wrap_length=atoi(next_arg.c_str());
}
if (arg == "-max_iter")
{
arguments.max_iter=atoi(next_arg.c_str());
}
if (arg == "-number" or arg=="-n")
{
arguments.numbMutations=atoi(next_arg.c_str());
}
if (arg == "-p")
{
arguments.percent=atof(next_arg.c_str());
}
if (arg == "-dir" or arg=="-d")
{
arguments.outDirectory=next_arg.c_str();
}
if (arg == "-lineage" or arg == "-l")
{
arguments.lineage=true;
arguments.branches=atoi(next_arg.c_str());
}
if (arg == "-mut_count")
{
mutation_count_from_cmdline=atoi(next_arg.c_str());
//where did this argument come from?
}
if(arg == "-no_text" || arg=="-notext")
{
cout << "no output!"<<endl;
getchar();
arguments.outputMode="none";
}
if(arg == "-simple_text" || arg=="-simpletext")
{
arguments.outputMode="simple";
}
if(arg == "-text" || arg=="-fulltext")
{
arguments.outputMode="fulltext";
}
if(arg== "-HTML" || arg=="-html" || arg=="-web")
{
arguments.outputMode="HTML";
}
if(arg== "-fulloutput" || arg=="-alloutput")
{
arguments.outputMode="all";
}
if(arg=="-annotate")
{
cout << "turning annotation on"<<endl;
arguments.annotateFlag=true;
}
if (arg == "-c")
{
arguments.low_prob_cutoff=atof(next_arg.c_str())/100.0;
}
if (arg == "-ignore_CDR3" || arg =="-ignoreCDR3" || arg =="-ignorecdr3")
{
arguments.ignore_CDR3=true;
}
if(arg == "-ignoreJ" || arg=="-ignore_J" || arg=="-ignore_j"|| arg=="-ignorej")
{
arguments.ignoreJ=true;
}
if(arg == "-ignoreV" || arg =="-ignore_V"|| arg=="-ignore_v"|| arg=="-ignorev")
{
arguments.ignoreV=true;
}
if (arg == "-start")
{
SMUA_start=atoi(next_arg.c_str());
}
if (arg == "-end")
{
SMUA_end=atoi(next_arg.c_str());
}
if (arg == "-replace_J_upto")
{
arguments.replace_J_upto=atoi(next_arg.c_str());
}
if (arg == "-species")
{
arguments.species=next_arg;
}
if (arg == "-chain")
{
arguments.chain_type=next_arg;
}
if (arg == "-clean_first")
{
arguments.clean_SMUA_first=true;
}
if (arg == "-random_seed" && arg == "-seed")
{
random_seed=atoi(next_arg.c_str());
user_provided_random_seed=true;
}
if(arg == "-threads" || arg == "-num_threads")
{
num_threads=atoi(next_arg.c_str());
}
if (arg == "-remutate")
{
bool remutate=true;
//cerr << "REMUTATE HAS NOT YET BEEN IMPLEMENTED. FEATURE COMING SOON!\n";
}
if (arg == "-output_seqs")
{
arguments.output_seqs=true;
}
if (arg == "-UCA_sequence")
{
arguments.input_UCA_sequence=next_arg;
}
if (arg == "-ignore_warnings")
{
arguments.ignore_warnings=true;
}
if (arg=="-igoreStopCodon")
{
arguments.stopcodon=false;
}
if(arg == "-resetamo" or arg == "-reset_amo" or arg == "-reload_amo")
{
cout << "reseting AMO file\n";
resetARMOfile=true;
}
if(arg=="-findpairs")
{
arguments.aaMuts=next_arg;
}
if(arg=="-rank")
{
arguments.rank=true;
}
if(arg=="-reverse")
{
reverse=true;
}
if(arg=="-stack")
{
htmlStackName=next_arg;
}
i++;
}
if( SMUA_filename.size()<1 || !fexists(SMUA_filename))
{
cout << "Error in sequence file\n\n";
helpMenu();
}
if(mutability_filename.size()<1 || !fexists(mutability_filename))
{
cout << "Error in mutability file\n\n";
helpMenu();
}
if(substitution_filename.size()<1 || !fexists(substitution_filename))
{
cout << "Error in substitution file\n\n";
helpMenu();
}
if ((num_threads==0)||(num_threads>max_num_threads)){num_threads=max_num_threads;}
///setup random num generation
std::random_device rd;
int seed;
if (user_provided_random_seed)
{seed=random_seed;}
else
{seed=rd();}
std::mt19937 gen(seed);
arguments.gen=gen;
std::uniform_real_distribution<double> dis(0, 1);
arguments.dis=dis;
//define color ladder
//vector<double> color_ladder{0.0001, 0.001, 0.01, 0.02, 0.10, 0.20, 0.5, 1};
///load dna_to_aa map
map<string,string> dna_to_aa_map;
get_aa_tranx_map(dna_to_aa_map);
if(!fexists("AMA.css") && (arguments.outputMode=="HTML" || arguments.outputMode=="all"))
writeAMA();
if(!fexists("sequence_color.css") && (arguments.outputMode=="HTML" || arguments.outputMode=="all"))
writeColor();
if(!fexists("freq_sequence_color.css") && (arguments.outputMode=="HTML" || arguments.outputMode=="all"))
writeFreqColor();
///read input sequence alignment
map <string, string> sequences;
vector <string> sequence_names;
vector<vector<string> > SMUA_alignments_and_markup;
if (UCAtype.compare("cloanalyst")==0)
{
read_SMUA_file(SMUA_filename, SMUA_alignments_and_markup);
}
else if (UCAtype.compare("partis")==0)
{
int pos =SMUA_filename.find_last_of(".");
string ext =SMUA_filename.substr(pos+1);
if(string("csv").compare(ext)==0)
{
read_PARTIScsv_file(SMUA_filename,SMUA_alignments_and_markup);
}
else if(string("yaml").compare(ext)==0)
{
read_PARTISyaml_file(SMUA_filename,SMUA_alignments_and_markup);
}
else
{
cerr << "Unidentified partis file type.\n ARMADiLLO supports yaml or csv file types from partis"<<endl;
exit(1);
}
}
else if(UCAtype.compare("individual")==0)
{
if( ucaname.size()<1 || !fexists(ucaname))
{
cout << "Error in uca file\n\n";
helpMenu();
}
cout << "mark up: "<<markupFile<<endl;
if(markupFile.size()<1 || !fexists(markupFile))
readIndividualFiles(SMUA_filename,ucaname,SMUA_alignments_and_markup);
else
readIndividualFiles(SMUA_filename,ucaname,markupFile,SMUA_alignments_and_markup);
}
else
{
cout << "unknown UCA collection sequence"<<endl;
cout << "exiting"<<endl;
exit(1);
return 0;
}
if(reverse)//records name either reverse or forward
{
for(int j=SMUA_alignments_and_markup.size()-1; j>-1; j--)
{
sequence_names.push_back(SMUA_alignments_and_markup[j][0]);
}
}
else
{
for(int j=0; j<SMUA_alignments_and_markup.size(); j++)
{
sequence_names.push_back(SMUA_alignments_and_markup[j][0]);
}
}
cerr << "highlighting residues with less than " << arguments.low_prob_cutoff << " probability for mutation\n";
///amino acids vector
vector<char> amino_acids={'A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R','S','T','V','W','Y'};
///load S5F files
map <string, S5F_mut> S5F_5mers;
load_S5F_files(mutability_filename,substitution_filename, S5F_5mers);
const std::string freq_directory=freq_dir;
map<string,map<int, map<char,double> >> v_input;
if (arguments.quick==true)
{
arguments.ignore_CDR3=true;
if(fexists(amoFile) && !resetARMOfile)
{
clock_t begin=clock();
std::ifstream infs(amoFile,std::ios::binary);
boost::archive::binary_iarchive ina(infs);
ina >> v_input;
clock_t end=clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
}
else
{
std::mutex mtx;
mtx.lock();
read_V(freq_directory, v_input);
mtx.unlock();
}
}
cout << "NAME\t#AA_MUTS\t#MUTS\t<.02\t<.01\t<.001\t<.0001\t#INS\t#DEL\t#INDELS/3\tCDR3_LEN\tsum(log(P))\n";
//map<string, map<string, string> > J_genes=J_genes_list();
//iterate through the SMUA file and perform mutation analysis for each sequence
//double total_elapsed_time=0;
if (SMUA_end==-1)
{
SMUA_end=SMUA_alignments_and_markup.size();
}
if (SMUA_end>SMUA_alignments_and_markup.size())
{
SMUA_end=SMUA_alignments_and_markup.size();
}
//for(int i=0; i<SMUA_alignments_and_markup.size(); i++)
int MAX_THREADS=num_threads;
int size=SMUA_end;
if(arguments.annotateFlag)
{
for(int i=SMUA_start;i<SMUA_end;i++)
{
vector<string> tmp;
annotation[SMUA_alignments_and_markup[i][0]]=tmp;
}
}
map<string, vector<Seq> > seq_map;
for(int i=SMUA_start;i<SMUA_end;i+=MAX_THREADS)
{
int number_of_threads=min(MAX_THREADS,size-i);
vector<thread> thread_list;
for(int j=0;j<number_of_threads;j++)
{
thread_list.push_back(thread(run_entry,std::ref(S5F_5mers),std::ref(dna_to_aa_map),SMUA_alignments_and_markup[i+j],std::ref(v_input),std::ref(arguments),std::ref(annotation),std::ref(seq_map)));
}
for(int j=0;j<number_of_threads;j++)
{
thread_list[j].join();
}
//getchar();
}
if (arguments.quick==true)
{
ofstream outfs(amoFile);
boost::archive::binary_oarchive outa(outfs);
outa << v_input;
}
if(arguments.annotateFlag)
{
int pos =SMUA_filename.find_last_of(".");
annotationFile=SMUA_filename.substr(0,pos)+".annotation.txt";
ofstream file_out;
file_out.open(annotationFile.c_str());
for(int i=SMUA_start;i<SMUA_end;i++)
{
file_out << SMUA_alignments_and_markup[i][0]<<";";
for(int j=0;j<annotation[SMUA_alignments_and_markup[i][0]].size();j++)
{
if(j==0)
file_out << annotation[SMUA_alignments_and_markup[i][0]][j];
else
file_out << ","<<annotation[SMUA_alignments_and_markup[i][0]][j];
}
file_out << endl;
}
}
//sequence color code - need to build the inputs
//HTML::Table html_table;
//html_table.hclass="results";
if(htmlStackName.size()>1)//test if html stack name is given
{
int len=htmlStackName.size();//if name doesn't end in html, add it
if(len > 3 && htmlStackName.substr(len - 4)!="html")
{
htmlStackName=htmlStackName+".html";
}
//pass to function to take the seq_map and create the html
printTileStack(htmlStackName, seq_map, sequence_names, arguments.line_wrap_length, arguments.low_prob_cutoff, arguments.color_ladder);
}
//cerr << "TOTAL ELAPSED TIME: " << total_elapsed_time << "\n";
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// FUNCTION DEFINITIONS
///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void run_entry(map<string,S5F_mut> &S5F_5mers,map<string,string> &dna_to_aa_map, vector<string> SMUA_entries, map<string,map<int, map<char,double> >> &v_input, Arguments &arg, map<string,vector<string>> &annotation,map<string,vector<Seq>> &seq_map)
{
//double total_elapsed_time=0;
//clock_t begin=clock();
string seq=SMUA_entries[1];
for (auto & c: seq) c = toupper(c);
int Acount = countChar(seq,'A');
int Gcount = countChar(seq,'G');
int Ccount = countChar(seq,'C');
int Tcount = countChar(seq,'T');
int dashcount = countChar(seq,'-');
if(seq.size()>Acount+Gcount+Ccount+Tcount+dashcount)
{
cerr << "Unidentified nucleotide in sequence: "<<SMUA_entries[0]<<endl;
cerr <<"ARMADiLLO cannot accept amino acid sequences or nucleotide sequences with ambuguity codes"<<endl;
cerr << "Skipping sequence: "<<SMUA_entries[0]<<endl;
writeError(SMUA_entries[0]+".tiles.html",SMUA_entries[0]);
writeError(SMUA_entries[0]+".ARMADiLLO.html",SMUA_entries[0]);
return;
}
NabEntry nab(SMUA_entries,arg);
nab.rank=arg.rank;
if(arg.clean_SMUA_first)
{
if (nab.cleanSMUA(arg.species,arg.chain_type,arg.replace_J_upto)<0)
return;
vector<bool> _shield_mutations(nab.markup_string.length(),false);
nab.shield_mutations=_shield_mutations;
}
if(arg.input_UCA_sequence!="")
{
nab.replaceUCA(dna_to_aa_map,arg.input_UCA_sequence,arg.ignore_warnings);
nab.markup_mask=nab.parseMarkup();
}
//cout << arg.ignoreV<<"\t"<<arg.ignore_CDR3<<"\t"<<arg.ignoreJ<<endl;
nab.createShield(arg.ignore_CDR3,arg.ignoreV,arg.ignoreJ);
if(arg.quick)
{
//nab.Jgene_sequence=J_genes_list(arg.species,nab.Jgene);
nab.replaceTable(S5F_5mers,dna_to_aa_map, v_input,arg);
if(arg.output_seqs)//write out simulated sequences if argument is true
cerr << "No sequences generated during quick generations\n";
}
else
{
bool goodSim=nab.SimulateSequences(S5F_5mers,dna_to_aa_map,arg.gen,arg.dis,arg.max_iter,arg.branches,arg.lineage);
if(!goodSim)
{
nab.printlog();
return;
}
if(arg.output_seqs)//write out simulated sequences if argument is true
{
nab.outputSimSeqs(arg.max_iter,arg.branches);
}
}
if(arg.aaMuts!="")
{
nab.countAAPairs(arg.aaMuts);
}
vector<string> SNPs;
if(arg.rank)
SNPs=nab.printResults(S5F_5mers,dna_to_aa_map,arg.line_wrap_length,arg.low_prob_cutoff,arg.color_rank_ladder);
else
SNPs=nab.printResults(S5F_5mers,dna_to_aa_map,arg.line_wrap_length,arg.low_prob_cutoff,arg.color_ladder);
if(arg.annotateFlag)
{
annotation[nab.sequence_name]=SNPs;
}
nab.printlog();
seq_map[nab.sequence_name]=nab.aa_out;//creates the aa_out map to pass to the tile out function
//clock_t end=clock();
//double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
//cerr << "TIME: " << nab.sequence_name << " took " << elapsed_secs << " to process\n";
//total_elapsed_time+=elapsed_secs;
return;
}
void print_output_for_tiles_view(string filename, vector<vector<Seq> > &all_sequences, vector<string> sequence_names, int line_wrap_length, double low_prob_cutoff, vector<double> &color_ladder,bool rank)
{
vector<vector<vector<Seq> > > split_all_sequences;
vector2D_to_3D(all_sequences,line_wrap_length, split_all_sequences);
///output html header
string file_string="";
file_string+="<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>\n";
file_string+="<head>\n";
file_string+=" <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />\n";
file_string+=" <title>Antibody Mutation Analysis</title>\n";
file_string+=" <link rel='stylesheet' href='sequence_color.css' />\n";
file_string+="</head>\n";
file_string+="<body>\n";
file_string+="<p></p><br>\n";
//cerr << "number of splits: " << split_all_sequences.size() << "\n";
//for each split, make a HTML table and print it
int counter=1;
for(int i=0; i<split_all_sequences.size(); i++)
{
HTML::Table html_table;
html_table.hclass="results";
//convert seq vector to html table
convert_2D_seq_vector_to_HTML_table_for_tiles_view(split_all_sequences[i],sequence_names,html_table, low_prob_cutoff, color_ladder, counter,rank);
//missing step: stylize the table
//print HTML tables
html_table.print(file_string);
file_string+="<p></p>\n";
}
file_string+="<br><br><br>\n";
file_string+="<p><table class=\"results\" align=center><tr><td class=\"noborder\"><font align=center size=\"4\"><b>Mutation Probability: </b></font></td>";
file_string+="<td class=\"color_cat7 mut\"><div class=\"mm\"> </div></td><td class=\"noborder\">≥20\% </td>";
file_string+="<td class=\"color_cat6 mut\"><div class=\"mm\"> </div></td><td class=\"noborder\">20-10\% </td>";
file_string+="<td class=\"color_cat5 mut\"><div class=\"mm\"> </div></td><td class=\"noborder\">10-2\% </td>";
file_string+="<td class=\"color_cat4 mut\"><div class=\"mm\"> </div></td><td class=\"noborder\">2-1\% </td>";
file_string+="<td class=\"color_cat3 mut\"><div class=\"mm\"> </div></td><td class=\"noborder\">1.0-0.1\% </td>";
file_string+="<td class=\"color_cat2 mut\"><div class=\"mm\"> </div></td><td class=\"noborder\">0.10-0.01\% </td> ";
file_string+="<td class=\"color_cat1 mut\"><div class=\"mm\"> </div></td><td class=\"noborder\"><0.01\%</td></tr></table></p>\n";
//file_string+="<p><br></p><p align=\"center\"><img src=\"Mutation_Probability_legend.png\" alt=\"Mutation Probability Legend\" height=\"25\"></p>\n";
if(rank)
file_string+="<p><table class=\"results\" align=center><tr><td class=\"noborder\"><font align=center size=\"4\"><b>Probability Rank-legend Broke: </b></font></td>";
else
file_string+="<p><table class=\"results\" align=center><tr><td class=\"noborder\"><font align=center size=\"4\"><b>Mutation Probability: </b></font></td>";
/*for(int i=6;i>-1;i--)
{
if(i==6)
file_string+="<td class=\"color_cat7 mut\"><div class=\"mm\"> </div></td><td class=\"noborder\">≥"+formatDouble(color_ladder[i-1]*100)+"\% </td>";
else if(i==0)
file_string+="<td class=\"color_cat"+to_string(i+1)+ " mut\"><div class=\"mm\"> </div></td><td class=\"noborder\"><"+formatDouble(color_ladder[i]*100) +"\%</td>";
else
file_string+="<td class=\"color_cat"+to_string(i+1)+" mut\"><div class=\"mm\"> </div></td><td class=\"noborder\">"+formatDouble(color_ladder[i]*100)+"-"+formatDouble(color_ladder[i-1]*100)+"\% </td> ";
}*/
file_string+="</tr></table></p>\n";
file_string+="</body>\n</html>\n";
ofstream file_out;
file_out.open(filename.c_str());
file_out << file_string;
file_out.close();
}
void print_freq_table_to_file(string filename, map<int, map<char,double> > &positional_aa_freqs)
{
ofstream file_out;
file_out.open(filename.c_str());
///amino acids vector
vector<char> amino_acids={'A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R','S','T','V','W','Y'};
file_out << "pos";
for(int i=0; i<amino_acids.size(); i++){file_out << "," << amino_acids[i];}
file_out << "\n";
for(int j=0; j<positional_aa_freqs.size(); j++)
{
file_out << j+1;
for(int i=0; i<amino_acids.size(); i++)
{
file_out << "," <<positional_aa_freqs[j][amino_acids[i]];
}
file_out << "\n";
}
file_out.close();
/* vector<string> mutation_list;
vector<float> values_list;
for(int j=0; j<positional_aa_freqs.size(); j++)
{
string mut;
for(int i=0; i<amino_acids.size(); i++)
{
mut=to_string(positional_aa_freqs[j][amino_acids[i]])+"_"+amino_acids[i]+to_string(j);
mutation_list.push_back(mut);
values_list.push_back(positional_aa_freqs[j][amino_acids[i]]);
}
}
sort(mutation_list.begin(),mutation_list.end());
for(int i=0; i<mutation_list.size();i++)
{
//cout << mutation_list[i]<<endl;
}
sort(values_list.begin(),values_list.end());
vector<float>::iterator ip;
ip=std::unique(values_list.begin(),values_list.begin()+values_list.size());
values_list.resize(std::distance(values_list.begin(),ip));
for(int i=0;i<values_list.size();i++)
cout << values_list[i]<<endl;
*/
}
void print_HTML_freq_table_to_file(string filename, map<int, map<char,double> > &positional_aa_freqs,string aa_sequence,vector<double> &color_ladder)
{
vector<char> amino_acids={'A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R','S','T','V','W','Y'};
string file_string="";
file_string+="<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>\n";
file_string+="<head>\n";
file_string+=" <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />\n";
file_string+=" <title>Antibody Mutation Analysis</title>\n";
file_string+=" <link rel='stylesheet' href='freq_sequence_color.css' />\n";
//file_string+=" <link rel='stylesheet' href='AMA.css' />\n";
file_string+="<style>\n";
file_string+=".tooltip{\n position: relative;\ndisplay: inline-block;\n}";
file_string+=".tooltiptext {\n visibility: hidden;\n width: 100px;\n background-color: #fffff5;\n color: #000000;\n font-size: 12px;\n font-family:'Monospace';\n text-align: center;\n padding: 5px 0;\n position: absolute;\n top: 100%;\n left: 50%;\n margin-left: -30px;\n z-index: 1;\n}";
file_string+=".tooltiptext::after {\n content: \" \";\n position: absolute;\n bottom: 100%;\n left: 50%;\n margin-left: -5px;\n border-width: 5px;\n border-style: solid;\n border-color: transparent transparent black transparent;\n}";
file_string+=".tooltip:hover .tooltiptext {\n visibility: visible;\n}\n";
file_string+=" </style>\n";
file_string+="</head>\n";
file_string+="<body>\n";
file_string+="<table cellspacing=\"0\" border=\"1\">\n<colgroup span=\"1\" width=\"100\"></colgroup><colgroup span=\"20\" width=\"60\"></colgroup>\n<tr>\n";
file_string+="<td height=\"20\" align=\"left\"></td>\n";
for(int i=0; i<amino_acids.size(); i++)
{
string s;
file_string+= "<td align=\"center\"><b>" +s.replace(0,1,1,amino_acids[i])+"</b></td>\n";
}
file_string+= "</tr>\n";
for(int j=0; j<positional_aa_freqs.size(); j++)
{
//html_file_out << j+1;
file_string+= "<tr><td height=\"20\" align=\"center\" sdval=\"3\"><b>"+to_string(j+1)+":"+aa_sequence[j] +"</b></td>\n";
for(int i=0; i<amino_acids.size(); i++)
{
char aa_char[10];
sprintf(aa_char,"%.2f%%",100*positional_aa_freqs[j][amino_acids[i]]);
string aa_str(aa_char);
for(int k=0; k<color_ladder.size(); k++)
{
if (positional_aa_freqs[j][amino_acids[i]] <= color_ladder[k])
{
ostringstream ss;
if(aa_sequence[j]=='X')
file_string+= "<td height=\"20\" align=\"center\" sdval=\"3\" class=\"color_cat8\"><div class=\"tooltip\">"+aa_str;
else if (aa_sequence[j]==amino_acids[i])
{
file_string+= "<td height=\"20\" align=\"center\" sdval=\"3\" class=\"color_cat"+to_string(k+1)+"\"><div class=\"tooltip\">"+aa_str;
}
else
{
file_string+= "<td height=\"20\" align=\"center\" sdval=\"3\" class=\"color_cat"+to_string(k+1)+" mut\"><div class=\"tooltip\">"+aa_str;
}
break;
}
}
char tooltip_char[75];
sprintf(tooltip_char,"pos:%d<br>%c->%c : %.2f%%",j+1,aa_sequence[j],amino_acids[i],100*positional_aa_freqs[j][amino_acids[i]]);
string tooltip(tooltip_char);
file_string+="<span class=\"tooltiptext\"><b>"+tooltip+"</b><br></span></div></td>\n";
}
file_string+= "</tr>\n";
}
file_string+="<body>\n</html>\n";
ofstream html_file_out;
replace_all(filename,"txt","html");
html_file_out.open(filename);
html_file_out<<file_string;
html_file_out.close();
}
void print_output(string filename, vector<vector<Seq> > &all_sequences, vector<string> sequence_names, int line_wrap_length, double low_prob_cutoff)
{
vector<vector<vector<Seq> > > split_all_sequences;
vector2D_to_3D(all_sequences,line_wrap_length, split_all_sequences);
///output html header
string file_string="";
file_string+="<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>\n";
file_string+="<head>\n";
file_string+=" <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />\n";
file_string+=" <title>Antibody Mutation Analysis</title>\n";
file_string+=" <link rel='stylesheet' href='AMA.css' />\n";
file_string+="</head>\n";
file_string+="<body>\n";
file_string+="<p></p><br>\n";
//cerr << "number of splits: " << split_all_sequences.size() << "\n";
//for each split, make a HTML table and print it
for(int i=0; i<split_all_sequences.size(); i++)
{
HTML::Table html_table;
html_table.hclass="results";
//convert seq vector to html table
convert_2D_seq_vector_to_HTML_table(split_all_sequences[i],sequence_names,html_table, low_prob_cutoff);
//missing step: stylize the table
//print HTML tables
html_table.print(file_string);
file_string+="<p></p>\n";
}
file_string+="<body>\n</html>\n";
ofstream file_out;
file_out.open(filename.c_str());
file_out << file_string;
file_out.close();
}
void get_mutability_scores(map<string,S5F_mut> &S5F_model, string sequence, int last_mutate_position, bool is_shielded, vector<bool> &shield_mutations, vector<double> &last_mut_scores, vector<double> &mut_scores, double &last_sum_mut_scores, double &sum_mut_scores)
{
mut_scores.clear();
if (last_mutate_position==-2) //start from scratch
{
mut_scores.push_back(1);///first two positions set to neutral
mut_scores.push_back(1);///
sum_mut_scores=2.0;
for(int i=2; i<sequence.length()-2; i++)
{
string fivemer=sequence.substr(i-2,5);
if (fivemer.find('-') != std::string::npos) //if there is a gap in fivemer, correct for it if not in middle pos
{
string new_fivemer="";
correct_for_fivemer_with_gap(i,sequence,new_fivemer);
fivemer=new_fivemer;
}
double mut_score;
if (fivemer == "NO_SCORE"){mut_score=0;}
else if (S5F_model.find(fivemer)==S5F_model.end()){cerr << "ERROR: inside simulation, can't find fivemer " << fivemer << " in S5F model\n"; mut_score=0;}
else if ((is_shielded) && (shield_mutations[i]))//shield from mutation
{
mut_score=0;
}
else
{
mut_score=S5F_model[fivemer].score;
}
mut_scores.push_back(mut_score);
sum_mut_scores+=mut_score;
}
mut_scores.push_back(1);///last two positions set to neutral
mut_scores.push_back(1);///
sum_mut_scores+=2.0;
}
else //just recalculate in 5mer region around last mutate position
{
if (last_mutate_position<2 || last_mutate_position>=sequence.length()-2)
{
}
mut_scores=last_mut_scores;
sum_mut_scores=last_sum_mut_scores;
//only recompute the -2 to +2 windows
//get indices of non-gap -2 +2, in case we're in the middle of a gapped region
int five_prime_count=0, three_prime_count=0;
string fivemer="";
// cerr << "last mutate pos: " << last_mutate_position << "\n";
vector<int> window_indices;
window_indices.push_back(last_mutate_position);
int j=last_mutate_position-1;
while(five_prime_count<2 && j>=0) //5 prime side
{
if (sequence[j] !='-')
{
//cerr << "5 prime: " << j << "\n";
window_indices.push_back(j);
five_prime_count++;
}
j--;
}
j=last_mutate_position+1;
while(three_prime_count<2 && j<sequence.length()) //3 prime side
{
if (sequence[j] !='-')
{
// cerr << "3 prime: " << j << "\n";
window_indices.push_back(j);
three_prime_count++;
}
j++;
}
sort(window_indices.begin(), window_indices.end()); //OPTIMIZE: does this really need to be sorted?
//iterate through indices stored from previous step and recompute the fivemer score for these
for(int i=0; i<window_indices.size(); i++)
{
// cerr << "window indices " << i << "\t" << window_indices[i] << "\n";
int index=window_indices[i];
//if (index == -1){cerr << "FATAL ERROR: Fivemer fail in gappy UCA region\n"; cerr << "last mutate pos: " << last_mutate_position << "\n"; exit(1);}
if (index<2 || index >= sequence.length()-2){sum_mut_scores-=mut_scores[index]; mut_scores[index]=1; sum_mut_scores++; continue;}//if at edges because of following gaps
string fivemer=sequence.substr(index-2,5);
if (fivemer.find('-') != std::string::npos) //if there is a gap in fivemer, correct for it if not in middle pos
{
string new_fivemer="";
correct_for_fivemer_with_gap(index,sequence,new_fivemer);
fivemer=new_fivemer;
}
double mut_score;
if (fivemer == "NO_SCORE"){mut_score=0;}
else if (S5F_model.find(fivemer)==S5F_model.end()){cerr << "ERROR: inside simulation, can't find fivemer " << fivemer << " in S5F model\n"; mut_score=0;}
else if ((is_shielded) && (shield_mutations[index]))//shield from mutation
{
mut_score=0;
}
else
{
mut_score=S5F_model[fivemer].score;
}
sum_mut_scores-=mut_scores[index]; //subtract out old score from +/- 2 window from sum
mut_scores[index]=mut_score; //new score
sum_mut_scores+=mut_score; //add back new score to sum
}
}
}
int simulate_S5F_mutation(string sequence, int &num_mutations, map<string,S5F_mut> &S5F_model, mt19937 &gen, uniform_real_distribution<double> &dis, bool kill_stop_seqs, vector<string> &mutant_sequences, bool is_shielded, vector<bool> &shield_mutations)
{
if (num_mutations==0){mutant_sequences.push_back(sequence); return 0;}
///iterate num_mutations times
int last_mutate_position=-2;
vector<double> last_mut_scores(sequence.length(), 0.0);