Change directory error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change directory error
# 8  
Old 11-11-2014
I guess I have a lot to learn Smilie.... thank you

I will do some tutorials on the different programs and hopefully that will be a good start.
# 9  
Old 11-11-2014
Quote:
Originally Posted by cmccabe
I guess I have a lot to learn Smilie.... thank you

I will do some tutorials on the different programs and hopefully that will be a good start.
If I may chime in an opinion.
You have been trying for sometime to make a Bash wrapper for some Perl code. You mentioned Awk as well. I have the feeling you have been trying to absorb as much you could of any and all of these scripting means.

Why do you not concentrate a bit on just Perl and make that concept you have been trying a robust one? Perl would be a more homogenous and robust solution; and creating a menu is not that difficult on it. The end result of learning Perl is that it would benefit far more in your scientist projects that the shell or even Awk could do.

Last edited by Aia; 11-11-2014 at 03:00 PM..
This User Gave Thanks to Aia For This Post:
# 10  
Old 11-11-2014
Quote:
Originally Posted by junior-helper
... ... ...

Don,
thank you for the hint.
Is it possible to make the functions "return", without major changes?
I admit I misused the functions as goto replacement Smilie
Your code assumes that each patient will only have one panel (so you have your user enter a patient ID and a panel number together). My code assumes that a single patient will frequently go through multiple panels (so I have my user enter a patient ID and then enter panel numbers in a loop for that patient; and when all panels have been processed for that patient, loop through other patients).

To get rid of the recursion in your code, remove the additional function and change every call to additional into a return statement. You will then either need to insert a loop into your menu function or at the end of your script, change:
Code:
menu

to:
Code:
while [ 1 ]
do  menu
done

And then, finally, remove the call to menu in menu and change calls to menu in match to return statements.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 11  
Old 11-11-2014
That is a great suggestion Aia. I have been trying to make a bash to make it easier for users to use without needing much scripting, ideally none but as little as needed. Anyway, below is a start (I hope):
Code:
 
#!/usr/bin/perl

use strict;
use warnings;
use Switch;

my $input = '';

while ($input ne '7')
{
    clear_screen();

    print "type 1 for mutation match\n".
          "type 2 for annovar conversion\n". 
          "type 3 for individual or sanger annotation\n". 
          "type 4 for batch or sanger annotation\n". 
          "type 5 for individual noonan syndrome anotation\n".
		  "type 6 for batch noonan syndrome annotation\n".
          "type 7 to end\n";
		  
		   print "Enter your choice: ";
    $input = <STDIN>;
    chomp($input);

    switch ($input)
    {
        case '1'
        {
            $input = '';

I am not sure if that is correct or how to call a command based on the menu selection. For example, if 1 is inputed, then something like this needs to result.

Code:
 
printf "Enter ID  : "; read id
	printf "What panel: "; read panel
		cd 'C:\Users\cmccabe\Desktop\annovar'
			[ -z "$id" ] && break
			OMR=Output_Mutation_Report
	perl -aF/\\t/ -lne 'BEGIN{%m=map{chomp;s/\cM|\cJ//g;$p=join("\t",(split/\t/)[4,5]);($p,$_)} <>;$m{"#CHROM\tINFO"}=$m{"Chr\tSegment Position"}};/SEGPOS=(\d+)/ || /\t(INFO)\t/ or next;$p=$F[0]."\t".$1;exists $m{$p} and print join("\t",$_,$m{$p})' ${id}_${panel}_${OMR}.txt < ${id}_${panel}_${OMR}_Filtered.vcf > ${id}_matched.vcf

Thank you all for the great suggestions and ideas, I have a lot to learn Smilie.
# 12  
Old 11-13-2014
@cmccabe

If you are serious about it, you are going to have to learn and figure out what this line does first and how to converted from a command line invocation to a Perl code script. Calling Perl inside Perl would be a bit silly.

Code:
perl -aF/\\t/ -lne 'BEGIN{%m=map{chomp;s/\cM|\cJ//g;$p=join("\t",(split/\t/)[4,5]);($p,$_)} <>;$m{"#CHROM\tINFO"}=$m{"Chr\tSegment Position"}};/SEGPOS=(\d+)/ || /\t(INFO)\t/ or next;$p=$F[0]."\t".$1;exists $m{$p} and print join("\t",$_,$m{$p})' ${id}_${panel}_${OMR}.txt < ${id}_${panel}_${OMR}_Filtered.vcf > ${id}_matched.vcf

Since it is very ugly and hard to read, I separated them in sections to facilitate understanding for you.

Code:
perl -aF/\\t/ -lne '

  BEGIN{
    %m = map{ chomp;
              s/\cM|\cJ//g;
              $p = join ("\t",(split/\t/)[4,5]);
              ($p, $_)
            } <>;
    $m{"#CHROM\tINFO"} = $m{"Chr\tSegment Position"}
  };

  /SEGPOS=(\d+)/ || /\t(INFO)\t/ or next;
  $p=$F[0]."\t".$1;
  exists $m{$p} and print join("\t",$_,$m{$p})

' ${id}_${panel}_${OMR}.txt < ${id}_${panel}_${OMR}_Filtered.vcf > ${id}_matched.vcf

Also, you'll need to figure out how to deal with all those shell variables, like ${id}, ${panel}, etc. Perl would not inherit those from the shell automatically.

Once, you do that, you might be ready for a menu.
There are many ways of doing it, depending on the design. Here's just a guide:

Code:
#!/usr/bin/perl

use strict;
use warnings;

sub run {
    print "I am running...\n";
}

sub walk {
    print "I am walking...\n";
}

sub dance {
    print "I am dancing...\n";
}

sub stop {
    print "See you!\n";
    exit;
}


sub menu {
    my $items  = shift;
    my $number = 1;

    print "\nWhat would you like me to do:\n";
    for my $item ( @{$items} ) {
        printf "%d: %s\n", $number++, $item->{'message'};
    }
    print "\nEnter option number: ";

    while (my $input = <STDIN>) {
        chomp $input;
        if ($input =~ m/\d+/
                and $input <= @{$items}
                and $input > 0) {
            return &{$items->[$input -1]->{$input}};
        }

        print "\nInvalid option, please try again: ";
    }
}

my @choice = (
    { 'message' =>     'running', '1' =>   \&run, },
    { 'message' =>     'dancing', '2' => \&dance, },
    { 'message' =>     'walking', '3' =>  \&walk, },
    { 'message' => 'end program', '4' =>  \&stop, },
);

while (1) {
    menu( \@choice );
}

There are no substitute for practicing and practicing some more.

After practice for some time, you could take a look at CPAN and search for menus. There are some modules to build some menus, like Term::Menus

Last edited by Aia; 11-13-2014 at 01:43 AM.. Reason: Missing last braquet
# 13  
Old 11-13-2014
Hi.
Quote:
Originally Posted by cmccabe
Thank you all for the great suggestions and ideas, I have a lot to learn
Yes, practice. It would probably be useful to look over some books. The page at Perl - Programming - Books & Videos - O'Reilly Media lists an abundance of books.

An older list for scientific computing is available at recommendations on scientific computing with Perl

If you write a lot of perl, look into the utility perltidy, it will save you many times over. Always look in your repositories for perl codes, there are a ton of libraries available. You might find some things at The Comprehensive Perl Archive Network - www.cpan.org that are useful. I never install anything in my system unless I use the package manager (apt-get, yum zypper, etc), but rather I put codes in ~/bin, and ~/executable.

We used O'Reilly books in the perl classes I taught. You are out of the beginner state when you have written 100 non-trivial programs ( drl's rule Smilie ) I'd recommend Learning, Intermediate, and Mastering for a full course. You didn't say what your field is, but if it's the life sciences, then the Bioinformatics might be useful.

A few books from publisher Manning are also useful, Data Munging, and OO perl.

However, before you start on this, you may wish to compare perl and Python, say from The Fall Of Perl, The Web's Most Promising Language

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 14  
Old 11-15-2014
Thank you Smilie. My field is life sciences, specifically Molecular Diagnostics and genetics. Bioinformatics is a necessary and important skill to have and develop.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change directory shell

#!/bin/bash echo -n "Enter number of sanger patients : "; read id perl -ne 'chomp; system ("perl table_annovar.pl $_ humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo")' < file.txt I have the above script... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Change Directory

Hi All, There is a code like below in my script ############################################### ###Create Directories and Sub-Directories ############################################### dpdir=DP_FROM_${from}_TO_${to} mkdir $dpdir cd $dpdir mkdir AWQM WFCONTROLLER PROVCO PRISM ... (1 Reply)
Discussion started by: pvmanikandan
1 Replies

3. Shell Programming and Scripting

Change to directory and search some file in that directory in single command

I am trying to do the following task : export ENV=aaa export ENV_PATH=$(cd /apps | ls | grep $ENV) However, it's not working. What's the way to change to directory and search some file in that directory in single command Please help. (2 Replies)
Discussion started by: saurau
2 Replies

4. UNIX for Dummies Questions & Answers

How to change database directory to another directory?

Hi, I Installed mysql on my CentOS 6.2 Server. But when I tried to change the location of /var/lib/mysql to another directory. I can't start the mysql. Below is what I've done yum install mysql mysql-server mysql-devel mkdir /path/to/new/ cp -R /var/lib/mysql /path/to/new chown -R... (1 Reply)
Discussion started by: ganitolngyundre
1 Replies

5. Shell Programming and Scripting

change directory if available

I have a simple shell script that prompts the user to enter a directory to navigate to. What i want it to do and i don't know how to do this is if the directory is invalid automatically navigate to the home directory. echo "enter a directory to navigate to:" read directory cd $directory... (6 Replies)
Discussion started by: icelated
6 Replies

6. Shell Programming and Scripting

Change all filenames in a directory

I have a directory of files and each file has a random 5 digit string at the beginning that needs to be removed. Plus, there are some files that will be identically named after the 5 digit string is removed and I want those eliminated or moved. any ideas? (17 Replies)
Discussion started by: crumb
17 Replies

7. UNIX for Dummies Questions & Answers

Change Directory

I have a directory that is existing under my root dir of the FTP server. The DIR name is 'Software Patch'. I want to move in to that DIR to download some patches. But, when I issued a command 'cd SOftware Patch', the system said that it cannot find the dir 'Software'. I tried all possible ways like... (2 Replies)
Discussion started by: vskr72
2 Replies

8. Shell Programming and Scripting

Change Directory via a script?

I would like to have a script that would change my current working directory. However, any time I execute a 'cd' command in a script, it holds only for the life of that script -- the working directory on exit is the same as when the script was initiated. Is it possible to have the script return... (3 Replies)
Discussion started by: George Borrmann
3 Replies

9. Shell Programming and Scripting

change directory

hi, Iam in directory A. I run a script from there. inside the script i have a command cd B. When i come out of the script directory is A only. Even when i come out scrip i want the directory to be B How to achieve (2 Replies)
Discussion started by: mkan
2 Replies

10. Shell Programming and Scripting

change directory

Hi all, I'm trying to wirte a small shell script in Linux. My script has the flow like, cmd1 cmd2 cd testdata cmd3 After exiting the program, the CWD remains the same as where I execute the program. I need it to be changed to the latest updated directory in the program. How can I do... (1 Reply)
Discussion started by: vadivel
1 Replies
Login or Register to Ask a Question