<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl

use strict;
use warnings;
use File::Copy;
use File::Basename;
use Statistics::R;

my $directory = $ARGV[0];
my $dir = dirname($directory);
my $file = basename($directory);

my $train_start = $ARGV[1];
my $train_end = $ARGV[2];
my $test_start = $ARGV[3];
my $test_end = $ARGV[4];

open DATA,"&lt;","$dir/$file" or die "$!";
chomp(my $data = &lt;DATA&gt;);
close DATA;
my @data = split /\t/,$data;
my $total = scalar(@data);
copy("$dir/$file","$dir/data_$total.txt") or die "$!";

open LOG,"&gt;","$dir/log.txt" or die "$!";
my $output = "";
while($total &gt; 2){
   my $input = "data_"."$total.txt";
   print LOG "$input:\n";
   print "$input:\n";
   open IN,"&lt;","$dir/$input" or die "$!";
   my $drop = $total - 1;
   $output = "data_"."$drop.txt";
   my %drop_acc = ();
   for my $count (2..$total){
   print "$count\t";
   &amp;drop($count);
   my $R = Statistics::R -&gt; new();
   $R -&gt; startR;
   $R -&gt; send(qq'library(class)');
   $R -&gt; send(qq'library(e1071)');
   $R -&gt; send(qq'svmdata &lt;- read.delim("$dir/$output",header = FALSE,sep = "\t")');
   $R -&gt; send(qq'n &lt;- length(svmdata)');
   $R -&gt; send(qq'trainset &lt;- svmdata[$train_start:$train_end,2:n]');
   $R -&gt; send(qq'trainlabel &lt;- svmdata[$train_start:$train_end,1]');
   $R -&gt; send(qq'testset &lt;- svmdata[$test_start:$test_end,2:n]');
   $R -&gt; send(qq'testlabel &lt;- svmdata[$test_start:$test_end,1]');
   $R -&gt; send(qq'model &lt;- svm(trainset,trainlabel,type="C-classification")');
   $R -&gt; send(qq'predlabel &lt;- predict(model,testset)');
   $R -&gt; send(qq'result &lt;- table(predlabel,testlabel)');
   $R -&gt; send(qq'acc &lt;- (result[1,1]+result[2,2])/(result[1,1]+result[1,2]+result[2,1]+result[2,2])');
   $R -&gt; send(qq'print(acc)');
   my $read = $R -&gt; read;
   $read =~ s/\[\d\]\s+(\d+)/$1/g;
   print "$read\n";
   $drop_acc{$count} = $read;
   $R -&gt; stopR();
   }
   my @keys = sort{$drop_acc{$b} &lt;=&gt; $drop_acc{$a}} keys %drop_acc;
   for(@keys){print LOG "$_\t$drop_acc{$_}\n";}
   print LOG "drop: $keys[0]\n";
   print "sort:\n";
   for(@keys){print "$_\t$drop_acc{$_}\n";}
   print "drop: $keys[0]\n";
   &amp;drop($keys[0]);
   $total--;
}

sub drop {
    my $drop = shift;
    open OUT,"&gt;","$dir/$output" or die "$!";
    seek IN,0,0;
    while(&lt;IN&gt;){
           chomp;
           my @data = split /\t/;
           my $out = $data[0];
           for my $index (1..$total-1){
               $out = join("\t",$out,$data[$index]) if $index+1 != $drop;
           }
           print OUT "$out\n";
    }
    close OUT;
}</pre></body></html>