简体   繁体   English

有人可以帮我弄清楚我的代码有什么问题吗? 它将 RNA 序列翻译成蛋白质

[英]Can someone help me figure out what's wrong with my code?. It translates RNA sequences into proteins

I have this code that translates RNA sequences into proteins.我有将 RNA 序列翻译成蛋白质的代码。 My question is where should I throw an error?.我的问题是我应该在哪里抛出错误?。 The code should throw an error after all tests have been checked.检查完所有测试后,代码应该会抛出错误。 My problem is that if I throw the error at else if (rnaSequence[i] != key) throw new Error ('Invalid codon');我的问题是,如果我将错误抛出else if (rnaSequence[i] != key) throw new Error ('Invalid codon'); all other tests aren't evaluated.不评估所有其他测试。

... ...

let obj = {
  AUG: 'Methionine',
  UUU: 'Phenylalanine',
  UUC: 'Phenylalanine',
  UUA: 'Leucine',
  UUG: 'Leucine',
  UCU: 'Serine',
  UCC: 'Serine',
  UCA: 'Serine',
  UCG: 'Serine',
  UAU: 'Tyrosine',
  UAC: 'Tyrosine',
  UGU: 'Cysteine',
  UGC: 'Cysteine',
  UGG: 'Tryptophan',
  UAA: 'STOP',
  UAG: 'STOP',
  UGA: 'STOP'
}; 

const translate = (str) => {
  let rnaSequence = (str == null) ? null : str.match(/[A-Z]{1,3}/g);
  let proteinArr = [];

  for (let [key, value] of Object.entries(obj)) {
    if (rnaSequence == null) return proteinArr;
    else if (rnaSequence[0] == key) proteinArr.unshift(value);
    else for (let i = 1; i <= rnaSequence.length; i++) {
      if (rnaSequence[i] == key) proteinArr.push(value);
      // else if (rnaSequence[i] != key) throw new Error ('Invalid codon');
    }
  }

  for (let j = 0; j <= proteinArr.length; j++) {
    if (proteinArr[j] == "STOP") proteinArr.splice(j);
  }
  return proteinArr;
};
...


These are my tests:
...
 test('Small RNA strand', () => {
    expect(translate('AUGUUUUCU')).toEqual(['Methionine', 'Phenylalanine', 'Serine']);
  });

  xtest('Invalid codon throws error', () => {
    expect(() => translate('LOL')).toThrow(new Error('Invalid codon'));
  });

  xtest('Invalid codon throws error', () => {
    expect(() => translate('AUGOO')).toThrow(new Error('Invalid codon'));
  });

... ...

You're running this against every key in your object:您正在针对对象中的每个键运行它:

else if (rnaSequence[i] != key)

Let's say, for example, your RNA sequence is:例如,假设您的 RNA 序列是:

"AUGUUUUCU"

this is translated into the array:这被翻译成数组:

["AUG", "UUU", "UCU"]

You then loop over every key-value pair in your object (ie the entries of your object).然后循环遍历对象中的每个键值对(即对象的条目)。 The first key being "AUG" .第一个key"AUG" Your first if-condition is false, but the following if-condition will execute:您的第一个 if 条件为 false,但会执行以下 if 条件:

else if (rnaSequence[0] == key) proteinArr.unshift(value);

as rnaSequence[0] holds "AUG" and key also equals "AUG" .因为rnaSequence[0]持有"AUG"并且key也等于"AUG" This now results in your RNA array looking like:这现在导致您的 RNA 阵列看起来像:

["Methionine", "AUG", "UCU", "UUU"]

as .unshift(value) will add the key's associated value to the array.因为.unshift(value)会将键的关联值添加到数组中。 You then proceed to your next key-value pair in your object.然后继续处理对象中的下一个键值对。 This time it is UUU: 'Phenylalanine' .这次是UUU: 'Phenylalanine' Upon the second iteration, you again look at your if statements.在第二次迭代时,您再次查看 if 语句。 The first if-condition is false , the second is also false , as "Methionine" is not equal to the key "UUU" .第一个 if 条件为false ,第二个条件也为false ,因为"Methionine"不等于键"UUU" And so you're third if-condition would then be true, and so your error would be thrown.所以你是第三个 if 条件然后是真的,所以你的错误会被抛出。


If you want to fix your code you can create a results array.如果你想修复你的代码,你可以创建一个results数组。 Instead of using unshift() on your RNA array, you can use .push() on the results array.您可以在结果数组上使用.push() ,而不是在 RNA 阵列上使用unshift() You'd also need to loop over all keys in your object before you throw any errors to determine whether the RNA protein isn't in your object.在抛出任何错误以确定 RNA 蛋白是否不在您的对象中之前,您还需要遍历对象中的所有键。

However, in my opinion, the easiest would be to re-think your approach.但是,在我看来,最简单的方法是重新考虑您的方法。 Currently, you're looping over your object to see if your RNA protein is in your object and to get its associated protein value.目前,您正在遍历您的对象以查看您的 RNA 蛋白质是否在您的对象中并获得其相关的蛋白质值。 There are better ways to do this by using the key in obj or hasOwnProperty or simply indexing the object with bracket notation .通过使用key in objhasOwnPropertykey in obj或简单地用括号表示法索引对象,有更好的方法来做到这一点。

What I suggest to you is to get an array of RNA sequences from your original string like you are currently doing.我向您建议的是,像您目前所做的那样,从原始字符串中获取一系列 RNA 序列。 Then, use .map() on this array to map each string to its associated protein by looking it up in the object:然后,在这个数组上使用.map()通过在对象中查找它来将每个字符串映射到其关联的蛋白质:

"AUGUUUUCU" ---> ["AUG", "UUU", "UCU"] ---> ['Methionine', 'Phenylalanine', 'Serine']

You can also do some validation checks by using .every() to check that every RNA strand in your array has a key-value pair in the obj .您还可以使用.every()进行一些验证检查,以检查数组中的每个 RNA 链在obj是否都有一个键值对。

See example below:请参阅下面的示例:

 const obj = { AUG: 'Methionine', UUU: 'Phenylalanine', UUC: 'Phenylalanine', UUA: 'Leucine', UUG: 'Leucine', UCU: 'Serine', UCC: 'Serine', UCA: 'Serine', UCG: 'Serine', UAU: 'Tyrosine', UAC: 'Tyrosine', UGU: 'Cysteine', UGC: 'Cysteine', UGG: 'Tryptophan', UAA: 'STOP', UAG: 'STOP', UGA: 'STOP' }; const translate = str => { const rnaSequence = (str == null) ? null : str.match(/[AZ]{1,3}/g); if(!rnaSequence) return "Invalid codon"; // throw here instead: throw new Error ('Invalid codon'); const allValid = rnaSequence.every(key => key in obj); // check all keys in object if(!allValid) return "Invalid codon"; // throw here instead: throw new Error ('Invalid codon'); return rnaSequence.map(key => obj[key]); } console.log(translate('AUGUUUUCU')) // ['Methionine', 'Phenylalanine', 'Serine']); console.log(translate('LOL')) // new Error('Invalid codon') console.log(translate('AUGOO')) // new Error('Invalid codon')

You could either have a boolean variable that is set to true if an invalid sequence is hit (inside the else if) and then check that flag after the for loop within another if statement.如果命中无效序列(在 else if 内),您可以将布尔变量设置为 true,然后在另一个 if 语句中的 for 循环之后检查该标志。

Or或者

You could have an array that gets invalid sequences pushed to it and then check if that array is empty or not after the for loop.您可以有一个将无效序列推送到它的数组,然后在 for 循环之后检查该数组是否为空。 This method is more descriptive if you're looking to see which ones failed.如果您想查看哪些失败了,则此方法更具描述性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM