Filage

#_lab1

Sep 16th, 2024
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.IO;
  5. using System.Numerics;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8.  
  9. namespace GeneticsProject
  10. {
  11.     public struct GeneticData
  12.     {
  13.         public string name;
  14.         public string organism;
  15.         public string formula;
  16.     }
  17.  
  18.     class Program
  19.     {
  20.         static List<GeneticData> data = new List<GeneticData>();
  21.         static int count = 1;
  22.        
  23.  
  24.         static string GetFormula(string proteinName)
  25.         {
  26.             foreach (GeneticData item in data)
  27.             {
  28.                 if (item.name.Equals(proteinName)) return item.formula;
  29.             }
  30.             return null;
  31.         }
  32.         static void ReadGeneticData(string filename)
  33.         {
  34.             StreamReader reader = new StreamReader(filename);
  35.             while (!reader.EndOfStream)
  36.             {
  37.                 string line = reader.ReadLine();
  38.                 string[] fragments = line.Split('\t');
  39.                 GeneticData protein;
  40.                 protein.name = fragments[0];
  41.                 protein.organism = fragments[1];
  42.                 protein.formula = fragments[2];
  43.                 data.Add(protein);
  44.                 count++;
  45.             }
  46.             reader.Close();
  47.         }
  48.         static void ReadHandleCommands(string filename)
  49.         {
  50.             StreamReader reader = new StreamReader(filename);
  51.             StreamWriter writer = new StreamWriter("genedata.txt");
  52.             writer.WriteLine("Maksim Dragun");
  53.             writer.WriteLine("Genetic Searching");
  54.             int counter = 0;
  55.             writer.WriteLine("================================================");
  56.             while (!reader.EndOfStream)
  57.             {
  58.                 string line = reader.ReadLine();
  59.                 counter++;
  60.                 string[] command = line.Split('\t');
  61.                 if (command[0].Equals("search"))
  62.                 {
  63.                     writer.WriteLine($"{counter.ToString("D3")}   {"search"}   {Decoding(command[1])}");
  64.                     writer.WriteLine("organism                      protein");
  65.                     int index = Search(command[1]);
  66.                     if (index != -1)
  67.                         writer.WriteLine($"{data[index].organism}    {data[index].name}");
  68.                     else
  69.                         writer.WriteLine("NOT FOUND");
  70.                     writer.WriteLine("================================================");
  71.                 }
  72.                 if (command[0].Equals("diff"))
  73.                 {
  74.                     writer.WriteLine($"{counter.ToString("D3")}   {"diff"}   {command[1]}  {command[2]}");
  75.                     int diffNum = Diff(command[1], command[2]);
  76.                     writer.WriteLine("amino-acids difference:");
  77.                     if (diffNum == -1)
  78.                         writer.WriteLine("MISSING" + command[1]);
  79.                     else if (diffNum == -2)
  80.                         writer.WriteLine("MISSING" + command[2]);
  81.                     else
  82.                         writer.WriteLine($"{diffNum}");
  83.                     writer.WriteLine("================================================");
  84.                 }
  85.                 if (command[0].Equals("mode"))
  86.                 {
  87.                     writer.WriteLine($"{counter.ToString("D3")}   {"mode"}   {command[1]}");
  88.                     string res = Mode(command[1]);
  89.                     writer.WriteLine("amino - acid occurs:");
  90.                     if (res == "no")
  91.                         writer.WriteLine("MISSING" + command[1]);
  92.                     else
  93.                         writer.WriteLine(res);
  94.                     writer.WriteLine("================================================");
  95.                 }
  96.             }
  97.             reader.Close();
  98.             writer.Close();
  99.         }
  100.         static bool IsValid(string formula)
  101.         {
  102.             List<char> letters = new List<char>() { 'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y' };
  103.             foreach (char ch in formula)
  104.             {
  105.                 if (!letters.Contains(ch)) return false;
  106.             }
  107.             return true;
  108.         }
  109.         static string Encoding(string formula)
  110.         {
  111.             string encoded = String.Empty;
  112.             for (int i = 0; i < formula.Length; i++)
  113.             {
  114.                 char ch = formula[i];
  115.                 int count = 1;
  116.                 while (i < formula.Length - 1 && formula[i + 1] == ch)
  117.                 {
  118.                     count++;
  119.                     i++;
  120.                 }
  121.                 if (count > 2) encoded = encoded + count + ch;
  122.                 if (count == 1) encoded = encoded + ch;
  123.                 if (count == 2) encoded = encoded + ch + ch;
  124.  
  125.             }
  126.             return encoded;
  127.  
  128.         }
  129.         static string Decoding(string formula)
  130.         {
  131.             string decoded = String.Empty;
  132.             for (int i = 0; i < formula.Length; i++)
  133.             {
  134.                 if (char.IsDigit(formula[i]))
  135.                 {
  136.                     char letter = formula[i + 1];
  137.                     int conversion = formula[i] - '0';
  138.                     for (int j = 0; j < conversion - 1; j++) decoded = decoded + letter;
  139.                 }
  140.                 else decoded = decoded + formula[i];
  141.             }
  142.             return decoded;
  143.         }
  144.         static int Search(string amino_acid)
  145.         {
  146.             string decoded = Decoding(amino_acid);
  147.             for (int i = 0; i < data.Count; i++)
  148.             {
  149.                 if (data[i].formula.Contains(decoded)) return i;
  150.             }
  151.             return -1;
  152.         }
  153.         static int Diff(string protein1, string protein2)
  154.         {
  155.             string  min, formula1 = "", formula2 = "";
  156.             formula1 = GetFormula(protein1);
  157.             formula2 = GetFormula(protein2);
  158.             if (formula1 == "")
  159.                 return -1;
  160.             if (formula2 == "")
  161.                 return -2;
  162.             int minLength = Math.Min(formula1.Length, formula2.Length);
  163.             int difference = 0;
  164.             for(int j = 0; j < minLength; j++)
  165.             {
  166.                 if (formula1[j] != formula2[j]) difference++;
  167.             }
  168.             int n = 1;
  169.             difference += Math.Abs(formula1.Length - formula2.Length);
  170.  
  171.             return difference;
  172.         }
  173.         static string Mode(string protein)
  174.         {
  175.             string formula1 = "";
  176.             formula1 = GetFormula(protein);
  177.             if (formula1 == "")
  178.             {
  179.                 return "no";
  180.             }
  181.             Dictionary<char, int> frequency = new Dictionary<char, int>();
  182.             int max = 0; char maxAmino = ' ';
  183.             for(int j = 0; j < formula1.Length; j++)
  184.             {
  185.                 if (frequency.ContainsKey(formula1[j]))
  186.                 {
  187.                     frequency[formula1[j]]++;
  188.                 }
  189.                 else
  190.                 {
  191.                     frequency[formula1[j]] = 1;
  192.                 }
  193.             }
  194.             foreach(var c in frequency) {
  195.                 if (c.Value > max)
  196.                 {
  197.                     max = c.Value;
  198.                     maxAmino = c.Key;
  199.                 }
  200.                 else if (c.Value == max && c.Key < maxAmino)
  201.                 {
  202.                     maxAmino = c.Key;
  203.                 }
  204.             }
  205.          
  206.             string res = maxAmino + "      " + max;
  207.             return res;
  208.         }
  209.         static void Main(string[] args)
  210.         {
  211.             ReadGeneticData("sequences.0.txt");
  212.             ReadHandleCommands("commands.0.txt");
  213.         }
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment