Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. #include <time.h>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <vector>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. int min(int i1, int i2, int i3){
  11. if(i1 <= i2 && i1 <= i3)
  12. return i1;
  13. if(i2 <= i1 && i2 <= i3)
  14. return i2;
  15. if(i3 <= i2 && i3 <= i1)
  16. return i3;
  17. }
  18.  
  19. int whichmin(int i1, int i2, int i3){
  20. if(i2 <= i1 && i2 <= i3)
  21. return 2;
  22. if(i1 <= i2 && i1 <= i3)
  23. return 1;
  24. if(i3 <= i2 && i3 <= i1)
  25. return 3;
  26. }
  27.  
  28. int main(int argc, char** argv){
  29. ifstream input;
  30. ofstream output;
  31. output.open("output.txt");
  32.  
  33. input.open(argv[1]);
  34.  
  35. string seq;
  36.  
  37. while(input >> seq){
  38.  
  39. vector<char> seq1;
  40. vector<char> seq2;
  41.  
  42. int comma = 0; //copy values before comma into seq1
  43. for(int i = 0; i < seq.length(); i++){
  44. if(seq[i] == ',')
  45. break;
  46. seq1.push_back(seq[i]);
  47. comma++;
  48. }
  49.  
  50. //copy values after comma into seq2
  51. for(int i = comma + 1; i < seq.length(); i++){
  52. seq2.push_back(seq[i]);
  53. }
  54.  
  55. //convert A to 1, T to 2, G to 3, C to 4 in both vectors
  56. for(int i = 0; i < seq1.size(); i++){
  57. if(seq1[i] == 'A')
  58. seq1[i] = '1';
  59. else if(seq1[i] == 'T')
  60. seq1[i] = '2';
  61. else if(seq1[i] == 'G')
  62. seq1[i] = '3';
  63. else if(seq1[i] == 'C')
  64. seq1[i] = '4';
  65. }
  66.  
  67. for(int i = 0; i < seq2.size(); i++){
  68. if(seq2[i] == 'A')
  69. seq2[i] = '1';
  70. else if(seq2[i] == 'T')
  71. seq2[i] = '2';
  72. else if(seq2[i] == 'G')
  73. seq2[i] = '3';
  74. else if(seq2[i] == 'C')
  75. seq2[i] = '4';
  76. }
  77.  
  78. ifstream matrixf;
  79. matrixf.open(argv[2]);
  80.  
  81. string lines[6];
  82. int matrix[5][5];
  83.  
  84. for(int i = 0; i < 6; i++){ //grab the table
  85. matrixf >> lines[i];
  86. }
  87.  
  88. for(int i = 0; i < 5; i++){ //copy important values
  89. matrix[i][0] = int (lines[i+1][2]) - 48;
  90. matrix[i][1] = int (lines[i+1][4]) - 48;
  91. matrix[i][2] = int (lines[i+1][6]) - 48;
  92. matrix[i][3] = int (lines[i+1][8]) - 48;
  93. matrix[i][4] = int (lines[i+1][10]) - 48;
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100. int cost[seq1.size()+1][seq2.size()+1];
  101. int path[seq1.size()+1][seq2.size()+1];
  102. cost[0][0] = matrix[0][0];
  103. int sumx = 0, sumy = 0; //base cases. We need base case to be cost
  104. //of aligning string will all inserts
  105. //for path base case is coming from [i-1] for [i][o]
  106. //and coming from [j-1] for [0][j]
  107. for(int i = 1; i < seq1.size()+1; i++){
  108. int n = int (seq1[i-1]) - 48;
  109. sumx += matrix[0][n];
  110. cost[i][0] = sumx;
  111. path[i][0] = 1;
  112. }
  113. for(int i = 1; i < seq2.size()+1; i++){
  114. int n = int (seq2[i-1]) - 48;
  115. sumy += matrix[0][n];
  116. cost[0][i] = sumy;
  117. path[0][i] = 2;
  118. }
  119. //dynamic programming
  120. for(int i = 1; i < seq1.size() + 1; i++){
  121. for(int j = 1; j < seq2.size() + 1; j++){
  122.  
  123. int s1, s2;
  124. s1 = int (seq1[i-1]) - 48;
  125. s2 = int (seq2[j-1]) - 48;
  126. cost[i][j] = min(cost[i-1][j] + matrix[s1][0],
  127. cost[i][j-1] + matrix[0][s2],
  128. cost[i-1][j-1] + matrix[s1][s2]);
  129. path[i][j] = whichmin(cost[i-1][j] + matrix[s1][0],
  130. cost[i][j-1] + matrix[0][s2],
  131. cost[i-1][j-1] + matrix[s1][s2]);
  132. }
  133. }
  134.  
  135. int iteri = seq1.size(), iterj = seq2.size();
  136. vector<char> alseq1; //trace back the path
  137. vector<char> alseq2;
  138. while(iteri != 0 || iterj != 0){
  139. if(path[iteri][iterj] == 3){
  140. alseq1.insert(alseq1.begin(),seq1[iteri-1]);
  141. alseq2.insert(alseq2.begin(),seq2[iterj-1]);
  142. iteri--;
  143. iterj--;
  144. }else if(path[iteri][iterj] == 2){
  145. alseq1.insert(alseq1.begin(),'-');
  146. alseq2.insert(alseq2.begin(),seq2[iterj-1]);
  147. iterj--;
  148.  
  149. }else{
  150. alseq1.insert(alseq1.begin(),seq1[iteri-1]);
  151. alseq2.insert(alseq2.begin(),'-');
  152. iteri--;
  153. }
  154. }
  155.  
  156. for(int i = 0; i < alseq1.size(); i++){ //convert numbers back to letters
  157. if(alseq1[i] == '1'){
  158. alseq1[i] = 'A';
  159. }
  160. if(alseq1[i] == '2'){
  161. alseq1[i] = 'T';
  162. }
  163. if(alseq1[i] == '3'){
  164. alseq1[i] = 'G';
  165. }
  166. if(alseq1[i] == '4'){
  167. alseq1[i] = 'C';
  168. }
  169. }
  170.  
  171. for(int i = 0; i < alseq2.size(); i++){
  172. if(alseq2[i] == '1'){
  173. alseq2[i] = 'A';
  174. }
  175. if(alseq2[i] == '2'){
  176. alseq2[i] = 'T';
  177. }
  178. if(alseq2[i] == '3'){
  179. alseq2[i] = 'G';
  180. }
  181. if(alseq2[i] == '4'){
  182. alseq2[i] = 'C';
  183. }
  184. }
  185.  
  186. for(int i = 0; i < alseq1.size(); i++) //output to file
  187. output << alseq1[i];
  188. output << ',';
  189. for(int i = 0; i < alseq2.size(); i++)
  190. output << alseq2[i];
  191.  
  192. output << ":" << cost[seq1.size()][seq2.size()] << endl;
  193. }
  194.  
  195. output.close();
  196. return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement