简体   繁体   English

将行转换为列

[英]Convert rows into columns

I have a file in rows as below and would like to convert into two column format. 我有一个如下所示的行文件,并希望转换为两列格式。

>00000_x1688514
TGCTTGGACTACATATGGTTGAGGGTTGTA
>00001_x238968
TGCTTGGACTACATATTGTTGAGGGTTGTA
...

Desired output is 期望的输出是

>00000_x1688514 TGCTTGGACTACATATGGTTGAGGGTTGTA
>00001_x238968 TGCTTGGACTACATATTGTTGAGGGTTGTA
...

I would appreciate any help. 我将不胜感激任何帮助。 Thanks. 谢谢。

I don't know if you are aware of the BioPerl modules for reading/writing and other genetic functions. 我不知道您是否了解BioPerl模块的读/写和其他遗传功能。 Your problem can be written like this. 你的问题可以像这样写。

#!/usr/bin/perl
use strict;
use warnings;
use Bio::SeqIO;

my $file = 'o33.txt';
my $in  = Bio::SeqIO->new( -file   =>  $file,
                           -format => 'fasta');

while ( my $seq = $in->next_seq() ) {
    print $seq->id, "\t", $seq->seq, "\n";
}

__END__
00000_x1688514  TGCTTGGACTACATATGGTTGAGGGTTGTA
00001_x238968   TGCTTGGACTACATATTGTTGAGGGTTGTA

In python: 在python中:

fd = open('filepath')
cols = izip(fd, fd)
with open('output_filepath') as outfile:
    for col in cols:
        outfile.write('\t'.join(col).replace('\n', '') +'\n')

The desired output should be in output_filepath 所需的输出应该在output_filepath

Another Perl option is to set the record delimiter to '>', to read in two lines at a time, then substitute the newline for a tab: 另一个Perl选项是将记录分隔符设置为“>”,一次读取两行,然后用换行符替换选项卡:

use Modern::Perl;

local $/ = '>';
do { s/\n/\t/; print }
  for <DATA>;

__DATA__
>00000_x1688514
TGCTTGGACTACATATGGTTGAGGGTTGTA
>00001_x238968
TGCTTGGACTACATATTGTTGAGGGTTGTA

Output: 输出:

>00000_x1688514 TGCTTGGACTACATATGGTTGAGGGTTGTA
>00001_x238968  TGCTTGGACTACATATTGTTGAGGGTTGTA

For a file: 对于文件:

use Modern::Perl;
use autodie;

open my $inFile,  '<', 'inFile.txt';
open my $outFile, '>', 'outFile.txt';

local $/ = '>';
do { s/\n/\t/; print $outFile $_ }
  for <$inFile>;

close $inFile;
close $outFile;

Hope this helps! 希望这可以帮助!

One approach: 一种方法:

perl -i -pe 's/\n/ / unless m/^[ACGT]+$/' FILENAME

This will in-place edit the file FILENAME , replacing a newline with a space in every line that isn't a string of A's, C's, G's, and T's. 这将就地编辑文件FILENAME ,用不是A,C,G和T字符串的每一行中的空格替换换行符。

Using awk : 使用awk

awk '{ printf "%s", $0 (substr( $0, 1, 1 ) == ">" ? " " : ORS) }' infile

Output: 输出:

>00000_x1688514 TGCTTGGACTACATATGGTTGAGGGTTGTA
>00001_x238968 TGCTTGGACTACATATTGTTGAGGGTTGTA

In Ruby I'd use something like: 在Ruby中我会使用类似的东西:

File.readlines('test.txt').map(&:strip).each_slice(2) do |row|
  puts row.join(' ')
end

Which outputs: 哪个输出:

>00000_x1688514 TGCTTGGACTACATATGGTTGAGGGTTGTA
>00001_x238968 TGCTTGGACTACATATTGTTGAGGGTTGTA

A tidier Python solution: 更整洁的Python解决方案:

from itertools import izip

with open('test.txt') as inf, open('newtest.txt', 'w') as outf:
    for head,body in izip(inf, inf):
        outf.write(head.rstrip() + ' ' + body)

Assuming the input is in true FASTA format, you can use awk and the getline function: 假设输入是真正的FASTA格式,您可以使用awkgetline函数:

awk '/^>/ { printf "%s ", $0; getline; print }' file.txt

Output: 输出:

>00000_x1688514 TGCTTGGACTACATATGGTTGAGGGTTGTA
>00001_x238968 TGCTTGGACTACATATTGTTGAGGGTTGTA

HTH HTH

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

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