Mercurial > repos > cpt > cpt_psm_recombine
comparison lib/CPT/External/TMHMM.pm @ 1:97ef96676b48 draft
planemo upload commit 94b0cd1fff0826c6db3e7dc0c91c0c5a8be8bb0c
author | cpt |
---|---|
date | Mon, 05 Jun 2023 02:51:26 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:b18e8268bf4e | 1:97ef96676b48 |
---|---|
1 package CPT::External::TMHMM; | |
2 no warnings; | |
3 use Moose; | |
4 use File::Temp qw(tempfile); | |
5 use IPC::Run qw(run); | |
6 use File::Temp qw(tempdir); | |
7 use File::Copy qw(move); | |
8 | |
9 has 'sequence' => ( is => 'rw', isa => 'Str' ); | |
10 has 'response' => ( is => 'rw', isa => 'Str' ); | |
11 | |
12 has 'num_predicted' => ( is => 'rw', isa => 'Int' ); | |
13 has 'predicted_locations' => ( is => 'rw', isa => 'ArrayRef' ); | |
14 has 'prob_n_in' => ( is => 'rw', isa => 'Str' ); | |
15 has 'hash' => ( is => 'rw', isa => 'Str' ); | |
16 has 'picture_location' => ( is => 'rw', isa => 'Str' ); | |
17 | |
18 my ( $fh, $filename, $image ); | |
19 | |
20 sub create_fasta_file { | |
21 my ( $self, $seq ) = @_; | |
22 ( $fh, $filename ) = tempfile( "cpt.tmhmm.XXXXXXX", UNLINK => 1 ); | |
23 | |
24 #printf $fh ">%s\n%s\n", 'seq', $seq; | |
25 printf $fh "%s\n", $seq; | |
26 return $filename; | |
27 } | |
28 | |
29 sub parse_text { | |
30 my ($self) = @_; | |
31 unless ( $self->predicted_locations() ) { | |
32 $self->predicted_locations( [] ); | |
33 } | |
34 unless ( $self->num_predicted() ) { | |
35 $self->num_predicted(0); | |
36 } | |
37 foreach my $line ( split( /\n/, $self->response() ) ) { | |
38 if ( $line =~ /Number of predicted TMHs:\s*(\d+)/ ) { | |
39 $self->num_predicted($1); | |
40 } | |
41 elsif ( $line =~ /Total prob of N-in:\s*([0-9.]*)/ ) { | |
42 $self->prob_n_in($1); | |
43 } | |
44 elsif ( $line =~ /TMHMM2.0\s*TMhelix\s*([0-9]+)\s*([0-9]+)/ ) { | |
45 | |
46 #$self->num_predicted($self->num_predicted()+1); | |
47 push( @{ $self->predicted_locations() }, [ $1, $2 ] ); | |
48 } | |
49 } | |
50 | |
51 # # seq Length: 145 | |
52 # # seq Number of predicted TMHs: 1 | |
53 # # seq Exp number of AAs in TMHs: 20.91302 | |
54 # # seq Exp number, first 60 AAs: 20.91265 | |
55 # # seq Total prob of N-in: 0.04659 | |
56 # # seq POSSIBLE N-term signal sequence | |
57 # seq TMHMM2.0 outside 1 3 | |
58 # seq TMHMM2.0 TMhelix 4 23 | |
59 # seq TMHMM2.0 inside 24 145 | |
60 } | |
61 | |
62 sub analyze { | |
63 my ( $self, $seq ) = @_; | |
64 | |
65 # Set our hash | |
66 require Digest::MD5; | |
67 $self->hash( Digest::MD5::md5_hex($seq) ); | |
68 | |
69 # Set our sequence | |
70 $self->sequence($seq); | |
71 | |
72 # Tmp dir to run in | |
73 my $dir = tempdir( CLEANUP => 1 ); | |
74 my $tmpfile = $self->create_fasta_file($seq); | |
75 | |
76 # Plot and use specified workdir | |
77 my @cmd = ( 'tmhmm.pl', '-plot', '-workdir', $dir, '<', $tmpfile ); | |
78 | |
79 # Run the command | |
80 my ( $in, $out, $err ); | |
81 run \@cmd, \$in, \$out, \$err; | |
82 | |
83 # If error,we error | |
84 if ($err) { | |
85 | |
86 # print STDERR "Error: $err\n"; | |
87 # Kinda a crappy way to handle this... | |
88 $self->response($err); | |
89 return 0; | |
90 } | |
91 { | |
92 | |
93 # Move the created plot to a known location | |
94 my @tmhmm_files = glob("$dir/*/*.png"); | |
95 | |
96 # There's really only one png but this is just as easy to write | |
97 for my $png (@tmhmm_files) { | |
98 $image = sprintf( "/tmp/cpt.ext.tmhmm.%s.png", $self->hash() ); | |
99 move( $png, $image ); | |
100 } | |
101 | |
102 # Module to remove the temporary dir so we clean up after | |
103 # ourselves quickly, since this programme seems to open a LARGE | |
104 # number of file handles. | |
105 require File::Path; | |
106 File::Path::remove_tree($dir); | |
107 | |
108 # set response and parse it, then return OK. | |
109 $self->response($out); | |
110 $self->parse_text(); | |
111 return 1; | |
112 } | |
113 } | |
114 | |
115 sub cleanup { | |
116 if ( defined($fh) ) { | |
117 close($fh); | |
118 unlink($filename); | |
119 if ( -e $image ) { | |
120 unlink($image); | |
121 } | |
122 } | |
123 } | |
124 | |
125 END { | |
126 if ( defined($fh) ) { | |
127 close($fh); | |
128 unlink($filename); | |
129 if ( -e $image ) { | |
130 unlink($image); | |
131 } | |
132 } | |
133 } | |
134 | |
135 no Moose; | |
136 1; | |
137 | |
138 __END__ | |
139 | |
140 =pod | |
141 | |
142 =encoding UTF-8 | |
143 | |
144 =head1 NAME | |
145 | |
146 CPT::External::TMHMM | |
147 | |
148 =head1 VERSION | |
149 | |
150 version 1.99.4 | |
151 | |
152 =head1 AUTHOR | |
153 | |
154 Eric Rasche <rasche.eric@yandex.ru> | |
155 | |
156 =head1 COPYRIGHT AND LICENSE | |
157 | |
158 This software is Copyright (c) 2014 by Eric Rasche. | |
159 | |
160 This is free software, licensed under: | |
161 | |
162 The GNU General Public License, Version 3, June 2007 | |
163 | |
164 =cut |