我有两个文件:
(one.txt) 看起来像这样:
>ENST001
(((....)))
(((...)))
>ENST002
(((((((.......))))))
((((...)))
我还有 10000 个 ENST
(two.txt) 看起来像这样:
>ENST001 110
>ENST002 59
对于所有 ENST 的其余部分,依此类推
我基本上想通过(two.txt)中的两个字段的组合替换(one.txt)中的ENST,因此结果将如下所示:
>ENST001_110
(((....)))
(((...)))
>ENST002_59
(((((((.......))))))
((((...)))
我为此编写了一个 matlab 脚本,但由于它会循环 (two.txt) 中的所有行,因此需要 6 个小时才能完成,所以我认为使用 awk、sed、grep 甚至 perl 我们可以在几分钟内得到结果. 这是我在matlab中所做的:
frf = fopen('one.txt', 'r');
frp = fopen('two.txt', 'r');
fw = fopen('result.txt', 'w');
while feof(frf) == 0
line = fgetl(frf);
first_char = line(1);
if strcmp(first_char, '>') == 1 % if the line in one.txt start by > it is the ID
id_fold = strrep(line, '>', ''); % Reomve the > symbol
frewind(frp) % Rewind two.txt file after each loop
while feof(frp) == 0
raw = fgetl(frp);
scan = textscan(raw, '%s%s');
id_pos = scan{1}{1};
pos = scan{2}{1};
if strcmp(id_fold, id_pos) == 1 % if both ids are the same
id_new = ['>', id_fold, '_', pos];
fprintf(fw, '%s\n', id_new);
end
end
else
fprintf(fw, '%s\n', line); % if the line doesn't start by > print it to results
end
结尾