/***************************************************************************** # Copyright (C) 1994-2008 by David Gordon. # All rights reserved. # # This software is part of a beta-test version of the Consed/Autofinish # package. It should not be redistributed or # used for any commercial purpose, including commercially funded # sequencing, without written permission from the author and the # University of Washington. # # This software is provided ``AS IS'' and any express or implied # warranties, including, but not limited to, the implied warranties of # merchantability and fitness for a particular purpose, are disclaimed. # In no event shall the authors or the University of Washington be # liable for any direct, indirect, incidental, special, exemplary, or # consequential damages (including, but not limited to, procurement of # substitute goods or services; loss of use, data, or profits; or # business interruption) however caused and on any theory of liability, # whether in contract, strict liability, or tort (including negligence # or otherwise) arising in any way out of the use of this software, even # if advised of the possibility of such damage. # # Building Consed from source is error prone and not simple which is # why I provide executables. Due to time limitations I cannot # provide any assistance in building Consed. Even if you do not # modify the source, you may introduce errors due to using a # different version of the compiler, a different version of motif, # different versions of other libraries than I used, etc. For this # reason, if you discover Consed bugs, I can only offer help with # those bugs if you first reproduce those bugs with an executable # provided by me--not an executable you have built. # # Modifying Consed is also difficult. Although Consed is modular, # some modules are used by many other modules. Thus making a change # in one place can have unforeseen effects on many other features. # It may takes months for you to notice these other side-effects # which may not seen connected at all. It is not feasable for me to # provide help with modifying Consed sources because of the # potentially huge amount of time involved. # #*****************************************************************************/ #include "guiExportConsensusWithOptions.h" #include "handleWindowManagerDelete2.h" #include #include #include #include #include #include #include #include "contig.h" #include "popupErrorMessage.h" #include "bIsNumericMaybeWithWhitespace.h" #include using namespace std; #include "filename.h" #include "guiapp.h" #include "consed.h" #include "hp_exception_kludge.h" static void cbFastaFormatButtonChanged( Widget wid, XtPointer pClientData, XmToggleButtonCallbackStruct *pXmToggleButtonCallbackStruct ) { TRY_CATCH_WRAPPER( guiExportConsensusWithOptions* pGEC = (guiExportConsensusWithOptions*) pClientData; pGEC->userPushedFastaFormatOrPhdFormatButton( pXmToggleButtonCallbackStruct->set ); ); } static void cbUserPushedCancel( Widget wid, XtPointer pClientData, XtPointer pCallData ) { guiExportConsensusWithOptions* pGuiExport = (guiExportConsensusWithOptions*) pClientData; TRY_CATCH_WRAPPER( delete pGuiExport; ); } static void cbUserPushedOK( Widget wid, XtPointer pClientData, XtPointer pCallData ) { guiExportConsensusWithOptions* pGuiExport = (guiExportConsensusWithOptions*) pClientData; TRY_CATCH_WRAPPER( if ( pGuiExport->bUserPushedOK() ) delete pGuiExport; ); } static void cbWriteWholeConsensus( Widget wid, XtPointer pClientData, XtPointer pCallData ) { guiExportConsensusWithOptions* pGuiExport = (guiExportConsensusWithOptions*) pClientData; TRY_CATCH_WRAPPER( pGuiExport->userPushedWriteWholeOrPartOfConsensusRadioButton(); ); } guiExportConsensusWithOptions :: guiExportConsensusWithOptions( Widget widParentShell, Contig* pContig ) : pContig_( pContig ), bAlreadyDeletedGui_( false ) { widPopupShell_ = XtVaCreatePopupShell( "exportConsensus", topLevelShellWidgetClass, widParentShell, XmNtitle, (char*) "Export Consensus", XmNtransient, False, XmNdeleteResponse, XmDO_NOTHING, NULL ); handleWindowManagerDelete2( widPopupShell_, cbUserPushedCancel, this ); Widget widForm = XtVaCreateManagedWidget( "form", xmFormWidgetClass, widPopupShell_, XmNancestorSensitive, True, NULL ); widOKButton_ = XtVaCreateManagedWidget( "OK", xmPushButtonWidgetClass, widForm, XmNbottomAttachment, XmATTACH_FORM, XmNbottomOffset, 10, XmNleftAttachment, XmATTACH_POSITION, XmNleftPosition, 35, XmNrightAttachment, XmATTACH_POSITION, XmNrightPosition, 45, NULL ); XtAddCallback( widOKButton_, XmNactivateCallback, cbUserPushedOK, this ); widCancelButton_ = XtVaCreateManagedWidget( "Cancel", xmPushButtonWidgetClass, widForm, XmNbottomAttachment, XmATTACH_FORM, XmNbottomOffset, 10, XmNleftAttachment, XmATTACH_POSITION, XmNleftPosition, 55, XmNrightAttachment, XmATTACH_POSITION, XmNrightPosition, 65, NULL ); XtAddCallback( widCancelButton_, XmNactivateCallback, cbUserPushedCancel, this ); Widget widWholeOrPartLabel = XtVaCreateManagedWidget( "Write Whole or Part of Consensus?", xmLabelWidgetClass, widForm, XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM, NULL ); Widget widWholeOrPartRadioButton = XmCreateRadioBox( widForm, "radio1", NULL, 0 ); XtVaSetValues( widWholeOrPartRadioButton, XmNtraversalOn, True, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, widWholeOrPartLabel, XmNleftAttachment, XmATTACH_FORM, XmNorientation, XmHORIZONTAL, NULL ); widWriteWholeConsensus_ = XtVaCreateManagedWidget( "whole", xmToggleButtonWidgetClass, widWholeOrPartRadioButton, XmNset, False, // default NULL ); Widget widWritePartOfConsensus = XtVaCreateManagedWidget( "part", xmToggleButtonWidgetClass, widWholeOrPartRadioButton, XmNset, True, // default NULL ); XtAddCallback( widWriteWholeConsensus_, XmNvalueChangedCallback, cbWriteWholeConsensus, this ); XtManageChild( widWholeOrPartRadioButton ); const int nLabelPosition = 20; const int nFieldPosition = 25; widStartPositionLabel_ = XtVaCreateManagedWidget( "Start Position:", xmLabelWidgetClass, widForm, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, widWholeOrPartRadioButton, XmNtopOffset, 20, XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_POSITION, XmNrightPosition, nLabelPosition, XmNalignment, XmALIGNMENT_END, NULL ); widStartPosition_ = XtVaCreateManagedWidget( "start", xmTextFieldWidgetClass, widForm, XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET, XmNtopWidget, widStartPositionLabel_, // XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET, // XmNbottomWidget, widStartPositionLabel_, XmNleftAttachment, XmATTACH_POSITION, XmNleftPosition, nFieldPosition, XmNcolumns, 10, NULL ); widEndPositionLabel_ = XtVaCreateManagedWidget( "End Position:", xmLabelWidgetClass, widForm, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, widStartPosition_, XmNtopOffset, 10, XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_POSITION, XmNrightPosition, nLabelPosition, XmNalignment, XmALIGNMENT_END, NULL ); widEndPosition_ = XtVaCreateManagedWidget( "end", xmTextFieldWidgetClass, widForm, XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET, XmNtopWidget, widEndPositionLabel_, // XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET, // XmNbottomWidget, widEndPositionLabel_, XmNleftAttachment, XmATTACH_POSITION, XmNleftPosition, nFieldPosition, XmNcolumns, 10, NULL ); Widget widFastaFormatOrPhdFormatLabel = XtVaCreateManagedWidget( "Format: ", xmLabelWidgetClass, widForm, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, widEndPosition_, XmNtopOffset, 10, XmNleftAttachment, XmATTACH_FORM, NULL ); Widget widFastaFormatOrPhdFormatRadioButton = XmCreateRadioBox( widForm, "radio3", NULL, 0 ); XtVaSetValues( widFastaFormatOrPhdFormatRadioButton, XmNtraversalOn, True, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, widFastaFormatOrPhdFormatLabel, XmNleftAttachment, XmATTACH_FORM, XmNorientation, XmHORIZONTAL, NULL ); widFastaFormat_ = XtVaCreateManagedWidget( "Fasta", xmToggleButtonWidgetClass, widFastaFormatOrPhdFormatRadioButton, XmNset, True, // default NULL ); XtAddCallback( widFastaFormat_, XmNvalueChangedCallback, (XtCallbackProc) cbFastaFormatButtonChanged, this ); Widget widPhdFormat = XtVaCreateManagedWidget( "Phd", xmToggleButtonWidgetClass, widFastaFormatOrPhdFormatRadioButton, XmNset, False, NULL ); XtManageChild( widFastaFormatOrPhdFormatRadioButton ); widBasesAndQualOrJustBasesLabel_ = XtVaCreateManagedWidget( "Write Both Bases File and Qual File or Just Bases File?", xmLabelWidgetClass, widForm, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, widFastaFormatOrPhdFormatRadioButton, XmNtopOffset, 10, XmNleftAttachment, XmATTACH_FORM, NULL ); widBasesAndQualOrJustBasesRadioButton_ = XmCreateRadioBox( widForm, "radio2", NULL, 0 ); XtVaSetValues( widBasesAndQualOrJustBasesRadioButton_, XmNtraversalOn, True, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, widBasesAndQualOrJustBasesLabel_, XmNleftAttachment, XmATTACH_FORM, XmNorientation, XmHORIZONTAL, NULL ); widWriteBothFiles_ = XtVaCreateManagedWidget( "Both Files", xmToggleButtonWidgetClass, widBasesAndQualOrJustBasesRadioButton_, XmNset, False, // default NULL ); widWriteJustBasesFile_ = XtVaCreateManagedWidget( "Just Bases File", xmToggleButtonWidgetClass, widBasesAndQualOrJustBasesRadioButton_, XmNset, True, // default NULL ); XtManageChild( widBasesAndQualOrJustBasesRadioButton_ ); XtVaSetValues( widBasesAndQualOrJustBasesRadioButton_, XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, widOKButton_, XmNbottomOffset, 20, NULL ); XtPopup( widPopupShell_, XtGrabNone ); } void guiExportConsensusWithOptions :: deleteGui() { XtPopdown( widPopupShell_ ); XtDestroyWidget( widPopupShell_ ); bAlreadyDeletedGui_ = true; } guiExportConsensusWithOptions :: ~guiExportConsensusWithOptions() { if ( !bAlreadyDeletedGui_ ) { deleteGui(); } } bool guiExportConsensusWithOptions :: bUserPushedOK() { int nStartUnpaddedConsPos; int nEndUnpaddedConsPos; bool bWriteWholeContig = false; if ( XmToggleButtonGetState( widWriteWholeConsensus_ ) == True ) { bWriteWholeContig = true; } else { if ( !bCheckRange( nStartUnpaddedConsPos, nEndUnpaddedConsPos ) ) return( false ); } bool bFastaFormat = false; if ( XmToggleButtonGetState( widFastaFormat_ ) == True ) bFastaFormat = true; // if reached here, all checks have passed on the range bool bWriteBothFiles = XmToggleButtonGetState( widWriteBothFiles_ ); // Now ask user what to call the file FileName soAssemblyDirName( ConsEd::pGetAssembly()->soGetAceFileName().soGetDirectory() ); FileName soDirMask; FileName soTryOutFileName; FileName soBasesFileName; FileName soQualFileName; if ( bFastaFormat ) { soDirMask = soAssemblyDirName + "*.fasta"; soTryOutFileName = soAssemblyDirName + pContig_->soGetName() + ".fasta"; } else { soDirMask = soAssemblyDirName + "*.phd.1"; soTryOutFileName = soAssemblyDirName + pContig_->soGetName() + ".c1.phd.1"; } deleteGui(); bool bGoAhead = false; while( !bGoAhead ) { soBasesFileName = GuiApp::popupFileSelector( "Save consensus bases to file", soDirMask, soTryOutFileName ); // if user cancelled or did not supply name, leave and pop down window if ( soBasesFileName.isNull() ) return( true ); if ( bWriteBothFiles ) { soQualFileName = soBasesFileName + ".qual"; } if ( soBasesFileName.bFileByThisNameExists() ) { bGoAhead = GuiApp::popupDecisionMessage( "File %s exists. Save anyway?", soBasesFileName.data() ); } else { bGoAhead = true; } if ( bGoAhead && bWriteBothFiles ) { if ( soQualFileName.bFileByThisNameExists() ) { bGoAhead = GuiApp::popupDecisionMessage( "File %s exists. Save anyway?", soQualFileName.data() ); } } } // while( !bGoAhead ) // If reached here, the user wants to save the file and // has enterred a file that either doesn't already exist // or already exists and it is ok to overwrite it if ( bFastaFormat ) { pContig_->writeConsensusToFastaFile2( bWriteBothFiles, soBasesFileName, soQualFileName, bWriteWholeContig, nStartUnpaddedConsPos, nEndUnpaddedConsPos ); } else { pContig_->writeConsensusToPhdFile( soBasesFileName, bWriteWholeContig, nStartUnpaddedConsPos, nEndUnpaddedConsPos ); } } bool guiExportConsensusWithOptions :: bCheckRange( int& nStart, int& nEnd ) { // check that the positions enterred by the user are numbers, // are within range of the contig, and are <= char* szValue; RWCString soValue; szValue = XmTextFieldGetString( widStartPosition_ ); soValue = szValue; XtFree( szValue ); if ( !bIsNumericMaybeWithWhitespace( soValue, nStart ) ) { popupErrorMessage( "start field must be numeric" ); return( false ); } szValue = XmTextFieldGetString( widEndPosition_ ); soValue = szValue; XtFree( szValue ); if ( !bIsNumericMaybeWithWhitespace( soValue, nEnd ) ) { popupErrorMessage( "end field must be numeric" ); return( false ); } if ( ! ( nStart <= nEnd ) ) { popupErrorMessage( "start position must be <= end position" ); return( false ); } if ( ! ( pContig_->nGetUnpaddedStartIndex() <= nStart ) ) { popupErrorMessage( "The contig has positions from %d to %d but the start position %d is not on the contig", pContig_->nGetUnpaddedStartIndex(), pContig_->nGetUnpaddedEndIndex(), nStart ); return( false ); } if ( ! ( nEnd <= pContig_->nGetUnpaddedEndIndex() ) ) { popupErrorMessage( "The contig has positions from %d to %d but the end position %d is not on the contig", pContig_->nGetUnpaddedStartIndex(), pContig_->nGetUnpaddedEndIndex(), nEnd ); return( false ); } return( true ); } void guiExportConsensusWithOptions :: userPushedWriteWholeOrPartOfConsensusRadioButton() { bool bSensitiveFlag; if ( XmToggleButtonGetState( widWriteWholeConsensus_ ) == True ) bSensitiveFlag = False; else bSensitiveFlag = True; XtVaSetValues( widStartPositionLabel_, XmNsensitive, bSensitiveFlag, NULL ); XtVaSetValues( widStartPosition_, XmNsensitive, bSensitiveFlag, NULL ); XtVaSetValues( widEndPositionLabel_, XmNsensitive, bSensitiveFlag, NULL ); XtVaSetValues( widEndPosition_, XmNsensitive, bSensitiveFlag, NULL ); } void guiExportConsensusWithOptions :: userPushedFastaFormatOrPhdFormatButton( bool bUserPushedFastaFormatButton ) { XtVaSetValues( widBasesAndQualOrJustBasesLabel_, XmNsensitive, bUserPushedFastaFormatButton, NULL ); XtVaSetValues( widBasesAndQualOrJustBasesRadioButton_, XmNsensitive, bUserPushedFastaFormatButton, NULL ); XtVaSetValues( widWriteBothFiles_, XmNsensitive, bUserPushedFastaFormatButton, NULL ); XtVaSetValues( widWriteJustBasesFile_, XmNsensitive, bUserPushedFastaFormatButton, NULL ); }