Extract characters every 2000 characters and save files

I need to make input for a program that is really out of my programming skills, so I have not tried anything. I hope you help me.

I have many text files, starting with the ">" sign for the sample name, line break, and then the data at 0 and 1 for each sample.

The data looks like this (much larger):

 >SampleName_ZN189A
 01000001000000000000100011100000000111000000001000
 00110000100000000000010000000000001100000010000000
 00110000000000001110000010010011111000000100010000
 00000110000001000000010100000000010000001000001110
 >SampleName_ZN189B
 00110000001101000001011100000000000000000000010001
 00010000000000000010010000000000100100000001000000
 00000000000000000000000010000000000010111010000000
 01000110000000110000001010010000001111110101000000

Note. After every 50 characters, a line break occurs.

What do I need to do:

Extract the first 2000 characters of the data of each sample in my file and save it with the same name followed by the window number. For example, if this file was named: Testfile_1.txt, it should look like this (I extracted the first 50 characters of the data):

 >SampleName_ZN189A
 01000001000000000000100011100000000111000000001000
 >SampleName_ZN189B
 00110000001101000001011100000000000000000000010001

And this file should be named like this: Testfile_1_window1.txt

1500 3500, Testfile_1_window2.txt, 3000 5000 Testfile_1_window3.txt ..... 2000 .

, 2000 500 .

.

2:

, , perl python, .

+3
1

Perl , , , .

use strict;use warnings;
local $/='>';
open(my $fh,'<','filename') or die $!;    
while (my $chunk = <$fh>){
    chomp($chunk);
    $chunk =~ s!^(.+?)\n+!!is;
    my $samplename = $1;
    ### how many should be a constant or should be calculated on the fly, currently I set it to 50
    for(my $i=0;$i<50;$i++){        
        my $data = substr($chunk,$i*1500,$i*1500+2000);
        next if ! $data;##skip if there is not data
        my $filename = "Testfile_".$samplename."_window".$i.".txt";
        open(my $ofh,'>',$filename) or die $filename,$!;
        print $ofh "<$samplename\n$data\n";
        close($ofh);
    }
}
close($fh);    
+1

All Articles