On Thursday 23,August,2012 12:46 AM, Jim Gibson wrote:
>
> On Aug 22, 2012, at 8:32 AM, lina wrote:
>
>> Hi,
>>
>> I have a data file, wish to split into 9 small files, each one starts
>>
>>
>> 2:MODEL 1
>> 1552:ENDMDL
>>
>> 1554:MODEL 2
>> 3104:ENDMDL
>>
>> 3106:MODEL 3
>> 4656:ENDMDL
>>
>> till
>>
>> 12418:MODEL 9
>> 13968:ENDMDL
>>
>>
>> The left number are the line numbers.
>>
>> I can get those 9 output file via some sed commands, but here I wish to
>> try using perl.
>>
>> The output 9 files and origin files first_9 with the not-working perl
>> file had been pushed to https://github.com/linanil/Temp
>
> That Perl program is only 19 lines, so you may as well include it in your
> email.
>
> I would recommend doing the following:
>
> 1. Open the input file
> 2. Read each line in the input file
> 3. Define a "write" flag variable that is true if an output file is open,
> false if not.
> 4. If the line read from the input file starts with 'MODEL', open an output
> file and set the write flag to true.
> 5. If the line read from the input file starts with 'ENDMDL', close the
> output and reset the write flag.
> 6. If neither conditions described in 4. and 5. is true, and the write flag
> is true, write the input line to the output file.
>
This is a very clear mindmap of doing it.
> Be sure to use a different name each time you open an output file or you will
> overwrite the files and end up with only one. You can use the model number
> extracted from the MODEL line:
>
> if( $line =~ m{ \A MODEL \s* (\d+) }x ) {
> $model_number = $1;
> // open output file with name incorporating $model_number
Thanks, Jim,
my $in_fn="first_9.pdb";
open my $fh, '<', $in_fn;
my $write_flag;
my $ofh;
while(my $line = <$fh>){
if( $line =~ m{ \A MODEL \s* (\d+) }x ) {
my $model_number = $1;
open $ofh, '>', "$model_number.pdb";
$write_flag = 1;
}
if ( $line =~ /^ENDMDL/ ){
print $ofh $line;
close $ofh;
$write_flag = 0;
}
if($write_flag){
print $ofh $line;
}
}
It works, if you think some parts, there are some better way of doing
it, please let me know.
Best regards,
P.S I have pushed to the git.
> ...
>
> Good luck!
>
>
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/