fdillon has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I have been trying to get this short script to print out a suffix tree to a .txt file, so that I might process the tree (looking for the longest repeated subsequence). On Windows XP:

#!/usr/bin/perl; use SuffixTree; use warnings; use strict; my $filename7 = "C:\\MB\\Cp\\mississippi_out_7.txt"; open(MY7, ">>$filename7") or die "Unable to open $filename7: $!\n"; my $str="catgatgttttccctatgggatttttgaa"; my $tree=create_tree($str); print_tree($tree); my $position = find_substring($tree, "ttttg"); printf("\nPosition of (ttttg) in the sequence is %ld.\n\n", $p +osition); print MY7 "$tree"; #tried to output to .txt print MY7 "${$tree}"; #tried to dereference exit 0;
The script comes packaged (as a Synopsis) with davido's SuffixTree.pm. It works fine but outputs only to the screen. I tried to print $tree to a .txt file as shown above, and got this output:
_p_SUFFIX_TREE=SCALAR(0x224fa4)
The attempt to dereference and output yielded:
2273160
to <STDIN> and to the output file.

Is there a simple way to do this?

Many thanks,

fdillon

Replies are listed 'Best First'.
Re: Printing out to a .txt file.
by ikegami (Patriarch) on Apr 07, 2005 at 22:02 UTC

    Without knowing more about SuffixTree, I can only come up with:

    { local *STDOUT = *MY7; print_tree($tree); }

    Update: I see this is module is on CPAN (despite beingin the root namespace!) and that the print_tree function uses C (not Perl) printf calls. As such, this probably doesn't work. I don't know if any of the solutions here with work. If the three already submitted don't work, you'll have to redirect the output from the shell (perl script.pl > mississippi_out_7.txt, because the module doesn't allow you to specify an alternate output file.

Re: Printing out to a .txt file.
by Roy Johnson (Monsignor) on Apr 07, 2005 at 22:02 UTC
    Have you tried reopening STDOUT? If you've already got a filehandle, it goes:
    open(STDOUT, '>&MY7') or warn "$!";
    otherwise, just open a file for writing normally, using STDOUT as your filehandle. You can save your STDOUT filehandle by
    open(SAVE_STDOUT, '>&STDOUT');
    first, then restore it with
    open(STDOUT, '>&SAVE_STDOUT');
    after.
    Update: Oh, ikegami's looks much easier.

    Caution: Contents may have been coded under pressure.
Re: Printing out to a .txt file.
by tlm (Prior) on Apr 07, 2005 at 22:12 UTC

    OK, this is a shot in the dark, but it's simple enough that it's worth trying. It's banking on the possibility that print_tree may be printing to the currently selected handle, as opposed to printing explicitly to STDOUT or to who knows what else:

    my $str="catgatgttttccctatgggatttttgaa"; my $tree=create_tree($str); my $filename7 = "C:\\MB\\Cp\\mississippi_out_7.txt"; open(MY7, ">>$filename7") or die "Unable to open $filename7: $!\n"; my $orig = select(MY7); print_tree($tree); select($orig);
    See select for more details. Note however that select is unusual in that it has two completely different meanings in Perl; here I'm using the select that operates on filehandles (obviously), not the one that operates on bit vectors.

    the lowliest monk

Re: Printing out to a .txt file.
by graff (Chancellor) on Apr 08, 2005 at 02:41 UTC
    Have you tried redirecting STDOUT to the file on the command line? I believe that even the native MS-DOS Prompt shell supports this (but I would still prefer a windows port of bash). Just add  >> your\file.name at the end of the command line when you run your script.

    Single angle bracket would truncate an existing file before writing to it, so the file content would be only the output of the current run; double angle brackets will append to the end of an existing file, or create a new file if it doesn't exist yet -- just like in the perl "open" call (which is, after all, designed to mimic shell usage in this regard).

Re: Printing out to a .txt file.
by polettix (Vicar) on Apr 08, 2005 at 12:38 UTC
    The module seems to be in quite an early stage of life; quoting the BUGS section of the manpage:
    This Perl interface was mostly built automatically (using SWIG). Little to no attention was given to testing. In future relases of this Perl Module (along with its underlying ANSI-C implementation) we hope to fix all problems that might currenly interfere with successful usage of this module. Please send bug reports to the author(s) of this module.
    (Bold is mine) Given this and the output problems you're encountering, are you sure you really want to use it?

    Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

    Don't fool yourself.
Re: Printing out to a .txt file.
by fdillon (Novice) on Apr 10, 2005 at 09:22 UTC
    To ikegami, Roy Johnson, Pustular Postulant, graff, and frodo72:

    Thanks for the advice; I started with graff's (it worked well) and will be digesting the rest of the replies this weekend.

    frodo72: I know the module is somewhat rough-hewn, not to mention untested -- I wish I could find one that was more polished. It totally amazes me that it was a SWIG translation and it works... but it does!

    The beauty of a suffix tree is that it gives the whole spectrum of repeated subsequences very quickly. So it (with Perl) is powerful at extracting data from dna from persons with Huntington's Disease or hereditary non-polyposis colon cancer and several other diseases where the defect may be a repeated (CAG)*n or another short, repeated theme.

    Thanks, all, again for the help.

    fdillon