| 1 | package Software::GenoScan::HairpinClassifier; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Software::GenoScan::Regression qw(calcHpFeatures getRegModel readRegModel hairpinRVA); | ||
| 7 | require Exporter; | ||
| 8 | |||
| 9 | our $VERSION = "v1.0.4"; | ||
| 10 | our @ISA = qw(Exporter); | ||
| 11 | our %EXPORT_TAGS = ("all" => [ qw( | ||
| 12 | classifyHairpins | ||
| 13 | ) ] ); | ||
| 14 | our @EXPORT_OK = (@{ $EXPORT_TAGS{"all"} }); | ||
| 15 | |||
| 16 | #Description: Wrapper function for hairpin classification | ||
| 17 | #Parameters: (1) Output directory, (2) regression model file, (3) verbose flag, (4) log array | ||
| 18 | #Return value: None | ||
| 19 | sub classifyHairpins($ $ $ $){ | ||
| 20 | my ($OUTPUT_DIR, $REGRESSION, $VERBOSE, $logArray) = @_; | ||
| 21 | if(! -e $OUTPUT_DIR){ | ||
| 22 | system("mkdir $OUTPUT_DIR"); | ||
| 23 | } | ||
| 24 | if(!-e "$OUTPUT_DIR/step_4"){ | ||
| 25 | system("mkdir $OUTPUT_DIR/step_4"); | ||
| 26 | } | ||
| 27 | if(!-e "$OUTPUT_DIR/step_4/folded"){ | ||
| 28 | system("mkdir $OUTPUT_DIR/step_4/folded"); | ||
| 29 | } | ||
| 30 | if(!-e "$OUTPUT_DIR/step_4/visual"){ | ||
| 31 | system("mkdir $OUTPUT_DIR/step_4/visual"); | ||
| 32 | } | ||
| 33 | if(!-e "$OUTPUT_DIR/step_4/annotation"){ | ||
| 34 | system("mkdir $OUTPUT_DIR/step_4/annotation"); | ||
| 35 | } | ||
| 36 | push(@{$logArray}, "Step 4: classify hairpins\n"); | ||
| 37 | if($REGRESSION){ | ||
| 38 | readRegModel($REGRESSION); | ||
| 39 | } | ||
| 40 | my %coefficients = getRegModel(); | ||
| 41 | |||
| 42 | #Open classification report file | ||
| 43 | open(REPORT, ">$OUTPUT_DIR/step_4/log_reg_model_report") or die "GenoScan error: Unable to write log_reg_model_report\n"; | ||
| 44 | |||
| 45 | #Refold, visualize and annotate | ||
| 46 | hairpinRVA("$OUTPUT_DIR/step_3", "$OUTPUT_DIR/step_4/folded", "$OUTPUT_DIR/step_4/visual", "$OUTPUT_DIR/step_4/annotation", $VERBOSE, $logArray); | ||
| 47 | |||
| 48 | #Classify chunks based on logistic regression model | ||
| 49 | my $fileCounter = 0; | ||
| 50 | my @files = split(/\n+/, qx(ls $OUTPUT_DIR/step_4/annotation)); | ||
| 51 | my $numFiles = scalar(@files); | ||
| 52 | my @annotFiles = grep(/[^~]$/, @files); | ||
| 53 | foreach my $annotFile (@annotFiles){ | ||
| 54 | $fileCounter++; | ||
| 55 | if($VERBOSE){ | ||
| 56 | print(" Classifying chunk $fileCounter/$numFiles\r"); | ||
| 57 | } | ||
| 58 | |||
| 59 | #Read annotated chunk | ||
| 60 | open(HAIRPINS, "$OUTPUT_DIR/step_4/annotation/$annotFile") or die "GenoScan error: Unable to read annotation file\n"; | ||
| 61 | my @hairpins = split(/>/, join("", <HAIRPINS>)); | ||
| 62 | shift(@hairpins); | ||
| 63 | close(HAIRPINS); | ||
| 64 | |||
| 65 | #Classify hairpins | ||
| 66 | foreach my $hp (@hairpins){ | ||
| 67 | my ($header, $visual, $annot) = split(/\n\n/, $hp); | ||
| 68 | my ($name, $seq, $sec) = split(/\n/, $header); | ||
| 69 | my %features = calcHpFeatures($hp); | ||
| 70 | my @covars = keys(%features); | ||
| 71 | |||
| 72 | #Classification | ||
| 73 | my $probability = $coefficients{"(Intercept)"}; | ||
| 74 | foreach my $covar (@covars){ | ||
| 75 | $probability += $coefficients{$covar} * $features{$covar}; | ||
| 76 | } | ||
| 77 | $probability = exp($probability) / (1 + exp($probability)); | ||
| 78 | print(REPORT "$name\t$seq\t$probability\n"); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | if($VERBOSE){ | ||
| 82 | print("\n"); | ||
| 83 | } | ||
| 84 | close(REPORT); | ||
| 85 | } | ||
| 86 | |||
| 87 | return 1; | ||
| 88 |